|
|
@ -3,7 +3,9 @@ package main |
|
|
|
import ( |
|
|
|
"os" |
|
|
|
"os/signal" |
|
|
|
"time" |
|
|
|
|
|
|
|
. "github.com/tendermint/tendermint/common" |
|
|
|
"github.com/tendermint/tendermint/config" |
|
|
|
"github.com/tendermint/tendermint/p2p" |
|
|
|
) |
|
|
@ -18,7 +20,7 @@ const ( |
|
|
|
|
|
|
|
type Node struct { |
|
|
|
sw *p2p.Switch |
|
|
|
book *p2p.AddressBook |
|
|
|
book *p2p.AddrBook |
|
|
|
quit chan struct{} |
|
|
|
dialing *CMap |
|
|
|
} |
|
|
@ -50,7 +52,7 @@ func NewNode() *Node { |
|
|
|
sw := p2p.NewSwitch(chDescs) |
|
|
|
book := p2p.NewAddrBook(config.AppDir + "/addrbook.json") |
|
|
|
|
|
|
|
return &New{ |
|
|
|
return &Node{ |
|
|
|
sw: sw, |
|
|
|
book: book, |
|
|
|
quit: make(chan struct{}, 0), |
|
|
@ -59,20 +61,21 @@ func NewNode() *Node { |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Node) Start() { |
|
|
|
log.Infof("Starting node") |
|
|
|
n.sw.Start() |
|
|
|
n.book.Start() |
|
|
|
go p2p.PexHandler(sw, book) |
|
|
|
go n.ensurePeersHandler(sw, book) |
|
|
|
go p2p.PexHandler(n.sw, n.book) |
|
|
|
go n.ensurePeersHandler() |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Node) initPeer(peer *Peer) { |
|
|
|
if peer.IsOutgoing() { |
|
|
|
func (n *Node) initPeer(peer *p2p.Peer) { |
|
|
|
if peer.IsOutbound() { |
|
|
|
// TODO: initiate PEX
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Add a Listener to accept incoming peer connections.
|
|
|
|
func (n *Node) AddListener(l Listener) { |
|
|
|
func (n *Node) AddListener(l p2p.Listener) { |
|
|
|
go func() { |
|
|
|
for { |
|
|
|
inConn, ok := <-l.Connections() |
|
|
@ -92,18 +95,56 @@ func (n *Node) AddListener(l Listener) { |
|
|
|
|
|
|
|
// Ensures that sufficient peers are connected.
|
|
|
|
func (n *Node) ensurePeers() { |
|
|
|
numPeers := len(n.sw.Peers()) |
|
|
|
numPeers := n.sw.NumOutboundPeers() |
|
|
|
numDialing := n.dialing.Size() |
|
|
|
numToDial = minNumPeers - (numPeers + numDialing) |
|
|
|
numToDial := minNumPeers - (numPeers + numDialing) |
|
|
|
if numToDial <= 0 { |
|
|
|
return |
|
|
|
} |
|
|
|
for i := 0; i < numToDial; i++ { |
|
|
|
// XXX
|
|
|
|
newBias := MinInt(numPeers, 8)*10 + 10 |
|
|
|
var picked *p2p.NetAddress |
|
|
|
// Try to fetch a new peer 3 times.
|
|
|
|
// This caps the maximum number of tries to 3 * numToDial.
|
|
|
|
for j := 0; i < 3; j++ { |
|
|
|
picked = n.book.PickAddress(newBias) |
|
|
|
if picked == nil { |
|
|
|
log.Infof("Empty addrbook.") |
|
|
|
return |
|
|
|
} |
|
|
|
if n.sw.Peers().Has(picked) { |
|
|
|
continue |
|
|
|
} else { |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
if picked == nil { |
|
|
|
continue |
|
|
|
} |
|
|
|
n.dialing.Set(picked.String(), picked) |
|
|
|
n.book.MarkAttempt(picked) |
|
|
|
go func() { |
|
|
|
log.Infof("Dialing addr: %v", picked) |
|
|
|
conn, err := picked.DialTimeout(peerDialTimeoutSeconds * time.Second) |
|
|
|
n.dialing.Delete(picked.String()) |
|
|
|
if err != nil { |
|
|
|
// ignore error.
|
|
|
|
return |
|
|
|
} |
|
|
|
peer, err := n.sw.AddPeerWithConnection(conn, true) |
|
|
|
if err != nil { |
|
|
|
log.Warnf("Error trying to add new outbound peer connection:%v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
n.initPeer(peer) |
|
|
|
}() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Node) ensurePeersHandler() { |
|
|
|
// fire once immediately.
|
|
|
|
n.ensurePeers() |
|
|
|
// fire periodically
|
|
|
|
timer := NewRepeatTimer(ensurePeersPeriodSeconds * time.Second) |
|
|
|
FOR_LOOP: |
|
|
|
for { |
|
|
@ -131,7 +172,8 @@ func main() { |
|
|
|
|
|
|
|
n := NewNode() |
|
|
|
l := p2p.NewDefaultListener("tcp", ":8001") |
|
|
|
n.AddListener() |
|
|
|
n.AddListener(l) |
|
|
|
n.Start() |
|
|
|
|
|
|
|
if false { |
|
|
|
// TODO remove
|
|
|
@ -141,7 +183,7 @@ func main() { |
|
|
|
log.Infof("Error connecting to it: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
peer, err := sw.AddPeerWithConnection(conn, true) |
|
|
|
peer, err := n.sw.AddPeerWithConnection(conn, true) |
|
|
|
if err != nil { |
|
|
|
log.Infof("Error adding peer with connection: %v", err) |
|
|
|
return |
|
|
|