Skip to content

Commit ecf5cc9

Browse files
author
Darioush Jalali
authored
use triedb config for reference root (#1366)
1 parent 84c5361 commit ecf5cc9

File tree

15 files changed

+69
-92
lines changed

15 files changed

+69
-92
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
324324
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward))
325325
}
326326
// Commit block
327-
root, err := statedb.Commit(vmContext.BlockNumber.Uint64(), chainConfig.IsEIP158(vmContext.BlockNumber), false)
327+
root, err := statedb.Commit(vmContext.BlockNumber.Uint64(), chainConfig.IsEIP158(vmContext.BlockNumber))
328328
if err != nil {
329329
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
330330
}
@@ -366,7 +366,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
366366
}
367367
}
368368
// Commit and re-open to start with a clean state.
369-
root, _ := statedb.Commit(0, false, false)
369+
root, _ := statedb.Commit(0, false)
370370
statedb, _ = state.New(root, sdb, nil)
371371
return statedb
372372
}

cmd/evm/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func runCmd(ctx *cli.Context) error {
281281
output, leftOverGas, stats, err := timedExec(bench, execFunc)
282282

283283
if ctx.Bool(DumpFlag.Name) {
284-
statedb.Commit(genesisConfig.Number, true, false)
284+
statedb.Commit(genesisConfig.Number, true)
285285
fmt.Println(string(statedb.Dump(nil)))
286286
}
287287

core/blockchain.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,11 @@ type CacheConfig struct {
196196
// triedbConfig derives the configures for trie database.
197197
func (c *CacheConfig) triedbConfig() *triedb.Config {
198198
config := &triedb.Config{Preimages: c.Preimages}
199-
if c.StateScheme == rawdb.HashScheme {
199+
if c.StateScheme == rawdb.HashScheme || c.StateScheme == "" {
200200
config.HashDB = &hashdb.Config{
201-
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
202-
StatsPrefix: trieCleanCacheStatsNamespace,
201+
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
202+
StatsPrefix: trieCleanCacheStatsNamespace,
203+
ReferenceRootAtomicallyOnUpdate: true,
203204
}
204205
}
205206
if c.StateScheme == rawdb.PathScheme {
@@ -1222,9 +1223,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12221223
// diff layer for the block.
12231224
var err error
12241225
if bc.snaps == nil {
1225-
_, err = state.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), true)
1226+
_, err = state.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()))
12261227
} else {
1227-
_, err = state.CommitWithSnap(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.snaps, block.Hash(), block.ParentHash(), true)
1228+
_, err = state.CommitWithSnap(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.snaps, block.Hash(), block.ParentHash())
12281229
}
12291230
if err != nil {
12301231
return err
@@ -1756,9 +1757,9 @@ func (bc *BlockChain) reprocessBlock(parent *types.Block, current *types.Block)
17561757
// If snapshots are enabled, call CommitWithSnaps to explicitly create a snapshot
17571758
// diff layer for the block.
17581759
if bc.snaps == nil {
1759-
return statedb.Commit(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), false)
1760+
return statedb.Commit(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()))
17601761
}
1761-
return statedb.CommitWithSnap(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), bc.snaps, current.Hash(), current.ParentHash(), false)
1762+
return statedb.CommitWithSnap(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), bc.snaps, current.Hash(), current.ParentHash())
17621763
}
17631764

17641765
// initSnapshot instantiates a Snapshot instance and adds it to [bc]
@@ -1899,8 +1900,7 @@ func (bc *BlockChain) reprocessState(current *types.Block, reexec uint64) error
18991900
// Flatten snapshot if initialized, holding a reference to the state root until the next block
19001901
// is processed.
19011902
if err := bc.flattenSnapshot(func() error {
1902-
triedb.Reference(root, common.Hash{})
1903-
if previousRoot != (common.Hash{}) {
1903+
if previousRoot != (common.Hash{}) && previousRoot != root {
19041904
triedb.Dereference(previousRoot)
19051905
}
19061906
previousRoot = root

core/chain_makers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
300300
}
301301

302302
// Write state changes to db
303-
root, err := statedb.Commit(b.header.Number.Uint64(), config.IsEIP158(b.header.Number), false)
303+
root, err := statedb.Commit(b.header.Number.Uint64(), config.IsEIP158(b.header.Number))
304304
if err != nil {
305305
panic(fmt.Sprintf("state write error: %v", err))
306306
}

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (g *Genesis) toBlock(db ethdb.Database, triedb *triedb.Database) *types.Blo
328328
}
329329
}
330330

331-
statedb.Commit(0, false, false)
331+
statedb.Commit(0, false)
332332
// Commit newly generated states into disk if it's not empty.
333333
if root != types.EmptyRootHash {
334334
if err := triedb.Commit(root, true); err != nil {

core/state/state_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestDump(t *testing.T) {
6868
// write some of them to the trie
6969
s.state.updateStateObject(obj1)
7070
s.state.updateStateObject(obj2)
71-
root, _ := s.state.Commit(0, false, false)
71+
root, _ := s.state.Commit(0, false)
7272

7373
// check that DumpToCollector contains the state objects that are in trie
7474
s.state, _ = New(root, tdb, nil)
@@ -127,7 +127,7 @@ func TestIterativeDump(t *testing.T) {
127127
// write some of them to the trie
128128
s.state.updateStateObject(obj1)
129129
s.state.updateStateObject(obj2)
130-
root, _ := s.state.Commit(0, false, false)
130+
root, _ := s.state.Commit(0, false)
131131
s.state, _ = New(root, tdb, nil)
132132

133133
b := &bytes.Buffer{}
@@ -153,7 +153,7 @@ func TestNull(t *testing.T) {
153153
var value common.Hash
154154

155155
s.state.SetState(address, common.Hash{}, value)
156-
s.state.Commit(0, false, false)
156+
s.state.Commit(0, false)
157157

158158
if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) {
159159
t.Errorf("expected empty current value, got %x", value)
@@ -225,7 +225,7 @@ func TestSnapshot2(t *testing.T) {
225225
so0.deleted = false
226226
state.setStateObject(so0)
227227

228-
root, _ := state.Commit(0, false, false)
228+
root, _ := state.Commit(0, false)
229229
state, _ = New(root, state.db, nil)
230230

231231
// and one with deleted == true

core/state/statedb.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,14 +1217,14 @@ func (s *StateDB) handleDestruction(nodes *trienode.MergedNodeSet) (map[common.A
12171217
}
12181218

12191219
// Commit writes the state to the underlying in-memory trie database.
1220-
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, referenceRoot bool) (common.Hash, error) {
1221-
return s.commit(block, deleteEmptyObjects, nil, common.Hash{}, common.Hash{}, referenceRoot)
1220+
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, error) {
1221+
return s.commit(block, deleteEmptyObjects, nil, common.Hash{}, common.Hash{})
12221222
}
12231223

12241224
// CommitWithSnap writes the state to the underlying in-memory trie database and
12251225
// generates a snapshot layer for the newly committed state.
1226-
func (s *StateDB) CommitWithSnap(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash, referenceRoot bool) (common.Hash, error) {
1227-
return s.commit(block, deleteEmptyObjects, snaps, blockHash, parentHash, referenceRoot)
1226+
func (s *StateDB) CommitWithSnap(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash) (common.Hash, error) {
1227+
return s.commit(block, deleteEmptyObjects, snaps, blockHash, parentHash)
12281228
}
12291229

12301230
// Once the state is committed, tries cached in stateDB (including account
@@ -1234,7 +1234,7 @@ func (s *StateDB) CommitWithSnap(block uint64, deleteEmptyObjects bool, snaps *s
12341234
//
12351235
// The associated block number of the state transition is also provided
12361236
// for more chain context.
1237-
func (s *StateDB) commit(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash, referenceRoot bool) (common.Hash, error) {
1237+
func (s *StateDB) commit(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash) (common.Hash, error) {
12381238
// Short circuit in case any database failure occurred earlier.
12391239
if s.dbErr != nil {
12401240
return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
@@ -1343,14 +1343,8 @@ func (s *StateDB) commit(block uint64, deleteEmptyObjects bool, snaps *snapshot.
13431343
if root != origin {
13441344
start := time.Now()
13451345
set := triestate.New(s.accountsOrigin, s.storagesOrigin, incomplete)
1346-
if referenceRoot {
1347-
if err := s.db.TrieDB().UpdateAndReferenceRoot(root, origin, block, nodes, set); err != nil {
1348-
return common.Hash{}, err
1349-
}
1350-
} else {
1351-
if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil {
1352-
return common.Hash{}, err
1353-
}
1346+
if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil {
1347+
return common.Hash{}, err
13541348
}
13551349
s.originalRoot = root
13561350
if metrics.EnabledExpensive {

core/state/statedb_fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (test *stateTest) run() bool {
233233
} else {
234234
state.IntermediateRoot(true) // call intermediateRoot at the transaction boundary
235235
}
236-
nroot, err := state.Commit(0, true, false) // call commit at the block boundary
236+
nroot, err := state.Commit(0, true) // call commit at the block boundary
237237
if err != nil {
238238
panic(err)
239239
}

core/state/statedb_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ func TestIntermediateLeaks(t *testing.T) {
126126
}
127127

128128
// Commit and cross check the databases.
129-
transRoot, err := transState.Commit(0, false, false)
129+
transRoot, err := transState.Commit(0, false)
130130
if err != nil {
131131
t.Fatalf("failed to commit transition state: %v", err)
132132
}
133133
if err = transNdb.Commit(transRoot, false); err != nil {
134134
t.Errorf("can not commit trie %v to persistent database", transRoot.Hex())
135135
}
136136

137-
finalRoot, err := finalState.Commit(0, false, false)
137+
finalRoot, err := finalState.Commit(0, false)
138138
if err != nil {
139139
t.Fatalf("failed to commit final state: %v", err)
140140
}
@@ -542,7 +542,7 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
542542
func TestTouchDelete(t *testing.T) {
543543
s := newStateEnv()
544544
s.state.getOrNewStateObject(common.Address{})
545-
root, _ := s.state.Commit(0, false, false)
545+
root, _ := s.state.Commit(0, false)
546546
s.state, _ = NewWithSnapshot(root, s.state.db, s.state.snap)
547547

548548
snapshot := s.state.Snapshot()
@@ -630,7 +630,7 @@ func TestCopyCommitCopy(t *testing.T) {
630630
t.Fatalf("second copy committed storage slot mismatch: have %x, want %x", val, sval)
631631
}
632632
// Commit state, ensure states can be loaded from disk
633-
root, _ := state.Commit(0, false, false)
633+
root, _ := state.Commit(0, false)
634634
state, _ = New(root, tdb, nil)
635635
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
636636
t.Fatalf("state post-commit balance mismatch: have %v, want %v", balance, 42)
@@ -744,7 +744,7 @@ func TestCommitCopy(t *testing.T) {
744744
t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
745745
}
746746
// Copy the committed state database, the copied one is not functional.
747-
state.Commit(0, true, false)
747+
state.Commit(0, true)
748748
copied := state.Copy()
749749
if balance := copied.GetBalance(addr); balance.Cmp(uint256.NewInt(0)) != 0 {
750750
t.Fatalf("unexpected balance: have %v", balance)
@@ -778,7 +778,7 @@ func TestDeleteCreateRevert(t *testing.T) {
778778
addr := common.BytesToAddress([]byte("so"))
779779
state.SetBalance(addr, uint256.NewInt(1))
780780

781-
root, _ := state.Commit(0, false, false)
781+
root, _ := state.Commit(0, false)
782782
state, _ = NewWithSnapshot(root, state.db, state.snap)
783783

784784
// Simulate self-destructing in one transaction, then create-reverting in another
@@ -790,7 +790,7 @@ func TestDeleteCreateRevert(t *testing.T) {
790790
state.RevertToSnapshot(id)
791791

792792
// Commit the entire state and make sure we don't crash and have the correct state
793-
root, _ = state.Commit(0, true, false)
793+
root, _ = state.Commit(0, true)
794794
state, _ = NewWithSnapshot(root, state.db, state.snap)
795795

796796
if state.getStateObject(addr) != nil {
@@ -833,7 +833,7 @@ func testMissingTrieNodes(t *testing.T, scheme string) {
833833
a2 := common.BytesToAddress([]byte("another"))
834834
state.SetBalance(a2, uint256.NewInt(100))
835835
state.SetCode(a2, []byte{1, 2, 4})
836-
root, _ = state.Commit(0, false, false)
836+
root, _ = state.Commit(0, false)
837837
t.Logf("root: %x", root)
838838
// force-flush
839839
tdb.Commit(root, false)
@@ -857,7 +857,7 @@ func testMissingTrieNodes(t *testing.T, scheme string) {
857857
}
858858
// Modify the state
859859
state.SetBalance(addr, uint256.NewInt(2))
860-
root, err := state.Commit(0, false, false)
860+
root, err := state.Commit(0, false)
861861
if err == nil {
862862
t.Fatalf("expected error, got root :%x", root)
863863
}
@@ -1053,7 +1053,7 @@ func TestFlushOrderDataLoss(t *testing.T) {
10531053
state.SetState(common.Address{a}, common.Hash{a, s}, common.Hash{a, s})
10541054
}
10551055
}
1056-
root, err := state.Commit(0, false, false)
1056+
root, err := state.Commit(0, false)
10571057
if err != nil {
10581058
t.Fatalf("failed to commit state trie: %v", err)
10591059
}
@@ -1132,7 +1132,7 @@ func TestResetObject(t *testing.T) {
11321132
state.CreateAccount(addr)
11331133
state.SetBalance(addr, uint256.NewInt(2))
11341134
state.SetState(addr, slotB, common.BytesToHash([]byte{0x2}))
1135-
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{}, false)
1135+
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{})
11361136

11371137
// Ensure the original account is wiped properly
11381138
snap := snaps.Snapshot(root)
@@ -1163,7 +1163,7 @@ func TestDeleteStorage(t *testing.T) {
11631163
value := common.Hash(uint256.NewInt(uint64(10 * i)).Bytes32())
11641164
state.SetState(addr, slot, value)
11651165
}
1166-
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{}, false)
1166+
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{})
11671167
// Init phase done, create two states, one with snap and one without
11681168
fastState, _ := New(root, db, snaps)
11691169
slowState, _ := New(root, db, nil)

core/txpool/blobpool/blobpool_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ func TestOpenDrops(t *testing.T) {
583583
statedb.AddBalance(crypto.PubkeyToAddress(overcapper.PublicKey), uint256.NewInt(10000000))
584584
statedb.AddBalance(crypto.PubkeyToAddress(duplicater.PublicKey), uint256.NewInt(1000000))
585585
statedb.AddBalance(crypto.PubkeyToAddress(repeater.PublicKey), uint256.NewInt(1000000))
586-
statedb.Commit(0, true, false)
586+
statedb.Commit(0, true)
587587

588588
chain := &testBlockChain{
589589
config: testChainConfig,
@@ -702,7 +702,7 @@ func TestOpenIndex(t *testing.T) {
702702
// Create a blob pool out of the pre-seeded data
703703
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewDatabase(memorydb.New())), nil)
704704
statedb.AddBalance(addr, uint256.NewInt(1_000_000_000))
705-
statedb.Commit(0, true, false)
705+
statedb.Commit(0, true)
706706

707707
chain := &testBlockChain{
708708
config: testChainConfig,
@@ -804,7 +804,7 @@ func TestOpenHeap(t *testing.T) {
804804
statedb.AddBalance(addr1, uint256.NewInt(1_000_000_000))
805805
statedb.AddBalance(addr2, uint256.NewInt(1_000_000_000))
806806
statedb.AddBalance(addr3, uint256.NewInt(1_000_000_000))
807-
statedb.Commit(0, true, false)
807+
statedb.Commit(0, true)
808808

809809
chain := &testBlockChain{
810810
config: testChainConfig,
@@ -884,7 +884,7 @@ func TestOpenCap(t *testing.T) {
884884
statedb.AddBalance(addr1, uint256.NewInt(1_000_000_000))
885885
statedb.AddBalance(addr2, uint256.NewInt(1_000_000_000))
886886
statedb.AddBalance(addr3, uint256.NewInt(1_000_000_000))
887-
statedb.Commit(0, true, false)
887+
statedb.Commit(0, true)
888888

889889
chain := &testBlockChain{
890890
config: testChainConfig,
@@ -1302,7 +1302,7 @@ func TestAdd(t *testing.T) {
13021302
store.Put(blob)
13031303
}
13041304
}
1305-
statedb.Commit(0, true, false)
1305+
statedb.Commit(0, true)
13061306
store.Close()
13071307

13081308
// Create a blob pool out of the pre-seeded dats
@@ -1375,7 +1375,7 @@ func benchmarkPoolPending(b *testing.B, datacap uint64) {
13751375
statedb.AddBalance(addr, uint256.NewInt(1_000_000_000))
13761376
pool.add(tx)
13771377
}
1378-
statedb.Commit(0, true, false)
1378+
statedb.Commit(0, true)
13791379
defer pool.Close()
13801380

13811381
// Benchmark assembling the pending

0 commit comments

Comments
 (0)