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.

56 lines
1.3 KiB

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