From 8f659a9441ecc8acb7c81d6fc69f22593cfb1205 Mon Sep 17 00:00:00 2001 From: zjubfd <296179868@qq.com> Date: Wed, 23 Mar 2022 12:35:30 +0800 Subject: [PATCH] skip verification on acccount storage root to tolerate with fastnode --- core/blockchain_diff_test.go | 5 ++++ core/state_processor.go | 46 +++++++----------------------------- 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/core/blockchain_diff_test.go b/core/blockchain_diff_test.go index 2575843a92..3b74743f54 100644 --- a/core/blockchain_diff_test.go +++ b/core/blockchain_diff_test.go @@ -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) diff --git a/core/state_processor.go b/core/state_processor.go index 14fe9b4b92..cc81c4c3b7 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -18,7 +18,6 @@ package core import ( "bytes" - "errors" "fmt" "math/big" "math/rand" @@ -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) && @@ -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) @@ -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)