Skip to content

Commit a686ac6

Browse files
committed
feat: add config to enable block database
1 parent e4d9473 commit a686ac6

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

graft/coreth/plugin/evm/config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ type Config struct {
139139
StateSyncRequestSize uint16 `json:"state-sync-request-size"`
140140

141141
// Database Settings
142-
InspectDatabase bool `json:"inspect-database"` // Inspects the database on startup if enabled.
142+
InspectDatabase bool `json:"inspect-database"` // Inspects the database on startup if enabled.
143+
BlockDatabaseEnabled bool `json:"block-database-enabled"` // Use block database for storing block data
144+
// SkipBlockDatabaseAutoMigrate skips auto-migrating block data from key-value
145+
// database to the block database. Only new blocks will be stored in the block database.
146+
SkipBlockDatabaseAutoMigrate bool `json:"skip-block-database-auto-migrate"`
143147

144148
// SkipUpgradeCheck disables checking that upgrades must take place before the last
145149
// accepted block. Skipping this check is useful when a node operator does not update

graft/coreth/plugin/evm/vm.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ var (
130130
metadataPrefix = []byte("metadata")
131131
warpPrefix = []byte("warp")
132132
ethDBPrefix = []byte("ethdb")
133+
blockDBPrefix = []byte("blockdb")
133134
)
134135

135136
var (
@@ -312,7 +313,9 @@ func (vm *VM) Initialize(
312313
}
313314

314315
// Initialize the database
315-
vm.initializeDBs(db)
316+
if err := vm.initializeDBs(db); err != nil {
317+
return err
318+
}
316319
if vm.config.InspectDatabase {
317320
if err := vm.inspectDatabases(); err != nil {
318321
return err

graft/coreth/plugin/evm/vm_database.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
package evm
55

66
import (
7+
"context"
8+
"errors"
9+
"path/filepath"
10+
"strconv"
711
"time"
812

913
"github.com/ava-labs/libevm/common"
@@ -13,13 +17,15 @@ import (
1317
"github.com/ava-labs/avalanchego/database/prefixdb"
1418
"github.com/ava-labs/avalanchego/database/versiondb"
1519
"github.com/ava-labs/avalanchego/vms/evm/database"
20+
"github.com/ava-labs/avalanchego/vms/evm/database/blockdb"
1621

1722
avalanchedatabase "github.com/ava-labs/avalanchego/database"
23+
heightindexdb "github.com/ava-labs/avalanchego/x/blockdb"
1824
)
1925

2026
// initializeDBs initializes the databases used by the VM.
2127
// coreth always uses the avalanchego provided database.
22-
func (vm *VM) initializeDBs(db avalanchedatabase.Database) {
28+
func (vm *VM) initializeDBs(db avalanchedatabase.Database) error {
2329
// Use NewNested rather than New so that the structure of the database
2430
// remains the same regardless of the provided baseDB type.
2531
vm.chaindb = rawdb.NewDatabase(database.New(prefixdb.NewNested(ethDBPrefix, db)))
@@ -30,6 +36,12 @@ func (vm *VM) initializeDBs(db avalanchedatabase.Database) {
3036
// that warp signatures are committed to the database atomically with
3137
// the last accepted block.
3238
vm.warpDB = prefixdb.New(warpPrefix, db)
39+
40+
// initBlockDB must be called after acceptedBlockDB and chaindb is created.
41+
if err := vm.initBlockDB(db); err != nil {
42+
return err
43+
}
44+
return nil
3345
}
3446

3547
func (vm *VM) inspectDatabases() error {
@@ -80,3 +92,48 @@ func inspectDB(db avalanchedatabase.Database, label string) error {
8092
log.Info("Database statistics", "label", label, "total", total.String(), "count", count)
8193
return nil
8294
}
95+
96+
// initBlockDB wraps the chaindb with a blockdb.Database that
97+
// stores blocks data in separate databases when enabled.
98+
func (vm *VM) initBlockDB(db avalanchedatabase.Database) error {
99+
// Error if block database has been created and then disabled
100+
metaDB := prefixdb.New(blockDBPrefix, db)
101+
enabled, err := blockdb.IsEnabled(metaDB)
102+
if err != nil {
103+
return err
104+
}
105+
if !vm.config.BlockDatabaseEnabled {
106+
if enabled {
107+
return errors.New("block database should not be disabled after it has been enabled")
108+
}
109+
return nil
110+
}
111+
112+
version := strconv.FormatUint(heightindexdb.IndexFileVersion, 10)
113+
path := filepath.Join(vm.ctx.ChainDataDir, "blockdb", version)
114+
_, lastAcceptedHeight, err := vm.ReadLastAccepted()
115+
if err != nil {
116+
return err
117+
}
118+
stateSyncEnabled := vm.stateSyncEnabled(lastAcceptedHeight)
119+
cfg := heightindexdb.DefaultConfig().WithSyncToDisk(false)
120+
blockDB, initialized, err := blockdb.New(
121+
metaDB,
122+
vm.chaindb,
123+
path,
124+
stateSyncEnabled,
125+
cfg,
126+
vm.ctx.Log,
127+
vm.sdkMetrics,
128+
)
129+
if err != nil {
130+
return err
131+
}
132+
if initialized && !vm.config.SkipBlockDatabaseAutoMigrate {
133+
if err := blockDB.StartMigration(context.Background()); err != nil {
134+
return err
135+
}
136+
}
137+
vm.chaindb = blockDB
138+
return nil
139+
}

graft/coreth/plugin/evm/vm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,6 @@ func TestInspectDatabases(t *testing.T) {
22062206
db = memdb.New()
22072207
)
22082208

2209-
vm.initializeDBs(db)
2209+
require.NoError(t, vm.initializeDBs(db))
22102210
require.NoError(t, vm.inspectDatabases())
22112211
}

graft/coreth/sync/blocksync/syncer.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/ava-labs/libevm/ethdb"
1414
"github.com/ava-labs/libevm/log"
1515

16+
"github.com/ava-labs/avalanchego/vms/evm/database/blockdb"
17+
1618
syncpkg "github.com/ava-labs/avalanchego/graft/coreth/sync"
1719
statesyncclient "github.com/ava-labs/avalanchego/graft/coreth/sync/client"
1820
)
@@ -75,6 +77,19 @@ func (s *BlockSyncer) Sync(ctx context.Context) error {
7577
nextHeight := s.fromHeight
7678
blocksToFetch := s.blocksToFetch
7779

80+
// Init blockdb with the first block we will be fetching
81+
firstBlockToFetch := nextHeight - blocksToFetch + 1
82+
if db, ok := s.db.(*blockdb.Database); ok {
83+
log.Info(
84+
"Initializing block databases on block syncer",
85+
"nextHeight", nextHeight,
86+
"minHeight", firstBlockToFetch,
87+
)
88+
if err := db.InitBlockDBs(firstBlockToFetch); err != nil {
89+
return err
90+
}
91+
}
92+
7893
// first, check for blocks already available on disk so we don't
7994
// request them from peers.
8095
for blocksToFetch > 0 {

0 commit comments

Comments
 (0)