Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion graft/coreth/plugin/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ type Config struct {
StateSyncRequestSize uint16 `json:"state-sync-request-size"`

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

// SkipUpgradeCheck disables checking that upgrades must take place before the last
// accepted block. Skipping this check is useful when a node operator does not update
Expand Down
5 changes: 4 additions & 1 deletion graft/coreth/plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ var (
metadataPrefix = []byte("metadata")
warpPrefix = []byte("warp")
ethDBPrefix = []byte("ethdb")
blockDBPrefix = []byte("blockdb")
)

var (
Expand Down Expand Up @@ -312,7 +313,9 @@ func (vm *VM) Initialize(
}

// Initialize the database
vm.initializeDBs(db)
if err := vm.initializeDBs(db); err != nil {
return err
}
if vm.config.InspectDatabase {
if err := vm.inspectDatabases(); err != nil {
return err
Expand Down
69 changes: 68 additions & 1 deletion graft/coreth/plugin/evm/vm_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@
package evm

import (
"errors"
"path/filepath"
"strconv"
"time"

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/rawdb"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/log"

"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/vms/evm/database"
"github.com/ava-labs/avalanchego/vms/evm/database/blockdb"

avalanchedatabase "github.com/ava-labs/avalanchego/database"
heightindexdb "github.com/ava-labs/avalanchego/x/blockdb"
)

const blockDBFolder = "blockdb"

// initializeDBs initializes the databases used by the VM.
// coreth always uses the avalanchego provided database.
func (vm *VM) initializeDBs(db avalanchedatabase.Database) {
func (vm *VM) initializeDBs(db avalanchedatabase.Database) error {
// Use NewNested rather than New so that the structure of the database
// remains the same regardless of the provided baseDB type.
vm.chaindb = rawdb.NewDatabase(database.New(prefixdb.NewNested(ethDBPrefix, db)))
Expand All @@ -30,6 +38,15 @@ func (vm *VM) initializeDBs(db avalanchedatabase.Database) {
// that warp signatures are committed to the database atomically with
// the last accepted block.
vm.warpDB = prefixdb.New(warpPrefix, db)

// newChainDB must be created after acceptedBlockDB because it uses it to
// determine if state sync is enabled.
chaindb, err := vm.newChainDB(db)
if err != nil {
return err
}
vm.chaindb = chaindb
return nil
}

func (vm *VM) inspectDatabases() error {
Expand Down Expand Up @@ -80,3 +97,53 @@ func inspectDB(db avalanchedatabase.Database, label string) error {
log.Info("Database statistics", "label", label, "total", total.String(), "count", count)
return nil
}

// newChainDB creates a new chain database
// If block database is enabled, it will create a blockdb.Database that
// stores blocks data in separate databases.
// If block database is not enabled but had been previously enabled, it will return an error.
func (vm *VM) newChainDB(db avalanchedatabase.Database) (ethdb.Database, error) {
// Use NewNested rather than New so that the structure of the database
// remains the same regardless of the provided baseDB type.
chainDB := rawdb.NewDatabase(database.New(prefixdb.NewNested(ethDBPrefix, db)))

// Error if block database has been enabled/created and then disabled
stateDB := prefixdb.New(blockDBPrefix, db)
enabled, err := blockdb.IsEnabled(stateDB)
if err != nil {
return nil, err
}
if !vm.config.BlockDatabaseEnabled {
if enabled {
return nil, errors.New("block database should not be disabled after it has been enabled")
}
return chainDB, nil
}

version := strconv.FormatUint(heightindexdb.IndexFileVersion, 10)
dbPath := filepath.Join(vm.ctx.ChainDataDir, blockDBFolder, version)
_, lastAcceptedHeight, err := vm.ReadLastAccepted()
if err != nil {
return nil, err
}
stateSyncEnabled := vm.stateSyncEnabled(lastAcceptedHeight)
config := heightindexdb.DefaultConfig().WithSyncToDisk(false)
blockDB, initialized, err := blockdb.New(
stateDB,
chainDB,
dbPath,
stateSyncEnabled,
config,
vm.ctx.Log,
vm.sdkMetrics,
)
if err != nil {
return nil, err
}
if initialized && !vm.config.SkipBlockDatabaseAutoMigrate {
if err := blockDB.StartMigration(); err != nil {
return nil, err
}
}
return blockDB, nil
}
14 changes: 13 additions & 1 deletion graft/coreth/sync/blocksync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"errors"
"fmt"

"github.com/ava-labs/avalanchego/vms/evm/database/blockdb"

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Build Antithesis avalanchego images

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Build Antithesis xsvm images

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-24.04)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-24.04)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-22.04)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-22.04)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (custom-arm64-jammy)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (custom-arm64-jammy)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (custom-arm64-noble)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (custom-arm64-noble)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (macos-14)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:

Check failure on line 11 in graft/coreth/sync/blocksync/syncer.go

View workflow job for this annotation

GitHub Actions / Unit (macos-14)

no required module provides package github.com/ava-labs/avalanchego/vms/evm/database/blockdb; to add it:
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/rawdb"
"github.com/ava-labs/libevm/ethdb"
Expand Down Expand Up @@ -75,10 +76,21 @@
nextHeight := s.fromHeight
blocksToFetch := s.blocksToFetch

// Init blockdb with the first block we will be fetching
firstBlockToFetch := nextHeight - blocksToFetch + 1
if db, ok := s.db.(*blockdb.Database); ok {
log.Info(
"Initializing block databases on block syncer",
"nextHeight", nextHeight,
"minHeight", firstBlockToFetch,
)
db.InitBlockDBs(firstBlockToFetch)
}

// first, check for blocks already available on disk so we don't
// request them from peers.
for blocksToFetch > 0 {
blk := rawdb.ReadBlock(s.db, nextHash, nextHeight)
blk := blockdb.ReadBlock(s.db, nextHash, nextHeight)
if blk == nil {
// block was not found
break
Expand Down
Loading