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() + // 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()) - state.Manifest().SetHash(block.Hash()) + chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash()[0:4]) - 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 + 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) diff --git a/tests/vm/gh_test.go b/tests/vm/gh_test.go index 2aece215e..47b588268 100644 --- a/tests/vm/gh_test.go +++ b/tests/vm/gh_test.go @@ -81,7 +81,7 @@ func RunVmTest(p string, t *testing.T) { for name, test := range tests { /* helper.Logger.SetLogLevel(5) - if name != "jump0_jumpdest2" { + if name != "createNameRegistratorZeroMem" { continue } */