mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-09 06:33:16 +00:00
types: move NodeInfo from p2p (#6618)
This commit is contained in:
220
types/node_info.go
Normal file
220
types/node_info.go
Normal file
@@ -0,0 +1,220 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/tendermint/tendermint/libs/bytes"
|
||||
tmstrings "github.com/tendermint/tendermint/libs/strings"
|
||||
tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
|
||||
)
|
||||
|
||||
const (
|
||||
maxNodeInfoSize = 10240 // 10KB
|
||||
maxNumChannels = 16 // plenty of room for upgrades, for now
|
||||
)
|
||||
|
||||
// Max size of the NodeInfo struct
|
||||
func MaxNodeInfoSize() int {
|
||||
return maxNodeInfoSize
|
||||
}
|
||||
|
||||
// ProtocolVersion contains the protocol versions for the software.
|
||||
type ProtocolVersion struct {
|
||||
P2P uint64 `json:"p2p"`
|
||||
Block uint64 `json:"block"`
|
||||
App uint64 `json:"app"`
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
// NodeInfo is the basic node information exchanged
|
||||
// between two peers during the Tendermint P2P handshake.
|
||||
type NodeInfo struct {
|
||||
ProtocolVersion ProtocolVersion `json:"protocol_version"`
|
||||
|
||||
// Authenticate
|
||||
NodeID NodeID `json:"id"` // authenticated identifier
|
||||
ListenAddr string `json:"listen_addr"` // accepting incoming
|
||||
|
||||
// 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 bytes.HexBytes `json:"channels"` // channels this node knows about
|
||||
|
||||
// ASCIIText fields
|
||||
Moniker string `json:"moniker"` // arbitrary moniker
|
||||
Other NodeInfoOther `json:"other"` // other application specific data
|
||||
}
|
||||
|
||||
// NodeInfoOther is the misc. applcation specific data
|
||||
type NodeInfoOther struct {
|
||||
TxIndex string `json:"tx_index"`
|
||||
RPCAddress string `json:"rpc_address"`
|
||||
}
|
||||
|
||||
// ID returns the node's peer ID.
|
||||
func (info NodeInfo) ID() NodeID {
|
||||
return info.NodeID
|
||||
}
|
||||
|
||||
// Validate checks the self-reported NodeInfo is safe.
|
||||
// It returns an error if there
|
||||
// are too many Channels, if there are any duplicate Channels,
|
||||
// if the ListenAddr is malformed, or if the ListenAddr is a host name
|
||||
// that can not be resolved to some IP.
|
||||
// TODO: constraints for Moniker/Other? Or is that for the UI ?
|
||||
// JAE: It needs to be done on the client, but to prevent ambiguous
|
||||
// unicode characters, maybe it's worth sanitizing it here.
|
||||
// In the future we might want to validate these, once we have a
|
||||
// name-resolution system up.
|
||||
// International clients could then use punycode (or we could use
|
||||
// url-encoding), and we just need to be careful with how we handle that in our
|
||||
// clients. (e.g. off by default).
|
||||
func (info NodeInfo) Validate() error {
|
||||
|
||||
// ID is already validated.
|
||||
|
||||
// Validate ListenAddr.
|
||||
_, err := NewNetAddressString(info.ID().AddressString(info.ListenAddr))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Network is validated in CompatibleWith.
|
||||
|
||||
// Validate Version
|
||||
if len(info.Version) > 0 &&
|
||||
(!tmstrings.IsASCIIText(info.Version) || tmstrings.ASCIITrim(info.Version) == "") {
|
||||
|
||||
return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got %v", info.Version)
|
||||
}
|
||||
|
||||
// Validate Channels - ensure max and check for duplicates.
|
||||
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{}{}
|
||||
}
|
||||
|
||||
// Validate Moniker.
|
||||
if !tmstrings.IsASCIIText(info.Moniker) || tmstrings.ASCIITrim(info.Moniker) == "" {
|
||||
return fmt.Errorf("info.Moniker must be valid non-empty ASCII text without tabs, but got %v", info.Moniker)
|
||||
}
|
||||
|
||||
// Validate Other.
|
||||
other := info.Other
|
||||
txIndex := other.TxIndex
|
||||
switch txIndex {
|
||||
case "", "on", "off":
|
||||
default:
|
||||
return fmt.Errorf("info.Other.TxIndex should be either 'on', 'off', or empty string, got '%v'", txIndex)
|
||||
}
|
||||
// XXX: Should we be more strict about address formats?
|
||||
rpcAddr := other.RPCAddress
|
||||
if len(rpcAddr) > 0 && (!tmstrings.IsASCIIText(rpcAddr) || tmstrings.ASCIITrim(rpcAddr) == "") {
|
||||
return fmt.Errorf("info.Other.RPCAddress=%v must be valid ASCII text without tabs", rpcAddr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompatibleWith checks if two NodeInfo are compatible with each other.
|
||||
// CONTRACT: two nodes are compatible if the Block version and network match
|
||||
// and they have at least one channel in common.
|
||||
func (info NodeInfo) CompatibleWith(other NodeInfo) error {
|
||||
if info.ProtocolVersion.Block != other.ProtocolVersion.Block {
|
||||
return fmt.Errorf("peer is on a different Block version. Got %v, expected %v",
|
||||
other.ProtocolVersion.Block, info.ProtocolVersion.Block)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
OUTER_LOOP:
|
||||
for _, ch1 := range info.Channels {
|
||||
for _, ch2 := range other.Channels {
|
||||
if ch1 == ch2 {
|
||||
found = true
|
||||
break OUTER_LOOP // only need one
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("peer has no common channels. Our channels: %v ; Peer channels: %v", info.Channels, other.Channels)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (info NodeInfo) NetAddress() (*NetAddress, error) {
|
||||
idAddr := info.ID().AddressString(info.ListenAddr)
|
||||
return NewNetAddressString(idAddr)
|
||||
}
|
||||
|
||||
func (info NodeInfo) ToProto() *tmp2p.NodeInfo {
|
||||
|
||||
dni := new(tmp2p.NodeInfo)
|
||||
dni.ProtocolVersion = tmp2p.ProtocolVersion{
|
||||
P2P: info.ProtocolVersion.P2P,
|
||||
Block: info.ProtocolVersion.Block,
|
||||
App: info.ProtocolVersion.App,
|
||||
}
|
||||
|
||||
dni.NodeID = string(info.NodeID)
|
||||
dni.ListenAddr = info.ListenAddr
|
||||
dni.Network = info.Network
|
||||
dni.Version = info.Version
|
||||
dni.Channels = info.Channels
|
||||
dni.Moniker = info.Moniker
|
||||
dni.Other = tmp2p.NodeInfoOther{
|
||||
TxIndex: info.Other.TxIndex,
|
||||
RPCAddress: info.Other.RPCAddress,
|
||||
}
|
||||
|
||||
return dni
|
||||
}
|
||||
|
||||
func NodeInfoFromProto(pb *tmp2p.NodeInfo) (NodeInfo, error) {
|
||||
if pb == nil {
|
||||
return NodeInfo{}, errors.New("nil node info")
|
||||
}
|
||||
dni := NodeInfo{
|
||||
ProtocolVersion: ProtocolVersion{
|
||||
P2P: pb.ProtocolVersion.P2P,
|
||||
Block: pb.ProtocolVersion.Block,
|
||||
App: pb.ProtocolVersion.App,
|
||||
},
|
||||
NodeID: NodeID(pb.NodeID),
|
||||
ListenAddr: pb.ListenAddr,
|
||||
Network: pb.Network,
|
||||
Version: pb.Version,
|
||||
Channels: pb.Channels,
|
||||
Moniker: pb.Moniker,
|
||||
Other: NodeInfoOther{
|
||||
TxIndex: pb.Other.TxIndex,
|
||||
RPCAddress: pb.Other.RPCAddress,
|
||||
},
|
||||
}
|
||||
|
||||
return dni, nil
|
||||
}
|
||||
161
types/node_info_test.go
Normal file
161
types/node_info_test.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
tmnet "github.com/tendermint/tendermint/libs/net"
|
||||
"github.com/tendermint/tendermint/version"
|
||||
)
|
||||
|
||||
const testCh = 0x01
|
||||
|
||||
func TestNodeInfoValidate(t *testing.T) {
|
||||
|
||||
// empty fails
|
||||
ni := NodeInfo{}
|
||||
assert.Error(t, ni.Validate())
|
||||
|
||||
channels := make([]byte, maxNumChannels)
|
||||
for i := 0; i < maxNumChannels; i++ {
|
||||
channels[i] = byte(i)
|
||||
}
|
||||
dupChannels := make([]byte, 5)
|
||||
copy(dupChannels, channels[:5])
|
||||
dupChannels = append(dupChannels, testCh)
|
||||
|
||||
nonASCII := "¢§µ"
|
||||
emptyTab := "\t"
|
||||
emptySpace := " "
|
||||
|
||||
testCases := []struct {
|
||||
testName string
|
||||
malleateNodeInfo func(*NodeInfo)
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
"Too Many Channels",
|
||||
func(ni *NodeInfo) { ni.Channels = append(channels, byte(maxNumChannels)) }, // nolint: gocritic
|
||||
true,
|
||||
},
|
||||
{"Duplicate Channel", func(ni *NodeInfo) { ni.Channels = dupChannels }, true},
|
||||
{"Good Channels", func(ni *NodeInfo) { ni.Channels = ni.Channels[:5] }, false},
|
||||
|
||||
{"Invalid NetAddress", func(ni *NodeInfo) { ni.ListenAddr = "not-an-address" }, true},
|
||||
{"Good NetAddress", func(ni *NodeInfo) { ni.ListenAddr = "0.0.0.0:26656" }, false},
|
||||
|
||||
{"Non-ASCII Version", func(ni *NodeInfo) { ni.Version = nonASCII }, true},
|
||||
{"Empty tab Version", func(ni *NodeInfo) { ni.Version = emptyTab }, true},
|
||||
{"Empty space Version", func(ni *NodeInfo) { ni.Version = emptySpace }, true},
|
||||
{"Empty Version", func(ni *NodeInfo) { ni.Version = "" }, false},
|
||||
|
||||
{"Non-ASCII Moniker", func(ni *NodeInfo) { ni.Moniker = nonASCII }, true},
|
||||
{"Empty tab Moniker", func(ni *NodeInfo) { ni.Moniker = emptyTab }, true},
|
||||
{"Empty space Moniker", func(ni *NodeInfo) { ni.Moniker = emptySpace }, true},
|
||||
{"Empty Moniker", func(ni *NodeInfo) { ni.Moniker = "" }, true},
|
||||
{"Good Moniker", func(ni *NodeInfo) { ni.Moniker = "hey its me" }, false},
|
||||
|
||||
{"Non-ASCII TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = nonASCII }, true},
|
||||
{"Empty tab TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = emptyTab }, true},
|
||||
{"Empty space TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = emptySpace }, true},
|
||||
{"Empty TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = "" }, false},
|
||||
{"Off TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = "off" }, false},
|
||||
|
||||
{"Non-ASCII RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = nonASCII }, true},
|
||||
{"Empty tab RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = emptyTab }, true},
|
||||
{"Empty space RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = emptySpace }, true},
|
||||
{"Empty RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = "" }, false},
|
||||
{"Good RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = "0.0.0.0:26657" }, false},
|
||||
}
|
||||
|
||||
nodeKeyID := testNodeID()
|
||||
name := "testing"
|
||||
|
||||
// test case passes
|
||||
ni = testNodeInfo(nodeKeyID, name)
|
||||
ni.Channels = channels
|
||||
assert.NoError(t, ni.Validate())
|
||||
|
||||
for _, tc := range testCases {
|
||||
ni := testNodeInfo(nodeKeyID, name)
|
||||
ni.Channels = channels
|
||||
tc.malleateNodeInfo(&ni)
|
||||
err := ni.Validate()
|
||||
if tc.expectErr {
|
||||
assert.Error(t, err, tc.testName)
|
||||
} else {
|
||||
assert.NoError(t, err, tc.testName)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testNodeID() NodeID {
|
||||
return NodeIDFromPubKey(ed25519.GenPrivKey().PubKey())
|
||||
}
|
||||
|
||||
func testNodeInfo(id NodeID, name string) NodeInfo {
|
||||
return testNodeInfoWithNetwork(id, name, "testing")
|
||||
}
|
||||
|
||||
func testNodeInfoWithNetwork(id NodeID, name, network string) NodeInfo {
|
||||
return NodeInfo{
|
||||
ProtocolVersion: ProtocolVersion{
|
||||
P2P: version.P2PProtocol,
|
||||
Block: version.BlockProtocol,
|
||||
App: 0,
|
||||
},
|
||||
NodeID: id,
|
||||
ListenAddr: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
|
||||
Network: network,
|
||||
Version: "1.2.3-rc0-deadbeef",
|
||||
Channels: []byte{testCh},
|
||||
Moniker: name,
|
||||
Other: NodeInfoOther{
|
||||
TxIndex: "on",
|
||||
RPCAddress: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func getFreePort() int {
|
||||
port, err := tmnet.GetFreePort()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return port
|
||||
}
|
||||
|
||||
func TestNodeInfoCompatible(t *testing.T) {
|
||||
nodeKey1ID := testNodeID()
|
||||
nodeKey2ID := testNodeID()
|
||||
name := "testing"
|
||||
|
||||
var newTestChannel byte = 0x2
|
||||
|
||||
// test NodeInfo is compatible
|
||||
ni1 := testNodeInfo(nodeKey1ID, name)
|
||||
ni2 := testNodeInfo(nodeKey2ID, name)
|
||||
assert.NoError(t, ni1.CompatibleWith(ni2))
|
||||
|
||||
// add another channel; still compatible
|
||||
ni2.Channels = []byte{newTestChannel, testCh}
|
||||
assert.NoError(t, ni1.CompatibleWith(ni2))
|
||||
|
||||
testCases := []struct {
|
||||
testName string
|
||||
malleateNodeInfo func(*NodeInfo)
|
||||
}{
|
||||
{"Wrong block version", func(ni *NodeInfo) { ni.ProtocolVersion.Block++ }},
|
||||
{"Wrong network", func(ni *NodeInfo) { ni.Network += "-wrong" }},
|
||||
{"No common channels", func(ni *NodeInfo) { ni.Channels = []byte{newTestChannel} }},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
ni := testNodeInfo(nodeKey2ID, name)
|
||||
tc.malleateNodeInfo(&ni)
|
||||
assert.Error(t, ni1.CompatibleWith(ni))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user