7 cfg "github.com/vapor/config"
8 "github.com/vapor/consensus"
9 "github.com/vapor/errors"
10 "github.com/vapor/p2p/signlib"
11 "github.com/vapor/version"
14 const maxNodeInfoSize = 10240 // 10Kb
17 errDiffMajorVersion = errors.New("Peer is on a different major version.")
18 errDiffNetwork = errors.New("Peer is on a different network name.")
19 errDiffNetworkID = errors.New("Peer has different network ID.")
22 //NodeInfo peer node info
23 type NodeInfo struct {
24 PubKey string `json:"pub_key"`
25 Moniker string `json:"moniker"`
26 Network string `json:"network"`
27 //NetworkID used to isolate subnets with same network name
28 NetworkID uint64 `json:"network_id"`
29 RemoteAddr string `json:"remote_addr"`
30 ListenAddr string `json:"listen_addr"`
31 Version string `json:"version"` // major.minor.revision
32 ServiceFlag consensus.ServiceFlag `json:"service_flag"`
33 // other application specific data
34 Other []string `json:"other"`
37 func NewNodeInfo(config *cfg.Config, pubkey signlib.PubKey, listenAddr string, netID uint64) *NodeInfo {
39 PubKey: pubkey.String(),
40 Moniker: config.Moniker,
41 Network: config.ChainID,
43 ListenAddr: listenAddr,
44 Version: version.Version,
45 ServiceFlag: consensus.DefaultServices,
49 type VersionCompatibleWith func(remoteVerStr string) (bool, error)
51 // CompatibleWith checks if two NodeInfo are compatible with eachother.
52 // CONTRACT: two nodes are compatible if the major version matches and network match
53 func (info *NodeInfo) compatibleWith(other *NodeInfo, versionCompatibleWith VersionCompatibleWith) error {
54 if info.Network != other.Network {
55 return errors.Wrapf(errDiffNetwork, "Peer network: %v, node network: %v", other.Network, info.Network)
58 if info.NetworkID != other.NetworkID {
59 return errors.Wrapf(errDiffNetworkID, "Peer network id: %v, node network id: %v", other.NetworkID, info.NetworkID)
62 compatible, err := versionCompatibleWith(other.Version)
68 return errors.Wrapf(errDiffMajorVersion, "Peer version: %v, node version: %v", other.Version, info.Version)
74 func (info NodeInfo) DoFilter(ip string, pubKey string) error {
75 if info.PubKey == pubKey {
82 //listenHost peer listener ip address
83 func (info NodeInfo) listenHost() string {
84 host, _, _ := net.SplitHostPort(info.ListenAddr)
88 //remoteAddrHost peer external ip address
89 func (info NodeInfo) RemoteAddrHost() string {
90 host, _, _ := net.SplitHostPort(info.RemoteAddr)
94 //String representation
95 func (info NodeInfo) String() string {
96 return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [listen %v],networkID: %x, service: %v,version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.ListenAddr, info.NetworkID, info.ServiceFlag, info.Version, info.Other)