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.

90 lines
2.4 KiB

  1. package types
  2. import (
  3. "time"
  4. "github.com/tendermint/tendermint/libs/bytes"
  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 bytes.HexBytes
  12. PartsHeader CanonicalPartSetHeader
  13. }
  14. type CanonicalPartSetHeader struct {
  15. Hash bytes.HexBytes
  16. Total int
  17. }
  18. type CanonicalProposal struct {
  19. Type SignedMsgType // type alias for byte
  20. Height int64 `binary:"fixed64"`
  21. Round int64 `binary:"fixed64"`
  22. POLRound int64 `binary:"fixed64"`
  23. BlockID CanonicalBlockID
  24. Timestamp time.Time
  25. ChainID string
  26. }
  27. type CanonicalVote struct {
  28. Type SignedMsgType // type alias for byte
  29. Height int64 `binary:"fixed64"`
  30. Round int64 `binary:"fixed64"`
  31. BlockID CanonicalBlockID
  32. Timestamp time.Time
  33. ChainID string
  34. }
  35. //-----------------------------------
  36. // Canonicalize the structs
  37. func CanonicalizeBlockID(blockID BlockID) CanonicalBlockID {
  38. return CanonicalBlockID{
  39. Hash: blockID.Hash,
  40. PartsHeader: CanonicalizePartSetHeader(blockID.PartsHeader),
  41. }
  42. }
  43. func CanonicalizePartSetHeader(psh PartSetHeader) CanonicalPartSetHeader {
  44. return CanonicalPartSetHeader{
  45. psh.Hash,
  46. psh.Total,
  47. }
  48. }
  49. func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal {
  50. return CanonicalProposal{
  51. Type: ProposalType,
  52. Height: proposal.Height,
  53. Round: int64(proposal.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int)
  54. POLRound: int64(proposal.POLRound),
  55. BlockID: CanonicalizeBlockID(proposal.BlockID),
  56. Timestamp: proposal.Timestamp,
  57. ChainID: chainID,
  58. }
  59. }
  60. func CanonicalizeVote(chainID string, vote *Vote) CanonicalVote {
  61. return CanonicalVote{
  62. Type: vote.Type,
  63. Height: vote.Height,
  64. Round: int64(vote.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int)
  65. BlockID: CanonicalizeBlockID(vote.BlockID),
  66. Timestamp: vote.Timestamp,
  67. ChainID: chainID,
  68. }
  69. }
  70. // CanonicalTime can be used to stringify time in a canonical way.
  71. func CanonicalTime(t time.Time) string {
  72. // Note that sending time over amino resets it to
  73. // local time, we need to force UTC here, so the
  74. // signatures match
  75. return tmtime.Canonical(t).Format(TimeFormat)
  76. }