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.

121 lines
3.5 KiB

  1. package types
  2. import (
  3. "time"
  4. wire "github.com/tendermint/go-wire"
  5. "github.com/tendermint/go-wire/data"
  6. )
  7. // canonical json is go-wire's json for structs with fields in alphabetical order
  8. // timeFormat is used for generating the sigs
  9. const timeFormat = wire.RFC3339Millis
  10. type CanonicalJSONBlockID struct {
  11. Hash data.Bytes `json:"hash,omitempty"`
  12. PartsHeader CanonicalJSONPartSetHeader `json:"parts,omitempty"`
  13. }
  14. type CanonicalJSONPartSetHeader struct {
  15. Hash data.Bytes `json:"hash"`
  16. Total int `json:"total"`
  17. }
  18. type CanonicalJSONProposal struct {
  19. BlockPartsHeader CanonicalJSONPartSetHeader `json:"block_parts_header"`
  20. Height int64 `json:"height"`
  21. POLBlockID CanonicalJSONBlockID `json:"pol_block_id"`
  22. POLRound int `json:"pol_round"`
  23. Round int `json:"round"`
  24. Timestamp string `json:"timestamp"`
  25. }
  26. type CanonicalJSONVote struct {
  27. BlockID CanonicalJSONBlockID `json:"block_id"`
  28. Height int64 `json:"height"`
  29. Round int `json:"round"`
  30. Timestamp string `json:"timestamp"`
  31. Type byte `json:"type"`
  32. }
  33. type CanonicalJSONHeartbeat struct {
  34. Height int64 `json:"height"`
  35. Round int `json:"round"`
  36. Sequence int `json:"sequence"`
  37. ValidatorAddress data.Bytes `json:"validator_address"`
  38. ValidatorIndex int `json:"validator_index"`
  39. }
  40. //------------------------------------
  41. // Messages including a "chain id" can only be applied to one chain, hence "Once"
  42. type CanonicalJSONOnceProposal struct {
  43. ChainID string `json:"chain_id"`
  44. Proposal CanonicalJSONProposal `json:"proposal"`
  45. }
  46. type CanonicalJSONOnceVote struct {
  47. ChainID string `json:"chain_id"`
  48. Vote CanonicalJSONVote `json:"vote"`
  49. }
  50. type CanonicalJSONOnceHeartbeat struct {
  51. ChainID string `json:"chain_id"`
  52. Heartbeat CanonicalJSONHeartbeat `json:"heartbeat"`
  53. }
  54. //-----------------------------------
  55. // Canonicalize the structs
  56. func CanonicalBlockID(blockID BlockID) CanonicalJSONBlockID {
  57. return CanonicalJSONBlockID{
  58. Hash: blockID.Hash,
  59. PartsHeader: CanonicalPartSetHeader(blockID.PartsHeader),
  60. }
  61. }
  62. func CanonicalPartSetHeader(psh PartSetHeader) CanonicalJSONPartSetHeader {
  63. return CanonicalJSONPartSetHeader{
  64. psh.Hash,
  65. psh.Total,
  66. }
  67. }
  68. func CanonicalProposal(proposal *Proposal) CanonicalJSONProposal {
  69. return CanonicalJSONProposal{
  70. BlockPartsHeader: CanonicalPartSetHeader(proposal.BlockPartsHeader),
  71. Height: proposal.Height,
  72. Timestamp: CanonicalTime(proposal.Timestamp),
  73. POLBlockID: CanonicalBlockID(proposal.POLBlockID),
  74. POLRound: proposal.POLRound,
  75. Round: proposal.Round,
  76. }
  77. }
  78. func CanonicalVote(vote *Vote) CanonicalJSONVote {
  79. return CanonicalJSONVote{
  80. BlockID: CanonicalBlockID(vote.BlockID),
  81. Height: vote.Height,
  82. Round: vote.Round,
  83. Timestamp: CanonicalTime(vote.Timestamp),
  84. Type: vote.Type,
  85. }
  86. }
  87. func CanonicalHeartbeat(heartbeat *Heartbeat) CanonicalJSONHeartbeat {
  88. return CanonicalJSONHeartbeat{
  89. heartbeat.Height,
  90. heartbeat.Round,
  91. heartbeat.Sequence,
  92. heartbeat.ValidatorAddress,
  93. heartbeat.ValidatorIndex,
  94. }
  95. }
  96. func CanonicalTime(t time.Time) string {
  97. // note that sending time over go-wire resets it to
  98. // local time, we need to force UTC here, so the
  99. // signatures match
  100. return t.UTC().Format(timeFormat)
  101. }