From 443b140d6e9e44ee2de7e861af3aaa419ae706b4 Mon Sep 17 00:00:00 2001 From: VM Date: Wed, 28 Feb 2024 10:34:46 +0800 Subject: [PATCH] core: set path mode as default --- cmd/utils/flags.go | 3 +++ core/blockchain.go | 2 +- core/chain_makers.go | 6 ++++-- core/rawdb/accessors_trie.go | 8 ++++---- core/state/database.go | 1 + graphql/graphql_test.go | 4 +++- trie/database.go | 28 +++++++++++++--------------- trie/triedb/pathdb/database.go | 2 +- 8 files changed, 30 insertions(+), 24 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index f94d381363..f8c01b5487 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -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 diff --git a/core/blockchain.go b/core/blockchain.go index 8ecf8bd3bf..0c21c7c203 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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() } diff --git a/core/chain_makers.go b/core/chain_makers.go index cb6ed504c8..1786a8e1ea 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -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. @@ -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++ { diff --git a/core/rawdb/accessors_trie.go b/core/rawdb/accessors_trie.go index e392809924..f06c2f2f7b 100644 --- a/core/rawdb/accessors_trie.go +++ b/core/rawdb/accessors_trie.go @@ -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 diff --git a/core/state/database.go b/core/state/database.go index 071881b43e..3c726ca1e9 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -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 diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 65b85a0922..9640b5a798 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -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" @@ -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, }, { @@ -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 { diff --git a/trie/database.go b/trie/database.go index df83dd081c..369a91c65b 100644 --- a/trie/database.go +++ b/trie/database.go @@ -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 @@ -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 { @@ -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 } diff --git a/trie/triedb/pathdb/database.go b/trie/triedb/pathdb/database.go index aacd6b9323..bb5934bf28 100644 --- a/trie/triedb/pathdb/database.go +++ b/trie/triedb/pathdb/database.go @@ -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 }