2014-09-14 15:37:32 -07:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
"github.com/tendermint/tendermint/binary"
|
2014-09-14 15:37:32 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pexErrInvalidMessage = errors.New("Invalid PEX message")
|
|
|
|
|
|
|
|
const (
|
|
|
|
PexCh = byte(0x00)
|
|
|
|
ensurePeersPeriodSeconds = 30
|
|
|
|
minNumOutboundPeers = 10
|
|
|
|
maxNumPeers = 50
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
PEXReactor handles PEX (peer exchange) and ensures that an
|
|
|
|
adequate number of peers are connected to the switch.
|
|
|
|
*/
|
|
|
|
type PEXReactor struct {
|
|
|
|
sw *Switch
|
|
|
|
quit chan struct{}
|
|
|
|
started uint32
|
|
|
|
stopped uint32
|
|
|
|
|
|
|
|
book *AddrBook
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
func NewPEXReactor(book *AddrBook) *PEXReactor {
|
2014-09-14 15:37:32 -07:00
|
|
|
pexR := &PEXReactor{
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
book: book,
|
|
|
|
}
|
|
|
|
return pexR
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Implements Reactor
|
|
|
|
func (pexR *PEXReactor) Start(sw *Switch) {
|
2014-09-14 15:37:32 -07:00
|
|
|
if atomic.CompareAndSwapUint32(&pexR.started, 0, 1) {
|
|
|
|
log.Info("Starting PEXReactor")
|
2014-10-22 17:20:44 -07:00
|
|
|
pexR.sw = sw
|
2014-09-14 15:37:32 -07:00
|
|
|
go pexR.ensurePeersRoutine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Implements Reactor
|
2014-09-14 15:37:32 -07:00
|
|
|
func (pexR *PEXReactor) Stop() {
|
|
|
|
if atomic.CompareAndSwapUint32(&pexR.stopped, 0, 1) {
|
|
|
|
log.Info("Stopping PEXReactor")
|
|
|
|
close(pexR.quit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (pexR *PEXReactor) GetChannels() []*ChannelDescriptor {
|
|
|
|
return []*ChannelDescriptor{
|
|
|
|
&ChannelDescriptor{
|
2014-10-04 19:16:49 -07:00
|
|
|
Id: PexCh,
|
|
|
|
Priority: 1,
|
2014-09-14 15:37:32 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (pexR *PEXReactor) AddPeer(peer *Peer) {
|
|
|
|
if peer.IsOutbound() {
|
|
|
|
pexR.SendAddrs(peer, pexR.book.OurAddresses())
|
|
|
|
if pexR.book.NeedMoreAddrs() {
|
|
|
|
pexR.RequestPEX(peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
2014-10-22 17:20:44 -07:00
|
|
|
func (pexR *PEXReactor) RemovePeer(peer *Peer, reason interface{}) {
|
2014-09-14 15:37:32 -07:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
// Handles incoming PEX messages.
|
|
|
|
func (pexR *PEXReactor) Receive(chId byte, src *Peer, msgBytes []byte) {
|
|
|
|
|
|
|
|
// decode message
|
2014-12-29 16:04:38 -08:00
|
|
|
msg, err := DecodeMessage(msgBytes)
|
|
|
|
if err != nil {
|
2014-12-29 18:39:19 -08:00
|
|
|
log.Warn("Error decoding message", "error", err)
|
2014-12-29 16:04:38 -08:00
|
|
|
return
|
|
|
|
}
|
2014-12-29 18:39:19 -08:00
|
|
|
log.Info("Received message", "msg", msg)
|
2014-09-14 15:37:32 -07:00
|
|
|
|
|
|
|
switch msg.(type) {
|
|
|
|
case *pexRequestMessage:
|
|
|
|
// src requested some peers.
|
|
|
|
// TODO: prevent abuse.
|
|
|
|
addrs := pexR.book.GetSelection()
|
2014-12-09 18:49:04 -08:00
|
|
|
msg := &pexAddrsMessage{Addrs: addrs}
|
2014-09-14 15:37:32 -07:00
|
|
|
queued := src.TrySend(PexCh, msg)
|
|
|
|
if !queued {
|
|
|
|
// ignore
|
|
|
|
}
|
2014-12-09 18:49:04 -08:00
|
|
|
case *pexAddrsMessage:
|
2014-09-14 15:37:32 -07:00
|
|
|
// We received some peer addresses from src.
|
|
|
|
// TODO: prevent abuse.
|
|
|
|
// (We don't want to get spammed with bad peers)
|
2014-12-29 15:14:54 -08:00
|
|
|
srcAddr := src.Connection().RemoteAddress
|
2014-12-09 18:49:04 -08:00
|
|
|
for _, addr := range msg.(*pexAddrsMessage).Addrs {
|
2014-09-14 15:37:32 -07:00
|
|
|
pexR.book.AddAddress(addr, srcAddr)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Ignore unknown message.
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Asks peer for more addresses.
|
|
|
|
func (pexR *PEXReactor) RequestPEX(peer *Peer) {
|
|
|
|
peer.TrySend(PexCh, &pexRequestMessage{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pexR *PEXReactor) SendAddrs(peer *Peer, addrs []*NetAddress) {
|
2014-12-09 18:49:04 -08:00
|
|
|
peer.Send(PexCh, &pexAddrsMessage{Addrs: addrs})
|
2014-10-22 17:20:44 -07:00
|
|
|
}
|
|
|
|
|
2014-09-14 15:37:32 -07:00
|
|
|
// Ensures that sufficient peers are connected. (continuous)
|
|
|
|
func (pexR *PEXReactor) ensurePeersRoutine() {
|
|
|
|
// fire once immediately.
|
|
|
|
pexR.ensurePeers()
|
|
|
|
// fire periodically
|
2015-01-16 00:31:34 -08:00
|
|
|
timer := NewRepeatTimer("pex", ensurePeersPeriodSeconds*time.Second)
|
2014-09-14 15:37:32 -07:00
|
|
|
FOR_LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.Ch:
|
|
|
|
pexR.ensurePeers()
|
|
|
|
case <-pexR.quit:
|
|
|
|
break FOR_LOOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures that sufficient peers are connected. (once)
|
|
|
|
func (pexR *PEXReactor) ensurePeers() {
|
|
|
|
numOutPeers, _, numDialing := pexR.sw.NumPeers()
|
|
|
|
numToDial := minNumOutboundPeers - (numOutPeers + numDialing)
|
2015-01-20 15:31:31 -08:00
|
|
|
log.Debug("Ensure peers", "numOutPeers", numOutPeers, "numDialing", numDialing, "numToDial", numToDial)
|
2014-09-14 15:37:32 -07:00
|
|
|
if numToDial <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
toDial := NewCMap()
|
|
|
|
|
|
|
|
// Try to pick numToDial addresses to dial.
|
|
|
|
// TODO: improve logic.
|
|
|
|
for i := 0; i < numToDial; i++ {
|
|
|
|
newBias := MinInt(numOutPeers, 8)*10 + 10
|
|
|
|
var picked *NetAddress
|
|
|
|
// Try to fetch a new peer 3 times.
|
|
|
|
// This caps the maximum number of tries to 3 * numToDial.
|
2015-01-14 00:02:34 -08:00
|
|
|
for j := 0; j < 3; j++ {
|
2015-01-20 15:31:31 -08:00
|
|
|
try := pexR.book.PickAddress(newBias)
|
|
|
|
if try == nil {
|
|
|
|
break
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
2015-01-20 15:31:31 -08:00
|
|
|
if toDial.Has(try.String()) ||
|
|
|
|
pexR.sw.IsDialing(try) ||
|
|
|
|
pexR.sw.Peers().Has(try.String()) {
|
2014-09-14 15:37:32 -07:00
|
|
|
continue
|
|
|
|
} else {
|
2015-01-20 15:31:31 -08:00
|
|
|
picked = try
|
2014-09-14 15:37:32 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if picked == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toDial.Set(picked.String(), picked)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial picked addresses
|
|
|
|
for _, item := range toDial.Values() {
|
|
|
|
picked := item.(*NetAddress)
|
|
|
|
go func() {
|
|
|
|
_, err := pexR.sw.DialPeerWithAddress(picked)
|
|
|
|
if err != nil {
|
|
|
|
pexR.book.MarkAttempt(picked)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-12-09 18:49:04 -08:00
|
|
|
// Messages
|
2014-09-14 15:37:32 -07:00
|
|
|
|
|
|
|
const (
|
|
|
|
msgTypeUnknown = byte(0x00)
|
|
|
|
msgTypeRequest = byte(0x01)
|
|
|
|
msgTypeAddrs = byte(0x02)
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: check for unnecessary extra bytes at the end.
|
2014-12-29 16:04:38 -08:00
|
|
|
func DecodeMessage(bz []byte) (msg interface{}, err error) {
|
|
|
|
n := new(int64)
|
|
|
|
msgType := bz[0]
|
|
|
|
r := bytes.NewReader(bz)
|
2014-12-29 18:09:06 -08:00
|
|
|
// log.Debug(Fmt("decoding msg bytes: %X", bz))
|
2014-12-29 16:04:38 -08:00
|
|
|
switch msgType {
|
2014-09-14 15:37:32 -07:00
|
|
|
case msgTypeRequest:
|
2014-12-29 16:04:38 -08:00
|
|
|
msg = &pexRequestMessage{}
|
2014-09-14 15:37:32 -07:00
|
|
|
case msgTypeAddrs:
|
2015-01-14 20:34:53 -08:00
|
|
|
msg = binary.ReadBinary(&pexAddrsMessage{}, r, n, &err)
|
2014-09-14 15:37:32 -07:00
|
|
|
default:
|
2014-12-29 16:04:38 -08:00
|
|
|
msg = nil
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
2014-12-29 16:04:38 -08:00
|
|
|
return
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
A pexRequestMessage requests additional peer addresses.
|
|
|
|
*/
|
|
|
|
type pexRequestMessage struct {
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (m *pexRequestMessage) TypeByte() byte { return msgTypeRequest }
|
2014-09-14 15:37:32 -07:00
|
|
|
|
|
|
|
func (m *pexRequestMessage) String() string {
|
|
|
|
return "[pexRequest]"
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
A message with announced peer addresses.
|
|
|
|
*/
|
2014-12-09 18:49:04 -08:00
|
|
|
type pexAddrsMessage struct {
|
2014-09-14 15:37:32 -07:00
|
|
|
Addrs []*NetAddress
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (m *pexAddrsMessage) TypeByte() byte { return msgTypeAddrs }
|
2014-09-14 15:37:32 -07:00
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (m *pexAddrsMessage) String() string {
|
|
|
|
return fmt.Sprintf("[pexAddrs %v]", m.Addrs)
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|