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.

116 lines
3.6 KiB

  1. package types
  2. import (
  3. "time"
  4. cmn "github.com/tendermint/tendermint/libs/common"
  5. tmtime "github.com/tendermint/tendermint/types/time"
  6. )
  7. // Canonical* wraps the structs in types for amino encoding them for use in SignBytes / the Signable interface.
  8. // TimeFormat is used for generating the sigs
  9. const TimeFormat = time.RFC3339Nano
  10. type CanonicalBlockID struct {
  11. Hash cmn.HexBytes `json:"hash,omitempty"`
  12. PartsHeader CanonicalPartSetHeader `json:"parts,omitempty"`
  13. }
  14. type CanonicalPartSetHeader struct {
  15. Hash cmn.HexBytes `json:"hash,omitempty"`
  16. Total int `json:"total,omitempty"`
  17. }
  18. type CanonicalProposal struct {
  19. ChainID string `json:"@chain_id"`
  20. Type string `json:"@type"`
  21. BlockPartsHeader CanonicalPartSetHeader `json:"block_parts_header"`
  22. Height int64 `json:"height"`
  23. POLBlockID CanonicalBlockID `json:"pol_block_id"`
  24. POLRound int `json:"pol_round"`
  25. Round int `json:"round"`
  26. Timestamp time.Time `json:"timestamp"`
  27. }
  28. type CanonicalVote struct {
  29. ChainID string `json:"@chain_id"`
  30. Type string `json:"@type"`
  31. BlockID CanonicalBlockID `json:"block_id"`
  32. Height int64 `json:"height"`
  33. Round int `json:"round"`
  34. Timestamp time.Time `json:"timestamp"`
  35. VoteType byte `json:"type"`
  36. }
  37. type CanonicalHeartbeat struct {
  38. ChainID string `json:"@chain_id"`
  39. Type string `json:"@type"`
  40. Height int64 `json:"height"`
  41. Round int `json:"round"`
  42. Sequence int `json:"sequence"`
  43. ValidatorAddress Address `json:"validator_address"`
  44. ValidatorIndex int `json:"validator_index"`
  45. }
  46. //-----------------------------------
  47. // Canonicalize the structs
  48. func CanonicalizeBlockID(blockID BlockID) CanonicalBlockID {
  49. return CanonicalBlockID{
  50. Hash: blockID.Hash,
  51. PartsHeader: CanonicalizePartSetHeader(blockID.PartsHeader),
  52. }
  53. }
  54. func CanonicalizePartSetHeader(psh PartSetHeader) CanonicalPartSetHeader {
  55. return CanonicalPartSetHeader{
  56. psh.Hash,
  57. psh.Total,
  58. }
  59. }
  60. func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal {
  61. return CanonicalProposal{
  62. ChainID: chainID,
  63. Type: "proposal",
  64. BlockPartsHeader: CanonicalizePartSetHeader(proposal.BlockPartsHeader),
  65. Height: proposal.Height,
  66. Timestamp: proposal.Timestamp,
  67. POLBlockID: CanonicalizeBlockID(proposal.POLBlockID),
  68. POLRound: proposal.POLRound,
  69. Round: proposal.Round,
  70. }
  71. }
  72. func CanonicalizeVote(chainID string, vote *Vote) CanonicalVote {
  73. return CanonicalVote{
  74. ChainID: chainID,
  75. Type: "vote",
  76. BlockID: CanonicalizeBlockID(vote.BlockID),
  77. Height: vote.Height,
  78. Round: vote.Round,
  79. Timestamp: vote.Timestamp,
  80. VoteType: vote.Type,
  81. }
  82. }
  83. func CanonicalizeHeartbeat(chainID string, heartbeat *Heartbeat) CanonicalHeartbeat {
  84. return CanonicalHeartbeat{
  85. ChainID: chainID,
  86. Type: "heartbeat",
  87. Height: heartbeat.Height,
  88. Round: heartbeat.Round,
  89. Sequence: heartbeat.Sequence,
  90. ValidatorAddress: heartbeat.ValidatorAddress,
  91. ValidatorIndex: heartbeat.ValidatorIndex,
  92. }
  93. }
  94. // CanonicalTime can be used to stringify time in a canonical way.
  95. func CanonicalTime(t time.Time) string {
  96. // Note that sending time over amino resets it to
  97. // local time, we need to force UTC here, so the
  98. // signatures match
  99. return tmtime.Canonical(t).Format(TimeFormat)
  100. }