Merge branch 'release/poc5-rc4'

pull/150/head
obscuren 11 years ago
commit 485e04d9df
  1. 2
      README.md
  2. 78
      ethereum.go
  3. 10
      ethpub/pub.go
  4. 10
      ethrpc/server.go
  5. 2
      ethutil/config.go
  6. 47
      peer.go

@ -6,7 +6,7 @@ Ethereum
Ethereum Go Development package (C) Jeffrey Wilcke Ethereum Go Development package (C) Jeffrey Wilcke
Ethereum is currently in its testing phase. The current state is "Proof Ethereum is currently in its testing phase. The current state is "Proof
of Concept 5.0 RC3". For build instructions see the [Wiki](https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum(Go)). of Concept 5.0 RC4". For build instructions see the [Wiki](https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum(Go)).
Ethereum Go is split up in several sub packages Please refer to each Ethereum Go is split up in several sub packages Please refer to each
individual package for more information. individual package for more information.

@ -2,6 +2,7 @@ package eth
import ( import (
"container/list" "container/list"
"fmt"
"github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethdb" "github.com/ethereum/eth-go/ethdb"
"github.com/ethereum/eth-go/ethrpc" "github.com/ethereum/eth-go/ethrpc"
@ -9,9 +10,11 @@ import (
"github.com/ethereum/eth-go/ethwire" "github.com/ethereum/eth-go/ethwire"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -122,12 +125,20 @@ func (s *Ethereum) TxPool() *ethchain.TxPool {
return s.txPool return s.txPool
} }
func (s *Ethereum) ServerCaps() Caps {
return s.serverCaps
}
func (s *Ethereum) AddPeer(conn net.Conn) { func (s *Ethereum) AddPeer(conn net.Conn) {
peer := NewPeer(conn, s, true) peer := NewPeer(conn, s, true)
if peer != nil && s.peers.Len() < s.MaxPeers { if peer != nil {
s.peers.PushBack(peer) if s.peers.Len() < s.MaxPeers {
peer.Start() s.peers.PushBack(peer)
peer.Start()
} else {
ethutil.Config.Log.Debugf("[SERV] Max connected peers reached. Not adding incoming peer.")
}
} }
} }
@ -142,15 +153,51 @@ func (s *Ethereum) ConnectToPeer(addr string) error {
if s.peers.Len() < s.MaxPeers { if s.peers.Len() < s.MaxPeers {
var alreadyConnected bool var alreadyConnected bool
ahost, _, _ := net.SplitHostPort(addr)
var chost string
ips, err := net.LookupIP(ahost)
if err != nil {
return err
} else {
// If more then one ip is available try stripping away the ipv6 ones
if len(ips) > 1 {
var ipsv4 []net.IP
// For now remove the ipv6 addresses
for _, ip := range ips {
if strings.Contains(ip.String(), "::") {
continue
} else {
ipsv4 = append(ipsv4, ip)
}
}
if len(ipsv4) == 0 {
return fmt.Errorf("[SERV] No IPV4 addresses available for hostname")
}
// Pick a random ipv4 address, simulating round-robin DNS.
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Intn(len(ipsv4))
chost = ipsv4[i].String()
} else {
if len(ips) == 0 {
return fmt.Errorf("[SERV] No IPs resolved for the given hostname")
return nil
}
chost = ips[0].String()
}
}
eachPeer(s.peers, func(p *Peer, v *list.Element) { eachPeer(s.peers, func(p *Peer, v *list.Element) {
if p.conn == nil { if p.conn == nil {
return return
} }
phost, _, _ := net.SplitHostPort(p.conn.RemoteAddr().String()) phost, _, _ := net.SplitHostPort(p.conn.RemoteAddr().String())
ahost, _, _ := net.SplitHostPort(addr)
if phost == ahost { if phost == chost {
alreadyConnected = true alreadyConnected = true
ethutil.Config.Log.Debugf("[SERV] Peer %s already added.\n", chost)
return return
} }
}) })
@ -278,8 +325,21 @@ func (s *Ethereum) Start(seed bool) {
} }
func (s *Ethereum) Seed() { func (s *Ethereum) Seed() {
ethutil.Config.Log.Debugln("Seeding") ethutil.Config.Log.Debugln("[SERV] Retrieving seed nodes")
// DNS Bootstrapping
// Eth-Go Bootstrapping
ips, er := net.LookupIP("seed.bysh.me")
if er == nil {
peers := []string{}
for _, ip := range ips {
node := fmt.Sprintf("%s:%d", ip.String(), 30303)
ethutil.Config.Log.Debugln("[SERV] Found DNS Go Peer:", node)
peers = append(peers, node)
}
s.ProcessPeerList(peers)
}
// Official DNS Bootstrapping
_, nodes, err := net.LookupSRV("eth", "tcp", "ethereum.org") _, nodes, err := net.LookupSRV("eth", "tcp", "ethereum.org")
if err == nil { if err == nil {
peers := []string{} peers := []string{}
@ -293,11 +353,11 @@ func (s *Ethereum) Seed() {
for _, a := range addr { for _, a := range addr {
// Build string out of SRV port and Resolved IP // Build string out of SRV port and Resolved IP
peer := net.JoinHostPort(a, port) peer := net.JoinHostPort(a, port)
log.Println("Found DNS Bootstrap Peer:", peer) ethutil.Config.Log.Debugln("[SERV] Found DNS Bootstrap Peer:", peer)
peers = append(peers, peer) peers = append(peers, peer)
} }
} else { } else {
log.Println("Couldn't resolve :", target) ethutil.Config.Log.Debugln("[SERV} Couldn't resolve :", target)
} }
} }
// Connect to Peer list // Connect to Peer list

@ -6,16 +6,18 @@ import (
) )
type PEthereum struct { type PEthereum struct {
manager ethchain.EthManager
stateManager *ethchain.StateManager stateManager *ethchain.StateManager
blockChain *ethchain.BlockChain blockChain *ethchain.BlockChain
txPool *ethchain.TxPool txPool *ethchain.TxPool
} }
func NewPEthereum(sm *ethchain.StateManager, bc *ethchain.BlockChain, txp *ethchain.TxPool) *PEthereum { func NewPEthereum(manager ethchain.EthManager) *PEthereum {
return &PEthereum{ return &PEthereum{
sm, manager,
bc, manager.StateManager(),
txp, manager.BlockChain(),
manager.TxPool(),
} }
} }

@ -1,6 +1,7 @@
package ethrpc package ethrpc
import ( import (
"fmt"
"github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethpub"
"github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethutil"
"net" "net"
@ -48,15 +49,16 @@ func (s *JsonRpcServer) Start() {
} }
} }
func NewJsonRpcServer(ethp *ethpub.PEthereum) *JsonRpcServer { func NewJsonRpcServer(ethp *ethpub.PEthereum, port int) (*JsonRpcServer, error) {
l, err := net.Listen("tcp", ":30304") sport := fmt.Sprintf(":%d", port)
l, err := net.Listen("tcp", sport)
if err != nil { if err != nil {
ethutil.Config.Log.Infoln("Error starting JSON-RPC") return nil, err
} }
return &JsonRpcServer{ return &JsonRpcServer{
listener: l, listener: l,
quit: make(chan bool), quit: make(chan bool),
ethp: ethp, ethp: ethp,
} }, nil
} }

@ -50,7 +50,7 @@ func ReadConfig(base string) *config {
} }
} }
Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC3"} Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC4"}
Config.Log = NewLogger(LogFile|LogStd, LogLevelDebug) Config.Log = NewLogger(LogFile|LogStd, LogLevelDebug)
Config.SetClientString("/Ethereum(G)") Config.SetClientString("/Ethereum(G)")
} }

@ -2,6 +2,7 @@ package eth
import ( import (
"bytes" "bytes"
"container/list"
"fmt" "fmt"
"github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethutil"
@ -146,6 +147,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
port: 30303, port: 30303,
pubkey: pubkey, pubkey: pubkey,
blocksRequested: 10, blocksRequested: 10,
caps: ethereum.ServerCaps(),
} }
} }
@ -400,22 +402,22 @@ func (p *Peer) HandleInbound() {
case ethwire.MsgPeersTy: case ethwire.MsgPeersTy:
// Received a list of peers (probably because MsgGetPeersTy was send) // Received a list of peers (probably because MsgGetPeersTy was send)
// Only act on message if we actually requested for a peers list // Only act on message if we actually requested for a peers list
//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.Len()) peers := make([]string, data.Len())
// Parse each possible peer // Parse each possible peer
for i := 0; i < data.Len(); i++ { for i := 0; i < data.Len(); i++ {
value := data.Get(i) value := data.Get(i)
peers[i] = unpackAddr(value.Get(0), value.Get(1).Uint()) peers[i] = unpackAddr(value.Get(0), value.Get(1).Uint())
} }
// Connect to the list of peers // Connect to the list of peers
p.ethereum.ProcessPeerList(peers) p.ethereum.ProcessPeerList(peers)
// Mark unrequested again // Mark unrequested again
p.requestedPeerList = false p.requestedPeerList = false
//} }
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
@ -514,6 +516,15 @@ func (p *Peer) Stop() {
p.writeMessage(ethwire.NewMessage(ethwire.MsgDiscTy, "")) p.writeMessage(ethwire.NewMessage(ethwire.MsgDiscTy, ""))
p.conn.Close() p.conn.Close()
} }
// Pre-emptively remove the peer; don't wait for reaping. We already know it's dead if we are here
p.ethereum.peerMut.Lock()
defer p.ethereum.peerMut.Unlock()
eachPeer(p.ethereum.peers, func(peer *Peer, e *list.Element) {
if peer == p {
p.ethereum.peers.Remove(e)
}
})
} }
func (p *Peer) pushHandshake() error { func (p *Peer) pushHandshake() error {
@ -533,7 +544,10 @@ func (p *Peer) peersMessage() *ethwire.Msg {
outPeers := make([]interface{}, len(p.ethereum.InOutPeers())) outPeers := make([]interface{}, len(p.ethereum.InOutPeers()))
// Serialise each peer // Serialise each peer
for i, peer := range p.ethereum.InOutPeers() { for i, peer := range p.ethereum.InOutPeers() {
outPeers[i] = peer.RlpData() // Don't return localhost as valid peer
if !net.ParseIP(peer.conn.RemoteAddr().String()).IsLoopback() {
outPeers[i] = peer.RlpData()
}
} }
// Return the message to the peer with the known list of connected clients // Return the message to the peer with the known list of connected clients
@ -549,7 +563,7 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
c := msg.Data c := msg.Data
if c.Get(0).Uint() != ProtocolVersion { if c.Get(0).Uint() != ProtocolVersion {
ethutil.Config.Log.Debugln("Invalid peer version. Require protocol v5") ethutil.Config.Log.Debugln("Invalid peer version. Require protocol:", ProtocolVersion)
p.Stop() p.Stop()
return return
} }
@ -573,7 +587,6 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
} }
// Catch up with the connected peer // Catch up with the connected peer
// p.CatchupWithPeer(p.ethereum.BlockChain().CurrentBlock.Hash())
p.SyncWithBlocks() p.SyncWithBlocks()
// Set the peer's caps // Set the peer's caps

Loading…
Cancel
Save