From 5c5de90f0c283a85a55c71eaa45b3f45d78134c8 Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Wed, 26 Oct 2016 11:52:32 +0100 Subject: [PATCH] core, trie: Correctly propagate blocknum/hash, and fix a consensus issue --- core/blockchain.go | 2 +- core/state/statedb.go | 2 +- trie/directcache.go | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index ce1b79a6da..56de73c445 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -547,7 +547,7 @@ func (self *BlockChain) GetBlockByNumber(number uint64) *types.Block { // IsCanonChainBlock checks whether the given block is in the current canonical chain. func (self *BlockChain) IsCanonChainBlock(number uint64, hash common.Hash) bool { - return number < uint64(self.currentBlock.Number().Int64()) && GetCanonicalHash(self.chainDb, number) == hash + return GetCanonicalHash(self.chainDb, number) == hash } // [deprecated by eth/62] diff --git a/core/state/statedb.go b/core/state/statedb.go index a135dbabd4..c0c731eda2 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -188,7 +188,7 @@ func (self *StateDB) SetBlockContext(blockHash common.Hash, blockNum uint64, val if validator == nil { validator = &trie.NullCacheValidator{} } - storage := trie.NewDirectCache(self.trie, self.db, CachePrefix, validator, true) + storage := trie.NewDirectCache(self.trie, self.db, CachePrefix, blockNum, blockHash, validator, true) self.storage = trie.NewSecure(storage, self.db) } diff --git a/trie/directcache.go b/trie/directcache.go index 1ca04f2284..9fe736111f 100644 --- a/trie/directcache.go +++ b/trie/directcache.go @@ -79,11 +79,13 @@ func (cv *NullCacheValidator) IsCanonChainBlock(num uint64, hash common.Hash) bo return false } -func NewDirectCache(pm PersistentMap, db Database, keyPrefix []byte, validator CacheValidator, complete bool) *DirectCache { +func NewDirectCache(pm PersistentMap, db Database, keyPrefix []byte, blockNum uint64, blockHash common.Hash, validator CacheValidator, complete bool) *DirectCache { return &DirectCache{ data: pm, db: db, keyPrefix: keyPrefix, + blockNum: blockNum, + blockHash: blockHash, validator: validator, complete: complete, dirty: make(map[string]bool), @@ -151,7 +153,7 @@ func (dc *DirectCache) getCached(key []byte) ([]byte, bool) { return nil, false } - canonical := dc.validator.IsCanonChainBlock(data.BlockNum, data.BlockHash) + canonical := dc.blockNum > 0 && data.BlockNum < dc.blockNum && dc.validator.IsCanonChainBlock(data.BlockNum, data.BlockHash) return data.Value, canonical }