OSDN Git Service

new repo
[bytom/vapor.git] / p2p / node_info.go
1 package p2p
2
3 import (
4         "fmt"
5         "net"
6         "strconv"
7
8         crypto "github.com/tendermint/go-crypto"
9
10         "github.com/vapor/version"
11 )
12
13 const maxNodeInfoSize = 10240 // 10Kb
14
15 //NodeInfo peer node info
16 type NodeInfo struct {
17         PubKey     crypto.PubKeyEd25519 `json:"pub_key"`
18         Moniker    string               `json:"moniker"`
19         Network    string               `json:"network"`
20         RemoteAddr string               `json:"remote_addr"`
21         ListenAddr string               `json:"listen_addr"`
22         Version    string               `json:"version"` // major.minor.revision
23         Other      []string             `json:"other"`   // other application specific data
24 }
25
26 // CompatibleWith checks if two NodeInfo are compatible with eachother.
27 // CONTRACT: two nodes are compatible if the major version matches and network match
28 func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
29         compatible, err := version.CompatibleWith(other.Version)
30         if err != nil {
31                 return err
32         }
33         if !compatible {
34                 return fmt.Errorf("Peer is on a different major version. Peer version: %v, node version: %v", other.Version, info.Version)
35         }
36
37         if info.Network != other.Network {
38                 return fmt.Errorf("Peer is on a different network. Peer network: %v, node network: %v", other.Network, info.Network)
39         }
40         return nil
41 }
42
43 //ListenHost peer listener ip address
44 func (info *NodeInfo) ListenHost() string {
45         host, _, _ := net.SplitHostPort(info.ListenAddr)
46         return host
47 }
48
49 //ListenPort peer listener port
50 func (info *NodeInfo) ListenPort() int {
51         _, port, _ := net.SplitHostPort(info.ListenAddr)
52         portInt, err := strconv.Atoi(port)
53         if err != nil {
54                 return -1
55         }
56         return portInt
57 }
58
59 //RemoteAddrHost peer external ip address
60 func (info *NodeInfo) RemoteAddrHost() string {
61         host, _, _ := net.SplitHostPort(info.RemoteAddr)
62         return host
63 }
64
65 //String representation
66 func (info NodeInfo) String() string {
67         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)
68 }