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 RemoteAddr string `json:"remote_addr"`
23 ListenAddr string `json:"listen_addr"`
24 Version string `json:"version"` // major.minor.revision
25 // other application specific data
26 //field 0: node service flags. field 1: node alias.
27 Other []string `json:"other"`
30 func NewNodeInfo(config *cfg.Config, pubkey crypto.PubKeyEd25519, listenAddr string) *NodeInfo {
31 other := []string{strconv.FormatUint(uint64(consensus.DefaultServices), 10)}
32 if config.NodeAlias != "" {
33 other = append(other, config.NodeAlias)
37 Moniker: config.Moniker,
38 Network: config.ChainID,
39 ListenAddr: listenAddr,
40 Version: version.Version,
45 // CompatibleWith checks if two NodeInfo are compatible with eachother.
46 // CONTRACT: two nodes are compatible if the major version matches and network match
47 func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
48 compatible, err := version.CompatibleWith(other.Version)
53 return fmt.Errorf("Peer is on a different major version. Peer version: %v, node version: %v", other.Version, info.Version)
56 if info.Network != other.Network {
57 return fmt.Errorf("Peer is on a different network. Peer network: %v, node network: %v", other.Network, info.Network)
62 func (info *NodeInfo) getPubkey() crypto.PubKeyEd25519 {
66 //ListenHost peer listener ip address
67 func (info *NodeInfo) listenHost() string {
68 host, _, _ := net.SplitHostPort(info.ListenAddr)
72 //RemoteAddrHost peer external ip address
73 func (info *NodeInfo) remoteAddrHost() string {
74 host, _, _ := net.SplitHostPort(info.RemoteAddr)
78 //GetNetwork get node info network field
79 func (info *NodeInfo) GetNetwork() string {
83 //String representation
84 func (info NodeInfo) String() string {
85 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)