You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "github.com/tendermint/go-crypto"
  8. )
  9. const maxNodeInfoSize = 10240 // 10Kb
  10. type NodeInfo struct {
  11. PubKey crypto.PubKeyEd25519 `json:"pub_key"`
  12. Moniker string `json:"moniker"`
  13. Network string `json:"network"`
  14. RemoteAddr string `json:"remote_addr"`
  15. ListenAddr string `json:"listen_addr"`
  16. Version string `json:"version"` // major.minor.revision
  17. Other []string `json:"other"` // other application specific data
  18. }
  19. // CONTRACT: two nodes are compatible if the major/minor versions match and network match
  20. func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
  21. iMajor, iMinor, _, iErr := splitVersion(info.Version)
  22. oMajor, oMinor, _, oErr := splitVersion(other.Version)
  23. // if our own version number is not formatted right, we messed up
  24. if iErr != nil {
  25. return iErr
  26. }
  27. // version number must be formatted correctly ("x.x.x")
  28. if oErr != nil {
  29. return oErr
  30. }
  31. // major version must match
  32. if iMajor != oMajor {
  33. return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
  34. }
  35. // minor version must match
  36. if iMinor != oMinor {
  37. return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
  38. }
  39. // nodes must be on the same network
  40. if info.Network != other.Network {
  41. return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
  42. }
  43. return nil
  44. }
  45. func (info *NodeInfo) ListenHost() string {
  46. host, _, _ := net.SplitHostPort(info.ListenAddr)
  47. return host
  48. }
  49. func (info *NodeInfo) ListenPort() int {
  50. _, port, _ := net.SplitHostPort(info.ListenAddr)
  51. port_i, err := strconv.Atoi(port)
  52. if err != nil {
  53. return -1
  54. }
  55. return port_i
  56. }
  57. func splitVersion(version string) (string, string, string, error) {
  58. spl := strings.Split(version, ".")
  59. if len(spl) != 3 {
  60. return "", "", "", fmt.Errorf("Invalid version format %v", version)
  61. }
  62. return spl[0], spl[1], spl[2], nil
  63. }