"fmt"
"net"
"strconv"
- "strings"
- crypto "github.com/tendermint/go-crypto"
+ "github.com/tendermint/go-crypto"
+
+ cfg "github.com/bytom/bytom/config"
+ "github.com/bytom/bytom/consensus"
+ "github.com/bytom/bytom/version"
)
const maxNodeInfoSize = 10240 // 10Kb
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
+ // 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) *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,
+ 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
-// and they have at least one channel in common.
func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
- iMajor, iMinor, _, iErr := splitVersion(info.Version)
- oMajor, oMinor, _, oErr := splitVersion(other.Version)
-
- // if our own version number is not formatted right, we messed up
- if iErr != nil {
- return iErr
+ compatible, err := version.CompatibleWith(other.Version)
+ if err != nil {
+ return err
}
-
- // version number must be formatted correctly ("x.x.x")
- if oErr != nil {
- return oErr
+ if !compatible {
+ return fmt.Errorf("Peer is on a different major version. Peer version: %v, node version: %v", other.Version, info.Version)
}
- // major version must match
- if iMajor != oMajor {
- return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
+ 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
+}
- // minor version must match
- if iMinor != oMinor {
- return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
- }
-
- // nodes must be on the same network
- if info.Network != other.Network {
- return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
+func (info NodeInfo) DoFilter(ip string, pubKey string) error {
+ if info.PubKey.String() == pubKey {
+ return ErrConnectSelf
}
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 {
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)
}
-
-func splitVersion(version string) (string, string, string, error) {
- spl := strings.Split(version, ".")
- if len(spl) != 3 {
- return "", "", "", fmt.Errorf("Invalid version format %v", version)
- }
- return spl[0], spl[1], spl[2], nil
-}