Skip to content

Commit

Permalink
refactor(p2p/nodeinfo)!: remove NodeInfo suffix (cometbft#4305)
Browse files Browse the repository at this point in the history
Refs cometbft#4302

**Important: no logic has been changed; just moving stuff and updating
interfaces.**
  • Loading branch information
melekes authored Oct 24, 2024
1 parent dd9b65b commit b1de30a
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 96 deletions.
10 changes: 5 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ func CustomReactors(reactors map[string]p2p.Reactor) Option {
// NOTE: This is a bit messy now with the type casting but is
// cleaned up in the following version when NodeInfo is changed from
// and interface to a concrete type
if ni, ok := n.nodeInfo.(ni.DefaultNodeInfo); ok {
if ni, ok := n.nodeInfo.(ni.Default); ok {
for _, chDesc := range reactor.StreamDescriptors() {
if !ni.HasChannel(chDesc.StreamID()) {
ni.Channels = append(ni.Channels, chDesc.StreamID())
}
}
n.nodeInfo = ni
} else {
n.Logger.Error("Node info is not of type DefaultNodeInfo. Custom reactor channels can not be added.")
n.Logger.Error("Node info is not of type ni.Default. Custom reactor channels can not be added.")
}
}
}
Expand Down Expand Up @@ -1026,13 +1026,13 @@ func makeNodeInfo(
txIndexer txindex.TxIndexer,
genDoc *types.GenesisDoc,
state sm.State,
) (ni.DefaultNodeInfo, error) {
) (ni.Default, error) {
txIndexerStatus := "on"
if _, ok := txIndexer.(*null.TxIndex); ok {
txIndexerStatus = "off"
}

nodeInfo := ni.DefaultNodeInfo{
nodeInfo := ni.Default{
ProtocolVersion: ni.NewProtocolVersion(
version.P2PProtocol, // global
state.Version.Consensus.Block,
Expand All @@ -1049,7 +1049,7 @@ func makeNodeInfo(
statesync.SnapshotChannel, statesync.ChunkChannel,
},
Moniker: config.Moniker,
Other: ni.DefaultNodeInfoOther{
Other: ni.DefaultOther{
TxIndex: txIndexerStatus,
RPCAddress: config.RPC.ListenAddress,
},
Expand Down
4 changes: 2 additions & 2 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestNodeSetAppVersion(t *testing.T) {
assert.Equal(t, state.Version.Consensus.App, appVersion)

// check version is set in node info
assert.Equal(t, n.nodeInfo.(ni.DefaultNodeInfo).ProtocolVersion.App, appVersion)
assert.Equal(t, n.nodeInfo.(ni.Default).ProtocolVersion.App, appVersion)
}

func TestPprofServer(t *testing.T) {
Expand Down Expand Up @@ -513,7 +513,7 @@ func TestNodeNewNodeCustomReactors(t *testing.T) {
assert.True(t, customBlocksyncReactor.IsRunning())
assert.Equal(t, customBlocksyncReactor, n.Switch().Reactor("BLOCKSYNC"))

channels := n.NodeInfo().(ni.DefaultNodeInfo).Channels
channels := n.NodeInfo().(ni.Default).Channels
assert.Contains(t, channels, mempl.MempoolChannel)
assert.Contains(t, channels, cr.Channels[0].StreamID())
}
Expand Down
6 changes: 3 additions & 3 deletions p2p/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ func exchangeNodeInfo(ourNodeInfo ni.NodeInfo, c net.Conn, timeout time.Duration
)

go func(errc chan<- error, c net.Conn) {
ourNodeInfoProto := ourNodeInfo.(ni.DefaultNodeInfo).ToProto()
ourNodeInfoProto := ourNodeInfo.(ni.Default).ToProto()
_, err := protoio.NewDelimitedWriter(c).WriteMsg(ourNodeInfoProto)
errc <- err
}(errc, c)
go func(errc chan<- error, c net.Conn) {
protoReader := protoio.NewDelimitedReader(c, ni.MaxNodeInfoSize())
protoReader := protoio.NewDelimitedReader(c, ni.MaxSize())
_, err := protoReader.ReadMsg(&pbpeerNodeInfo)
errc <- err
}(errc, c)
Expand All @@ -183,7 +183,7 @@ func exchangeNodeInfo(ourNodeInfo ni.NodeInfo, c net.Conn, timeout time.Duration
}
}

peerNodeInfo, err = ni.DefaultNodeInfoFromToProto(&pbpeerNodeInfo)
peerNodeInfo, err = ni.DefaultFromToProto(&pbpeerNodeInfo)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/mock/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (*Peer) HasChannel(_ byte) bool { return true }
func (*Peer) TrySend(_ p2p.Envelope) bool { return true }
func (*Peer) Send(_ p2p.Envelope) bool { return true }
func (mp *Peer) NodeInfo() ni.NodeInfo {
return ni.DefaultNodeInfo{
return ni.Default{
DefaultNodeID: mp.addr.ID,
ListenAddr: mp.addr.DialString(),
}
Expand Down
50 changes: 25 additions & 25 deletions p2p/nodeinfo/node_info.go → p2p/nodeinfo/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
maxNumChannels = 16 // plenty of room for upgrades, for now
)

// Max size of the NodeInfo struct.
func MaxNodeInfoSize() int {
// MaxSize returns the maximum size of the NodeInfo struct.
func MaxSize() int {
return maxNodeInfoSize
}

Expand Down Expand Up @@ -53,12 +53,12 @@ func NewProtocolVersion(p2p, block, app uint64) ProtocolVersion {

// -------------------------------------------------------------

// Assert DefaultNodeInfo satisfies NodeInfo.
var _ NodeInfo = DefaultNodeInfo{}
// Assert Default satisfies NodeInfo.
var _ NodeInfo = Default{}

// DefaultNodeInfo is the basic node information exchanged
// Default is the basic node information exchanged
// between two peers during the CometBFT P2P handshake.
type DefaultNodeInfo struct {
type Default struct {
ProtocolVersion ProtocolVersion `json:"protocol_version"`

// Authenticate
Expand All @@ -73,22 +73,22 @@ type DefaultNodeInfo struct {
Channels cmtbytes.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 DefaultOther `json:"other"` // other application specific data
}

// DefaultNodeInfoOther is the misc. application specific data.
type DefaultNodeInfoOther struct {
// DefaultOther is the misc. application specific data.
type DefaultOther struct {
TxIndex string `json:"tx_index"`
RPCAddress string `json:"rpc_address"`
}

// ID returns the node's peer ID.
func (info DefaultNodeInfo) ID() nodekey.ID {
func (info Default) ID() nodekey.ID {
return info.DefaultNodeID
}

// Validate checks the self-reported DefaultNodeInfo is safe.
// Validate checks the self-reported Default 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
Expand All @@ -101,7 +101,7 @@ func (info DefaultNodeInfo) ID() nodekey.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 Default) Validate() error {
// ID is already validated.

// Validate ListenAddr.
Expand Down Expand Up @@ -154,15 +154,15 @@ func (info DefaultNodeInfo) Validate() error {
return nil
}

// CompatibleWith checks if two DefaultNodeInfo are compatible with each other.
// CompatibleWith checks if two Default 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)
func (info Default) CompatibleWith(otherInfo NodeInfo) error {
other, ok := otherInfo.(Default)
if !ok {
return ErrInvalidNodeInfoType{
Type: reflect.TypeOf(otherInfo).String(),
Expected: fmt.Sprintf("%T", DefaultNodeInfo{}),
Expected: fmt.Sprintf("%T", Default{}),
}
}

Expand Down Expand Up @@ -206,20 +206,20 @@ OUTER_LOOP:
return nil
}

// NetAddr returns a NetAddr derived from the DefaultNodeInfo -
// NetAddr returns a NetAddr derived from the Default -
// 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) NetAddr() (*na.NetAddr, error) {
func (info Default) NetAddr() (*na.NetAddr, error) {
idAddr := na.IDAddrString(info.ID(), info.ListenAddr)
return na.NewFromString(idAddr)
}

func (info DefaultNodeInfo) HasChannel(chID byte) bool {
func (info Default) HasChannel(chID byte) bool {
return bytes.Contains(info.Channels, []byte{chID})
}

func (info DefaultNodeInfo) ToProto() *tmp2p.DefaultNodeInfo {
func (info Default) ToProto() *tmp2p.DefaultNodeInfo {
dni := new(tmp2p.DefaultNodeInfo)
dni.ProtocolVersion = tmp2p.ProtocolVersion{
P2P: info.ProtocolVersion.P2P,
Expand All @@ -241,12 +241,12 @@ func (info DefaultNodeInfo) ToProto() *tmp2p.DefaultNodeInfo {
return dni
}

func DefaultNodeInfoFromToProto(pb *tmp2p.DefaultNodeInfo) (DefaultNodeInfo, error) {
func DefaultFromToProto(pb *tmp2p.DefaultNodeInfo) (Default, error) {
if pb == nil {
return DefaultNodeInfo{}, ErrNoNodeInfo
return Default{}, ErrNoNodeInfo
}

dni := DefaultNodeInfo{
dni := Default{
ProtocolVersion: ProtocolVersion{
P2P: pb.ProtocolVersion.P2P,
Block: pb.ProtocolVersion.Block,
Expand All @@ -258,7 +258,7 @@ func DefaultNodeInfoFromToProto(pb *tmp2p.DefaultNodeInfo) (DefaultNodeInfo, err
Version: pb.Version,
Channels: pb.Channels,
Moniker: pb.Moniker,
Other: DefaultNodeInfoOther{
Other: DefaultOther{
TxIndex: pb.Other.TxIndex,
RPCAddress: pb.Other.RPCAddress,
},
Expand Down
84 changes: 42 additions & 42 deletions p2p/nodeinfo/node_info_test.go → p2p/nodeinfo/nodeinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func (mockNodeInfo) CompatibleWith(NodeInfo) error { retur
func (mockNodeInfo) Handshake(net.Conn, time.Duration) (NodeInfo, error) { return nil, nil }

func testNodeInfo(id nodekey.ID) NodeInfo {
return DefaultNodeInfo{
return Default{
ProtocolVersion: NewProtocolVersion(0, 0, 0),
DefaultNodeID: id,
ListenAddr: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
Network: "testing",
Version: "1.2.3-rc0-deadbeef",
Channels: []byte{testCh},
Moniker: "testing",
Other: DefaultNodeInfoOther{
Other: DefaultOther{
TxIndex: "on",
RPCAddress: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
},
Expand All @@ -53,7 +53,7 @@ func getFreePort() int {

func TestNodeInfoValidate(t *testing.T) {
// empty fails
ni := DefaultNodeInfo{}
ni := Default{}
require.Error(t, ni.Validate())

channels := make([]byte, maxNumChannels)
Expand All @@ -70,53 +70,53 @@ func TestNodeInfoValidate(t *testing.T) {

testCases := []struct {
testName string
malleateNodeInfo func(*DefaultNodeInfo)
malleateNodeInfo func(*Default)
expectErr bool
}{
{
"Too Many Channels",
func(ni *DefaultNodeInfo) { ni.Channels = append(channels, byte(maxNumChannels)) }, //nolint: makezero
func(ni *Default) { ni.Channels = append(channels, byte(maxNumChannels)) }, //nolint: makezero
true,
},
{"Duplicate Channel", func(ni *DefaultNodeInfo) { ni.Channels = dupChannels }, true},
{"Good Channels", func(ni *DefaultNodeInfo) { ni.Channels = ni.Channels[:5] }, false},

{"Invalid NetAddr", func(ni *DefaultNodeInfo) { ni.ListenAddr = "not-an-address" }, true},
{"Good NetAddr", func(ni *DefaultNodeInfo) { ni.ListenAddr = "0.0.0.0:26656" }, false},

{"Non-ASCII Version", func(ni *DefaultNodeInfo) { ni.Version = nonASCII }, true},
{"Empty tab Version", func(ni *DefaultNodeInfo) { ni.Version = emptyTab }, true},
{"Empty space Version", func(ni *DefaultNodeInfo) { ni.Version = emptySpace }, true},
{"Empty Version", func(ni *DefaultNodeInfo) { ni.Version = "" }, false},

{"Non-ASCII Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = nonASCII }, true},
{"Empty tab Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptyTab }, true},
{"Empty space Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptySpace }, true},
{"Empty Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "" }, true},
{"Good Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "hey its me" }, false},

{"Non-ASCII TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = nonASCII }, true},
{"Empty tab TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptyTab }, true},
{"Empty space TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptySpace }, true},
{"Empty TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "" }, false},
{"Off TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "off" }, false},

{"Non-ASCII RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = nonASCII }, true},
{"Empty tab RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptyTab }, true},
{"Empty space RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptySpace }, true},
{"Empty RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "" }, false},
{"Good RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "0.0.0.0:26657" }, false},
{"Duplicate Channel", func(ni *Default) { ni.Channels = dupChannels }, true},
{"Good Channels", func(ni *Default) { ni.Channels = ni.Channels[:5] }, false},

{"Invalid NetAddr", func(ni *Default) { ni.ListenAddr = "not-an-address" }, true},
{"Good NetAddr", func(ni *Default) { ni.ListenAddr = "0.0.0.0:26656" }, false},

{"Non-ASCII Version", func(ni *Default) { ni.Version = nonASCII }, true},
{"Empty tab Version", func(ni *Default) { ni.Version = emptyTab }, true},
{"Empty space Version", func(ni *Default) { ni.Version = emptySpace }, true},
{"Empty Version", func(ni *Default) { ni.Version = "" }, false},

{"Non-ASCII Moniker", func(ni *Default) { ni.Moniker = nonASCII }, true},
{"Empty tab Moniker", func(ni *Default) { ni.Moniker = emptyTab }, true},
{"Empty space Moniker", func(ni *Default) { ni.Moniker = emptySpace }, true},
{"Empty Moniker", func(ni *Default) { ni.Moniker = "" }, true},
{"Good Moniker", func(ni *Default) { ni.Moniker = "hey its me" }, false},

{"Non-ASCII TxIndex", func(ni *Default) { ni.Other.TxIndex = nonASCII }, true},
{"Empty tab TxIndex", func(ni *Default) { ni.Other.TxIndex = emptyTab }, true},
{"Empty space TxIndex", func(ni *Default) { ni.Other.TxIndex = emptySpace }, true},
{"Empty TxIndex", func(ni *Default) { ni.Other.TxIndex = "" }, false},
{"Off TxIndex", func(ni *Default) { ni.Other.TxIndex = "off" }, false},

{"Non-ASCII RPCAddress", func(ni *Default) { ni.Other.RPCAddress = nonASCII }, true},
{"Empty tab RPCAddress", func(ni *Default) { ni.Other.RPCAddress = emptyTab }, true},
{"Empty space RPCAddress", func(ni *Default) { ni.Other.RPCAddress = emptySpace }, true},
{"Empty RPCAddress", func(ni *Default) { ni.Other.RPCAddress = "" }, false},
{"Good RPCAddress", func(ni *Default) { ni.Other.RPCAddress = "0.0.0.0:26657" }, false},
}

nodeKey := nodekey.NodeKey{PrivKey: ed25519.GenPrivKey()}

// test case passes
ni = testNodeInfo(nodeKey.ID()).(DefaultNodeInfo)
ni = testNodeInfo(nodeKey.ID()).(Default)
ni.Channels = channels
require.NoError(t, ni.Validate())

for _, tc := range testCases {
ni := testNodeInfo(nodeKey.ID()).(DefaultNodeInfo)
ni := testNodeInfo(nodeKey.ID()).(Default)
ni.Channels = channels
tc.malleateNodeInfo(&ni)
err := ni.Validate()
Expand All @@ -135,8 +135,8 @@ func TestNodeInfoCompatible(t *testing.T) {
var newTestChannel byte = 0x2

// test NodeInfo is compatible
ni1 := testNodeInfo(nodeKey1.ID()).(DefaultNodeInfo)
ni2 := testNodeInfo(nodeKey2.ID()).(DefaultNodeInfo)
ni1 := testNodeInfo(nodeKey1.ID()).(Default)
ni2 := testNodeInfo(nodeKey2.ID()).(Default)
require.NoError(t, ni1.CompatibleWith(ni2))

// add another channel; still compatible
Expand All @@ -151,15 +151,15 @@ func TestNodeInfoCompatible(t *testing.T) {

testCases := []struct {
testName string
malleateNodeInfo func(*DefaultNodeInfo)
malleateNodeInfo func(*Default)
}{
{"Wrong block version", func(ni *DefaultNodeInfo) { ni.ProtocolVersion.Block++ }},
{"Wrong network", func(ni *DefaultNodeInfo) { ni.Network += "-wrong" }},
{"No common channels", func(ni *DefaultNodeInfo) { ni.Channels = []byte{newTestChannel} }},
{"Wrong block version", func(ni *Default) { ni.ProtocolVersion.Block++ }},
{"Wrong network", func(ni *Default) { ni.Network += "-wrong" }},
{"No common channels", func(ni *Default) { ni.Channels = []byte{newTestChannel} }},
}

for _, tc := range testCases {
ni := testNodeInfo(nodeKey2.ID()).(DefaultNodeInfo)
ni := testNodeInfo(nodeKey2.ID()).(Default)
tc.malleateNodeInfo(&ni)
require.Error(t, ni1.CompatibleWith(ni))
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func newPeer(
p := &peer{
peerConn: pc,
nodeInfo: nodeInfo,
channels: nodeInfo.(ni.DefaultNodeInfo).Channels,
channels: nodeInfo.(ni.Default).Channels,
Data: cmap.NewCMap(),
metrics: NopMetrics(),
pendingMetrics: newPeerPendingMetricsCache(),
Expand Down
2 changes: 1 addition & 1 deletion p2p/peer_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (mp *mockPeer) FlushStop() { mp.Stop() } //nolint:errcheck // ig
func (*mockPeer) HasChannel(byte) bool { return true }
func (*mockPeer) TrySend(Envelope) bool { return true }
func (*mockPeer) Send(Envelope) bool { return true }
func (*mockPeer) NodeInfo() ni.NodeInfo { return ni.DefaultNodeInfo{} }
func (*mockPeer) NodeInfo() ni.NodeInfo { return ni.Default{} }
func (*mockPeer) Status() ConnectionStatus { return ConnectionStatus{} }
func (mp *mockPeer) ID() nodekey.ID { return mp.id }
func (*mockPeer) IsOutbound() bool { return false }
Expand Down
Loading

0 comments on commit b1de30a

Please sign in to comment.