Skip to content

Commit c555608

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

File tree

19 files changed

+1179
-123
lines changed

19 files changed

+1179
-123
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/block/executor/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Manager interface {
5151
func NewManager(
5252
mempool mempool.Mempool,
5353
metrics metrics.Metrics,
54-
state state.State,
54+
state state.Interface,
5555
backend *executor.Backend,
5656
clk *mockable.Clock,
5757
onAccept func(*txs.Tx) error,
@@ -72,7 +72,7 @@ func NewManager(
7272

7373
type manager struct {
7474
backend *executor.Backend
75-
state state.State
75+
state state.Interface
7676
metrics metrics.Metrics
7777
mempool mempool.Mempool
7878
clk *mockable.Clock

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

vms/avm/factory.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ type Factory struct {
1616
}
1717

1818
func (f *Factory) New(logging.Logger) (interface{}, error) {
19-
return &VM{Config: f.Config}, nil
19+
return &VM{
20+
Config: f.Config,
21+
StateMigrationFactory: GForkStateMigrationFactory{
22+
CommitFrequency: 1_000,
23+
},
24+
},
25+
nil
2026
}

0 commit comments

Comments
 (0)