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 | |
parent | 615d20598ababa5988d5b36a48640c154d8866fd (diff) | |
download | dexon-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar dexon-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.gz dexon-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.bz2 dexon-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.lz dexon-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.xz dexon-57dc435f9b928f5de2a49736a2c71a7bf611289a.tar.zst dexon-57dc435f9b928f5de2a49736a2c71a7bf611289a.zip |
Added TD for each block
-rw-r--r-- | ethchain/block.go | 4 | ||||
-rw-r--r-- | ethchain/block_chain.go | 23 | ||||
-rw-r--r-- | ethereum.go | 14 | ||||
-rw-r--r-- | ethutil/list.go | 5 |
4 files changed, 44 insertions, 2 deletions
diff --git a/ethchain/block.go b/ethchain/block.go index 157be2a52..5e8aca33a 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -18,6 +18,7 @@ type BlockInfo struct { Number uint64 Hash []byte Parent []byte + TD *big.Int } func (bi *BlockInfo) RlpDecode(data []byte) { @@ -26,10 +27,11 @@ func (bi *BlockInfo) RlpDecode(data []byte) { bi.Number = decoder.Get(0).Uint() bi.Hash = decoder.Get(1).Bytes() bi.Parent = decoder.Get(2).Bytes() + bi.TD = decoder.Get(3).BigInt() } func (bi *BlockInfo) RlpEncode() []byte { - return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent}) + return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent, bi.TD}) } type Blocks []*Block 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()) diff --git a/ethereum.go b/ethereum.go index 79e722c26..09665336c 100644 --- a/ethereum.go +++ b/ethereum.go @@ -4,6 +4,7 @@ import ( "container/list" "encoding/json" "fmt" + "math/big" "math/rand" "net" "path" @@ -188,6 +189,18 @@ func (s *Ethereum) IsListening() bool { return s.listening } +func (s *Ethereum) HighestTDPeer() (td *big.Int) { + td = big.NewInt(0) + + eachPeer(s.peers, func(p *Peer, v *list.Element) { + if p.td.Cmp(td) > 0 { + td = p.td + } + }) + + return +} + func (s *Ethereum) AddPeer(conn net.Conn) { peer := NewPeer(conn, s, true) @@ -370,6 +383,7 @@ func (s *Ethereum) ReapDeadPeerHandler() { // Start the ethereum func (s *Ethereum) Start(seed bool) { s.reactor.Start() + s.blockPool.Start() // Bind to addr and port ln, err := net.Listen("tcp", ":"+s.Port) if err != nil { diff --git a/ethutil/list.go b/ethutil/list.go index 9db96cf18..a5147573a 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -2,6 +2,7 @@ package ethutil import ( "encoding/json" + "fmt" "reflect" ) @@ -29,6 +30,10 @@ func EmptyList() *List { // Get N element from the embedded slice. Returns nil if OOB. func (self *List) Get(i int) interface{} { + if self.list.Len() == 3 { + fmt.Println("get", i, self.list.Index(i).Interface()) + } + if self.list.Len() > i { return self.list.Index(i).Interface() } |