Skip to content

Commit

Permalink
skip verification on acccount storage root to tolerate with fastnode
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Mar 23, 2022
1 parent 21a3b11 commit 8f659a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 37 deletions.
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

0 comments on commit 8f659a9

Please sign in to comment.