Skip to content

Commit

Permalink
core: set path mode as default
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Feb 28, 2024
1 parent b7b64da commit 443b140
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 24 deletions.
3 changes: 3 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if ctx.String(GCModeFlag.Name) == "archive" && cfg.TransactionHistory != 0 {
cfg.TransactionHistory = 0
log.Warn("Disabled transaction unindexing for archive node")

cfg.StateScheme = rawdb.HashScheme
log.Warn("Forcing hash state-scheme for archive mode")
}
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) {
cfg.TrieCleanCache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
go throwaway.TriePrefetchInAdvance(block, signer)
}

//Process block using the parent state as reference point
// Process block using the parent state as reference point
if bc.pipeCommit {
statedb.EnablePipeCommit()
}
Expand Down
6 changes: 4 additions & 2 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
)

// BlockGen creates blocks for testing.
Expand Down Expand Up @@ -339,8 +340,9 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
}
return nil, nil
}
// Forcibly use hash-based state scheme for retaining all nodes in disk.
triedb := trie.NewDatabase(db, trie.HashDefaults)

// Forcibly use path-based state scheme for retaining all nodes in disk.
triedb := trie.NewDatabase(db, &trie.Config{PathDB: pathdb.Defaults})
defer triedb.Close()

for i := 0; i < n; i++ {
Expand Down
8 changes: 4 additions & 4 deletions core/rawdb/accessors_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,10 @@ func ParseStateScheme(provided string, disk ethdb.Database) (string, error) {
stored := ReadStateScheme(disk)
if provided == "" {
if stored == "" {
// use default scheme for empty database, flip it when
// path mode is chosen as default
log.Info("State scheme set to default", "scheme", "hash")
return HashScheme, nil
// use default scheme for empty database, now path mode
// is chosen as default
log.Info("State scheme set to default", "scheme", PathScheme)
return PathScheme, nil
}
log.Info("State scheme set to already existing disk db", "scheme", stored)
return stored, nil // reuse scheme of persistent scheme
Expand Down
1 change: 1 addition & 0 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
}
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
if err != nil {
fmt.Println("Failed to open trie: ", err)
return nil, err
}
return tr, nil
Expand Down
4 changes: 3 additions & 1 deletion graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestGraphQLBlockSerialization(t *testing.T) {
{
body: `{"query": "{block(number:\"0x0\"){number,gasUsed,gasLimit}}","variables": null}`,
want: `{"data":{"block":{"number":"0x0","gasUsed":"0x0","gasLimit":"0xaf79e0"}}}`,
//want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0x0\": invalid syntax"}],"data":{}}`,
// want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0x0\": invalid syntax"}],"data":{}}`,
code: 200,
},
{
Expand Down Expand Up @@ -444,6 +445,7 @@ func newGQLService(t *testing.T, stack *node.Node, shanghai bool, gspec *core.Ge
TrieDirtyCache: 5,
TrieTimeout: 60 * time.Minute,
SnapshotCache: 5,
StateScheme: rawdb.HashScheme,
}
var engine consensus.Engine = ethash.NewFaker()
if shanghai {
Expand Down
28 changes: 13 additions & 15 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,17 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
// Sanitize the config and use the default one if it's not specified.
dbScheme := rawdb.ReadStateScheme(diskdb)
if config == nil {
if dbScheme == rawdb.PathScheme {
config = &Config{
PathDB: pathdb.Defaults,
}
} else {
if dbScheme == rawdb.HashScheme {
config = HashDefaults
} else {
config = &Config{PathDB: pathdb.Defaults}
}
}
if config.PathDB == nil && config.HashDB == nil {
if dbScheme == rawdb.PathScheme {
config.PathDB = pathdb.Defaults
} else {
if dbScheme == rawdb.HashScheme {
config.HashDB = hashdb.Defaults
} else {
config.PathDB = pathdb.Defaults
}
}
var preimages *preimageStore
Expand All @@ -136,7 +134,7 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
/*
* 1. First, initialize db according to the user config
* 2. Second, initialize the db according to the scheme already used by db
* 3. Last, use the default scheme, namely hash scheme
* 3. Last, use the default scheme, namely path scheme
*/
if config.HashDB != nil {
if rawdb.ReadStateScheme(diskdb) == rawdb.PathScheme {
Expand All @@ -148,16 +146,16 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
log.Warn("incompatible state scheme", "old", rawdb.HashScheme, "new", rawdb.PathScheme)
}
db.backend = pathdb.New(diskdb, config.PathDB)
} else if strings.Compare(dbScheme, rawdb.PathScheme) == 0 {
if config.PathDB == nil {
config.PathDB = pathdb.Defaults
}
db.backend = pathdb.New(diskdb, config.PathDB)
} else {
} else if strings.Compare(dbScheme, rawdb.HashScheme) == 0 {
if config.HashDB == nil {
config.HashDB = hashdb.Defaults
}
db.backend = hashdb.New(diskdb, config.HashDB, mptResolver{})
} else {
if config.PathDB == nil {
config.PathDB = pathdb.Defaults
}
db.backend = pathdb.New(diskdb, config.PathDB)
}
return db
}
Expand Down
2 changes: 1 addition & 1 deletion trie/triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func New(diskdb ethdb.Database, config *Config) *Database {
log.Warn("Truncated extra state histories", "number", pruned)
}
}
log.Warn("Path-based state scheme is an experimental feature", "sync", db.config.SyncFlush)
log.Info("Using path-based state scheme", "sync", db.config.SyncFlush)
return db
}

Expand Down

0 comments on commit 443b140

Please sign in to comment.