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.

104 lines
2.9 KiB

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