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.

91 lines
2.5 KiB

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