Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R4R] skip verification on account storage root to tolerate with fastnode when doing diffsync #812

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/blockchain_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ func TestProcessDiffLayer(t *testing.T) {
if diff == nil {
continue
}
// change the storage root into emptyRoot
for idx, account := range diff.Accounts {
latestAccount, _ := snapshot.FullAccount(account.Blob)
diff.Accounts[idx].Blob = snapshot.SlimAccountRLP(latestAccount.Nonce, latestAccount.Balance, types.EmptyRootHash, latestAccount.CodeHash)
}
lightBackend.Chain().HandleDiffLayer(diff, "testpid", true)
}
_, err := lightBackend.chain.insertChain([]*types.Block{block}, true)
Expand Down
46 changes: 9 additions & 37 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package core

import (
"bytes"
"errors"
"fmt"
"math/big"
"math/rand"
Expand Down Expand Up @@ -223,20 +222,6 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
previousAccount.CodeHash = types.EmptyCodeHash
}

// skip no change account
if previousAccount.Nonce == latestAccount.Nonce &&
bytes.Equal(previousAccount.CodeHash, latestAccount.CodeHash) &&
previousAccount.Balance.Cmp(latestAccount.Balance) == 0 &&
previousAccount.Root == common.BytesToHash(latestAccount.Root) {
// It is normal to receive redundant message since the collected message is redundant.
log.Debug("receive redundant account change in diff layer", "account", diffAccount, "num", block.NumberU64())
snapMux.Lock()
delete(snapAccounts, diffAccount)
delete(snapStorage, diffAccount)
snapMux.Unlock()
continue
}

// update code
codeHash := common.BytesToHash(latestAccount.CodeHash)
if !bytes.Equal(latestAccount.CodeHash, previousAccount.CodeHash) &&
Expand All @@ -259,21 +244,17 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
}

//update storage
latestRoot := common.BytesToHash(latestAccount.Root)
if latestRoot != previousAccount.Root {
accountTrie, err := statedb.Database().OpenStorageTrie(addrHash, previousAccount.Root)
accountRootHash := previousAccount.Root
snapMux.RLock()
storageChange, exist := snapStorage[diffAccount]
snapMux.RUnlock()

if exist && len(storageChange) > 0 {
accountTrie, err := statedb.Database().OpenStorageTrie(addrHash, accountRootHash)
if err != nil {
errChan <- err
return
}
snapMux.RLock()
storageChange, exist := snapStorage[diffAccount]
snapMux.RUnlock()

if !exist {
errChan <- errors.New("missing storage change in difflayer")
return
}
for k, v := range storageChange {
if len(v) != 0 {
accountTrie.TryUpdate([]byte(k), v)
Expand All @@ -282,26 +263,17 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
}
}

// check storage root
accountRootHash := accountTrie.Hash()
if latestRoot != accountRootHash {
errChan <- errors.New("account storage root mismatch")
return
}
accountRootHash = accountTrie.Hash()
diffMux.Lock()
diffTries[diffAccount] = accountTrie
diffMux.Unlock()
} else {
snapMux.Lock()
delete(snapStorage, diffAccount)
snapMux.Unlock()
}

// can't trust the blob, need encode by our-self.
latestStateAccount := state.Account{
Nonce: latestAccount.Nonce,
Balance: latestAccount.Balance,
Root: common.BytesToHash(latestAccount.Root),
Root: accountRootHash,
CodeHash: latestAccount.CodeHash,
}
bz, err := rlp.EncodeToBytes(&latestStateAccount)
Expand Down