Skip to content

Commit 226ff65

Browse files
committed
update prefetch
remove prefetcher
1 parent 4607f60 commit 226ff65

27 files changed

+170
-119
lines changed

cmd/evm/internal/t8ntool/execution.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
166166
if chainConfig.IsByzantium(vmContext.BlockNumber) {
167167
statedb.Finalise(true)
168168
} else {
169-
stateRoot, err := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
169+
stateRoot := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
170170
if err != nil {
171171
return nil, nil, err
172172
}
@@ -202,10 +202,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
202202
txIndex++
203203
}
204204

205-
_, err := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
206-
if err != nil {
207-
return nil, nil, err
208-
}
205+
statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
209206
// Add mining reward?
210207
if miningReward > 0 {
211208
// Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
@@ -231,7 +228,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
231228
statedb.AddBalance(pre.Env.Coinbase, minerReward)
232229
}
233230
// Commit block
234-
root, _, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber), nil)
231+
statedb.Finalise(chainConfig.IsEIP158(vmContext.BlockNumber))
232+
statedb.AccountsIntermediateRoot()
233+
root, _, err := statedb.Commit(nil)
235234
if err != nil {
236235
fmt.Fprintf(os.Stderr, "Could not commit state: %v", err)
237236
return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
@@ -260,7 +259,9 @@ func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB
260259
}
261260
}
262261
// Commit and re-open to start with a clean state.
263-
root, _, _ := statedb.Commit(false, nil)
262+
statedb.Finalise(false)
263+
statedb.AccountsIntermediateRoot()
264+
root, _, _ := statedb.Commit(nil)
264265
statedb, _ = state.New(root, sdb, nil)
265266
return statedb
266267
}

cmd/evm/runner.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,10 @@ func runCmd(ctx *cli.Context) error {
268268
output, leftOverGas, stats, err := timedExec(bench, execFunc)
269269

270270
if ctx.GlobalBool(DumpFlag.Name) {
271-
statedb.Commit(true, nil)
272-
_, err := statedb.IntermediateRoot(true)
273-
if err != nil {
274-
return err
275-
}
271+
statedb.Finalise(true)
272+
statedb.AccountsIntermediateRoot()
273+
statedb.Commit(nil)
274+
statedb.IntermediateRoot(true)
276275
fmt.Println(string(statedb.Dump(false, false, true)))
277276
}
278277

cmd/evm/staterunner.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ func stateTestCmd(ctx *cli.Context) error {
101101
_, state, err := test.Run(st, cfg, false)
102102
// print state root for evmlab tracing
103103
if ctx.GlobalBool(MachineFlag.Name) && state != nil {
104-
root, err := state.IntermediateRoot(false)
105-
if err != nil {
106-
return err
107-
}
104+
root := state.IntermediateRoot(false)
108105
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", root)
109106
}
110107
if err != nil {

consensus/clique/clique.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
550550
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction, uncles []*types.Header,
551551
receipts *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64) (err error) {
552552
// No block rewards in PoA, so the state remains as is and uncles are dropped
553-
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
553+
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
554554
header.UncleHash = types.CalcUncleHash(nil)
555555
return
556556
}
@@ -561,7 +561,7 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
561561
txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error) {
562562
// No block rewards in PoA, so the state remains as is and uncles are dropped
563563
var err error
564-
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
564+
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
565565
if err != nil {
566566
return nil, nil, err
567567
}

consensus/ethash/consensus.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.
582582
receipts *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64) (err error) {
583583
// Accumulate any block and uncle rewards and commit the final state root
584584
accumulateRewards(chain.Config(), state, header, uncles)
585-
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
585+
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
586586
return
587587
}
588588

consensus/parlia/parlia.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
769769
wg := sync.WaitGroup{}
770770
wg.Add(2)
771771
go func() {
772-
rootHash, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
772+
rootHash = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
773773
wg.Done()
774774
}()
775775
go func() {
@@ -1211,11 +1211,7 @@ func (p *Parlia) applyTransaction(
12111211
if p.chainConfig.IsByzantium(header.Number) {
12121212
state.Finalise(true)
12131213
} else {
1214-
stateRoot, err := state.IntermediateRoot(p.chainConfig.IsEIP158(header.Number))
1215-
if err != nil {
1216-
return err
1217-
}
1218-
root = stateRoot.Bytes()
1214+
root = state.IntermediateRoot(p.chainConfig.IsEIP158(header.Number)).Bytes()
12191215
}
12201216
*usedGas += gasUsed
12211217
receipt := types.NewReceipt(root, false, *usedGas)

core/block_validator.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,25 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
131131
receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil))
132132
if receiptSha != header.ReceiptHash {
133133
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
134-
} else {
135-
return nil
136134
}
135+
return nil
137136
},
138137
}
139-
if !skipHeavyVerify {
138+
if skipHeavyVerify {
139+
validateFuns = append(validateFuns, func() error {
140+
if err := statedb.WaitPipeVerification(); err != nil {
141+
return err
142+
}
143+
statedb.Finalise(v.config.IsEIP158(header.Number))
144+
statedb.AccountsIntermediateRoot()
145+
return nil
146+
})
147+
} else {
140148
validateFuns = append(validateFuns, func() error {
141-
if root, err := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root || err != nil {
142-
return fmt.Errorf("invalid merkle root (remote: %x local: %x), err %v", header.Root, root, err)
143-
} else {
144-
return nil
149+
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
150+
return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root)
145151
}
152+
return nil
146153
})
147154
}
148155
validateRes := make(chan error, len(validateFuns))

core/blockchain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
17791779
}
17801780

17811781
// Commit all cached state changes into underlying memory database.
1782-
_, diffLayer, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()), bc.tryRewindBadBlocks, tryCommitTrieDB)
1782+
_, diffLayer, err := state.Commit(bc.tryRewindBadBlocks, tryCommitTrieDB)
17831783
if err != nil {
17841784
return NonStatTy, err
17851785
}

core/blockchain_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ func testBlockChainImport(chain types.Blocks, pipelineCommit bool, blockchain *B
217217
blockchain.chainmu.Lock()
218218
rawdb.WriteTd(blockchain.db, block.Hash(), block.NumberU64(), new(big.Int).Add(block.Difficulty(), blockchain.GetTdByHash(block.ParentHash())))
219219
rawdb.WriteBlock(blockchain.db, block)
220-
statedb.Commit(false, nil)
220+
statedb.Finalise(false)
221+
statedb.AccountsIntermediateRoot()
222+
statedb.Commit(nil)
221223
blockchain.chainmu.Unlock()
222224
}
223225
return nil

core/chain_makers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
223223
block, _, _ := b.engine.FinalizeAndAssemble(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)
224224

225225
// Write state changes to db
226-
root, _, err := statedb.Commit(config.IsEIP158(b.header.Number), nil)
226+
root, _, err := statedb.Commit(nil)
227227
if err != nil {
228228
panic(fmt.Sprintf("state write error: %v", err))
229229
}
@@ -254,7 +254,7 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.S
254254
} else {
255255
time = parent.Time() + 10 // block time is fixed at 10 seconds
256256
}
257-
root, _ := state.IntermediateRoot(chain.Config().IsEIP158(parent.Number()))
257+
root := state.IntermediateRoot(chain.Config().IsEIP158(parent.Number()))
258258
return &types.Header{
259259
Root: root,
260260
ParentHash: parent.Hash(),

core/genesis.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
278278
statedb.SetState(addr, key, value)
279279
}
280280
}
281-
root, _ := statedb.IntermediateRoot(false)
281+
root := statedb.IntermediateRoot(false)
282282
head := &types.Header{
283283
Number: new(big.Int).SetUint64(g.Number),
284284
Nonce: types.EncodeNonce(g.Nonce),
@@ -298,7 +298,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
298298
if g.Difficulty == nil {
299299
head.Difficulty = params.GenesisDifficulty
300300
}
301-
statedb.Commit(false, nil)
301+
statedb.Commit(nil)
302302
statedb.Database().TrieDB().Commit(root, true, nil)
303303

304304
return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil))

core/state/state_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ func TestDump(t *testing.T) {
5454
// write some of them to the trie
5555
s.state.updateStateObject(obj1)
5656
s.state.updateStateObject(obj2)
57-
s.state.Commit(false, nil)
57+
s.state.Finalise(false)
58+
s.state.AccountsIntermediateRoot()
59+
s.state.Commit(nil)
5860

5961
// check that DumpToCollector contains the state objects that are in trie
6062
got := string(s.state.Dump(false, false, true))
@@ -95,7 +97,9 @@ func TestNull(t *testing.T) {
9597
var value common.Hash
9698

9799
s.state.SetState(address, common.Hash{}, value)
98-
s.state.Commit(false, nil)
100+
s.state.Finalise(false)
101+
s.state.AccountsIntermediateRoot()
102+
s.state.Commit(nil)
99103

100104
if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) {
101105
t.Errorf("expected empty current value, got %x", value)
@@ -167,7 +171,9 @@ func TestSnapshot2(t *testing.T) {
167171
so0.deleted = false
168172
state.SetStateObject(so0)
169173

170-
root, _, _ := state.Commit(false, nil)
174+
state.Finalise(false)
175+
state.AccountsIntermediateRoot()
176+
root, _, _ := state.Commit(nil)
171177
state, _ = New(root, state.db, state.snaps)
172178

173179
// and one with deleted == true

core/state/statedb.go

+22-26
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,17 @@ func (s *StateDB) GetRefund() uint64 {
934934
return s.refund
935935
}
936936

937+
// GetRefund returns the current value of the refund counter.
938+
func (s *StateDB) WaitPipeVerification() error {
939+
// We need wait for the parent trie to commit
940+
if s.snap != nil {
941+
if valid := s.snap.WaitAndGetVerifyRes(); !valid {
942+
return fmt.Errorf("verification on parent snap failed")
943+
}
944+
}
945+
return nil
946+
}
947+
937948
// Finalise finalises the state by removing the s destructed objects and clears
938949
// the journal as well as the refunds. Finalise, however, will not push any updates
939950
// into the tries just yet. Only IntermediateRoot or Commit will do that.
@@ -986,21 +997,18 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
986997
// IntermediateRoot computes the current root hash of the state trie.
987998
// It is called in between transactions to get the root hash that
988999
// goes into transaction receipts.
989-
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) (common.Hash, error) {
1000+
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
9901001
if s.lightProcessed {
9911002
s.StopPrefetcher()
992-
return s.trie.Hash(), nil
1003+
return s.trie.Hash()
9931004
}
9941005
// Finalise all the dirty storage states and write them into the tries
9951006
s.Finalise(deleteEmptyObjects)
996-
err := s.AccountsIntermediateRoot()
997-
if err != nil {
998-
return common.Hash{}, err
999-
}
1000-
return s.StateIntermediateRoot(), nil
1007+
s.AccountsIntermediateRoot()
1008+
return s.StateIntermediateRoot()
10011009
}
10021010

1003-
func (s *StateDB) AccountsIntermediateRoot() error {
1011+
func (s *StateDB) AccountsIntermediateRoot() {
10041012
tasks := make(chan func())
10051013
finishCh := make(chan struct{})
10061014
defer close(finishCh)
@@ -1018,12 +1026,6 @@ func (s *StateDB) AccountsIntermediateRoot() error {
10181026
}()
10191027
}
10201028

1021-
// We need wait for the parent trie to commit
1022-
if s.snap != nil {
1023-
if valid := s.snap.WaitAndGetVerifyRes(); !valid {
1024-
return fmt.Errorf("verification on parent snap failed")
1025-
}
1026-
}
10271029
// Although naively it makes sense to retrieve the account trie and then do
10281030
// the contract storage and account updates sequentially, that short circuits
10291031
// the account prefetcher. Instead, let's process all the storage updates
@@ -1055,7 +1057,6 @@ func (s *StateDB) AccountsIntermediateRoot() error {
10551057
}
10561058
}
10571059
wg.Wait()
1058-
return nil
10591060
}
10601061

10611062
func (s *StateDB) StateIntermediateRoot() common.Hash {
@@ -1260,7 +1261,7 @@ func (s *StateDB) LightCommit() (common.Hash, *types.DiffLayer, error) {
12601261
}
12611262

12621263
// Commit writes the state to the underlying in-memory trie database.
1263-
func (s *StateDB) Commit(deleteEmptyObjects bool, failPostCommitFunc func(), postCommitFuncs ...func() error) (common.Hash, *types.DiffLayer, error) {
1264+
func (s *StateDB) Commit(failPostCommitFunc func(), postCommitFuncs ...func() error) (common.Hash, *types.DiffLayer, error) {
12641265
if s.dbErr != nil {
12651266
return common.Hash{}, nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
12661267
}
@@ -1290,13 +1291,6 @@ func (s *StateDB) Commit(deleteEmptyObjects bool, failPostCommitFunc func(), pos
12901291
snapUpdated = make(chan struct{})
12911292
}
12921293

1293-
s.Finalise(deleteEmptyObjects)
1294-
err := s.AccountsIntermediateRoot()
1295-
1296-
if err != nil {
1297-
return common.Hash{}, nil, err
1298-
}
1299-
13001294
commmitTrie := func() error {
13011295
commitErr := func() error {
13021296
if s.stateRoot = s.StateIntermediateRoot(); s.fullProcessed && s.expectedRoot != s.stateRoot {
@@ -1448,9 +1442,11 @@ func (s *StateDB) Commit(deleteEmptyObjects bool, failPostCommitFunc func(), pos
14481442
// - head layer is paired with HEAD state
14491443
// - head-1 layer is paired with HEAD-1 state
14501444
// - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state
1451-
if err := s.snaps.Cap(s.expectedRoot, s.snaps.CapLimit()); err != nil {
1452-
log.Warn("Failed to cap snapshot tree", "root", s.expectedRoot, "layers", s.snaps.CapLimit(), "err", err)
1453-
}
1445+
go func() {
1446+
if err := s.snaps.Cap(s.expectedRoot, s.snaps.CapLimit()); err != nil {
1447+
log.Warn("Failed to cap snapshot tree", "root", s.expectedRoot, "layers", s.snaps.CapLimit(), "err", err)
1448+
}
1449+
}()
14541450
}
14551451
}
14561452
return nil

0 commit comments

Comments
 (0)