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.

52 lines
1.5 KiB

  1. package types
  2. import (
  3. "fmt"
  4. cmn "github.com/tendermint/tendermint/libs/common"
  5. )
  6. // Heartbeat is a simple vote-like structure so validators can
  7. // alert others that they are alive and waiting for transactions.
  8. // Note: We aren't adding ",omitempty" to Heartbeat's
  9. // json field tags because we always want the JSON
  10. // representation to be in its canonical form.
  11. type Heartbeat struct {
  12. ValidatorAddress Address `json:"validator_address"`
  13. ValidatorIndex int `json:"validator_index"`
  14. Height int64 `json:"height"`
  15. Round int `json:"round"`
  16. Sequence int `json:"sequence"`
  17. Signature []byte `json:"signature"`
  18. }
  19. // SignBytes returns the Heartbeat bytes for signing.
  20. // It panics if the Heartbeat is nil.
  21. func (heartbeat *Heartbeat) SignBytes(chainID string) []byte {
  22. bz, err := cdc.MarshalJSON(CanonicalHeartbeat(chainID, heartbeat))
  23. if err != nil {
  24. panic(err)
  25. }
  26. return bz
  27. }
  28. // Copy makes a copy of the Heartbeat.
  29. func (heartbeat *Heartbeat) Copy() *Heartbeat {
  30. if heartbeat == nil {
  31. return nil
  32. }
  33. heartbeatCopy := *heartbeat
  34. return &heartbeatCopy
  35. }
  36. // String returns a string representation of the Heartbeat.
  37. func (heartbeat *Heartbeat) String() string {
  38. if heartbeat == nil {
  39. return "nil-heartbeat"
  40. }
  41. return fmt.Sprintf("Heartbeat{%v:%X %v/%02d (%v) %v}",
  42. heartbeat.ValidatorIndex, cmn.Fingerprint(heartbeat.ValidatorAddress),
  43. heartbeat.Height, heartbeat.Round, heartbeat.Sequence,
  44. fmt.Sprintf("/%X.../", cmn.Fingerprint(heartbeat.Signature[:])))
  45. }