Skip to content

Commit db6f8b7

Browse files
committed
Merkle-ize X-Chain state
Signed-off-by: Joshua Kim <[email protected]>
1 parent 73b7f02 commit db6f8b7

File tree

14 files changed

+782
-62
lines changed

14 files changed

+782
-62
lines changed

database/memdb/db.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package memdb
55

66
import (
77
"context"
8+
"fmt"
89
"slices"
910
"strings"
1011
"sync"
@@ -38,6 +39,23 @@ func New() *Database {
3839
return NewWithSize(DefaultSize)
3940
}
4041

42+
// TODO remove this
43+
//
44+
// Copy returns a Database with the same key-value pairs as db
45+
func Copy(db *Database) (*Database, error) {
46+
db.lock.Lock()
47+
defer db.lock.Unlock()
48+
49+
result := New()
50+
for k, v := range db.db {
51+
if err := result.Put([]byte(k), v); err != nil {
52+
return nil, fmt.Errorf("failed to insert key: %w", err)
53+
}
54+
}
55+
56+
return result, nil
57+
}
58+
4159
// NewWithSize returns a map pre-allocated to the provided size with the
4260
// Database interface methods implemented.
4361
func NewWithSize(size int) *Database {

vms/avm/block/executor/block.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (b *Block) Verify(context.Context) error {
201201
return nil
202202
}
203203

204-
func (b *Block) Accept(context.Context) error {
204+
func (b *Block) Accept(ctx context.Context) error {
205205
blkID := b.ID()
206206
defer b.manager.free(blkID)
207207

@@ -246,12 +246,16 @@ func (b *Block) Accept(context.Context) error {
246246
return err
247247
}
248248

249+
checksum, err := b.manager.state.Checksum(ctx)
250+
if err != nil {
251+
return fmt.Errorf("failed to get checksum: %w", err)
252+
}
249253
b.manager.backend.Ctx.Log.Trace(
250254
"accepted block",
251255
zap.Stringer("blkID", blkID),
252256
zap.Uint64("height", b.Height()),
253257
zap.Stringer("parentID", b.Parent()),
254-
zap.Stringer("checksum", b.manager.state.Checksum()),
258+
zap.Stringer("checksum", checksum),
255259
)
256260
return nil
257261
}

vms/avm/block/executor/block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func TestBlockAccept(t *testing.T) {
749749
// because we mock the call to shared memory
750750
mockManagerState.EXPECT().CommitBatch().Return(nil, nil)
751751
mockManagerState.EXPECT().Abort()
752-
mockManagerState.EXPECT().Checksum().Return(ids.Empty)
752+
mockManagerState.EXPECT().Checksum(gomock.Any()).Return(ids.Empty, nil)
753753

754754
mockSharedMemory := atomicmock.NewSharedMemory(ctrl)
755755
mockSharedMemory.EXPECT().Apply(gomock.Any(), gomock.Any()).Return(nil)

vms/avm/environment_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func setup(tb testing.TB, c *envConfig) *environment {
113113
}
114114

115115
vm := &VM{
116-
Config: vmStaticConfig,
116+
Config: vmStaticConfig,
117+
StateMigrationFactory: NoStateMigrationFactory{},
117118
}
118119

119120
vmDynamicConfig := DefaultConfig

0 commit comments

Comments
 (0)