OSDN Git Service

merge with dev
[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 type NodeInfo struct {
15         PubKey     crypto.PubKeyEd25519 `json:"pub_key"`
16         Moniker    string               `json:"moniker"`
17         Network    string               `json:"network"`
18         RemoteAddr string               `json:"remote_addr"`
19         ListenAddr string               `json:"listen_addr"`
20         Version    string               `json:"version"` // major.minor.revision
21         Other      []string             `json:"other"`   // other application specific data
22 }
23
24 // CompatibleWith checks if two NodeInfo are compatible with eachother.
25 // CONTRACT: two nodes are compatible if the major version matches and network match
26 // and they have at least one channel in common.
27 func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
28         iMajor, iMinor, _, iErr := splitVersion(info.Version)
29         oMajor, oMinor, _, oErr := splitVersion(other.Version)
30
31         // if our own version number is not formatted right, we messed up
32         if iErr != nil {
33                 return iErr
34         }
35
36         // version number must be formatted correctly ("x.x.x")
37         if oErr != nil {
38                 return oErr
39         }
40
41         // major version must match
42         if iMajor != oMajor {
43                 return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
44         }
45
46         // minor version must match
47         if iMinor != oMinor {
48                 return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
49         }
50
51         // nodes must be on the same network
52         if info.Network != other.Network {
53                 return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
54         }
55
56         return nil
57 }
58
59 func (info *NodeInfo) ListenHost() string {
60         host, _, _ := net.SplitHostPort(info.ListenAddr)
61         return host
62 }
63
64 func (info *NodeInfo) ListenPort() int {
65         _, port, _ := net.SplitHostPort(info.ListenAddr)
66         port_i, err := strconv.Atoi(port)
67         if err != nil {
68                 return -1
69         }
70         return port_i
71 }
72
73 func (info *NodeInfo) RemoteAddrHost() string {
74         host, _, _ := net.SplitHostPort(info.RemoteAddr)
75         return host
76 }
77
78 func (info NodeInfo) String() string {
79         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)
80 }
81
82 func splitVersion(version string) (string, string, string, error) {
83         spl := strings.Split(version, ".")
84         if len(spl) != 3 {
85                 return "", "", "", fmt.Errorf("Invalid version format %v", version)
86         }
87         return spl[0], spl[1], spl[2], nil
88 }