From 491c23a728e4f5cdedfa040aded6a6b136f6bee0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 9 Jan 2015 22:42:36 +0100 Subject: Moved the TD method from block processor. --- core/block_processor.go | 54 ++++++++++++++----------------------------------- core/chain_manager.go | 16 ++++++++++++++- 2 files changed, 30 insertions(+), 40 deletions(-) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index a9f948a46..4d93fcead 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -2,7 +2,6 @@ package core import ( "bytes" - "errors" "fmt" "math/big" "sync" @@ -217,44 +216,21 @@ func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big return } - // Calculate the new total difficulty and sync back to the db - if td, ok := sm.CalculateTD(block); ok { - // Sync the current block's state to the database and cancelling out the deferred Undo - state.Sync() - - state.Manifest().SetHash(block.Hash()) - - messages := state.Manifest().Messages - state.Manifest().Reset() - - chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash()[0:4]) - - sm.txpool.RemoveSet(block.Transactions()) - - return td, messages, nil - } else { - return nil, nil, errors.New("total diff failed") - } -} - -func (sm *BlockProcessor) CalculateTD(block *types.Block) (*big.Int, bool) { - uncleDiff := new(big.Int) - for _, uncle := range block.Uncles() { - uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty) - } - - // TD(genesis_block) = 0 and TD(B) = TD(B.parent) + sum(u.difficulty for u in B.uncles) + B.difficulty - td := new(big.Int) - td = td.Add(sm.bc.Td(), uncleDiff) - td = td.Add(td, block.Header().Difficulty) - - // The new TD will only be accepted if the new difficulty is - // is greater than the previous. - if td.Cmp(sm.bc.Td()) > 0 { - return td, true - } - - return nil, false + // Calculate the td for this block + td = CalculateTD(block, parent) + // Sync the current block's state to the database and cancelling out the deferred Undo + state.Sync() + // Set the block hashes for the current messages + state.Manifest().SetHash(block.Hash()) + messages = state.Manifest().Messages + // Reset the manifest XXX We need this? + state.Manifest().Reset() + // Remove transactions from the pool + sm.txpool.RemoveSet(block.Transactions()) + + chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash()[0:4]) + + return td, messages, nil } // Validates the current block. Returns an error if the block was invalid, diff --git a/core/chain_manager.go b/core/chain_manager.go index 90b5692ac..caa2e7b43 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -34,6 +34,20 @@ func CalcDifficulty(block, parent *types.Block) *big.Int { return diff } +func CalculateTD(block, parent *types.Block) *big.Int { + uncleDiff := new(big.Int) + for _, uncle := range block.Uncles() { + uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty) + } + + // TD(genesis_block) = 0 and TD(B) = TD(B.parent) + sum(u.difficulty for u in B.uncles) + B.difficulty + td := new(big.Int) + td = td.Add(parent.Td, uncleDiff) + td = td.Add(td, block.Header().Difficulty) + + return td +} + func CalcGasLimit(parent, block *types.Block) *big.Int { if block.Number().Cmp(big.NewInt(0)) == 0 { return ethutil.BigPow(10, 6) @@ -360,7 +374,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { cblock := self.currentBlock if td.Cmp(self.td) > 0 { if block.Header().Number.Cmp(new(big.Int).Add(cblock.Header().Number, ethutil.Big1)) < 0 { - chainlogger.Infof("Split detected. New head #%v (%x), was #%v (%x)\n", block.Header().Number, block.Hash()[:4], cblock.Header().Number, cblock.Hash()[:4]) + chainlogger.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, block.Hash()[:4], td, cblock.Header().Number, cblock.Hash()[:4], self.td) } self.setTotalDifficulty(td) -- cgit