8 "github.com/tendermint/go-crypto"
10 cfg "github.com/vapor/config"
11 "github.com/vapor/consensus"
12 "github.com/vapor/version"
15 const maxNodeInfoSize = 10240 // 10Kb
17 //NodeInfo peer node info
18 type NodeInfo struct {
19 PubKey crypto.PubKeyEd25519 `json:"pub_key"`
20 Moniker string `json:"moniker"`
21 Network string `json:"network"`
22 //NetworkID used to isolate subnets with same network name
23 NetworkID uint64 `json:"network_id"`
24 RemoteAddr string `json:"remote_addr"`
25 ListenAddr string `json:"listen_addr"`
26 Version string `json:"version"` // major.minor.revision
27 // other application specific data
28 //field 0: node service flags. field 1: node alias.
29 Other []string `json:"other"`
32 func NewNodeInfo(config *cfg.Config, pubkey crypto.PubKeyEd25519, listenAddr string, netID uint64) *NodeInfo {
33 other := []string{strconv.FormatUint(uint64(consensus.DefaultServices), 10)}
34 if config.NodeAlias != "" {
35 other = append(other, config.NodeAlias)
39 Moniker: config.Moniker,
40 Network: config.ChainID,
42 ListenAddr: listenAddr,
43 Version: version.Version,
48 // CompatibleWith checks if two NodeInfo are compatible with eachother.
49 // CONTRACT: two nodes are compatible if the major version matches and network match
50 func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
51 if info.Network != other.Network {
52 return fmt.Errorf("Peer is on a different network. Peer network: %v, node network: %v", other.Network, info.Network)
55 if info.NetworkID != other.NetworkID {
56 return fmt.Errorf("Network id dismatch. Peer network id: %v, node network id: %v", other.NetworkID, info.NetworkID)
59 compatible, err := version.CompatibleWith(other.Version)
64 return fmt.Errorf("Peer is on a different major version. Peer version: %v, node version: %v", other.Version, info.Version)
70 func (info *NodeInfo) getPubkey() crypto.PubKeyEd25519 {
74 //ListenHost peer listener ip address
75 func (info *NodeInfo) listenHost() string {
76 host, _, _ := net.SplitHostPort(info.ListenAddr)
80 //RemoteAddrHost peer external ip address
81 func (info *NodeInfo) remoteAddrHost() string {
82 host, _, _ := net.SplitHostPort(info.RemoteAddr)
86 //GetNetwork get node info network field
87 func (info *NodeInfo) GetNetwork() string {
91 //String representation
92 func (info NodeInfo) String() string {
93 return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [listen %v], version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.ListenAddr, info.Version, info.Other)