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.

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