Skip to content

Commit 0505956

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

File tree

14 files changed

+761
-66
lines changed

14 files changed

+761
-66
lines changed

database/memdb/db.go

Lines changed: 16 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,21 @@ func New() *Database {
3839
return NewWithSize(DefaultSize)
3940
}
4041

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