obscuren 11 роки тому
батько
коміт
57dc435f9b
4 змінених файлів з 44 додано та 2 видалено
  1. 3 1
      ethchain/block.go
  2. 22 1
      ethchain/block_chain.go
  3. 14 0
      ethereum.go
  4. 5 0
      ethutil/list.go

+ 3 - 1
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

+ 22 - 1
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())

+ 14 - 0
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 {

+ 5 - 0
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()
 	}