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.6 KiB

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