diff options
author | obscuren <geffobscura@gmail.com> | 2014-09-24 17:39:17 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-09-24 17:39:17 +0800 |
commit | 57dc435f9b928f5de2a49736a2c71a7bf611289a (patch) | |
tree | 9d6981298eef522382b8c549f190b7204d0dd494 /ethchain/block_chain.go | |
parent | 615d20598ababa5988d5b36a48640c154d8866fd (diff) | |
download | go-tangerine-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar go-tangerine-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.gz go-tangerine-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.bz2 go-tangerine-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.lz go-tangerine-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.xz go-tangerine-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.zst go-tangerine-57dc435f9b928f5de2a49736a2c71a7bf611289a.zip |
Added TD for each block
Diffstat (limited to 'ethchain/block_chain.go')
-rw-r--r-- | ethchain/block_chain.go | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go index 7c9b60fc5..c8e5c610e 100644 --- a/ethchain/block_chain.go +++ b/ethchain/block_chain.go @@ -2,6 +2,7 @@ package ethchain import ( "bytes" + "fmt" "math/big" "github.com/ethereum/eth-go/ethlog" @@ -191,6 +192,26 @@ func (bc *BlockChain) Add(block *Block) { ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock) } +func (self *BlockChain) CalcTotalDiff(block *Block) (*big.Int, error) { + parent := self.GetBlock(block.PrevHash) + if parent == nil { + return nil, fmt.Errorf("Unable to calculate total diff without known parent %x", block.PrevHash) + } + + parentTd := parent.BlockInfo().TD + + uncleDiff := new(big.Int) + for _, uncle := range block.Uncles { + uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty) + } + + td := new(big.Int) + td = td.Add(parentTd, uncleDiff) + td = td.Add(td, block.Difficulty) + + return td, nil +} + func (bc *BlockChain) GetBlock(hash []byte) *Block { data, _ := ethutil.Config.Db.Get(hash) if len(data) == 0 { @@ -234,7 +255,7 @@ func (bc *BlockChain) BlockInfo(block *Block) BlockInfo { // Unexported method for writing extra non-essential block info to the db func (bc *BlockChain) writeBlockInfo(block *Block) { bc.LastBlockNumber++ - bi := BlockInfo{Number: bc.LastBlockNumber, Hash: block.Hash(), Parent: block.PrevHash} + bi := BlockInfo{Number: bc.LastBlockNumber, Hash: block.Hash(), Parent: block.PrevHash, TD: bc.TD} // For now we use the block hash with the words "info" appended as key ethutil.Config.Db.Put(append(block.Hash(), []byte("Info")...), bi.RlpEncode()) |