提交 57dc435f 编写于 作者: O obscuren

Added TD for each block

上级 615d2059
...@@ -18,6 +18,7 @@ type BlockInfo struct { ...@@ -18,6 +18,7 @@ type BlockInfo struct {
Number uint64 Number uint64
Hash []byte Hash []byte
Parent []byte Parent []byte
TD *big.Int
} }
func (bi *BlockInfo) RlpDecode(data []byte) { func (bi *BlockInfo) RlpDecode(data []byte) {
...@@ -26,10 +27,11 @@ func (bi *BlockInfo) RlpDecode(data []byte) { ...@@ -26,10 +27,11 @@ func (bi *BlockInfo) RlpDecode(data []byte) {
bi.Number = decoder.Get(0).Uint() bi.Number = decoder.Get(0).Uint()
bi.Hash = decoder.Get(1).Bytes() bi.Hash = decoder.Get(1).Bytes()
bi.Parent = decoder.Get(2).Bytes() bi.Parent = decoder.Get(2).Bytes()
bi.TD = decoder.Get(3).BigInt()
} }
func (bi *BlockInfo) RlpEncode() []byte { 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 type Blocks []*Block
......
...@@ -2,6 +2,7 @@ package ethchain ...@@ -2,6 +2,7 @@ package ethchain
import ( import (
"bytes" "bytes"
"fmt"
"math/big" "math/big"
"github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethlog"
...@@ -191,6 +192,26 @@ func (bc *BlockChain) Add(block *Block) { ...@@ -191,6 +192,26 @@ func (bc *BlockChain) Add(block *Block) {
ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock) 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 { func (bc *BlockChain) GetBlock(hash []byte) *Block {
data, _ := ethutil.Config.Db.Get(hash) data, _ := ethutil.Config.Db.Get(hash)
if len(data) == 0 { if len(data) == 0 {
...@@ -234,7 +255,7 @@ func (bc *BlockChain) BlockInfo(block *Block) BlockInfo { ...@@ -234,7 +255,7 @@ func (bc *BlockChain) BlockInfo(block *Block) BlockInfo {
// Unexported method for writing extra non-essential block info to the db // Unexported method for writing extra non-essential block info to the db
func (bc *BlockChain) writeBlockInfo(block *Block) { func (bc *BlockChain) writeBlockInfo(block *Block) {
bc.LastBlockNumber++ 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 // 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()) ethutil.Config.Db.Put(append(block.Hash(), []byte("Info")...), bi.RlpEncode())
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"container/list" "container/list"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/big"
"math/rand" "math/rand"
"net" "net"
"path" "path"
...@@ -188,6 +189,18 @@ func (s *Ethereum) IsListening() bool { ...@@ -188,6 +189,18 @@ func (s *Ethereum) IsListening() bool {
return s.listening 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) { func (s *Ethereum) AddPeer(conn net.Conn) {
peer := NewPeer(conn, s, true) peer := NewPeer(conn, s, true)
...@@ -370,6 +383,7 @@ func (s *Ethereum) ReapDeadPeerHandler() { ...@@ -370,6 +383,7 @@ func (s *Ethereum) ReapDeadPeerHandler() {
// Start the ethereum // Start the ethereum
func (s *Ethereum) Start(seed bool) { func (s *Ethereum) Start(seed bool) {
s.reactor.Start() s.reactor.Start()
s.blockPool.Start()
// Bind to addr and port // Bind to addr and port
ln, err := net.Listen("tcp", ":"+s.Port) ln, err := net.Listen("tcp", ":"+s.Port)
if err != nil { if err != nil {
......
...@@ -2,6 +2,7 @@ package ethutil ...@@ -2,6 +2,7 @@ package ethutil
import ( import (
"encoding/json" "encoding/json"
"fmt"
"reflect" "reflect"
) )
...@@ -29,6 +30,10 @@ func EmptyList() *List { ...@@ -29,6 +30,10 @@ func EmptyList() *List {
// Get N element from the embedded slice. Returns nil if OOB. // Get N element from the embedded slice. Returns nil if OOB.
func (self *List) Get(i int) interface{} { 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 { if self.list.Len() > i {
return self.list.Index(i).Interface() return self.list.Index(i).Interface()
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册