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.

58 lines
1.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. package types
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type NodeInfo struct {
  7. Moniker string `json:"moniker"`
  8. ChainID string `json:"chain_id"`
  9. Version string `json:"version"`
  10. Revision string `json:"revision"`
  11. UUID string `json:"uuid"`
  12. Host string `json:"host"`
  13. P2PPort uint16 `json:"p2p_port"`
  14. RPCPort uint16 `json:"rpc_port"`
  15. }
  16. func (ni *NodeInfo) CompatibleWith(no *NodeInfo) error {
  17. iM, im, _, ie := splitVersion(ni.Version)
  18. oM, om, _, oe := splitVersion(no.Version)
  19. // if our own version number is not formatted right, we messed up
  20. if ie != nil {
  21. return ie
  22. }
  23. // version number must be formatted correctly ("x.x.x")
  24. if oe != nil {
  25. return oe
  26. }
  27. // major version must match
  28. if iM != oM {
  29. return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oM, iM)
  30. }
  31. // minor version must match
  32. if im != om {
  33. return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", om, im)
  34. }
  35. // nodes must be on the same chain_id
  36. if ni.ChainID != no.ChainID {
  37. return fmt.Errorf("Peer is on a different chain_id. Got %v, expected %v", no.ChainID, ni.ChainID)
  38. }
  39. return nil
  40. }
  41. func splitVersion(version string) (string, string, string, error) {
  42. spl := strings.Split(version, ".")
  43. if len(spl) != 3 {
  44. return "", "", "", fmt.Errorf("Invalid version format %v", version)
  45. }
  46. return spl[0], spl[1], spl[2], nil
  47. }