tendermint/p2p/types.go

82 lines
2.4 KiB
Go
Raw Normal View History

2015-10-25 18:21:51 -07:00
package p2p
import (
"fmt"
"net"
"strconv"
"strings"
2017-05-12 14:07:53 -07:00
crypto "github.com/tendermint/go-crypto"
2015-10-25 18:21:51 -07:00
)
2015-11-10 12:29:43 -08:00
const maxNodeInfoSize = 10240 // 10Kb
2015-10-25 18:21:51 -07:00
type NodeInfo struct {
PubKey crypto.PubKey `json:"pub_key"` // authenticated pubkey
Moniker string `json:"moniker"` // arbitrary moniker
Network string `json:"network"` // network/chain ID
RemoteAddr string `json:"remote_addr"` // address for the connection
ListenAddr string `json:"listen_addr"` // accepting incoming
Version string `json:"version"` // major.minor.revision
Other []string `json:"other"` // other application specific data
2015-10-25 18:21:51 -07:00
}
// CONTRACT: two nodes are compatible if the major/minor versions match and network match
2015-10-25 18:21:51 -07:00
func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
iMajor, iMinor, _, iErr := splitVersion(info.Version)
oMajor, oMinor, _, oErr := splitVersion(other.Version)
// if our own version number is not formatted right, we messed up
if iErr != nil {
return iErr
}
// version number must be formatted correctly ("x.x.x")
if oErr != nil {
return oErr
}
// major version must match
if iMajor != oMajor {
return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
}
// minor version must match
if iMinor != oMinor {
return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
}
// 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)
}
return nil
}
func (info *NodeInfo) ListenHost() string {
2017-11-27 14:05:55 -08:00
host, _, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas
2015-10-25 18:21:51 -07:00
return host
}
func (info *NodeInfo) ListenPort() int {
2017-11-27 14:05:55 -08:00
_, port, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas
2015-10-25 18:21:51 -07:00
port_i, err := strconv.Atoi(port)
if err != nil {
return -1
}
return port_i
}
2017-05-12 14:07:53 -07:00
func (info NodeInfo) String() string {
return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [remote %v, listen %v], version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.RemoteAddr, info.ListenAddr, info.Version, info.Other)
}
2015-10-25 18:21:51 -07:00
func splitVersion(version string) (string, string, string, error) {
spl := strings.Split(version, ".")
if len(spl) != 3 {
return "", "", "", fmt.Errorf("Invalid version format %v", version)
}
return spl[0], spl[1], spl[2], nil
}