p2p: remove NodeInfo interface and rename DefaultNodeInfo struct (#5799)

The `NodeInfo` interface does not appear to serve any purpose at all, so I removed it and renamed the `DefaultNodeInfo` struct to `NodeInfo` (including the Protobuf representations). Let me know if this is actually needed for anything.

Only the Protobuf rename is listed in the changelog, since we do not officially support API stability of the `p2p` package (according to `README.md`). The on-wire protocol remains compatible.
This commit is contained in:
Erik Grinaker
2020-12-15 18:54:25 +00:00
committed by GitHub
parent bcfc889f25
commit e198edf20e
19 changed files with 203 additions and 251 deletions
+20 -52
View File
@@ -3,7 +3,6 @@ package p2p
import (
"errors"
"fmt"
"reflect"
"github.com/tendermint/tendermint/libs/bytes"
tmstrings "github.com/tendermint/tendermint/libs/strings"
@@ -21,29 +20,6 @@ func MaxNodeInfoSize() int {
return maxNodeInfoSize
}
//-------------------------------------------------------------
// NodeInfo exposes basic info of a node
// and determines if we're compatible.
type NodeInfo interface {
ID() ID
nodeInfoAddress
nodeInfoTransport
}
type nodeInfoAddress interface {
NetAddress() (*NetAddress, error)
}
// nodeInfoTransport validates a nodeInfo and checks
// our compatibility with it. It's for use in the handshake.
type nodeInfoTransport interface {
Validate() error
CompatibleWith(other NodeInfo) error
}
//-------------------------------------------------------------
// ProtocolVersion contains the protocol versions for the software.
type ProtocolVersion struct {
P2P uint64 `json:"p2p"`
@@ -70,12 +46,9 @@ func NewProtocolVersion(p2p, block, app uint64) ProtocolVersion {
//-------------------------------------------------------------
// Assert DefaultNodeInfo satisfies NodeInfo
var _ NodeInfo = DefaultNodeInfo{}
// DefaultNodeInfo is the basic node information exchanged
// NodeInfo is the basic node information exchanged
// between two peers during the Tendermint P2P handshake.
type DefaultNodeInfo struct {
type NodeInfo struct {
ProtocolVersion ProtocolVersion `json:"protocol_version"`
// Authenticate
@@ -90,22 +63,22 @@ type DefaultNodeInfo struct {
Channels bytes.HexBytes `json:"channels"` // channels this node knows about
// ASCIIText fields
Moniker string `json:"moniker"` // arbitrary moniker
Other DefaultNodeInfoOther `json:"other"` // other application specific data
Moniker string `json:"moniker"` // arbitrary moniker
Other NodeInfoOther `json:"other"` // other application specific data
}
// DefaultNodeInfoOther is the misc. applcation specific data
type DefaultNodeInfoOther struct {
// 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 DefaultNodeInfo) ID() ID {
func (info NodeInfo) ID() ID {
return info.DefaultNodeID
}
// Validate checks the self-reported DefaultNodeInfo is safe.
// 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
@@ -118,7 +91,7 @@ func (info DefaultNodeInfo) ID() ID {
// 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 DefaultNodeInfo) Validate() error {
func (info NodeInfo) Validate() error {
// ID is already validated.
@@ -172,15 +145,10 @@ func (info DefaultNodeInfo) Validate() error {
return nil
}
// CompatibleWith checks if two DefaultNodeInfo are compatible with eachother.
// 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 DefaultNodeInfo) CompatibleWith(otherInfo NodeInfo) error {
other, ok := otherInfo.(DefaultNodeInfo)
if !ok {
return fmt.Errorf("wrong NodeInfo type. Expected DefaultNodeInfo, got %v", reflect.TypeOf(otherInfo))
}
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)
@@ -213,18 +181,18 @@ OUTER_LOOP:
return nil
}
// NetAddress returns a NetAddress derived from the DefaultNodeInfo -
// 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 DefaultNodeInfo) NetAddress() (*NetAddress, error) {
func (info NodeInfo) NetAddress() (*NetAddress, error) {
idAddr := IDAddressString(info.ID(), info.ListenAddr)
return NewNetAddressString(idAddr)
}
func (info DefaultNodeInfo) ToProto() *tmp2p.DefaultNodeInfo {
func (info NodeInfo) ToProto() *tmp2p.NodeInfo {
dni := new(tmp2p.DefaultNodeInfo)
dni := new(tmp2p.NodeInfo)
dni.ProtocolVersion = tmp2p.ProtocolVersion{
P2P: info.ProtocolVersion.P2P,
Block: info.ProtocolVersion.Block,
@@ -237,7 +205,7 @@ func (info DefaultNodeInfo) ToProto() *tmp2p.DefaultNodeInfo {
dni.Version = info.Version
dni.Channels = info.Channels
dni.Moniker = info.Moniker
dni.Other = tmp2p.DefaultNodeInfoOther{
dni.Other = tmp2p.NodeInfoOther{
TxIndex: info.Other.TxIndex,
RPCAddress: info.Other.RPCAddress,
}
@@ -245,11 +213,11 @@ func (info DefaultNodeInfo) ToProto() *tmp2p.DefaultNodeInfo {
return dni
}
func DefaultNodeInfoFromProto(pb *tmp2p.DefaultNodeInfo) (DefaultNodeInfo, error) {
func NodeInfoFromProto(pb *tmp2p.NodeInfo) (NodeInfo, error) {
if pb == nil {
return DefaultNodeInfo{}, errors.New("nil node info")
return NodeInfo{}, errors.New("nil node info")
}
dni := DefaultNodeInfo{
dni := NodeInfo{
ProtocolVersion: ProtocolVersion{
P2P: pb.ProtocolVersion.P2P,
Block: pb.ProtocolVersion.Block,
@@ -261,7 +229,7 @@ func DefaultNodeInfoFromProto(pb *tmp2p.DefaultNodeInfo) (DefaultNodeInfo, error
Version: pb.Version,
Channels: pb.Channels,
Moniker: pb.Moniker,
Other: DefaultNodeInfoOther{
Other: NodeInfoOther{
TxIndex: pb.Other.TxIndex,
RPCAddress: pb.Other.RPCAddress,
},