OSDN Git Service

Merge pull request #930 from Bytom/fix_bug
[bytom/bytom.git] / p2p / types.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 // CONTRACT: two nodes are compatible if the major/minor versions match and network match
25 func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
26         iMajor, iMinor, _, iErr := splitVersion(info.Version)
27         oMajor, oMinor, _, oErr := splitVersion(other.Version)
28
29         // if our own version number is not formatted right, we messed up
30         if iErr != nil {
31                 return iErr
32         }
33
34         // version number must be formatted correctly ("x.x.x")
35         if oErr != nil {
36                 return oErr
37         }
38
39         // major version must match
40         if iMajor != oMajor {
41                 return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
42         }
43
44         // minor version must match
45         if iMinor != oMinor {
46                 return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
47         }
48
49         // nodes must be on the same network
50         if info.Network != other.Network {
51                 return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
52         }
53
54         return nil
55 }
56
57 func (info *NodeInfo) ListenHost() string {
58         host, _, _ := net.SplitHostPort(info.ListenAddr)
59         return host
60 }
61
62 func (info *NodeInfo) ListenPort() int {
63         _, port, _ := net.SplitHostPort(info.ListenAddr)
64         port_i, err := strconv.Atoi(port)
65         if err != nil {
66                 return -1
67         }
68         return port_i
69 }
70
71 func (info NodeInfo) String() string {
72         return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [remote %v, listen %v], version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.RemoteAddr, info.ListenAddr, info.Version, info.Other)
73 }
74
75 func splitVersion(version string) (string, string, string, error) {
76         spl := strings.Split(version, ".")
77         if len(spl) != 3 {
78                 return "", "", "", fmt.Errorf("Invalid version format %v", version)
79         }
80         return spl[0], spl[1], spl[2], nil
81 }