From afd3a61bf01f366c2eeed12a518d407ef3f723cd Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Wed, 13 May 2020 12:52:40 -0500 Subject: [PATCH] remove unneeded bc methods --- cmd/geth/config.go | 1 - core/blockchain.go | 73 +++++++--------------- core/blockchain_test.go | 75 ----------------------- eth/backend.go | 11 ++-- eth/config.go | 4 -- statediff/testhelpers/mocks/blockchain.go | 3 - 6 files changed, 26 insertions(+), 141 deletions(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 84fec702c..d49c8f21f 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -156,7 +156,6 @@ func makeFullNode(ctx *cli.Context) *node.Node { utils.RegisterEthService(stack, &cfg.Eth) if ctx.GlobalBool(utils.StateDiffFlag.Name) { - cfg.Eth.StateDiff = true utils.RegisterStateDiffService(stack) } diff --git a/core/blockchain.go b/core/blockchain.go index 87d5a6d43..47187b097 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -110,12 +110,11 @@ const ( // CacheConfig contains the configuration values for the trie caching/pruning // that's resident in a blockchain. type CacheConfig struct { - TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory - TrieCleanNoPrefetch bool // Whether to disable heuristic state prefetching for followup blocks - TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk - TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node) - TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk - ProcessingStateDiffs bool // Whether statediffs processing should be taken into a account before a trie is pruned + TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory + TrieCleanNoPrefetch bool // Whether to disable heuristic state prefetching for followup blocks + TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk + TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node) + TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk } // BlockChain represents the canonical chain given a database with a genesis @@ -178,8 +177,6 @@ type BlockChain struct { badBlocks *lru.Cache // Bad block cache shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block. terminateInsert func(common.Hash, uint64) bool // Testing hook used to terminate ancient receipt chain insertion. - - stateDiffsProcessed map[common.Hash]int } // NewBlockChain returns a fully initialised block chain using information @@ -200,25 +197,23 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par txLookupCache, _ := lru.New(txLookupCacheLimit) futureBlocks, _ := lru.New(maxFutureBlocks) badBlocks, _ := lru.New(badBlockLimit) - stateDiffsProcessed := make(map[common.Hash]int) bc := &BlockChain{ - chainConfig: chainConfig, - cacheConfig: cacheConfig, - db: db, - triegc: prque.New(nil), - stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit), - quit: make(chan struct{}), - shouldPreserve: shouldPreserve, - bodyCache: bodyCache, - bodyRLPCache: bodyRLPCache, - receiptsCache: receiptsCache, - blockCache: blockCache, - txLookupCache: txLookupCache, - futureBlocks: futureBlocks, - engine: engine, - vmConfig: vmConfig, - badBlocks: badBlocks, - stateDiffsProcessed: stateDiffsProcessed, + chainConfig: chainConfig, + cacheConfig: cacheConfig, + db: db, + triegc: prque.New(nil), + stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit), + quit: make(chan struct{}), + shouldPreserve: shouldPreserve, + bodyCache: bodyCache, + bodyRLPCache: bodyRLPCache, + receiptsCache: receiptsCache, + blockCache: blockCache, + txLookupCache: txLookupCache, + futureBlocks: futureBlocks, + engine: engine, + vmConfig: vmConfig, + badBlocks: badBlocks, } bc.validator = NewBlockValidator(chainConfig, bc, engine) bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine) @@ -1294,11 +1289,6 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error { return nil } -func (bc *BlockChain) AddToStateDiffProcessedCollection(hash common.Hash) { - count := bc.stateDiffsProcessed[hash] - bc.stateDiffsProcessed[hash] = count + 1 -} - // WriteBlockWithState writes the block and all associated state to the database. func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { bc.chainmu.Lock() @@ -1390,18 +1380,6 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. bc.triegc.Push(root, number) break } - if bc.cacheConfig.ProcessingStateDiffs { - if !bc.rootAllowedToBeDereferenced(root.(common.Hash)) { - bc.triegc.Push(root, number) - break - } else { - log.Debug("Current root found in stateDiffsProcessed collection with a count of 2, okay to dereference", - "root", root.(common.Hash).Hex(), - "blockNumber", uint64(-number), - "size of stateDiffsProcessed", len(bc.stateDiffsProcessed)) - delete(bc.stateDiffsProcessed, root.(common.Hash)) - } - } log.Debug("Dereferencing", "root", root.(common.Hash).Hex()) triedb.Dereference(root.(common.Hash)) } @@ -1461,15 +1439,6 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. return status, nil } -// since we need the state tries of the current block and its parent in-memory -// in order to process statediffs, we should avoid dereferencing roots until -// its statediff and its child have been processed -func (bc *BlockChain) rootAllowedToBeDereferenced(root common.Hash) bool { - diffProcessedForSelfAndChildCount := 2 - count := bc.stateDiffsProcessed[root] - return count >= diffProcessedForSelfAndChildCount -} - // addFutureBlock checks if the block is within the max allowed window to get // accepted for future processing, and returns an error if the block is too far // ahead and was not added. diff --git a/core/blockchain_test.go b/core/blockchain_test.go index ac748319f..878888160 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -2363,81 +2363,6 @@ func TestDeleteCreateRevert(t *testing.T) { } } -func TestProcessingStateDiffs(t *testing.T) { - defaultTrieCleanCache := 256 - defaultTrieDirtyCache := 256 - defaultTrieTimeout := 60 * time.Minute - cacheConfig := &CacheConfig{ - TrieDirtyDisabled: false, - TrieCleanLimit: defaultTrieCleanCache, - TrieDirtyLimit: defaultTrieDirtyCache, - TrieTimeLimit: defaultTrieTimeout, - ProcessingStateDiffs: true, - } - db := rawdb.NewMemoryDatabase() - genesis := new(Genesis).MustCommit(db) - numberOfBlocks := TriesInMemory - engine := ethash.NewFaker() - blockchain, _ := NewBlockChain(db, cacheConfig, params.AllEthashProtocolChanges, engine, vm.Config{}, nil) - blocks := makeBlockChain(genesis, numberOfBlocks+1, engine, db, canonicalSeed) - _, err := blockchain.InsertChain(blocks) - if err != nil { - t.Fatalf("failed to create pristine chain: %v", err) - } - defer blockchain.Stop() - - //when adding a root hash to the collection, it will increment the count - firstStateRoot := blocks[0].Root() - blockchain.AddToStateDiffProcessedCollection(firstStateRoot) - value, ok := blockchain.stateDiffsProcessed[firstStateRoot] - if !ok { - t.Error("state root not found in collection") - } - if value != 1 { - t.Error("state root count not correct", "want", 1, "got", value) - } - - blockchain.AddToStateDiffProcessedCollection(firstStateRoot) - value, ok = blockchain.stateDiffsProcessed[firstStateRoot] - if !ok { - t.Error("state root not found in collection") - } - if value != 2 { - t.Error("state root count not correct", "want", 2, "got", value) - } - - moreBlocks := makeBlockChain(blocks[len(blocks)-1], 1, engine, db, canonicalSeed) - _, err = blockchain.InsertChain(moreBlocks) - if err != nil { - t.Error(err) - } - - //a root hash can be dereferenced when it's state diff and it's child's state diff have been processed - //(i.e. it has a count of 2 in stateDiffsProcessed) - nodes := blockchain.stateCache.TrieDB().Nodes() - if containsRootHash(nodes, firstStateRoot) { - t.Errorf("stateRoot %s in nodes, want: %t, got: %t", firstStateRoot.Hex(), false, true) - } - - //a root hash should still be in the in-mem db if it's child's state diff hasn't yet been processed - //(i.e. it has a count of 1 stateDiffsProcessed) - secondStateRoot := blocks[1].Root() - blockchain.AddToStateDiffProcessedCollection(secondStateRoot) - if !containsRootHash(nodes, secondStateRoot) { - t.Errorf("stateRoot %s in nodes, want: %t, got: %t", secondStateRoot.Hex(), true, false) - } - - //the stateDiffsProcessed collection is cleaned up once a hash has been dereferenced - _, ok = blockchain.stateDiffsProcessed[firstStateRoot] - if ok { - t.Errorf("stateRoot %s in stateDiffsProcessed collection, want: %t, got: %t", - firstStateRoot.Hex(), - false, - ok, - ) - } -} - func containsRootHash(collection []common.Hash, hash common.Hash) bool { for _, n := range collection { if n == hash { diff --git a/eth/backend.go b/eth/backend.go index b997f8c53..bda307d95 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -179,12 +179,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { EVMInterpreter: config.EVMInterpreter, } cacheConfig = &core.CacheConfig{ - TrieCleanLimit: config.TrieCleanCache, - TrieCleanNoPrefetch: config.NoPrefetch, - TrieDirtyLimit: config.TrieDirtyCache, - TrieDirtyDisabled: config.NoPruning, - TrieTimeLimit: config.TrieTimeout, - ProcessingStateDiffs: config.StateDiff, + TrieCleanLimit: config.TrieCleanCache, + TrieCleanNoPrefetch: config.NoPrefetch, + TrieDirtyLimit: config.TrieDirtyCache, + TrieDirtyDisabled: config.NoPruning, + TrieTimeLimit: config.TrieTimeout, } ) eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, chainConfig, eth.engine, vmConfig, eth.shouldPreserve) diff --git a/eth/config.go b/eth/config.go index cb1b3fc6b..2eaf21fbc 100644 --- a/eth/config.go +++ b/eth/config.go @@ -61,8 +61,6 @@ var DefaultConfig = Config{ Blocks: 20, Percentile: 60, }, - - StateDiff: false, } func init() { @@ -166,6 +164,4 @@ type Config struct { // MuirGlacier block override (TODO: remove after the fork) OverrideMuirGlacier *big.Int `toml:",omitempty"` - - StateDiff bool } diff --git a/statediff/testhelpers/mocks/blockchain.go b/statediff/testhelpers/mocks/blockchain.go index 68fc92cc2..1559a7834 100644 --- a/statediff/testhelpers/mocks/blockchain.go +++ b/statediff/testhelpers/mocks/blockchain.go @@ -38,9 +38,6 @@ type BlockChain struct { TDByHash map[common.Hash]*big.Int } -// AddToStateDiffProcessedCollection mock method -func (blockChain *BlockChain) AddToStateDiffProcessedCollection(hash common.Hash) {} - // SetBlocksForHashes mock method func (blockChain *BlockChain) SetBlocksForHashes(blocks map[common.Hash]*types.Block) { if blockChain.blocksToReturnByHash == nil {