Skip to content

Commit

Permalink
[R4R] fix validator pipecommit issue (bnb-chain#877)
Browse files Browse the repository at this point in the history
* fix validator account root issue in piepecommit

* fix test and imports

* add check for snap

* refacto a bit
  • Loading branch information
forcodedancing authored and j75689 committed Jun 7, 2022
1 parent bd67fb2 commit 6383e5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
5 changes: 2 additions & 3 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -140,10 +141,8 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
if err := statedb.WaitPipeVerification(); err != nil {
return err
}
statedb.CorrectAccountsRoot()
statedb.CorrectAccountsRoot(common.Hash{})
statedb.Finalise(v.config.IsEIP158(header.Number))
// State verification pipeline - accounts root are not calculated here, just populate needed fields for process
statedb.PopulateSnapAccountAndStorage()
return nil
})
} else {
Expand Down
17 changes: 15 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,23 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
}

//CorrectAccountsRoot will fix account roots in pipecommit mode
func (s *StateDB) CorrectAccountsRoot() {
if accounts, err := s.snap.Accounts(); err == nil && accounts != nil {
func (s *StateDB) CorrectAccountsRoot(blockRoot common.Hash) {
var snapshot snapshot.Snapshot
if blockRoot == (common.Hash{}) {
snapshot = s.snap
} else if s.snaps != nil {
snapshot = s.snaps.Snapshot(blockRoot)
}

if snapshot == nil {
return
}
if accounts, err := snapshot.Accounts(); err == nil && accounts != nil {
for _, obj := range s.stateObjects {
if !obj.deleted && !obj.rootCorrected && obj.data.Root == dummyRoot {
if account, exist := accounts[crypto.Keccak256Hash(obj.address[:])]; exist && len(account.Root) != 0 {
obj.data.Root = common.BytesToHash(account.Root)
obj.rootCorrected = true
}
}
}
Expand Down Expand Up @@ -1449,6 +1460,8 @@ func (s *StateDB) Commit(failPostCommitFunc func(), postCommitFuncs ...func() er
}
if s.pipeCommit {
defer close(snapUpdated)
// State verification pipeline - accounts root are not calculated here, just populate needed fields for process
s.PopulateSnapAccountAndStorage()
}
diffLayer.Destructs, diffLayer.Accounts, diffLayer.Storages = s.SnapToDiffLayer()
// Only update if there's a state transition (skip empty Clique blocks)
Expand Down

0 comments on commit 6383e5a

Please sign in to comment.