From 897558793f1f4628d07f44ea30ffe4032b8fcd8c Mon Sep 17 00:00:00 2001 From: fudongbai <296179868@qq.com> Date: Thu, 22 Jul 2021 14:56:23 +0800 Subject: [PATCH] address comments --- core/blockchain.go | 41 +------------------------------------ core/state/statedb.go | 47 +++++++++++++++++++++++++++++++++++++++++-- eth/downloader/api.go | 3 +-- p2p/rlpx/rlpx.go | 2 +- 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index be29afe3c1..3cc44fc4e5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -23,7 +23,6 @@ import ( "io" "math/big" mrand "math/rand" - "runtime" "sort" "sync" "sync/atomic" @@ -88,7 +87,6 @@ const ( maxFutureBlocks = 256 maxTimeFutureBlocks = 30 badBlockLimit = 10 - preLoadLimit = 64 maxBeyondBlocks = 2048 // BlockChainVersion ensures that an incompatible database forces a resync from scratch. @@ -1883,44 +1881,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er // Enable prefetching to pull in trie node paths while processing transactions statedb.StartPrefetcher("chain") activeState = statedb - - accounts := make(map[common.Address]bool, block.Transactions().Len()) - accountsSlice := make([]common.Address, 0, block.Transactions().Len()) - for _, tx := range block.Transactions() { - from, err := types.Sender(signer, tx) - if err != nil { - break - } - accounts[from] = true - if tx.To() != nil { - accounts[*tx.To()] = true - } - } - for account, _ := range accounts { - accountsSlice = append(accountsSlice, account) - } - if len(accountsSlice) >= preLoadLimit { - objsChan := make(chan []*state.StateObject, runtime.NumCPU()) - for i := 0; i < runtime.NumCPU(); i++ { - start := i * len(accountsSlice) / runtime.NumCPU() - end := (i + 1) * len(accountsSlice) / runtime.NumCPU() - if i+1 == runtime.NumCPU() { - end = len(accountsSlice) - } - go func(start, end int) { - objs := statedb.PreloadStateObject(accountsSlice[start:end]) - objsChan <- objs - }(start, end) - } - for i := 0; i < runtime.NumCPU(); i++ { - objs := <-objsChan - if objs != nil { - for _, obj := range objs { - statedb.SetStateObject(obj) - } - } - } - } + statedb.TryPreload(block, signer) //Process block using the parent state as reference point substart := time.Now() diff --git a/core/state/statedb.go b/core/state/statedb.go index d538068056..7940613cd6 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -38,7 +38,10 @@ import ( "github.com/ethereum/go-ethereum/trie" ) -const defaultNumOfSlots = 100 +const ( + preLoadLimit = 64 + defaultNumOfSlots = 100 +) type revision struct { id int @@ -516,7 +519,47 @@ func (s *StateDB) getStateObject(addr common.Address) *StateObject { return nil } -func (s *StateDB) PreloadStateObject(address []common.Address) []*StateObject { +func (s *StateDB) TryPreload(block *types.Block, signer types.Signer) { + accounts := make(map[common.Address]bool, block.Transactions().Len()) + accountsSlice := make([]common.Address, 0, block.Transactions().Len()) + for _, tx := range block.Transactions() { + from, err := types.Sender(signer, tx) + if err != nil { + break + } + accounts[from] = true + if tx.To() != nil { + accounts[*tx.To()] = true + } + } + for account, _ := range accounts { + accountsSlice = append(accountsSlice, account) + } + if len(accountsSlice) >= preLoadLimit && len(accountsSlice) > runtime.NumCPU() { + objsChan := make(chan []*StateObject, runtime.NumCPU()) + for i := 0; i < runtime.NumCPU(); i++ { + start := i * len(accountsSlice) / runtime.NumCPU() + end := (i + 1) * len(accountsSlice) / runtime.NumCPU() + if i+1 == runtime.NumCPU() { + end = len(accountsSlice) + } + go func(start, end int) { + objs := s.preloadStateObject(accountsSlice[start:end]) + objsChan <- objs + }(start, end) + } + for i := 0; i < runtime.NumCPU(); i++ { + objs := <-objsChan + if objs != nil { + for _, obj := range objs { + s.SetStateObject(obj) + } + } + } + } +} + +func (s *StateDB) preloadStateObject(address []common.Address) []*StateObject { // Prefer live objects if any is available if s.snap == nil { return nil diff --git a/eth/downloader/api.go b/eth/downloader/api.go index 47a2299a29..0fea49f7bc 100644 --- a/eth/downloader/api.go +++ b/eth/downloader/api.go @@ -20,9 +20,8 @@ import ( "context" "sync" - "github.com/ethereum/go-ethereum/common/gopool" - "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/common/gopool" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/rpc" ) diff --git a/p2p/rlpx/rlpx.go b/p2p/rlpx/rlpx.go index 2d6178bbbc..ab79c26a28 100644 --- a/p2p/rlpx/rlpx.go +++ b/p2p/rlpx/rlpx.go @@ -36,12 +36,12 @@ import ( "github.com/VictoriaMetrics/fastcache" "github.com/golang/snappy" + "github.com/oxtoacart/bpool" "golang.org/x/crypto/sha3" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/rlp" - "github.com/oxtoacart/bpool" ) var snappyCache *fastcache.Cache