Refactoring RlpValue => Value

pull/150/head
obscuren 11 years ago
parent 67de76c217
commit c2fb9f06ad
  1. 1
      ethereum.go
  2. 123
      peer.go

@ -247,6 +247,7 @@ func (s *Ethereum) Start() {
s.TxPool.Start() s.TxPool.Start()
if ethutil.Config.Seed { if ethutil.Config.Seed {
log.Println("Seeding")
// Testnet seed bootstrapping // Testnet seed bootstrapping
resp, err := http.Get("http://www.ethereum.org/servers.poc2.txt") resp, err := http.Get("http://www.ethereum.org/servers.poc2.txt")
if err != nil { if err != nil {

@ -1,7 +1,7 @@
package eth package eth
import ( import (
_ "bytes" "bytes"
"fmt" "fmt"
"github.com/ethereum/ethchain-go" "github.com/ethereum/ethchain-go"
"github.com/ethereum/ethutil-go" "github.com/ethereum/ethutil-go"
@ -123,9 +123,14 @@ type Peer struct {
// Indicated whether the node is catching up or not // Indicated whether the node is catching up or not
catchingUp bool catchingUp bool
Version string
} }
func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer { func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
pubkey := ethutil.NewValueFromBytes(data).Get(2).Bytes()
return &Peer{ return &Peer{
outputQueue: make(chan *ethwire.Msg, outputBufferSize), outputQueue: make(chan *ethwire.Msg, outputBufferSize),
quit: make(chan bool), quit: make(chan bool),
@ -135,6 +140,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
disconnect: 0, disconnect: 0,
connected: 1, connected: 1,
port: 30303, port: 30303,
pubkey: pubkey,
} }
} }
@ -148,6 +154,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
connected: 0, connected: 0,
disconnect: 0, disconnect: 0,
caps: caps, caps: caps,
Version: fmt.Sprintf("/Ethereum(G) v%s/%s", ethutil.Config.Ver, runtime.GOOS),
} }
// Set up the connection in another goroutine so we don't block the main thread // Set up the connection in another goroutine so we don't block the main thread
@ -267,7 +274,7 @@ func (p *Peer) HandleInbound() {
} }
case ethwire.MsgDiscTy: case ethwire.MsgDiscTy:
p.Stop() p.Stop()
log.Println("Disconnect peer:", DiscReason(msg.Data.Get(0).AsUint())) log.Println("Disconnect peer:", DiscReason(msg.Data.Get(0).Uint()))
case ethwire.MsgPingTy: case ethwire.MsgPingTy:
// Respond back with pong // Respond back with pong
p.QueueMessage(ethwire.NewMessage(ethwire.MsgPongTy, "")) p.QueueMessage(ethwire.NewMessage(ethwire.MsgPongTy, ""))
@ -279,30 +286,47 @@ func (p *Peer) HandleInbound() {
case ethwire.MsgBlockTy: case ethwire.MsgBlockTy:
// Get all blocks and process them // Get all blocks and process them
msg.Data = msg.Data msg.Data = msg.Data
var block *ethchain.Block var block, lastBlock *ethchain.Block
for i := msg.Data.Length() - 1; i >= 0; i-- { var err error
block = ethchain.NewBlockFromRlpValue(ethutil.NewValue(msg.Data.Get(i).AsRaw())) for i := msg.Data.Len() - 1; i >= 0; i-- {
err := p.ethereum.BlockManager.ProcessBlock(block) block = ethchain.NewBlockFromRlpValue(msg.Data.Get(i))
err = p.ethereum.BlockManager.ProcessBlock(block)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
break
} else {
lastBlock = block
} }
} }
// If we're catching up, try to catch up further. if err != nil {
if p.catchingUp && msg.Data.Length() > 1 { // If the parent is unknown try to catch up with this peer
if ethutil.Config.Debug { if ethchain.IsParentErr(err) {
blockInfo := p.ethereum.BlockManager.BlockChain().BlockInfo(block) log.Println("Attempting to catch up")
log.Printf("Synced to block height #%d\n", blockInfo.Number) p.catchingUp = false
p.CatchupWithPeer()
}
if ethchain.IsValidationErr(err) {
// TODO
}
} else {
// XXX Do we want to catch up if there were errors?
// If we're catching up, try to catch up further.
if p.catchingUp && msg.Data.Len() > 1 {
if ethutil.Config.Debug && lastBlock != nil {
blockInfo := lastBlock.BlockInfo()
log.Printf("Synced to block height #%d %x %x\n", blockInfo.Number, lastBlock.Hash(), blockInfo.Hash)
}
p.catchingUp = false
p.CatchupWithPeer()
} }
p.catchingUp = false
p.CatchupWithPeer()
} }
case ethwire.MsgTxTy: case ethwire.MsgTxTy:
// If the message was a transaction queue the transaction // If the message was a transaction queue the transaction
// in the TxPool where it will undergo validation and // in the TxPool where it will undergo validation and
// processing when a new block is found // processing when a new block is found
for i := 0; i < msg.Data.Length(); i++ { for i := 0; i < msg.Data.Len(); i++ {
p.ethereum.TxPool.QueueTransaction(ethchain.NewTransactionFromData(msg.Data.Get(i).Encode())) p.ethereum.TxPool.QueueTransaction(ethchain.NewTransactionFromData(msg.Data.Get(i).Encode()))
} }
case ethwire.MsgGetPeersTy: case ethwire.MsgGetPeersTy:
@ -317,10 +341,10 @@ func (p *Peer) HandleInbound() {
//if p.requestedPeerList { //if p.requestedPeerList {
data := msg.Data data := msg.Data
// Create new list of possible peers for the ethereum to process // Create new list of possible peers for the ethereum to process
peers := make([]string, data.Length()) peers := make([]string, data.Len())
// Parse each possible peer // Parse each possible peer
for i := 0; i < data.Length(); i++ { for i := 0; i < data.Len(); i++ {
value := ethutil.NewValue(data.Get(i).AsRaw()) value := data.Get(i)
peers[i] = unpackAddr(value.Get(0), value.Get(1).Uint()) peers[i] = unpackAddr(value.Get(0), value.Get(1).Uint())
} }
@ -333,7 +357,7 @@ func (p *Peer) HandleInbound() {
case ethwire.MsgGetChainTy: case ethwire.MsgGetChainTy:
var parent *ethchain.Block var parent *ethchain.Block
// Length minus one since the very last element in the array is a count // Length minus one since the very last element in the array is a count
l := msg.Data.Length() - 1 l := msg.Data.Len() - 1
// Ignore empty get chains // Ignore empty get chains
if l == 0 { if l == 0 {
break break
@ -345,7 +369,7 @@ func (p *Peer) HandleInbound() {
// Check each SHA block hash from the message and determine whether // Check each SHA block hash from the message and determine whether
// the SHA is in the database // the SHA is in the database
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
if data := msg.Data.Get(i).AsBytes(); p.ethereum.BlockManager.BlockChain().HasBlock(data) { if data := msg.Data.Get(i).Bytes(); p.ethereum.BlockManager.BlockChain().HasBlock(data) {
parent = p.ethereum.BlockManager.BlockChain().GetBlock(data) parent = p.ethereum.BlockManager.BlockChain().GetBlock(data)
break break
} }
@ -359,8 +383,8 @@ func (p *Peer) HandleInbound() {
// If no blocks are found we send back a reply with msg not in chain // If no blocks are found we send back a reply with msg not in chain
// and the last hash from get chain // and the last hash from get chain
lastHash := msg.Data.Get(l - 1) lastHash := msg.Data.Get(l - 1)
log.Printf("Sending not in chain with hash %x\n", lastHash.AsRaw()) //log.Printf("Sending not in chain with hash %x\n", lastHash.AsRaw())
p.QueueMessage(ethwire.NewMessage(ethwire.MsgNotInChainTy, []interface{}{lastHash.AsRaw()})) p.QueueMessage(ethwire.NewMessage(ethwire.MsgNotInChainTy, []interface{}{lastHash.Raw()}))
} }
case ethwire.MsgNotInChainTy: case ethwire.MsgNotInChainTy:
log.Printf("Not in chain %x\n", msg.Data) log.Printf("Not in chain %x\n", msg.Data)
@ -368,7 +392,7 @@ func (p *Peer) HandleInbound() {
// Unofficial but fun nonetheless // Unofficial but fun nonetheless
case ethwire.MsgTalkTy: case ethwire.MsgTalkTy:
log.Printf("%v says: %s\n", p.conn.RemoteAddr(), msg.Data.AsString()) log.Printf("%v says: %s\n", p.conn.RemoteAddr(), msg.Data.Str())
} }
} }
} }
@ -441,9 +465,8 @@ func (p *Peer) pushHandshake() error {
data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
pubkey := ethutil.NewValueFromBytes(data).Get(2).Bytes() pubkey := ethutil.NewValueFromBytes(data).Get(2).Bytes()
clientId := fmt.Sprintf("/Ethereum(G) v%s/%s", ethutil.Config.Ver, runtime.GOOS)
msg := ethwire.NewMessage(ethwire.MsgHandshakeTy, []interface{}{ msg := ethwire.NewMessage(ethwire.MsgHandshakeTy, []interface{}{
uint32(4), uint32(0), clientId, byte(p.caps), p.port, pubkey, uint32(4), uint32(0), p.Version, byte(p.caps), p.port, pubkey,
}) })
p.QueueMessage(msg) p.QueueMessage(msg)
@ -470,7 +493,7 @@ func (p *Peer) pushPeers() {
func (p *Peer) handleHandshake(msg *ethwire.Msg) { func (p *Peer) handleHandshake(msg *ethwire.Msg) {
c := msg.Data c := msg.Data
if c.Get(0).AsUint() != 4 { if c.Get(0).Uint() != 4 {
log.Println("Invalid peer version. Require protocol v4") log.Println("Invalid peer version. Require protocol v4")
p.Stop() p.Stop()
return return
@ -479,35 +502,49 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
// [PROTOCOL_VERSION, NETWORK_ID, CLIENT_ID, CAPS, PORT, PUBKEY] // [PROTOCOL_VERSION, NETWORK_ID, CLIENT_ID, CAPS, PORT, PUBKEY]
p.versionKnown = true p.versionKnown = true
var istr string
// If this is an inbound connection send an ack back // If this is an inbound connection send an ack back
if p.inbound { if p.inbound {
p.pubkey = c.Get(5).AsBytes() p.pubkey = c.Get(5).Bytes()
p.port = uint16(c.Get(4).AsUint()) p.port = uint16(c.Get(4).Uint())
// Self connect detection // Self connect detection
/* data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) pubkey := ethutil.NewValueFromBytes(data).Get(2).Bytes()
pubkey := ethutil.NewValueFromBytes(data).Get(2).Bytes() if bytes.Compare(pubkey, p.pubkey) == 0 {
if bytes.Compare(pubkey, p.pubkey) == 0 { p.Stop()
p.Stop()
return return
} }
*/
}
p.CatchupWithPeer() // Catch up with the connected peer
p.CatchupWithPeer()
istr = "inbound" // Set the peer's caps
} else { p.caps = Caps(c.Get(3).Byte())
p.CatchupWithPeer() // Get a reference to the peers version
p.Version = c.Get(2).Str()
istr = "outbound" log.Println(p)
}
func (p *Peer) String() string {
var strBoundType string
if p.inbound {
strBoundType = "inbound"
} else {
strBoundType = "outbound"
}
var strConnectType string
if atomic.LoadInt32(&p.disconnect) == 0 {
strConnectType = "connected"
} else {
strConnectType = "disconnected"
} }
p.caps = Caps(c.Get(3).AsByte()) return fmt.Sprintf("peer [%s] (%s) %v %s [%s]", strConnectType, strBoundType, p.conn.RemoteAddr(), p.Version, p.caps)
log.Printf("peer connect (%s) %v %s [%s]\n", istr, p.conn.RemoteAddr(), c.Get(2).AsString(), p.caps)
} }
func (p *Peer) CatchupWithPeer() { func (p *Peer) CatchupWithPeer() {

Loading…
Cancel
Save