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.

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