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.

53 lines
1.6 KiB

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