Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test forks with real ProcessWithParent #193

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions chain/block_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package chain
import (
"bytes"
"container/list"
"errors"
// "errors"
"fmt"
"math/big"
"sync"
Expand Down Expand Up @@ -266,11 +266,15 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I

sm.transState = state.Copy()

// XXX: the only reference to the eth object
// if this could be moved to BlockManager, testing wouldn't require
// the eth object
sm.eth.TxPool().RemoveSet(block.Transactions())

return td, messages, nil
} else {
return nil, nil, errors.New("total diff failed")
// this is a fork, so return the td
return td, nil, nil
}
}

Expand All @@ -288,16 +292,12 @@ func (sm *BlockManager) ApplyDiff(state *state.State, parent, block *types.Block
}

func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) {
uncleDiff := new(big.Int)
for _, uncle := range block.Uncles {
uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty)
td, err := sm.bc.CalcTotalDiff(block)
if err != nil {
fmt.Println(err)
return nil, false
}

// 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.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 {
Expand All @@ -307,7 +307,7 @@ func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) {
//sm.bc.SetTotalDifficulty(td)
}

return nil, false
return td, false
}

// Validates the current block. Returns an error if the block was invalid,
Expand Down
22 changes: 18 additions & 4 deletions chain/chain_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ func (self *ChainManager) GetBlock(hash []byte) *types.Block {
}
}
}

return nil
}

Expand Down Expand Up @@ -229,7 +228,8 @@ func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) {
return nil, fmt.Errorf("Unable to calculate total diff without known parent %x", block.PrevHash)
}

parentTd := parent.BlockInfo().TD
//parentTd := parent.BlockInfo().TD
parentTd := self.BlockInfo(parent).TD

uncleDiff := new(big.Int)
for _, uncle := range block.Uncles {
Expand All @@ -246,8 +246,20 @@ func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) {
func (bc *ChainManager) BlockInfo(block *types.Block) types.BlockInfo {
bi := types.BlockInfo{}
data, _ := ethutil.Config.Db.Get(append(block.Hash(), []byte("Info")...))
if len(data) == 0 {
if bc.workingChain != nil {
// Check the temp chain
for e := bc.workingChain.Front(); e != nil; e = e.Next() {
l := e.Value.(*link)
b := l.Block
if bytes.Compare(b.Hash(), block.Hash()) == 0 {
bi = types.BlockInfo{Number: b.Number.Uint64(), Hash: b.Hash(), Parent: b.PrevHash, TD: l.Td}
return bi
}
}
}
}
bi.RlpDecode(data)

return bi
}

Expand Down Expand Up @@ -275,8 +287,10 @@ func (self *ChainManager) InsertChain(chain *BlockChain, call func(*types.Block,
for e := chain.Front(); e != nil; e = e.Next() {
link := e.Value.(*link)

self.add(link.Block)
// must set TD before adding as
// add calls writeBlockInfo which writes the most recent TD
self.SetTotalDifficulty(link.Td)
self.add(link.Block)

call(link.Block, link.Messages)
}
Expand Down
Loading