mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 13:16:42 +00:00
Store block hash under block num even for non-canonical blocks
* Ensure block hash is stored with the block number as key even if a block is non-canonical (not a new tip of the chain), this is required by the new chain forking code in the edge cases tested by BlockTests/bcUncleTest.json * Rename and split up related DB / cache storage functions to make them easier to read. * Move if statement's simple statement to it's own line to get distinct stacktrace line number in case we get future bugs here.
This commit is contained in:
parent
28770307ba
commit
8589532ecc
2 changed files with 13 additions and 8 deletions
|
|
@ -296,7 +296,7 @@ func (bc *ChainManager) Reset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare the genesis block
|
// Prepare the genesis block
|
||||||
bc.write(bc.genesisBlock)
|
bc.writeBlock(bc.genesisBlock)
|
||||||
bc.insert(bc.genesisBlock)
|
bc.insert(bc.genesisBlock)
|
||||||
bc.currentBlock = bc.genesisBlock
|
bc.currentBlock = bc.genesisBlock
|
||||||
bc.makeCache()
|
bc.makeCache()
|
||||||
|
|
@ -318,7 +318,7 @@ func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) {
|
||||||
|
|
||||||
// Prepare the genesis block
|
// Prepare the genesis block
|
||||||
bc.genesisBlock = gb
|
bc.genesisBlock = gb
|
||||||
bc.write(bc.genesisBlock)
|
bc.writeBlock(bc.genesisBlock)
|
||||||
bc.insert(bc.genesisBlock)
|
bc.insert(bc.genesisBlock)
|
||||||
bc.currentBlock = bc.genesisBlock
|
bc.currentBlock = bc.genesisBlock
|
||||||
bc.makeCache()
|
bc.makeCache()
|
||||||
|
|
@ -347,16 +347,19 @@ func (self *ChainManager) Export(w io.Writer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *ChainManager) insert(block *types.Block) {
|
func (bc *ChainManager) writeBlockHash(block *types.Block) {
|
||||||
key := append(blockNumPre, block.Number().Bytes()...)
|
key := append(blockNumPre, block.Number().Bytes()...)
|
||||||
bc.blockDb.Put(key, block.Hash().Bytes())
|
bc.blockDb.Put(key, block.Hash().Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bc *ChainManager) insert(block *types.Block) {
|
||||||
|
bc.writeBlockHash(block)
|
||||||
bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes())
|
bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes())
|
||||||
bc.currentBlock = block
|
bc.currentBlock = block
|
||||||
bc.lastBlockHash = block.Hash()
|
bc.lastBlockHash = block.Hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *ChainManager) write(block *types.Block) {
|
func (bc *ChainManager) writeBlock(block *types.Block) {
|
||||||
enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
|
enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
|
||||||
key := append(blockHashPre, block.Hash().Bytes()...)
|
key := append(blockHashPre, block.Hash().Bytes()...)
|
||||||
bc.blockDb.Put(key, enc)
|
bc.blockDb.Put(key, enc)
|
||||||
|
|
@ -544,12 +547,14 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
|
||||||
cblock := self.currentBlock
|
cblock := self.currentBlock
|
||||||
// Write block to database. Eventually we'll have to improve on this and throw away blocks that are
|
// Write block to database. Eventually we'll have to improve on this and throw away blocks that are
|
||||||
// not in the canonical chain.
|
// not in the canonical chain.
|
||||||
self.write(block)
|
self.writeBlockHash(block)
|
||||||
|
self.writeBlock(block)
|
||||||
// Compare the TD of the last known block in the canonical chain to make sure it's greater.
|
// Compare the TD of the last known block in the canonical chain to make sure it's greater.
|
||||||
// At this point it's possible that a different chain (fork) becomes the new canonical chain.
|
// At this point it's possible that a different chain (fork) becomes the new canonical chain.
|
||||||
if block.Td.Cmp(self.td) > 0 {
|
if block.Td.Cmp(self.td) > 0 {
|
||||||
// Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging
|
// Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging
|
||||||
if previous := self.getBlockByNumber(block.NumberU64() - 1); previous.Hash() != block.ParentHash() {
|
previous := self.getBlockByNumber(block.NumberU64() - 1)
|
||||||
|
if previous.Hash() != block.ParentHash() {
|
||||||
chash := cblock.Hash()
|
chash := cblock.Hash()
|
||||||
hash := block.Hash()
|
hash := block.Hash()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) {
|
||||||
|
|
||||||
bman.bc.mu.Lock()
|
bman.bc.mu.Lock()
|
||||||
{
|
{
|
||||||
bman.bc.write(block)
|
bman.bc.writeBlock(block)
|
||||||
}
|
}
|
||||||
bman.bc.mu.Unlock()
|
bman.bc.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
@ -342,7 +342,7 @@ func TestGetAncestors(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, block := range chain {
|
for _, block := range chain {
|
||||||
chainMan.write(block)
|
chainMan.writeBlock(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
ancestors := chainMan.GetAncestors(chain[len(chain)-1], 4)
|
ancestors := chainMan.GetAncestors(chain[len(chain)-1], 4)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue