tendermint/p2p/peer.go

368 lines
9.8 KiB
Go
Raw Normal View History

2015-10-25 18:21:51 -07:00
package p2p
import (
"fmt"
"net"
2017-04-07 03:57:03 -07:00
"time"
2015-10-25 18:21:51 -07:00
2017-04-14 01:43:28 -07:00
"github.com/pkg/errors"
2017-10-04 13:40:45 -07:00
2017-04-07 03:57:03 -07:00
crypto "github.com/tendermint/go-crypto"
wire "github.com/tendermint/go-wire"
2017-04-21 15:02:25 -07:00
cmn "github.com/tendermint/tmlibs/common"
2017-11-09 11:57:40 -08:00
"github.com/tendermint/tmlibs/log"
2018-01-20 18:12:04 -08:00
2018-01-20 21:33:53 -08:00
tmconn "github.com/tendermint/tendermint/p2p/conn"
2015-10-25 18:21:51 -07:00
)
2017-09-12 17:49:22 -07:00
// Peer is an interface representing a peer connected on a reactor.
type Peer interface {
cmn.Service
QuitChan() <-chan struct{}
2017-09-12 17:49:22 -07:00
2018-01-20 21:33:53 -08:00
ID() ID // peer's cryptographic ID
IsOutbound() bool // did we dial the peer
IsPersistent() bool // do we redial this peer when we disconnect
NodeInfo() NodeInfo // peer's info
2018-01-20 18:12:04 -08:00
Status() tmconn.ConnectionStatus
2017-09-12 17:49:22 -07:00
Send(byte, interface{}) bool
TrySend(byte, interface{}) bool
Set(string, interface{})
Get(string) interface{}
}
2018-01-13 21:44:16 -08:00
//----------------------------------------------------------
// peer implements Peer.
2017-04-07 03:57:03 -07:00
//
// Before using a peer, you will need to perform a handshake on connection.
2017-09-12 17:49:22 -07:00
type peer struct {
2017-04-07 03:57:03 -07:00
cmn.BaseService
2015-10-25 18:21:51 -07:00
outbound bool
2017-04-07 03:57:03 -07:00
2018-01-20 18:12:04 -08:00
conn net.Conn // source connection
mconn *tmconn.MConnection // multiplex connection
2017-04-07 03:57:03 -07:00
persistent bool
2017-04-11 08:47:05 -07:00
config *PeerConfig
2015-10-25 18:21:51 -07:00
nodeInfo NodeInfo // peer's node info
channels []byte // channels the peer knows about
2017-09-12 17:49:22 -07:00
Data *cmn.CMap // User data.
2017-04-07 03:57:03 -07:00
}
2017-04-13 01:36:16 -07:00
// PeerConfig is a Peer configuration.
2017-04-11 08:47:05 -07:00
type PeerConfig struct {
2017-05-01 19:05:26 -07:00
AuthEnc bool `mapstructure:"auth_enc"` // authenticated encryption
2017-04-11 08:47:05 -07:00
2017-05-01 19:05:26 -07:00
// times are in seconds
HandshakeTimeout time.Duration `mapstructure:"handshake_timeout"`
DialTimeout time.Duration `mapstructure:"dial_timeout"`
2017-04-11 08:47:05 -07:00
2018-01-20 18:12:04 -08:00
MConfig *tmconn.MConnConfig `mapstructure:"connection"`
2017-04-11 08:47:05 -07:00
2017-05-01 19:05:26 -07:00
Fuzz bool `mapstructure:"fuzz"` // fuzz connection (for testing)
FuzzConfig *FuzzConnConfig `mapstructure:"fuzz_config"`
2017-04-11 08:47:05 -07:00
}
2017-04-13 01:36:16 -07:00
// DefaultPeerConfig returns the default config.
func DefaultPeerConfig() *PeerConfig {
2017-04-11 08:47:05 -07:00
return &PeerConfig{
AuthEnc: true,
2017-05-01 19:05:26 -07:00
HandshakeTimeout: 20, // * time.Second,
DialTimeout: 3, // * time.Second,
2018-01-20 18:12:04 -08:00
MConfig: tmconn.DefaultMConnConfig(),
2017-04-14 01:43:28 -07:00
Fuzz: false,
FuzzConfig: DefaultFuzzConnConfig(),
2017-04-11 08:47:05 -07:00
}
}
2018-01-20 21:33:53 -08:00
func newOutboundPeer(addr *NetAddress, reactorsByCh map[byte]Reactor, chDescs []*tmconn.ChannelDescriptor,
2018-01-13 21:44:16 -08:00
onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig, persistent bool) (*peer, error) {
2017-04-07 03:57:03 -07:00
conn, err := dial(addr, config)
if err != nil {
2017-04-14 01:43:28 -07:00
return nil, errors.Wrap(err, "Error creating peer")
2017-04-07 03:57:03 -07:00
}
2017-04-13 01:36:16 -07:00
peer, err := newPeerFromConnAndConfig(conn, true, reactorsByCh, chDescs, onPeerError, ourNodePrivKey, config)
2017-04-11 09:42:11 -07:00
if err != nil {
2017-10-03 16:36:01 -07:00
if err := conn.Close(); err != nil {
return nil, err
}
2017-04-11 09:42:11 -07:00
return nil, err
}
2018-01-13 21:44:16 -08:00
peer.persistent = persistent
2018-01-01 20:23:11 -08:00
2017-04-11 09:42:11 -07:00
return peer, nil
2017-04-07 03:57:03 -07:00
}
2018-01-20 18:12:04 -08:00
func newInboundPeer(conn net.Conn, reactorsByCh map[byte]Reactor, chDescs []*tmconn.ChannelDescriptor,
2017-12-28 22:53:41 -08:00
onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig) (*peer, error) {
2018-01-13 14:25:51 -08:00
// TODO: issue PoW challenge
2017-04-13 01:36:16 -07:00
return newPeerFromConnAndConfig(conn, false, reactorsByCh, chDescs, onPeerError, ourNodePrivKey, config)
}
2018-01-20 18:12:04 -08:00
func newPeerFromConnAndConfig(rawConn net.Conn, outbound bool, reactorsByCh map[byte]Reactor, chDescs []*tmconn.ChannelDescriptor,
2017-12-28 22:53:41 -08:00
onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig) (*peer, error) {
2017-04-11 08:47:05 -07:00
conn := rawConn
// Fuzz connection
if config.Fuzz {
// so we have time to do peer handshakes and get set up
conn = FuzzConnAfterFromConfig(conn, 10*time.Second, config.FuzzConfig)
}
2017-04-07 03:57:03 -07:00
// Encrypt connection
2017-04-11 08:47:05 -07:00
if config.AuthEnc {
p2p: peer should respect errors from SetDeadline Noticed while auditing the code that we aren't respecting (*net.Conn) SetDeadline errors which return after a connection has been killed and is simultaneously being used. For example given program, without SetDeadline error checks ```go package main import ( "log" "net" "time" ) func main() { conn, err := net.Dial("tcp", "tendermint.com:443") if err != nil { log.Fatal(err) } go func() { <-time.After(400 * time.Millisecond) conn.Close() }() for i := 0; i < 5; i++ { if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { log.Fatalf("set deadline #%d, err: %v", i, err) } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } } ``` erraneously gives ```shell 2017/11/14 17:46:28 Successfully set deadline #0 2017/11/14 17:46:29 Successfully set deadline #1 2017/11/14 17:46:29 Successfully set deadline #2 2017/11/14 17:46:29 Successfully set deadline #3 2017/11/14 17:46:29 Successfully set deadline #4 ``` However, if we properly fix it to respect that error with ```diff --- wild.go 2017-11-14 17:44:38.000000000 -0700 +++ main.go 2017-11-14 17:45:40.000000000 -0700 @@ -16,7 +16,9 @@ conn.Close() }() for i := 0; i < 5; i++ { - conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))) + if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { + log.Fatalf("set deadline #%d, err: %v", i, err) + } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } ``` properly catches any problems and gives ```shell $ go run main.go 2017/11/14 17:43:44 Successfully set deadline #0 2017/11/14 17:43:45 Successfully set deadline #1 2017/11/14 17:43:45 Successfully set deadline #2 2017/11/14 17:43:45 set deadline #3, err: set tcp 10.182.253.51:57395: use of closed network connection exit status 1 ```
2017-11-14 16:39:32 -08:00
if err := conn.SetDeadline(time.Now().Add(config.HandshakeTimeout * time.Second)); err != nil {
return nil, errors.Wrap(err, "Error setting deadline while encrypting connection")
}
2017-04-11 08:47:05 -07:00
2017-04-07 03:57:03 -07:00
var err error
2018-01-20 18:12:04 -08:00
conn, err = tmconn.MakeSecretConnection(conn, ourNodePrivKey)
2017-04-07 03:57:03 -07:00
if err != nil {
2017-04-14 01:43:28 -07:00
return nil, errors.Wrap(err, "Error creating peer")
2017-04-07 03:57:03 -07:00
}
}
2018-01-13 21:10:29 -08:00
// NodeInfo is set after Handshake
2017-09-12 17:49:22 -07:00
p := &peer{
2017-04-07 03:57:03 -07:00
outbound: outbound,
conn: conn,
config: config,
Data: cmn.NewCMap(),
}
2017-04-11 08:47:05 -07:00
p.mconn = createMConnection(conn, p, reactorsByCh, chDescs, onPeerError, config.MConfig)
2017-04-07 03:57:03 -07:00
2017-05-02 00:53:32 -07:00
p.BaseService = *cmn.NewBaseService(nil, "Peer", p)
2017-04-07 03:57:03 -07:00
return p, nil
}
2018-01-13 21:44:16 -08:00
//---------------------------------------------------
// Implements cmn.Service
// SetLogger implements BaseService.
2017-11-09 11:57:40 -08:00
func (p *peer) SetLogger(l log.Logger) {
p.Logger = l
p.mconn.SetLogger(l)
}
2018-01-13 21:44:16 -08:00
// OnStart implements BaseService.
func (p *peer) OnStart() error {
if err := p.BaseService.OnStart(); err != nil {
return err
}
err := p.mconn.Start()
return err
2017-04-07 03:57:03 -07:00
}
2018-01-13 21:44:16 -08:00
// OnStop implements BaseService.
func (p *peer) OnStop() {
p.BaseService.OnStop()
p.mconn.Stop() // stop everything and close the conn
}
//---------------------------------------------------
// Implements Peer
2017-04-07 03:57:03 -07:00
2018-01-13 21:44:16 -08:00
// ID returns the peer's ID - the hex encoded hash of its pubkey.
2018-01-20 21:33:53 -08:00
func (p *peer) ID() ID {
return PubKeyToID(p.PubKey())
2018-01-13 21:44:16 -08:00
}
// IsOutbound returns true if the connection is outbound, false otherwise.
func (p *peer) IsOutbound() bool {
return p.outbound
2015-10-25 18:21:51 -07:00
}
2017-04-07 03:57:03 -07:00
// IsPersistent returns true if the peer is persitent, false otherwise.
2017-09-12 17:49:22 -07:00
func (p *peer) IsPersistent() bool {
2017-04-07 03:57:03 -07:00
return p.persistent
}
2018-01-13 21:44:16 -08:00
// NodeInfo returns a copy of the peer's NodeInfo.
2018-01-20 21:33:53 -08:00
func (p *peer) NodeInfo() NodeInfo {
2018-01-13 21:44:16 -08:00
return p.nodeInfo
}
// Status returns the peer's ConnectionStatus.
2018-01-20 18:12:04 -08:00
func (p *peer) Status() tmconn.ConnectionStatus {
2018-01-13 21:44:16 -08:00
return p.mconn.Status()
}
// Send msg to the channel identified by chID byte. Returns false if the send
// queue is full after timeout, specified by MConnection.
func (p *peer) Send(chID byte, msg interface{}) bool {
if !p.IsRunning() {
// see Switch#Broadcast, where we fetch the list of peers and loop over
// them - while we're looping, one peer may be removed and stopped.
return false
} else if !p.hasChannel(chID) {
return false
2018-01-13 21:44:16 -08:00
}
return p.mconn.Send(chID, msg)
}
// TrySend msg to the channel identified by chID byte. Immediately returns
// false if the send queue is full.
func (p *peer) TrySend(chID byte, msg interface{}) bool {
if !p.IsRunning() {
return false
} else if !p.hasChannel(chID) {
return false
2018-01-13 21:44:16 -08:00
}
return p.mconn.TrySend(chID, msg)
}
// Get the data for a given key.
func (p *peer) Get(key string) interface{} {
return p.Data.Get(key)
}
// Set sets the data for the given key.
func (p *peer) Set(key string, data interface{}) {
p.Data.Set(key, data)
}
// hasChannel returns true if the peer reported
// knowing about the given chID.
func (p *peer) hasChannel(chID byte) bool {
for _, ch := range p.channels {
if ch == chID {
return true
}
}
2018-01-23 20:40:33 -08:00
// NOTE: probably will want to remove this
// but could be helpful while the feature is new
p.Logger.Debug("Unknown channel for peer", "channel", chID, "channels", p.channels)
return false
}
2018-01-13 21:44:16 -08:00
//---------------------------------------------------
// methods used by the Switch
// CloseConn should be called by the Switch if the peer was created but never started.
func (p *peer) CloseConn() {
p.conn.Close() // nolint: errcheck
}
// HandshakeTimeout performs the Tendermint P2P handshake between a given node and the peer
// by exchanging their NodeInfo. It sets the received nodeInfo on the peer.
2015-10-25 18:21:51 -07:00
// NOTE: blocking
2018-01-20 21:33:53 -08:00
func (p *peer) HandshakeTimeout(ourNodeInfo NodeInfo, timeout time.Duration) error {
2017-04-07 03:57:03 -07:00
// Set deadline for handshake so we don't block forever on conn.ReadFull
p2p: peer should respect errors from SetDeadline Noticed while auditing the code that we aren't respecting (*net.Conn) SetDeadline errors which return after a connection has been killed and is simultaneously being used. For example given program, without SetDeadline error checks ```go package main import ( "log" "net" "time" ) func main() { conn, err := net.Dial("tcp", "tendermint.com:443") if err != nil { log.Fatal(err) } go func() { <-time.After(400 * time.Millisecond) conn.Close() }() for i := 0; i < 5; i++ { if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { log.Fatalf("set deadline #%d, err: %v", i, err) } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } } ``` erraneously gives ```shell 2017/11/14 17:46:28 Successfully set deadline #0 2017/11/14 17:46:29 Successfully set deadline #1 2017/11/14 17:46:29 Successfully set deadline #2 2017/11/14 17:46:29 Successfully set deadline #3 2017/11/14 17:46:29 Successfully set deadline #4 ``` However, if we properly fix it to respect that error with ```diff --- wild.go 2017-11-14 17:44:38.000000000 -0700 +++ main.go 2017-11-14 17:45:40.000000000 -0700 @@ -16,7 +16,9 @@ conn.Close() }() for i := 0; i < 5; i++ { - conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))) + if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { + log.Fatalf("set deadline #%d, err: %v", i, err) + } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } ``` properly catches any problems and gives ```shell $ go run main.go 2017/11/14 17:43:44 Successfully set deadline #0 2017/11/14 17:43:45 Successfully set deadline #1 2017/11/14 17:43:45 Successfully set deadline #2 2017/11/14 17:43:45 set deadline #3, err: set tcp 10.182.253.51:57395: use of closed network connection exit status 1 ```
2017-11-14 16:39:32 -08:00
if err := p.conn.SetDeadline(time.Now().Add(timeout)); err != nil {
return errors.Wrap(err, "Error setting deadline")
}
2017-04-07 03:57:03 -07:00
2018-01-20 21:33:53 -08:00
var peerNodeInfo NodeInfo
2015-10-25 18:21:51 -07:00
var err1 error
var err2 error
2017-04-07 03:57:03 -07:00
cmn.Parallel(
2015-10-25 18:21:51 -07:00
func() {
2015-11-10 12:29:43 -08:00
var n int
2018-01-13 21:10:29 -08:00
wire.WriteBinary(&ourNodeInfo, p.conn, &n, &err1)
2015-10-25 18:21:51 -07:00
},
func() {
2015-11-10 12:29:43 -08:00
var n int
2018-01-20 21:33:53 -08:00
wire.ReadBinary(&peerNodeInfo, p.conn, MaxNodeInfoSize(), &n, &err2)
2017-05-02 00:53:32 -07:00
p.Logger.Info("Peer handshake", "peerNodeInfo", peerNodeInfo)
2015-10-25 18:21:51 -07:00
})
if err1 != nil {
2017-04-14 01:43:28 -07:00
return errors.Wrap(err1, "Error during handshake/write")
2015-10-25 18:21:51 -07:00
}
if err2 != nil {
2017-04-14 01:43:28 -07:00
return errors.Wrap(err2, "Error during handshake/read")
2015-10-25 18:21:51 -07:00
}
2017-04-07 03:57:03 -07:00
// Remove deadline
p2p: peer should respect errors from SetDeadline Noticed while auditing the code that we aren't respecting (*net.Conn) SetDeadline errors which return after a connection has been killed and is simultaneously being used. For example given program, without SetDeadline error checks ```go package main import ( "log" "net" "time" ) func main() { conn, err := net.Dial("tcp", "tendermint.com:443") if err != nil { log.Fatal(err) } go func() { <-time.After(400 * time.Millisecond) conn.Close() }() for i := 0; i < 5; i++ { if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { log.Fatalf("set deadline #%d, err: %v", i, err) } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } } ``` erraneously gives ```shell 2017/11/14 17:46:28 Successfully set deadline #0 2017/11/14 17:46:29 Successfully set deadline #1 2017/11/14 17:46:29 Successfully set deadline #2 2017/11/14 17:46:29 Successfully set deadline #3 2017/11/14 17:46:29 Successfully set deadline #4 ``` However, if we properly fix it to respect that error with ```diff --- wild.go 2017-11-14 17:44:38.000000000 -0700 +++ main.go 2017-11-14 17:45:40.000000000 -0700 @@ -16,7 +16,9 @@ conn.Close() }() for i := 0; i < 5; i++ { - conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))) + if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { + log.Fatalf("set deadline #%d, err: %v", i, err) + } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } ``` properly catches any problems and gives ```shell $ go run main.go 2017/11/14 17:43:44 Successfully set deadline #0 2017/11/14 17:43:45 Successfully set deadline #1 2017/11/14 17:43:45 Successfully set deadline #2 2017/11/14 17:43:45 set deadline #3, err: set tcp 10.182.253.51:57395: use of closed network connection exit status 1 ```
2017-11-14 16:39:32 -08:00
if err := p.conn.SetDeadline(time.Time{}); err != nil {
return errors.Wrap(err, "Error removing deadline")
}
2017-04-07 03:57:03 -07:00
p.setNodeInfo(peerNodeInfo)
2017-04-07 03:57:03 -07:00
return nil
}
func (p *peer) setNodeInfo(nodeInfo NodeInfo) {
p.nodeInfo = nodeInfo
// cache the channels so we dont copy nodeInfo
// every time we check hasChannel
p.channels = nodeInfo.Channels
}
2017-05-12 14:07:53 -07:00
// Addr returns peer's remote network address.
2017-09-12 17:49:22 -07:00
func (p *peer) Addr() net.Addr {
2017-04-07 03:57:03 -07:00
return p.conn.RemoteAddr()
}
2017-04-14 01:43:28 -07:00
// PubKey returns peer's public key.
2017-12-28 22:53:41 -08:00
func (p *peer) PubKey() crypto.PubKey {
2018-01-13 21:10:29 -08:00
if !p.nodeInfo.PubKey.Empty() {
2018-01-01 20:23:11 -08:00
return p.nodeInfo.PubKey
} else if p.config.AuthEnc {
2018-01-20 18:12:04 -08:00
return p.conn.(*tmconn.SecretConnection).RemotePubKey()
2017-04-06 05:43:45 -07:00
}
2018-01-01 20:23:11 -08:00
panic("Attempt to get peer's PubKey before calling Handshake")
2015-10-25 18:21:51 -07:00
}
2017-04-07 03:57:03 -07:00
// CanSend returns true if the send queue is not full, false otherwise.
2017-09-12 17:49:22 -07:00
func (p *peer) CanSend(chID byte) bool {
2015-10-25 18:21:51 -07:00
if !p.IsRunning() {
return false
}
return p.mconn.CanSend(chID)
}
2017-04-07 03:57:03 -07:00
// String representation.
2017-09-12 17:49:22 -07:00
func (p *peer) String() string {
2015-10-25 18:21:51 -07:00
if p.outbound {
2018-01-01 18:27:38 -08:00
return fmt.Sprintf("Peer{%v %v out}", p.mconn, p.ID())
2015-10-25 18:21:51 -07:00
}
2017-04-07 03:57:03 -07:00
2018-01-01 18:27:38 -08:00
return fmt.Sprintf("Peer{%v %v in}", p.mconn, p.ID())
2015-10-25 18:21:51 -07:00
}
// QuitChan returns a channel, which will be closed once peer is stopped.
func (p *peer) QuitChan() <-chan struct{} {
return p.Quit
}
2018-01-13 21:44:16 -08:00
//------------------------------------------------------------------
// helper funcs
2017-09-12 17:49:22 -07:00
2018-01-20 21:33:53 -08:00
func dial(addr *NetAddress, config *PeerConfig) (net.Conn, error) {
2017-05-01 19:05:26 -07:00
conn, err := addr.DialTimeout(config.DialTimeout * time.Second)
2017-04-07 03:57:03 -07:00
if err != nil {
return nil, err
}
return conn, nil
}
2018-01-20 18:12:04 -08:00
func createMConnection(conn net.Conn, p *peer, reactorsByCh map[byte]Reactor, chDescs []*tmconn.ChannelDescriptor,
onPeerError func(Peer, interface{}), config *tmconn.MConnConfig) *tmconn.MConnection {
2017-04-07 03:57:03 -07:00
onReceive := func(chID byte, msgBytes []byte) {
reactor := reactorsByCh[chID]
if reactor == nil {
cmn.PanicSanity(cmn.Fmt("Unknown channel %X", chID))
}
reactor.Receive(chID, p, msgBytes)
}
onError := func(r interface{}) {
onPeerError(p, r)
}
2018-01-20 18:12:04 -08:00
return tmconn.NewMConnectionWithConfig(conn, chDescs, onReceive, onError, config)
2017-04-07 03:57:03 -07:00
}