OSDN Git Service

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