tendermint/p2p/peer.go

351 lines
9.0 KiB
Go
Raw Normal View History

2015-10-25 18:21:51 -07:00
package p2p
import (
2018-01-01 18:27:38 -08:00
"encoding/hex"
2015-10-25 18:21:51 -07:00
"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"
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
2018-01-01 18:27:38 -08:00
ID() ID
2017-09-12 17:49:22 -07:00
IsOutbound() bool
IsPersistent() bool
NodeInfo() *NodeInfo
Status() ConnectionStatus
Send(byte, interface{}) bool
TrySend(byte, interface{}) bool
Set(string, interface{})
Get(string) interface{}
}
2017-04-07 03:57:03 -07:00
// Peer could be marked as persistent, in which case you can use
// Redial function to reconnect. Note that inbound peers can't be
// made persistent. They should be made persistent on the other end.
//
// 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
conn net.Conn // source connection
mconn *MConnection // multiplex connection
persistent bool
2017-04-11 08:47:05 -07:00
config *PeerConfig
2015-10-25 18:21:51 -07:00
2017-09-12 17:49:22 -07:00
nodeInfo *NodeInfo
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
2017-05-01 19:05:26 -07:00
MConfig *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,
2017-04-14 01:43:28 -07:00
MConfig: DefaultMConnConfig(),
Fuzz: false,
FuzzConfig: DefaultFuzzConnConfig(),
2017-04-11 08:47:05 -07:00
}
}
func newOutboundPeer(addr *NetAddress, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor,
2017-12-28 22:53:41 -08:00
onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig) (*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
}
return peer, nil
2017-04-07 03:57:03 -07:00
}
func newInboundPeer(conn net.Conn, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor,
2017-12-28 22:53:41 -08:00
onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig) (*peer, error) {
2017-04-13 01:36:16 -07:00
return newPeerFromConnAndConfig(conn, false, reactorsByCh, chDescs, onPeerError, ourNodePrivKey, config)
}
func newPeerFromConnAndConfig(rawConn net.Conn, outbound bool, reactorsByCh map[byte]Reactor, chDescs []*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
2017-04-11 08:47:05 -07:00
conn, err = 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
}
}
// Key and NodeInfo are 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
}
2017-11-09 11:57:40 -08:00
func (p *peer) SetLogger(l log.Logger) {
p.Logger = l
p.mconn.SetLogger(l)
}
2017-04-07 03:57:03 -07:00
// CloseConn should be used when the peer was created, but never started.
2017-09-12 17:49:22 -07:00
func (p *peer) CloseConn() {
2017-10-03 16:36:01 -07:00
p.conn.Close() // nolint: errcheck
2017-04-07 03:57:03 -07:00
}
2017-04-10 13:04:55 -07:00
// makePersistent marks the peer as persistent.
2017-09-12 17:49:22 -07:00
func (p *peer) makePersistent() {
2017-04-07 03:57:03 -07:00
if !p.outbound {
panic("inbound peers can't be made persistent")
}
p.persistent = true
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
}
// HandshakeTimeout performs a handshake between a given node and the peer.
2015-10-25 18:21:51 -07:00
// NOTE: blocking
2017-09-12 17:49:22 -07: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
2015-10-25 18:21:51 -07:00
var peerNodeInfo = new(NodeInfo)
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
2017-04-07 03:57:03 -07: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
2017-04-07 03:57:03 -07: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-11 08:47:05 -07:00
if p.config.AuthEnc {
2017-04-07 03:57:03 -07:00
// Check that the professed PubKey matches the sconn's.
2017-04-21 15:02:25 -07:00
if !peerNodeInfo.PubKey.Equals(p.PubKey().Wrap()) {
2017-04-07 03:57:03 -07:00
return fmt.Errorf("Ignoring connection with unmatching pubkey: %v vs %v",
peerNodeInfo.PubKey, p.PubKey())
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
2017-04-14 01:43:28 -07:00
peerNodeInfo.RemoteAddr = p.Addr().String()
2017-04-07 03:57:03 -07:00
2017-09-12 17:49:22 -07:00
p.nodeInfo = peerNodeInfo
2017-04-07 03:57:03 -07:00
return nil
}
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 {
2017-04-11 08:47:05 -07:00
if p.config.AuthEnc {
2017-04-07 03:57:03 -07:00
return p.conn.(*SecretConnection).RemotePubKey()
2017-04-06 05:43:45 -07:00
}
if p.NodeInfo() == nil {
2017-04-07 03:57:03 -07: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
return p.PubKey()
2015-10-25 18:21:51 -07:00
}
2017-04-07 03:57:03 -07:00
// OnStart implements BaseService.
2017-09-12 17:49:22 -07:00
func (p *peer) OnStart() error {
2017-09-21 09:38:48 -07:00
if err := p.BaseService.OnStart(); err != nil {
return err
}
err := p.mconn.Start()
2015-10-25 18:21:51 -07:00
return err
}
2017-04-07 03:57:03 -07:00
// OnStop implements BaseService.
2017-09-12 17:49:22 -07:00
func (p *peer) OnStop() {
2015-10-25 18:21:51 -07:00
p.BaseService.OnStop()
p.mconn.Stop()
}
2017-04-07 03:57:03 -07:00
// Connection returns underlying MConnection.
2017-09-12 17:49:22 -07:00
func (p *peer) Connection() *MConnection {
2015-10-25 18:21:51 -07:00
return p.mconn
}
2017-04-07 03:57:03 -07:00
// IsOutbound returns true if the connection is outbound, false otherwise.
2017-09-12 17:49:22 -07:00
func (p *peer) IsOutbound() bool {
2015-10-25 18:21:51 -07:00
return p.outbound
}
2017-04-07 03:57:03 -07:00
// Send msg to the channel identified by chID byte. Returns false if the send
// queue is full after timeout, specified by MConnection.
2017-09-12 17:49:22 -07:00
func (p *peer) Send(chID byte, msg interface{}) bool {
2015-10-25 18:21:51 -07:00
if !p.IsRunning() {
2017-04-07 03:57:03 -07:00
// 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.
2015-10-25 18:21:51 -07:00
return false
}
return p.mconn.Send(chID, msg)
}
2017-04-07 03:57:03 -07:00
// TrySend msg to the channel identified by chID byte. Immediately returns
// false if the send queue is full.
2017-09-12 17:49:22 -07:00
func (p *peer) TrySend(chID byte, msg interface{}) bool {
2015-10-25 18:21:51 -07:00
if !p.IsRunning() {
return false
}
return p.mconn.TrySend(chID, msg)
}
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
}
2017-04-07 03:57:03 -07:00
// Equals reports whenever 2 peers are actually represent the same node.
2017-09-12 17:49:22 -07:00
func (p *peer) Equals(other Peer) bool {
2018-01-01 18:27:38 -08:00
return p.ID() == other.ID()
2015-10-25 18:21:51 -07:00
}
2017-04-07 03:57:03 -07:00
// Get the data for a given key.
2017-09-12 17:49:22 -07:00
func (p *peer) Get(key string) interface{} {
2015-10-25 18:21:51 -07:00
return p.Data.Get(key)
}
2017-04-07 03:57:03 -07:00
2017-09-12 17:49:22 -07:00
// Set sets the data for the given key.
func (p *peer) Set(key string, data interface{}) {
p.Data.Set(key, data)
}
2018-01-01 18:27:38 -08:00
// Key returns the peer's ID - the hex encoded hash of its pubkey.
func (p *peer) ID() ID {
return ID(hex.EncodeToString(p.nodeInfo.PubKey.Address()))
2017-09-12 17:49:22 -07:00
}
// NodeInfo returns a copy of the peer's NodeInfo.
func (p *peer) NodeInfo() *NodeInfo {
if p.nodeInfo == nil {
return nil
}
2017-09-12 17:49:22 -07:00
n := *p.nodeInfo // copy
return &n
}
// Status returns the peer's ConnectionStatus.
func (p *peer) Status() ConnectionStatus {
return p.mconn.Status()
}
2017-04-11 08:47:05 -07: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
}
2017-09-12 17:49:22 -07:00
func createMConnection(conn net.Conn, p *peer, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor,
onPeerError func(Peer, interface{}), config *MConnConfig) *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)
}
2017-04-11 08:47:05 -07:00
return NewMConnectionWithConfig(conn, chDescs, onReceive, onError, config)
2017-04-07 03:57:03 -07:00
}