From fee99b3e8d543e16a217cd02ec1b87ba08ceecc2 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 5 May 2015 03:09:44 +0200 Subject: [PATCH 1/4] Simplify CalculateTD function --- core/chain_manager.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/core/chain_manager.go b/core/chain_manager.go index 29830188e3ac..71da08f5e2f1 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -52,10 +52,7 @@ func CalculateTD(block, parent *types.Block) *big.Int { if parent == nil { return block.Difficulty() } - - td := new(big.Int).Add(parent.Td, block.Header().Difficulty) - - return td + return new(big.Int).Add(parent.Td, block.Difficulty()) } func CalcGasLimit(parent *types.Block) *big.Int { From ff3a891afe4e530dbd67249375c2cdf63d361126 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 5 May 2015 03:48:55 +0200 Subject: [PATCH 2/4] Remove old CalcTotalDiff function, improve code comments --- core/chain_manager.go | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/core/chain_manager.go b/core/chain_manager.go index 71da08f5e2f1..9f3af2124584 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -74,10 +74,10 @@ type ChainManager struct { processor types.BlockProcessor eventMux *event.TypeMux genesisBlock *types.Block - // Last known total difficulty - mu sync.RWMutex - tsmu sync.RWMutex + mu sync.RWMutex + tsmu sync.RWMutex + // Last known total difficulty td *big.Int currentBlock *types.Block lastBlockHash common.Hash @@ -458,26 +458,6 @@ func (bc *ChainManager) setTotalDifficulty(td *big.Int) { bc.td = td } -func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { - parent := self.GetBlock(block.Header().ParentHash) - if parent == nil { - return nil, fmt.Errorf("Unable to calculate total diff without known parent %x", block.Header().ParentHash) - } - - parentTd := parent.Td - - uncleDiff := new(big.Int) - for _, uncle := range block.Uncles() { - uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty) - } - - td := new(big.Int) - td = td.Add(parentTd, uncleDiff) - td = td.Add(td, block.Header().Difficulty) - - return td, nil -} - func (bc *ChainManager) Stop() { close(bc.quit) @@ -534,7 +514,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { } block.Td = new(big.Int) - // Do not penelise on future block. We'll need a block queue eventually that will queue + // Do not penalize on future block. We'll need a block queue eventually that will queue // future block for future use if err == BlockFutureErr { block.SetQueued(true) From 28770307ba8551e8c22c07305f44191444d0546c Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 5 May 2015 07:29:41 +0200 Subject: [PATCH 3/4] Remove debug logging --- cmd/geth/blocktest.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/geth/blocktest.go b/cmd/geth/blocktest.go index 5c80ad07eb96..81a64b5f24e1 100644 --- a/cmd/geth/blocktest.go +++ b/cmd/geth/blocktest.go @@ -113,7 +113,6 @@ func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, er return ethereum, fmt.Errorf("Block Test load error: %v", err) } - fmt.Println("chain loaded") if err := test.ValidatePostState(statedb); err != nil { return ethereum, fmt.Errorf("post state validation failed: %v", err) } From 8589532ecc86fbb84d70b087c66a730b44a6b4f7 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 5 May 2015 07:35:01 +0200 Subject: [PATCH 4/4] 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. --- core/chain_manager.go | 17 +++++++++++------ core/chain_manager_test.go | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/core/chain_manager.go b/core/chain_manager.go index 9f3af2124584..5abe06b1602e 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -296,7 +296,7 @@ func (bc *ChainManager) Reset() { } // Prepare the genesis block - bc.write(bc.genesisBlock) + bc.writeBlock(bc.genesisBlock) bc.insert(bc.genesisBlock) bc.currentBlock = bc.genesisBlock bc.makeCache() @@ -318,7 +318,7 @@ func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) { // Prepare the genesis block bc.genesisBlock = gb - bc.write(bc.genesisBlock) + bc.writeBlock(bc.genesisBlock) bc.insert(bc.genesisBlock) bc.currentBlock = bc.genesisBlock bc.makeCache() @@ -347,16 +347,19 @@ func (self *ChainManager) Export(w io.Writer) error { return nil } -func (bc *ChainManager) insert(block *types.Block) { +func (bc *ChainManager) writeBlockHash(block *types.Block) { key := append(blockNumPre, block.Number().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.currentBlock = block bc.lastBlockHash = block.Hash() } -func (bc *ChainManager) write(block *types.Block) { +func (bc *ChainManager) writeBlock(block *types.Block) { enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block)) key := append(blockHashPre, block.Hash().Bytes()...) bc.blockDb.Put(key, enc) @@ -544,12 +547,14 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { cblock := self.currentBlock // Write block to database. Eventually we'll have to improve on this and throw away blocks that are // 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. // At this point it's possible that a different chain (fork) becomes the new canonical chain. 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 - 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() hash := block.Hash() diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index 50915459b906..93d80a2ea76a 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -86,7 +86,7 @@ func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) { bman.bc.mu.Lock() { - bman.bc.write(block) + bman.bc.writeBlock(block) } bman.bc.mu.Unlock() } @@ -342,7 +342,7 @@ func TestGetAncestors(t *testing.T) { } for _, block := range chain { - chainMan.write(block) + chainMan.writeBlock(block) } ancestors := chainMan.GetAncestors(chain[len(chain)-1], 4)