core: moved TD calculation from proc to chain

This commit is contained in:
obscuren 2015-04-20 12:01:20 +02:00
parent fa729a0c55
commit 97b0c4b697
6 changed files with 24 additions and 24 deletions

View File

@ -152,25 +152,25 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
// Process block will attempt to process the given block's transactions and applies them
// on top of the block's parent state (given it exists) and will return wether it was
// successful or not.
func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, logs state.Logs, err error) {
func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, err error) {
// Processing a blocks may never happen simultaneously
sm.mutex.Lock()
defer sm.mutex.Unlock()
header := block.Header()
if sm.bc.HasBlock(header.Hash()) {
return nil, nil, &KnownBlockError{header.Number, header.Hash()}
return nil, &KnownBlockError{header.Number, header.Hash()}
}
if !sm.bc.HasBlock(header.ParentHash) {
return nil, nil, ParentError(header.ParentHash)
return nil, ParentError(header.ParentHash)
}
parent := sm.bc.GetBlock(header.ParentHash)
return sm.processWithParent(block, parent)
}
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big.Int, logs state.Logs, err error) {
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs state.Logs, err error) {
sm.lastAttemptedBlock = block
// Create a new state based on the parent's root (e.g., create copy)
@ -183,7 +183,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
// There can be at most two uncles
if len(block.Uncles()) > 2 {
return nil, nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
return nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
}
receipts, err := sm.TransitionState(state, parent, block, false)
@ -232,7 +232,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
}
// Calculate the td for this block
td = CalculateTD(block, parent)
//td = CalculateTD(block, parent)
// Sync the current block's state to the database
state.Sync()
@ -244,7 +244,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
putTx(sm.extraDb, tx, block, uint64(i))
}
return td, state.Logs(), nil
return state.Logs(), nil
}
// Validates the current block. Returns an error if the block was invalid,

View File

@ -93,12 +93,12 @@ func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Dat
blocks := make(types.Blocks, max)
for i := 0; i < max; i++ {
block := makeBlock(bman, parent, i, db, seed)
td, _, err := bman.processWithParent(block, parent)
_, err := bman.processWithParent(block, parent)
if err != nil {
fmt.Println("process with parent failed", err)
panic(err)
}
block.Td = td
block.Td = CalculateTD(block, parent)
blocks[i] = block
parent = block
}

View File

@ -461,7 +461,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
}
// Call in to the block processor and check for errors. It's likely that if one block fails
// all others will fail too (unless a known block is returned).
td, logs, err := self.processor.Process(block)
logs, err := self.processor.Process(block)
if err != nil {
if IsKnownBlockErr(err) {
continue
@ -492,7 +492,8 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
return err
}
block.Td = td
block.Td = new(big.Int).Set(CalculateTD(block, self.GetBlock(block.ParentHash())))
self.mu.Lock()
{
@ -502,14 +503,14 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
self.write(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 td.Cmp(self.td) > 0 {
if block.Td.Cmp(self.td) > 0 {
//if block.Header().Number.Cmp(new(big.Int).Add(cblock.Header().Number, common.Big1)) < 0 {
if block.Number().Cmp(cblock.Number()) <= 0 {
chash := cblock.Hash()
hash := block.Hash()
if glog.V(logger.Info) {
glog.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], td, cblock.Header().Number, chash[:4], self.td)
glog.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], block.Td, cblock.Header().Number, chash[:4], self.td)
}
// during split we merge two different chains and create the new canonical chain
self.merge(self.getBlockByNumber(block.NumberU64()), block)
@ -518,7 +519,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
queueEvent.splitCount++
}
self.setTotalDifficulty(td)
self.setTotalDifficulty(block.Td)
self.insert(block)
jsonlogger.LogJson(&logger.EthChainNewHead{

View File

@ -52,6 +52,7 @@ func GenesisBlock(db common.Database) *types.Block {
}
statedb.Sync()
genesis.Header().Root = statedb.Root()
genesis.Td = params.GenesisDifficulty
return genesis
}

View File

@ -347,22 +347,20 @@ func (self *Block) Copy() *Block {
}
func (self *Block) String() string {
return fmt.Sprintf(`BLOCK(%x): Size: %v TD: %v {
NoNonce: %x
Header:
[
return fmt.Sprintf(`Block(#%v): Size: %v TD: %v {
MinerHash: %x
%v
]
Transactions:
%v
Uncles:
%v
}
`, self.header.Hash(), self.Size(), self.Td, self.header.HashNoNonce(), self.header, self.transactions, self.uncles)
`, self.Number(), self.Size(), self.Td, self.header.HashNoNonce(), self.header, self.transactions, self.uncles)
}
func (self *Header) String() string {
return fmt.Sprintf(`
return fmt.Sprintf(`Header(%x):
[
ParentHash: %x
UncleHash: %x
Coinbase: %x
@ -377,8 +375,8 @@ func (self *Header) String() string {
Time: %v
Extra: %s
MixDigest: %x
Nonce: %x`,
self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.MixDigest, self.Nonce)
Nonce: %x
]`, self.Hash(), self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.MixDigest, self.Nonce)
}
type Blocks []*Block

View File

@ -10,7 +10,7 @@ import (
)
type BlockProcessor interface {
Process(*Block) (*big.Int, state.Logs, error)
Process(*Block) (state.Logs, error)
}
const bloomLength = 256