2018-01-21 00:33:53 -05:00
|
|
|
package p2p
|
2015-10-25 18:21:51 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-04-26 23:40:29 -04:00
|
|
|
|
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2015-10-25 18:21:51 -07:00
|
|
|
)
|
|
|
|
|
2018-01-21 11:45:04 -05:00
|
|
|
const (
|
|
|
|
maxNodeInfoSize = 10240 // 10Kb
|
|
|
|
maxNumChannels = 16 // plenty of room for upgrades, for now
|
|
|
|
)
|
2015-11-10 12:29:43 -08:00
|
|
|
|
2018-04-26 23:40:29 -04:00
|
|
|
// Max size of the NodeInfo struct
|
2018-01-20 21:12:04 -05:00
|
|
|
func MaxNodeInfoSize() int {
|
|
|
|
return maxNodeInfoSize
|
|
|
|
}
|
|
|
|
|
2018-01-13 16:14:28 -05:00
|
|
|
// NodeInfo is the basic node information exchanged
|
2018-01-13 23:48:41 -05:00
|
|
|
// between two peers during the Tendermint P2P handshake.
|
2015-10-25 18:21:51 -07:00
|
|
|
type NodeInfo struct {
|
2018-01-13 23:48:41 -05:00
|
|
|
// Authenticate
|
2018-04-11 11:11:11 +03:00
|
|
|
ID ID `json:"id"` // authenticated identifier
|
|
|
|
ListenAddr string `json:"listen_addr"` // accepting incoming
|
2018-01-13 23:48:41 -05:00
|
|
|
|
2018-04-26 23:40:29 -04:00
|
|
|
// Check compatibility.
|
|
|
|
// Channels are HexBytes so easier to read as JSON
|
|
|
|
Network string `json:"network"` // network/chain ID
|
|
|
|
Version string `json:"version"` // major.minor.revision
|
|
|
|
Channels cmn.HexBytes `json:"channels"` // channels this node knows about
|
2018-01-13 23:48:41 -05:00
|
|
|
|
|
|
|
// Sanitize
|
|
|
|
Moniker string `json:"moniker"` // arbitrary moniker
|
|
|
|
Other []string `json:"other"` // other application specific data
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks the self-reported NodeInfo is safe.
|
p2p: introduce peerConn to simplify peer creation (#1226)
* expose AuthEnc in the P2P config
if AuthEnc is true, dialed peers must have a node ID in the address and
it must match the persistent pubkey from the secret handshake.
Refs #1157
* fixes after my own review
* fix docs
* fix build failure
```
p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal
```
* p2p: introduce peerConn to simplify peer creation
* Introduce `peerConn` containing the known fields of `peer`
* `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked
* Eliminates some mutable variables and makes the code flow better
* Simplifies the `newXxxPeer` funcs
* Use ID instead of PubKey where possible.
* SetPubKeyFilter -> SetIDFilter
* nodeInfo.Validate takes ID
* remove peer.PubKey()
* persistent node ids
* fixes from review
* test: use ip_plus_id.sh more
* fix invalid memory panic during fast_sync test
```
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e]
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]:
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]:
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0
xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0
xc420933e00, 0xc423f48801, 0x28, 0x2)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x
0, 0x0)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7
```
2018-02-27 06:54:40 -05:00
|
|
|
// It returns an error if there
|
|
|
|
// are too many Channels or any duplicate Channels.
|
2018-01-13 23:48:41 -05:00
|
|
|
// TODO: constraints for Moniker/Other? Or is that for the UI ?
|
p2p: introduce peerConn to simplify peer creation (#1226)
* expose AuthEnc in the P2P config
if AuthEnc is true, dialed peers must have a node ID in the address and
it must match the persistent pubkey from the secret handshake.
Refs #1157
* fixes after my own review
* fix docs
* fix build failure
```
p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal
```
* p2p: introduce peerConn to simplify peer creation
* Introduce `peerConn` containing the known fields of `peer`
* `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked
* Eliminates some mutable variables and makes the code flow better
* Simplifies the `newXxxPeer` funcs
* Use ID instead of PubKey where possible.
* SetPubKeyFilter -> SetIDFilter
* nodeInfo.Validate takes ID
* remove peer.PubKey()
* persistent node ids
* fixes from review
* test: use ip_plus_id.sh more
* fix invalid memory panic during fast_sync test
```
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e]
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]:
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]:
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0
xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0
xc420933e00, 0xc423f48801, 0x28, 0x2)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x
0, 0x0)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00)
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError
2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7
```
2018-02-27 06:54:40 -05:00
|
|
|
func (info NodeInfo) Validate() error {
|
2018-01-21 11:45:04 -05:00
|
|
|
if len(info.Channels) > maxNumChannels {
|
|
|
|
return fmt.Errorf("info.Channels is too long (%v). Max is %v", len(info.Channels), maxNumChannels)
|
|
|
|
}
|
|
|
|
|
|
|
|
channels := make(map[byte]struct{})
|
|
|
|
for _, ch := range info.Channels {
|
|
|
|
_, ok := channels[ch]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("info.Channels contains duplicate channel id %v", ch)
|
|
|
|
}
|
|
|
|
channels[ch] = struct{}{}
|
|
|
|
}
|
2018-01-13 23:48:41 -05:00
|
|
|
return nil
|
2015-10-25 18:21:51 -07:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:26:19 -05:00
|
|
|
// CompatibleWith checks if two NodeInfo are compatible with eachother.
|
2018-01-21 11:45:04 -05:00
|
|
|
// CONTRACT: two nodes are compatible if the major/minor versions match and network match
|
|
|
|
// and they have at least one channel in common.
|
2018-01-14 00:10:29 -05:00
|
|
|
func (info NodeInfo) CompatibleWith(other NodeInfo) error {
|
2015-10-25 18:21:51 -07:00
|
|
|
iMajor, iMinor, _, iErr := splitVersion(info.Version)
|
|
|
|
oMajor, oMinor, _, oErr := splitVersion(other.Version)
|
|
|
|
|
|
|
|
// if our own version number is not formatted right, we messed up
|
|
|
|
if iErr != nil {
|
|
|
|
return iErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// version number must be formatted correctly ("x.x.x")
|
|
|
|
if oErr != nil {
|
|
|
|
return oErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// major version must match
|
|
|
|
if iMajor != oMajor {
|
|
|
|
return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// minor version must match
|
|
|
|
if iMinor != oMinor {
|
|
|
|
return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodes must be on the same network
|
|
|
|
if info.Network != other.Network {
|
|
|
|
return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:45:04 -05:00
|
|
|
// if we have no channels, we're just testing
|
|
|
|
if len(info.Channels) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// for each of our channels, check if they have it
|
|
|
|
found := false
|
2018-01-24 14:27:37 -05:00
|
|
|
OUTER_LOOP:
|
2018-01-21 11:45:04 -05:00
|
|
|
for _, ch1 := range info.Channels {
|
|
|
|
for _, ch2 := range other.Channels {
|
|
|
|
if ch1 == ch2 {
|
|
|
|
found = true
|
2018-01-24 14:27:37 -05:00
|
|
|
break OUTER_LOOP // only need one
|
2018-01-21 11:45:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Peer has no common channels. Our channels: %v ; Peer channels: %v", info.Channels, other.Channels)
|
|
|
|
}
|
2015-10-25 18:21:51 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:26:19 -05:00
|
|
|
// NetAddress returns a NetAddress derived from the NodeInfo -
|
|
|
|
// it includes the authenticated peer ID and the self-reported
|
|
|
|
// ListenAddr. Note that the ListenAddr is not authenticated and
|
|
|
|
// may not match that address actually dialed if its an outbound peer.
|
2018-01-14 00:10:29 -05:00
|
|
|
func (info NodeInfo) NetAddress() *NetAddress {
|
2018-04-11 11:11:11 +03:00
|
|
|
netAddr, err := NewNetAddressString(IDAddressString(info.ID, info.ListenAddr))
|
2018-01-13 16:14:28 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err) // everything should be well formed by now
|
|
|
|
}
|
|
|
|
return netAddr
|
|
|
|
}
|
|
|
|
|
2017-05-12 23:07:53 +02:00
|
|
|
func (info NodeInfo) String() string {
|
2018-04-11 11:11:11 +03:00
|
|
|
return fmt.Sprintf("NodeInfo{id: %v, moniker: %v, network: %v [listen %v], version: %v (%v)}",
|
|
|
|
info.ID, info.Moniker, info.Network, info.ListenAddr, info.Version, info.Other)
|
2017-05-12 23:07:53 +02:00
|
|
|
}
|
|
|
|
|
2015-10-25 18:21:51 -07:00
|
|
|
func splitVersion(version string) (string, string, string, error) {
|
|
|
|
spl := strings.Split(version, ".")
|
|
|
|
if len(spl) != 3 {
|
|
|
|
return "", "", "", fmt.Errorf("Invalid version format %v", version)
|
|
|
|
}
|
|
|
|
return spl[0], spl[1], spl[2], nil
|
|
|
|
}
|