-
Notifications
You must be signed in to change notification settings - Fork 9
/
node_info.go
97 lines (80 loc) · 2.93 KB
/
node_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package p2p
import (
"fmt"
"net"
cfg "github.com/vapor/config"
"github.com/vapor/consensus"
"github.com/vapor/errors"
"github.com/vapor/p2p/signlib"
"github.com/vapor/version"
)
const maxNodeInfoSize = 10240 // 10Kb
var (
errDiffMajorVersion = errors.New("Peer is on a different major version.")
errDiffNetwork = errors.New("Peer is on a different network name.")
errDiffNetworkID = errors.New("Peer has different network ID.")
)
//NodeInfo peer node info
type NodeInfo struct {
PubKey string `json:"pub_key"`
Moniker string `json:"moniker"`
Network string `json:"network"`
//NetworkID used to isolate subnets with same network name
NetworkID uint64 `json:"network_id"`
RemoteAddr string `json:"remote_addr"`
ListenAddr string `json:"listen_addr"`
Version string `json:"version"` // major.minor.revision
ServiceFlag consensus.ServiceFlag `json:"service_flag"`
// other application specific data
Other []string `json:"other"`
}
func NewNodeInfo(config *cfg.Config, pubkey signlib.PubKey, listenAddr string, netID uint64) *NodeInfo {
return &NodeInfo{
PubKey: pubkey.String(),
Moniker: config.Moniker,
Network: config.ChainID,
NetworkID: netID,
ListenAddr: listenAddr,
Version: version.Version,
ServiceFlag: consensus.DefaultServices,
}
}
type VersionCompatibleWith func(remoteVerStr string) (bool, error)
// CompatibleWith checks if two NodeInfo are compatible with eachother.
// CONTRACT: two nodes are compatible if the major version matches and network match
func (info *NodeInfo) compatibleWith(other *NodeInfo, versionCompatibleWith VersionCompatibleWith) error {
if info.Network != other.Network {
return errors.Wrapf(errDiffNetwork, "Peer network: %v, node network: %v", other.Network, info.Network)
}
if info.NetworkID != other.NetworkID {
return errors.Wrapf(errDiffNetworkID, "Peer network id: %v, node network id: %v", other.NetworkID, info.NetworkID)
}
compatible, err := versionCompatibleWith(other.Version)
if err != nil {
return err
}
if !compatible {
return errors.Wrapf(errDiffMajorVersion, "Peer version: %v, node version: %v", other.Version, info.Version)
}
return nil
}
func (info NodeInfo) DoFilter(ip string, pubKey string) error {
if info.PubKey == pubKey {
return ErrConnectSelf
}
return nil
}
//listenHost peer listener ip address
func (info NodeInfo) listenHost() string {
host, _, _ := net.SplitHostPort(info.ListenAddr)
return host
}
//remoteAddrHost peer external ip address
func (info NodeInfo) remoteAddrHost() string {
host, _, _ := net.SplitHostPort(info.RemoteAddr)
return host
}
//String representation
func (info NodeInfo) String() string {
return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [listen %v],networkID: %x, service: %v,version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.ListenAddr, info.NetworkID, info.ServiceFlag, info.Version, info.Other)
}