Skip to content

Commit f3bf7e5

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

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-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: 59 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,13 @@ 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 are initialized
41+
// because it uses them.
42+
if err := vm.initBlockDB(db); err != nil {
43+
return err
44+
}
45+
return nil
3346
}
3447

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

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)