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.

112 lines
3.1 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "strings"
  7. crypto "github.com/tendermint/go-crypto"
  8. )
  9. const maxNodeInfoSize = 10240 // 10Kb
  10. // NodeInfo is the basic node information exchanged
  11. // between two peers during the Tendermint P2P handshake.
  12. type NodeInfo struct {
  13. // Authenticate
  14. PubKey crypto.PubKey `json:"pub_key"` // authenticated pubkey
  15. ListenAddr string `json:"listen_addr"` // accepting incoming
  16. // Check compatibility
  17. Network string `json:"network"` // network/chain ID
  18. Version string `json:"version"` // major.minor.revision
  19. // Sanitize
  20. Moniker string `json:"moniker"` // arbitrary moniker
  21. Other []string `json:"other"` // other application specific data
  22. }
  23. // Validate checks the self-reported NodeInfo is safe.
  24. // It returns an error if the info.PubKey doesn't match the given pubKey.
  25. // TODO: constraints for Moniker/Other? Or is that for the UI ?
  26. func (info NodeInfo) Validate(pubKey crypto.PubKey) error {
  27. if !info.PubKey.Equals(pubKey) {
  28. return fmt.Errorf("info.PubKey (%v) doesn't match peer.PubKey (%v)",
  29. info.PubKey, pubKey)
  30. }
  31. return nil
  32. }
  33. // CONTRACT: two nodes are compatible if the major/minor versions match and network match
  34. func (info NodeInfo) CompatibleWith(other NodeInfo) error {
  35. iMajor, iMinor, _, iErr := splitVersion(info.Version)
  36. oMajor, oMinor, _, oErr := splitVersion(other.Version)
  37. // if our own version number is not formatted right, we messed up
  38. if iErr != nil {
  39. return iErr
  40. }
  41. // version number must be formatted correctly ("x.x.x")
  42. if oErr != nil {
  43. return oErr
  44. }
  45. // major version must match
  46. if iMajor != oMajor {
  47. return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oMajor, iMajor)
  48. }
  49. // minor version must match
  50. if iMinor != oMinor {
  51. return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", oMinor, iMinor)
  52. }
  53. // nodes must be on the same network
  54. if info.Network != other.Network {
  55. return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
  56. }
  57. return nil
  58. }
  59. func (info NodeInfo) ID() ID {
  60. return PubKeyToID(info.PubKey)
  61. }
  62. func (info NodeInfo) NetAddress() *NetAddress {
  63. id := PubKeyToID(info.PubKey)
  64. addr := info.ListenAddr
  65. netAddr, err := NewNetAddressString(IDAddressString(id, addr))
  66. if err != nil {
  67. panic(err) // everything should be well formed by now
  68. }
  69. return netAddr
  70. }
  71. func (info NodeInfo) ListenHost() string {
  72. host, _, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas
  73. return host
  74. }
  75. func (info NodeInfo) ListenPort() int {
  76. _, port, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas
  77. port_i, err := strconv.Atoi(port)
  78. if err != nil {
  79. return -1
  80. }
  81. return port_i
  82. }
  83. func (info NodeInfo) String() string {
  84. 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)
  85. }
  86. func splitVersion(version string) (string, string, string, error) {
  87. spl := strings.Split(version, ".")
  88. if len(spl) != 3 {
  89. return "", "", "", fmt.Errorf("Invalid version format %v", version)
  90. }
  91. return spl[0], spl[1], spl[2], nil
  92. }