Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 14ecb1b

Browse files
all: enable pbss (#600)
* trie: enable pathdb: add path config and enable tests * core/rawdb: now also inspect the state freezer in pathdb; rename * cmd: working on cmd ronin * core: refactor; add pathbase config; fix tests - all: fix and enable tests for pathbase - blockchain: open triedb explicitly in blockchain functions and close right after use, since diskLayer inside pathdb is a skeleton - blockchain: when writeBlockWithState, pathbase will skip the explicit garbage collector, which is only needed for hashbase - genesis.go: nit: change check genesis state, ref ethereum/go-ethereum@08bf8a6 * tests: enable path tests * eth: enable path scheme - all: fix tests, enable path scheme tests - state_accessor: split function to retrieve statedb from block to hash scheme and path scheme * light, miner, les, ethclient: clean up tests * trie: refactor triereader, return err when state reader won't be created in hash and path * trie: fix failed test in iterator and sync test tie * trie,core: improve trie reader and add checking config nil when initing database * trie: statedb instance is committed, then it's not usable, a new instance must be created based on new root updated database, reference by commit 6d2aeb4 * cmd,les,eth: fixed unittest and adding flag Parrallel correctly * core, eth: fix tests * core: refactor and fix sync_test logic * tmp: disable pathbase for TestIsPeriodBlock, TestIsTrippEffective --------- Co-authored-by: Huy Ngo <[email protected]>
1 parent 8de85b0 commit 14ecb1b

File tree

128 files changed

+3013
-1330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+3013
-1330
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/ethereum/go-ethereum/log"
4444
"github.com/ethereum/go-ethereum/params"
4545
"github.com/ethereum/go-ethereum/rpc"
46+
"github.com/ethereum/go-ethereum/trie"
4647
)
4748

4849
// This nil assignment ensures at compile time that SimulatedBackend implements bind.ContractBackend.
@@ -77,7 +78,7 @@ type SimulatedBackend struct {
7778
// A simulated backend always uses chainID 1337.
7879
func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
7980
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
80-
genesis.MustCommit(database)
81+
genesis.MustCommit(database, trie.NewDatabase(database, trie.HashDefaults))
8182
blockchain, _ := core.NewBlockChain(database, nil, &genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
8283

8384
backend := &SimulatedBackend{

cmd/evm/internal/t8ntool/execution.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
258258
Difficulty: (*math.HexOrDecimal256)(vmContext.Difficulty),
259259
GasUsed: (math.HexOrDecimal64)(gasUsed),
260260
}
261+
// Re-create statedb instance with new root upon the updated database
262+
// for accessing latest states.
263+
statedb, err = state.New(root, statedb.Database(), nil)
264+
if err != nil {
265+
return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
266+
}
261267
return statedb, execRs, nil
262268
}
263269

cmd/evm/runner.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ import (
3030

3131
"github.com/ethereum/go-ethereum/eth/tracers/logger"
3232
"github.com/ethereum/go-ethereum/internal/flags"
33+
"github.com/ethereum/go-ethereum/trie"
34+
"github.com/ethereum/go-ethereum/trie/triedb/hashdb"
3335

3436
"github.com/ethereum/go-ethereum/cmd/evm/internal/compiler"
3537
"github.com/ethereum/go-ethereum/cmd/utils"
3638
"github.com/ethereum/go-ethereum/common"
3739
"github.com/ethereum/go-ethereum/core"
3840
"github.com/ethereum/go-ethereum/core/rawdb"
3941
"github.com/ethereum/go-ethereum/core/state"
42+
"github.com/ethereum/go-ethereum/core/types"
4043
"github.com/ethereum/go-ethereum/core/vm"
4144
"github.com/ethereum/go-ethereum/core/vm/runtime"
4245
"github.com/ethereum/go-ethereum/log"
@@ -139,11 +142,22 @@ func runCmd(ctx *cli.Context) error {
139142
gen := readGenesis(ctx.String(GenesisFlag.Name))
140143
genesisConfig = gen
141144
db := rawdb.NewMemoryDatabase()
142-
genesis := gen.MustCommit(db)
143-
statedb, _ = state.New(genesis.Root(), state.NewDatabase(db), nil)
145+
triedb := trie.NewDatabase(db, &trie.Config{
146+
HashDB: hashdb.Defaults,
147+
})
148+
defer triedb.Close()
149+
genesis := gen.MustCommit(db, triedb)
150+
sdb := state.NewDatabaseWithNodeDB(db, triedb)
151+
statedb, _ = state.New(genesis.Root(), sdb, nil)
144152
chainConfig = gen.Config
145153
} else {
146-
statedb, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
154+
db := rawdb.NewMemoryDatabase()
155+
triedb := trie.NewDatabase(db, &trie.Config{
156+
HashDB: hashdb.Defaults,
157+
})
158+
defer triedb.Close()
159+
sdb := state.NewDatabaseWithNodeDB(db, triedb)
160+
statedb, _ = state.New(types.EmptyRootHash, sdb, nil)
147161
genesisConfig = new(core.Genesis)
148162
}
149163
if ctx.String(SenderFlag.Name) != "" {

cmd/evm/staterunner.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"fmt"
23-
"github.com/ethereum/go-ethereum/eth/tracers/logger"
2423
"io/ioutil"
2524
"os"
2625

26+
"github.com/ethereum/go-ethereum/eth/tracers/logger"
27+
28+
"github.com/ethereum/go-ethereum/core/rawdb"
2729
"github.com/ethereum/go-ethereum/core/state"
30+
"github.com/ethereum/go-ethereum/core/state/snapshot"
2831
"github.com/ethereum/go-ethereum/core/vm"
2932
"github.com/ethereum/go-ethereum/log"
3033
"github.com/ethereum/go-ethereum/tests"
@@ -96,21 +99,22 @@ func stateTestCmd(ctx *cli.Context) error {
9699
results := make([]StatetestResult, 0, len(tests))
97100
for key, test := range tests {
98101
for _, st := range test.Subtests() {
102+
dump := ctx.Bool(DumpFlag.Name)
99103
// Run the test and aggregate the result
100104
result := &StatetestResult{Name: key, Fork: st.Fork, Pass: true}
101-
_, s, err := test.Run(st, cfg, false)
102-
// print state root for evmlab tracing
103-
if ctx.Bool(MachineFlag.Name) && s != nil {
104-
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", s.IntermediateRoot(false))
105-
}
106-
if err != nil {
107-
// Test failed, mark as so and dump any state to aid debugging
108-
result.Pass, result.Error = false, err.Error()
109-
if ctx.Bool(DumpFlag.Name) && s != nil {
110-
dump := s.RawDump(nil)
111-
result.State = &dump
105+
test.Run(st, cfg, false, rawdb.HashScheme, func(err error, snaps *snapshot.Tree, state *state.StateDB) {
106+
if err != nil {
107+
// Test failed, mark as so and dump any state to aid debugging
108+
result.Pass, result.Error = false, err.Error()
109+
if dump {
110+
dump := state.RawDump(nil)
111+
result.State = &dump
112+
}
112113
}
113-
}
114+
if ctx.Bool(MachineFlag.Name) && state != nil {
115+
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", state.IntermediateRoot(false))
116+
}
117+
})
114118

115119
results = append(results, *result)
116120

cmd/ronin/chaincmd.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/ethereum/go-ethereum/log"
3939
"github.com/ethereum/go-ethereum/metrics"
4040
"github.com/ethereum/go-ethereum/node"
41-
"github.com/ethereum/go-ethereum/trie"
4241
"github.com/urfave/cli/v2"
4342
)
4443

@@ -52,6 +51,8 @@ var (
5251
utils.DataDirFlag,
5352
utils.DBEngineFlag,
5453
utils.ForceOverrideChainConfigFlag,
54+
utils.CachePreimagesFlag,
55+
utils.StateSchemeFlag,
5556
},
5657
Category: "BLOCKCHAIN COMMANDS",
5758
Description: `
@@ -106,6 +107,9 @@ The dumpgenesis command dumps the genesis block configuration in JSON format to
106107
utils.MetricsInfluxDBBucketFlag,
107108
utils.MetricsInfluxDBOrganizationFlag,
108109
utils.TxLookupLimitFlag,
110+
utils.TransactionHistoryFlag,
111+
utils.StateSchemeFlag,
112+
utils.StateHistoryFlag,
109113
},
110114
Category: "BLOCKCHAIN COMMANDS",
111115
Description: `
@@ -125,6 +129,7 @@ processing will proceed even if an individual RLP-file import failure occurs.`,
125129
utils.DBEngineFlag,
126130
utils.CacheFlag,
127131
utils.SyncModeFlag,
132+
utils.StateSchemeFlag,
128133
},
129134
Category: "BLOCKCHAIN COMMANDS",
130135
Description: `
@@ -226,14 +231,13 @@ func initGenesis(ctx *cli.Context) error {
226231
utils.Fatalf("Failed to open database: %v", err)
227232
}
228233
// Create triedb firstly
229-
triedb := trie.NewDatabaseWithConfig(chaindb, &trie.Config{
230-
Preimages: ctx.Bool(utils.CachePreimagesFlag.Name),
231-
})
234+
235+
triedb := utils.MakeTrieDatabase(ctx, chaindb, ctx.Bool(utils.CachePreimagesFlag.Name), false)
236+
defer chaindb.Close()
232237
_, hash, err := core.SetupGenesisBlock(chaindb, triedb, genesis, overrideChainConfig)
233238
if err != nil {
234239
utils.Fatalf("Failed to write genesis block: %v", err)
235240
}
236-
chaindb.Close()
237241
log.Info("Successfully wrote genesis state", "database", name, "hash", hash)
238242
}
239243
return nil
@@ -471,10 +475,10 @@ func dump(ctx *cli.Context) error {
471475
if err != nil {
472476
return err
473477
}
474-
config := &trie.Config{
475-
Preimages: true, // always enable preimage lookup
476-
}
477-
state, err := state.New(root, state.NewDatabaseWithConfig(db, config), nil)
478+
triedb := utils.MakeTrieDatabase(ctx, db, true, false) // always enable preimage lookup
479+
defer triedb.Close()
480+
state, err := state.New(root, state.NewDatabaseWithNodeDB(db, triedb), nil)
481+
478482
if err != nil {
479483
return err
480484
}

cmd/ronin/dbcmd.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ WARNING: This is a low-level operation which may cause database corruption!`,
190190
utils.SepoliaFlag,
191191
utils.RinkebyFlag,
192192
utils.GoerliFlag,
193+
utils.StateSchemeFlag,
193194
},
194195
Description: "This command looks up the specified database key from the database.",
195196
}
@@ -476,6 +477,8 @@ func dbDumpTrie(ctx *cli.Context) error {
476477

477478
db := utils.MakeChainDatabase(ctx, stack, true)
478479
defer db.Close()
480+
triedb := utils.MakeTrieDatabase(ctx, db, false, true)
481+
defer triedb.Close()
479482
var (
480483
state []byte
481484
storage []byte
@@ -509,12 +512,16 @@ func dbDumpTrie(ctx *cli.Context) error {
509512
}
510513
}
511514
id := trie.StorageTrieID(common.BytesToHash(state), common.BytesToHash(account), common.BytesToHash(storage))
512-
theTrie, err := trie.New(id, trie.NewDatabase(db))
515+
theTrie, err := trie.New(id, triedb)
516+
if err != nil {
517+
return err
518+
}
519+
trieIt, err := theTrie.NodeIterator(start)
513520
if err != nil {
514521
return err
515522
}
516523
var count int64
517-
it := trie.NewIterator(theTrie.NodeIterator(start))
524+
it := trie.NewIterator(trieIt)
518525
for it.Next() {
519526
if max > 0 && count == max {
520527
fmt.Printf("Exiting after %d values\n", count)

cmd/ronin/genesis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ func TestCustomBackend(t *testing.T) {
173173
{ // Can't start pebble on top of leveldb
174174
initArgs: []string{"--db.engine", "leveldb"},
175175
execArgs: []string{"--db.engine", "pebble"},
176-
execExpect: `Fatal: Failed to register the Ethereum service: db.engine choice was pebble but found pre-existing leveldb database in specified data directory`,
176+
execExpect: `Fatal: Could not open database: db.engine choice was pebble but found pre-existing leveldb database in specified data directory`,
177177
},
178178
{ // Can't start leveldb on top of pebble
179179
initArgs: []string{"--db.engine", "pebble"},
180180
execArgs: []string{"--db.engine", "leveldb"},
181-
execExpect: `Fatal: Failed to register the Ethereum service: db.engine choice was leveldb but found pre-existing pebble database in specified data directory`,
181+
execExpect: `Fatal: Could not open database: db.engine choice was leveldb but found pre-existing pebble database in specified data directory`,
182182
},
183183
{ // Reject invalid backend choice
184184
initArgs: []string{"--db.engine", "mssql"},

cmd/ronin/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ var (
101101
utils.ExitWhenSyncedFlag,
102102
utils.GCModeFlag,
103103
utils.SnapshotFlag,
104-
utils.TxLookupLimitFlag,
104+
utils.TxLookupLimitFlag, // deprecated
105+
utils.TransactionHistoryFlag,
106+
utils.StateSchemeFlag,
107+
utils.StateHistoryFlag,
105108
utils.TriesInMemoryFlag,
106109
utils.LightServeFlag,
107110
utils.LightIngressFlag,

0 commit comments

Comments
 (0)