diff options
author | obscuren <geffobscura@gmail.com> | 2015-01-10 05:42:36 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-01-10 05:42:36 +0800 |
commit | 491c23a728e4f5cdedfa040aded6a6b136f6bee0 (patch) | |
tree | 1fa546abacc60e8c29c43f465485d407777565c4 /core/block_processor.go | |
parent | 351516c57cb5872e849ffca643526da5801564f2 (diff) | |
download | go-tangerine-491c23a728e4f5cdedfa040aded6a6b136f6bee0.tar go-tangerine-491c23a728e4f5cdedfa040aded6a6b136f6bee0.tar.gz go-tangerine-491c23a728e4f5cdedfa040aded6a6b136f6bee0.tar.bz2 go-tangerine-491c23a728e4f5cdedfa040aded6a6b136f6bee0.tar.lz go-tangerine-491c23a728e4f5cdedfa040aded6a6b136f6bee0.tar.xz go-tangerine-491c23a728e4f5cdedfa040aded6a6b136f6bee0.tar.zst go-tangerine-491c23a728e4f5cdedfa040aded6a6b136f6bee0.zip |
Moved the TD method from block processor.
Diffstat (limited to 'core/block_processor.go')
-rw-r--r-- | core/block_processor.go | 54 |
1 files changed, 15 insertions, 39 deletions
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, |