p2p: add NodeID.Validate(), replaces validateID()

This commit is contained in:
Erik Grinaker
2020-12-23 13:32:30 +01:00
committed by Erik Grinaker
parent 8e7d431f6f
commit cc3c18a6a7
3 changed files with 34 additions and 21 deletions

View File

@@ -2,6 +2,8 @@ package p2p
import (
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"github.com/tendermint/tendermint/crypto"
@@ -11,12 +13,38 @@ import (
)
// NodeID is a hex-encoded crypto.Address.
// FIXME: We should either ensure this is always lowercased, or add an Equal()
// for comparison that decodes to the binary byte slice first.
type NodeID string
// NodeIDByteLength is the length of a crypto.Address. Currently only 20.
// TODO: support other length addresses?
// FIXME: support other length addresses?
const NodeIDByteLength = crypto.AddressSize
// Bytes converts the node ID to it's binary byte representation.
func (id NodeID) Bytes() ([]byte, error) {
bz, err := hex.DecodeString(string(id))
if err != nil {
return nil, fmt.Errorf("invalid node ID encoding: %w", err)
}
return bz, nil
}
// Validate validates the NodeID.
func (id NodeID) Validate() error {
if len(id) == 0 {
return errors.New("no ID")
}
bz, err := id.Bytes()
if err != nil {
return err
}
if len(bz) != NodeIDByteLength {
return fmt.Errorf("invalid ID length - got %d, expected %d", len(bz), NodeIDByteLength)
}
return nil
}
//------------------------------------------------------------------------------
// Persistent peer ID
// TODO: encrypt on disk

View File

@@ -5,7 +5,6 @@
package p2p
import (
"encoding/hex"
"errors"
"flag"
"fmt"
@@ -52,7 +51,7 @@ func NewNetAddress(id NodeID, addr net.Addr) *NetAddress {
}
}
if err := validateID(id); err != nil {
if err := id.Validate(); err != nil {
panic(fmt.Sprintf("Invalid ID %v: %v (addr: %v)", id, err, addr))
}
@@ -75,7 +74,7 @@ func NewNetAddressString(addr string) (*NetAddress, error) {
}
// get ID
if err := validateID(NodeID(spl[0])); err != nil {
if err := NodeID(spl[0]).Validate(); err != nil {
return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
}
var id NodeID
@@ -262,7 +261,7 @@ func (na *NetAddress) Routable() bool {
// For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
// address or one that matches the RFC3849 documentation address format.
func (na *NetAddress) Valid() error {
if err := validateID(na.ID); err != nil {
if err := na.ID.Validate(); err != nil {
return fmt.Errorf("invalid ID: %w", err)
}
@@ -414,17 +413,3 @@ func removeProtocolIfDefined(addr string) string {
return addr
}
func validateID(id NodeID) error {
if len(id) == 0 {
return errors.New("no ID")
}
idBytes, err := hex.DecodeString(string(id))
if err != nil {
return err
}
if len(idBytes) != NodeIDByteLength {
return fmt.Errorf("invalid hex length - got %d, expected %d", len(idBytes), NodeIDByteLength)
}
return nil
}

View File

@@ -592,7 +592,7 @@ func (sw *Switch) AddPersistentPeers(addrs []string) error {
func (sw *Switch) AddUnconditionalPeerIDs(ids []string) error {
sw.Logger.Info("Adding unconditional peer ids", "ids", ids)
for i, id := range ids {
err := validateID(NodeID(id))
err := NodeID(id).Validate()
if err != nil {
return fmt.Errorf("wrong ID #%d: %w", i, err)
}
@@ -604,7 +604,7 @@ func (sw *Switch) AddUnconditionalPeerIDs(ids []string) error {
func (sw *Switch) AddPrivatePeerIDs(ids []string) error {
validIDs := make([]string, 0, len(ids))
for i, id := range ids {
err := validateID(NodeID(id))
err := NodeID(id).Validate()
if err != nil {
return fmt.Errorf("wrong ID #%d: %w", i, err)
}