OSDN Git Service

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