Skip to content

Commit

Permalink
Tidy up node info
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Sep 30, 2024
1 parent 398f86a commit 373f489
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 133 deletions.
4 changes: 2 additions & 2 deletions tm2/pkg/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (addr *Address) DecodeString(str string) error {
// ----------------------------------------
// ID

var errZeroID = errors.New("address ID is zero")
var ErrZeroID = errors.New("address ID is zero")

// The bech32 representation w/ bech32 prefix.
type ID string
Expand All @@ -144,7 +144,7 @@ func (id ID) String() string {

func (id ID) Validate() error {
if id.IsZero() {
return errZeroID
return ErrZeroID
}

var addr Address
Expand Down
10 changes: 0 additions & 10 deletions tm2/pkg/p2p/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import (
"github.com/gnolang/gno/tm2/pkg/errors"
)

// -----------------------------------------------------------------------------
// P2PConfig

const (
// FuzzModeDrop is a mode in which we randomly drop reads/writes, connections or sleep
FuzzModeDrop = iota
// FuzzModeDelay is a mode in which we randomly sleep
FuzzModeDelay
)

// P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer
type P2PConfig struct {
RootDir string `json:"rpc" toml:"home"`
Expand Down
33 changes: 17 additions & 16 deletions tm2/pkg/p2p/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ const (
maxNumChannels = 16 // plenty of room for upgrades, for now
)

var errInvalidNetworkAddress = errors.New("invalid network address")
var (
errInvalidNetworkAddress = errors.New("invalid node network address")
errInvalidVersion = errors.New("invalid node version")
errInvalidMoniker = errors.New("invalid node moniker")
errInvalidRPCAddress = errors.New("invalid node RPC address")
errExcessiveChannels = errors.New("excessive node channels")
errDuplicateChannels = errors.New("duplicate node channels")
errIncompatibleNetworks = errors.New("incompatible networks")
errNoCommonChannels = errors.New("no common channels")
)

// Max size of the NodeInfo struct
func MaxNodeInfoSize() int {
Expand Down Expand Up @@ -68,18 +77,18 @@ func (info NodeInfo) Validate() error {
if len(info.Version) > 0 &&
(!strings.IsASCIIText(info.Version) ||
strings.ASCIITrim(info.Version) == "") {
return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got %s", info.Version)
return errInvalidVersion
}

// Validate Channels - ensure max and check for duplicates.
if len(info.Channels) > maxNumChannels {
return fmt.Errorf("info.Channels is too long (%d). Max is %d", len(info.Channels), maxNumChannels)
return errExcessiveChannels
}

channelMap := make(map[byte]struct{}, len(info.Channels))
for _, ch := range info.Channels {
if _, ok := channelMap[ch]; ok {
return fmt.Errorf("info.Channels contains duplicate channel id %v", ch)
return errDuplicateChannels
}

// Mark the channel as present
Expand All @@ -88,13 +97,13 @@ func (info NodeInfo) Validate() error {

// Validate Moniker.
if !strings.IsASCIIText(info.Moniker) || strings.ASCIITrim(info.Moniker) == "" {
return fmt.Errorf("info.Moniker must be valid non-empty ASCII text without tabs, but got %s", info.Moniker)
return errInvalidMoniker
}

// XXX: Should we be more strict about address formats?
rpcAddr := info.Other.RPCAddress
if len(rpcAddr) > 0 && (!strings.IsASCIIText(rpcAddr) || strings.ASCIITrim(rpcAddr) == "") {
return fmt.Errorf("info.Other.RPCAddress=%s must be valid ASCII text without tabs", rpcAddr)
return errInvalidRPCAddress
}

return nil
Expand All @@ -116,11 +125,7 @@ func (info NodeInfo) CompatibleWith(other NodeInfo) error {

// Make sure nodes are on the same network
if info.Network != other.Network {
return fmt.Errorf(
"peer is on a different network. Got %q, expected %q",
other.Network,
info.Network,
)
return errIncompatibleNetworks
}

// Make sure there is at least 1 channel in common
Expand All @@ -140,11 +145,7 @@ func (info NodeInfo) CompatibleWith(other NodeInfo) error {
}

if !commonFound {
return fmt.Errorf(
"peer has no common channels. Our channels: %v ; Peer channels: %v",
info.Channels,
other.Channels,
)
return errNoCommonChannels
}

return nil
Expand Down
Loading

0 comments on commit 373f489

Please sign in to comment.