OSDN Git Service

Fix discovery msg codec hash error (#60)
[bytom/vapor.git] / p2p / node_info.go
index 61606b5..2e52e31 100644 (file)
@@ -5,8 +5,10 @@ import (
        "net"
        "strconv"
 
-       crypto "github.com/tendermint/go-crypto"
+       "github.com/tendermint/go-crypto"
 
+       cfg "github.com/vapor/config"
+       "github.com/vapor/consensus"
        "github.com/vapor/version"
 )
 
@@ -14,18 +16,46 @@ const maxNodeInfoSize = 10240 // 10Kb
 
 //NodeInfo peer node info
 type NodeInfo struct {
-       PubKey     crypto.PubKeyEd25519 `json:"pub_key"`
-       Moniker    string               `json:"moniker"`
-       Network    string               `json:"network"`
-       RemoteAddr string               `json:"remote_addr"`
-       ListenAddr string               `json:"listen_addr"`
-       Version    string               `json:"version"` // major.minor.revision
-       Other      []string             `json:"other"`   // other application specific data
+       PubKey  crypto.PubKeyEd25519 `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
+       // other application specific data
+       //field 0: node service flags. field 1: node alias.
+       Other []string `json:"other"`
+}
+
+func NewNodeInfo(config *cfg.Config, pubkey crypto.PubKeyEd25519, listenAddr string, netID uint64) *NodeInfo {
+       other := []string{strconv.FormatUint(uint64(consensus.DefaultServices), 10)}
+       if config.NodeAlias != "" {
+               other = append(other, config.NodeAlias)
+       }
+       return &NodeInfo{
+               PubKey:     pubkey,
+               Moniker:    config.Moniker,
+               Network:    config.ChainID,
+               NetworkID:  netID,
+               ListenAddr: listenAddr,
+               Version:    version.Version,
+               Other:      other,
+       }
 }
 
 // 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) error {
+       if info.Network != other.Network {
+               return fmt.Errorf("Peer is on a different network. Peer network: %v, node network: %v", other.Network, info.Network)
+       }
+
+       if info.NetworkID != other.NetworkID {
+               return fmt.Errorf("Network id dismatch. Peer network id: %v, node network id: %v", other.NetworkID, info.NetworkID)
+       }
+
        compatible, err := version.CompatibleWith(other.Version)
        if err != nil {
                return err
@@ -34,34 +64,30 @@ func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
                return fmt.Errorf("Peer is on a different major version. Peer version: %v, node version: %v", other.Version, info.Version)
        }
 
-       if info.Network != other.Network {
-               return fmt.Errorf("Peer is on a different network. Peer network: %v, node network: %v", other.Network, info.Network)
-       }
        return nil
 }
 
+func (info *NodeInfo) getPubkey() crypto.PubKeyEd25519 {
+       return info.PubKey
+}
+
 //ListenHost peer listener ip address
-func (info *NodeInfo) ListenHost() string {
+func (info *NodeInfo) listenHost() string {
        host, _, _ := net.SplitHostPort(info.ListenAddr)
        return host
 }
 
-//ListenPort peer listener port
-func (info *NodeInfo) ListenPort() int {
-       _, port, _ := net.SplitHostPort(info.ListenAddr)
-       portInt, err := strconv.Atoi(port)
-       if err != nil {
-               return -1
-       }
-       return portInt
-}
-
 //RemoteAddrHost peer external ip address
-func (info *NodeInfo) RemoteAddrHost() string {
+func (info *NodeInfo) remoteAddrHost() string {
        host, _, _ := net.SplitHostPort(info.RemoteAddr)
        return host
 }
 
+//GetNetwork get node info network field
+func (info *NodeInfo) GetNetwork() string {
+       return info.Network
+}
+
 //String representation
 func (info NodeInfo) String() string {
        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)