Skip to content

Commit

Permalink
update prefetch
Browse files Browse the repository at this point in the history
remove prefetcher
  • Loading branch information
unclezoro committed Jan 21, 2022
1 parent 4607f60 commit 731474c
Show file tree
Hide file tree
Showing 27 changed files with 166 additions and 119 deletions.
15 changes: 8 additions & 7 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
if chainConfig.IsByzantium(vmContext.BlockNumber) {
statedb.Finalise(true)
} else {
stateRoot, err := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
stateRoot := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -202,10 +202,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
txIndex++
}

_, err := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
if err != nil {
return nil, nil, err
}
statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
// Add mining reward?
if miningReward > 0 {
// Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
Expand All @@ -231,7 +228,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
statedb.AddBalance(pre.Env.Coinbase, minerReward)
}
// Commit block
root, _, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber), nil)
statedb.Finalise(chainConfig.IsEIP158(vmContext.BlockNumber))
statedb.AccountsIntermediateRoot()
root, _, err := statedb.Commit(nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not commit state: %v", err)
return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
Expand Down Expand Up @@ -260,7 +259,9 @@ func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB
}
}
// Commit and re-open to start with a clean state.
root, _, _ := statedb.Commit(false, nil)
statedb.Finalise(false)
statedb.AccountsIntermediateRoot()
root, _, _ := statedb.Commit(nil)
statedb, _ = state.New(root, sdb, nil)
return statedb
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,10 @@ func runCmd(ctx *cli.Context) error {
output, leftOverGas, stats, err := timedExec(bench, execFunc)

if ctx.GlobalBool(DumpFlag.Name) {
statedb.Commit(true, nil)
_, err := statedb.IntermediateRoot(true)
if err != nil {
return err
}
statedb.Finalise(true)
statedb.AccountsIntermediateRoot()
statedb.Commit(nil)
statedb.IntermediateRoot(true)
fmt.Println(string(statedb.Dump(false, false, true)))
}

Expand Down
5 changes: 1 addition & 4 deletions cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ func stateTestCmd(ctx *cli.Context) error {
_, state, err := test.Run(st, cfg, false)
// print state root for evmlab tracing
if ctx.GlobalBool(MachineFlag.Name) && state != nil {
root, err := state.IntermediateRoot(false)
if err != nil {
return err
}
root := state.IntermediateRoot(false)
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", root)
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction, uncles []*types.Header,
receipts *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64) (err error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil)
return
}
Expand All @@ -561,7 +561,7 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
var err error
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.
receipts *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64) (err error) {
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(chain.Config(), state, header, uncles)
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
return
}

Expand Down
8 changes: 2 additions & 6 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
rootHash, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
rootHash = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
wg.Done()
}()
go func() {
Expand Down Expand Up @@ -1211,11 +1211,7 @@ func (p *Parlia) applyTransaction(
if p.chainConfig.IsByzantium(header.Number) {
state.Finalise(true)
} else {
stateRoot, err := state.IntermediateRoot(p.chainConfig.IsEIP158(header.Number))
if err != nil {
return err
}
root = stateRoot.Bytes()
root = state.IntermediateRoot(p.chainConfig.IsEIP158(header.Number)).Bytes()
}
*usedGas += gasUsed
receipt := types.NewReceipt(root, false, *usedGas)
Expand Down
21 changes: 14 additions & 7 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,25 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil))
if receiptSha != header.ReceiptHash {
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
} else {
return nil
}
return nil
},
}
if !skipHeavyVerify {
if skipHeavyVerify {
validateFuns = append(validateFuns, func() error {
if err := statedb.WaitPipeVerification(); err != nil {
return err
}
statedb.Finalise(v.config.IsEIP158(header.Number))
statedb.AccountsIntermediateRoot()
return nil
})
} else {
validateFuns = append(validateFuns, func() error {
if root, err := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root || err != nil {
return fmt.Errorf("invalid merkle root (remote: %x local: %x), err %v", header.Root, root, err)
} else {
return nil
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root)
}
return nil
})
}
validateRes := make(chan error, len(validateFuns))
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}

// Commit all cached state changes into underlying memory database.
_, diffLayer, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()), bc.tryRewindBadBlocks, tryCommitTrieDB)
_, diffLayer, err := state.Commit(bc.tryRewindBadBlocks, tryCommitTrieDB)
if err != nil {
return NonStatTy, err
}
Expand Down
4 changes: 3 additions & 1 deletion core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ func testBlockChainImport(chain types.Blocks, pipelineCommit bool, blockchain *B
blockchain.chainmu.Lock()
rawdb.WriteTd(blockchain.db, block.Hash(), block.NumberU64(), new(big.Int).Add(block.Difficulty(), blockchain.GetTdByHash(block.ParentHash())))
rawdb.WriteBlock(blockchain.db, block)
statedb.Commit(false, nil)
statedb.Finalise(false)
statedb.AccountsIntermediateRoot()
statedb.Commit(nil)
blockchain.chainmu.Unlock()
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
block, _, _ := b.engine.FinalizeAndAssemble(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)

// Write state changes to db
root, _, err := statedb.Commit(config.IsEIP158(b.header.Number), nil)
root, _, err := statedb.Commit(nil)
if err != nil {
panic(fmt.Sprintf("state write error: %v", err))
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.S
} else {
time = parent.Time() + 10 // block time is fixed at 10 seconds
}
root, _ := state.IntermediateRoot(chain.Config().IsEIP158(parent.Number()))
root := state.IntermediateRoot(chain.Config().IsEIP158(parent.Number()))
return &types.Header{
Root: root,
ParentHash: parent.Hash(),
Expand Down
4 changes: 2 additions & 2 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
statedb.SetState(addr, key, value)
}
}
root, _ := statedb.IntermediateRoot(false)
root := statedb.IntermediateRoot(false)
head := &types.Header{
Number: new(big.Int).SetUint64(g.Number),
Nonce: types.EncodeNonce(g.Nonce),
Expand All @@ -298,7 +298,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
if g.Difficulty == nil {
head.Difficulty = params.GenesisDifficulty
}
statedb.Commit(false, nil)
statedb.Commit(nil)
statedb.Database().TrieDB().Commit(root, true, nil)

return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil))
Expand Down
12 changes: 9 additions & 3 deletions core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func TestDump(t *testing.T) {
// write some of them to the trie
s.state.updateStateObject(obj1)
s.state.updateStateObject(obj2)
s.state.Commit(false, nil)
s.state.Finalise(false)
s.state.AccountsIntermediateRoot()
s.state.Commit(nil)

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

s.state.SetState(address, common.Hash{}, value)
s.state.Commit(false, nil)
s.state.Finalise(false)
s.state.AccountsIntermediateRoot()
s.state.Commit(nil)

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

root, _, _ := state.Commit(false, nil)
state.Finalise(false)
state.AccountsIntermediateRoot()
root, _, _ := state.Commit(nil)
state, _ = New(root, state.db, state.snaps)

// and one with deleted == true
Expand Down
48 changes: 22 additions & 26 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,17 @@ func (s *StateDB) GetRefund() uint64 {
return s.refund
}

// GetRefund returns the current value of the refund counter.
func (s *StateDB) WaitPipeVerification() error {
// We need wait for the parent trie to commit
if s.snap != nil {
if valid := s.snap.WaitAndGetVerifyRes(); !valid {
return fmt.Errorf("verification on parent snap failed")
}
}
return nil
}

// Finalise finalises the state by removing the s destructed objects and clears
// the journal as well as the refunds. Finalise, however, will not push any updates
// into the tries just yet. Only IntermediateRoot or Commit will do that.
Expand Down Expand Up @@ -986,21 +997,18 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
// IntermediateRoot computes the current root hash of the state trie.
// It is called in between transactions to get the root hash that
// goes into transaction receipts.
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) (common.Hash, error) {
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
if s.lightProcessed {
s.StopPrefetcher()
return s.trie.Hash(), nil
return s.trie.Hash()
}
// Finalise all the dirty storage states and write them into the tries
s.Finalise(deleteEmptyObjects)
err := s.AccountsIntermediateRoot()
if err != nil {
return common.Hash{}, err
}
return s.StateIntermediateRoot(), nil
s.AccountsIntermediateRoot()
return s.StateIntermediateRoot()
}

func (s *StateDB) AccountsIntermediateRoot() error {
func (s *StateDB) AccountsIntermediateRoot() {
tasks := make(chan func())
finishCh := make(chan struct{})
defer close(finishCh)
Expand All @@ -1018,12 +1026,6 @@ func (s *StateDB) AccountsIntermediateRoot() error {
}()
}

// We need wait for the parent trie to commit
if s.snap != nil {
if valid := s.snap.WaitAndGetVerifyRes(); !valid {
return fmt.Errorf("verification on parent snap failed")
}
}
// Although naively it makes sense to retrieve the account trie and then do
// the contract storage and account updates sequentially, that short circuits
// the account prefetcher. Instead, let's process all the storage updates
Expand Down Expand Up @@ -1055,7 +1057,6 @@ func (s *StateDB) AccountsIntermediateRoot() error {
}
}
wg.Wait()
return nil
}

func (s *StateDB) StateIntermediateRoot() common.Hash {
Expand Down Expand Up @@ -1260,7 +1261,7 @@ func (s *StateDB) LightCommit() (common.Hash, *types.DiffLayer, error) {
}

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

s.Finalise(deleteEmptyObjects)
err := s.AccountsIntermediateRoot()

if err != nil {
return common.Hash{}, nil, err
}

commmitTrie := func() error {
commitErr := func() error {
if s.stateRoot = s.StateIntermediateRoot(); s.fullProcessed && s.expectedRoot != s.stateRoot {
Expand Down Expand Up @@ -1448,9 +1442,11 @@ func (s *StateDB) Commit(deleteEmptyObjects bool, failPostCommitFunc func(), pos
// - head layer is paired with HEAD state
// - head-1 layer is paired with HEAD-1 state
// - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state
if err := s.snaps.Cap(s.expectedRoot, s.snaps.CapLimit()); err != nil {
log.Warn("Failed to cap snapshot tree", "root", s.expectedRoot, "layers", s.snaps.CapLimit(), "err", err)
}
go func() {
if err := s.snaps.Cap(s.expectedRoot, s.snaps.CapLimit()); err != nil {
log.Warn("Failed to cap snapshot tree", "root", s.expectedRoot, "layers", s.snaps.CapLimit(), "err", err)
}
}()
}
}
return nil
Expand Down
Loading

0 comments on commit 731474c

Please sign in to comment.