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.

215 lines
7.3 KiB

7 years ago
  1. package types
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/tendermint/tendermint/libs/bytes"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. //-----------------------------------------------------------------------------
  10. // RoundStepType enum type
  11. // RoundStepType enumerates the state of the consensus state machine
  12. type RoundStepType uint8 // These must be numeric, ordered.
  13. // RoundStepType
  14. const (
  15. RoundStepNewHeight = RoundStepType(0x01) // Wait til CommitTime + timeoutCommit
  16. RoundStepNewRound = RoundStepType(0x02) // Setup new round and go to RoundStepPropose
  17. RoundStepPropose = RoundStepType(0x03) // Did propose, gossip proposal
  18. RoundStepPrevote = RoundStepType(0x04) // Did prevote, gossip prevotes
  19. RoundStepPrevoteWait = RoundStepType(0x05) // Did receive any +2/3 prevotes, start timeout
  20. RoundStepPrecommit = RoundStepType(0x06) // Did precommit, gossip precommits
  21. RoundStepPrecommitWait = RoundStepType(0x07) // Did receive any +2/3 precommits, start timeout
  22. RoundStepCommit = RoundStepType(0x08) // Entered commit state machine
  23. // NOTE: RoundStepNewHeight acts as RoundStepCommitWait.
  24. // NOTE: Update IsValid method if you change this!
  25. )
  26. // IsValid returns true if the step is valid, false if unknown/undefined.
  27. func (rs RoundStepType) IsValid() bool {
  28. return uint8(rs) >= 0x01 && uint8(rs) <= 0x08
  29. }
  30. // String returns a string
  31. func (rs RoundStepType) String() string {
  32. switch rs {
  33. case RoundStepNewHeight:
  34. return "RoundStepNewHeight"
  35. case RoundStepNewRound:
  36. return "RoundStepNewRound"
  37. case RoundStepPropose:
  38. return "RoundStepPropose"
  39. case RoundStepPrevote:
  40. return "RoundStepPrevote"
  41. case RoundStepPrevoteWait:
  42. return "RoundStepPrevoteWait"
  43. case RoundStepPrecommit:
  44. return "RoundStepPrecommit"
  45. case RoundStepPrecommitWait:
  46. return "RoundStepPrecommitWait"
  47. case RoundStepCommit:
  48. return "RoundStepCommit"
  49. default:
  50. return "RoundStepUnknown" // Cannot panic.
  51. }
  52. }
  53. //-----------------------------------------------------------------------------
  54. // RoundState defines the internal consensus state.
  55. // NOTE: Not thread safe. Should only be manipulated by functions downstream
  56. // of the cs.receiveRoutine
  57. type RoundState struct {
  58. Height int64 `json:"height"` // Height we are working on
  59. Round int32 `json:"round"`
  60. Step RoundStepType `json:"step"`
  61. StartTime time.Time `json:"start_time"`
  62. // Subjective time when +2/3 precommits for Block at Round were found
  63. CommitTime time.Time `json:"commit_time"`
  64. Validators *types.ValidatorSet `json:"validators"`
  65. Proposal *types.Proposal `json:"proposal"`
  66. ProposalBlock *types.Block `json:"proposal_block"`
  67. ProposalBlockParts *types.PartSet `json:"proposal_block_parts"`
  68. LockedRound int32 `json:"locked_round"`
  69. LockedBlock *types.Block `json:"locked_block"`
  70. LockedBlockParts *types.PartSet `json:"locked_block_parts"`
  71. // Last known round with POL for non-nil valid block.
  72. ValidRound int32 `json:"valid_round"`
  73. ValidBlock *types.Block `json:"valid_block"` // Last known block of POL mentioned above.
  74. // Last known block parts of POL mentioned above.
  75. ValidBlockParts *types.PartSet `json:"valid_block_parts"`
  76. Votes *HeightVoteSet `json:"votes"`
  77. CommitRound int32 `json:"commit_round"` //
  78. LastCommit *types.VoteSet `json:"last_commit"` // Last precommits at Height-1
  79. LastValidators *types.ValidatorSet `json:"last_validators"`
  80. TriggeredTimeoutPrecommit bool `json:"triggered_timeout_precommit"`
  81. }
  82. // Compressed version of the RoundState for use in RPC
  83. type RoundStateSimple struct {
  84. HeightRoundStep string `json:"height/round/step"`
  85. StartTime time.Time `json:"start_time"`
  86. ProposalBlockHash bytes.HexBytes `json:"proposal_block_hash"`
  87. LockedBlockHash bytes.HexBytes `json:"locked_block_hash"`
  88. ValidBlockHash bytes.HexBytes `json:"valid_block_hash"`
  89. Votes json.RawMessage `json:"height_vote_set"`
  90. Proposer types.ValidatorInfo `json:"proposer"`
  91. }
  92. // Compress the RoundState to RoundStateSimple
  93. func (rs *RoundState) RoundStateSimple() RoundStateSimple {
  94. votesJSON, err := rs.Votes.MarshalJSON()
  95. if err != nil {
  96. panic(err)
  97. }
  98. addr := rs.Validators.GetProposer().Address
  99. idx, _ := rs.Validators.GetByAddress(addr)
  100. return RoundStateSimple{
  101. HeightRoundStep: fmt.Sprintf("%d/%d/%d", rs.Height, rs.Round, rs.Step),
  102. StartTime: rs.StartTime,
  103. ProposalBlockHash: rs.ProposalBlock.Hash(),
  104. LockedBlockHash: rs.LockedBlock.Hash(),
  105. ValidBlockHash: rs.ValidBlock.Hash(),
  106. Votes: votesJSON,
  107. Proposer: types.ValidatorInfo{
  108. Address: addr,
  109. Index: idx,
  110. },
  111. }
  112. }
  113. // NewRoundEvent returns the RoundState with proposer information as an event.
  114. func (rs *RoundState) NewRoundEvent() types.EventDataNewRound {
  115. addr := rs.Validators.GetProposer().Address
  116. idx, _ := rs.Validators.GetByAddress(addr)
  117. return types.EventDataNewRound{
  118. Height: rs.Height,
  119. Round: rs.Round,
  120. Step: rs.Step.String(),
  121. Proposer: types.ValidatorInfo{
  122. Address: addr,
  123. Index: idx,
  124. },
  125. }
  126. }
  127. // CompleteProposalEvent returns information about a proposed block as an event.
  128. func (rs *RoundState) CompleteProposalEvent() types.EventDataCompleteProposal {
  129. // We must construct BlockID from ProposalBlock and ProposalBlockParts
  130. // cs.Proposal is not guaranteed to be set when this function is called
  131. blockID := types.BlockID{
  132. Hash: rs.ProposalBlock.Hash(),
  133. PartSetHeader: rs.ProposalBlockParts.Header(),
  134. }
  135. return types.EventDataCompleteProposal{
  136. Height: rs.Height,
  137. Round: rs.Round,
  138. Step: rs.Step.String(),
  139. BlockID: blockID,
  140. }
  141. }
  142. // RoundStateEvent returns the H/R/S of the RoundState as an event.
  143. func (rs *RoundState) RoundStateEvent() types.EventDataRoundState {
  144. return types.EventDataRoundState{
  145. Height: rs.Height,
  146. Round: rs.Round,
  147. Step: rs.Step.String(),
  148. }
  149. }
  150. // String returns a string
  151. func (rs *RoundState) String() string {
  152. return rs.StringIndented("")
  153. }
  154. // StringIndented returns a string
  155. func (rs *RoundState) StringIndented(indent string) string {
  156. return fmt.Sprintf(`RoundState{
  157. %s H:%v R:%v S:%v
  158. %s StartTime: %v
  159. %s CommitTime: %v
  160. %s Validators: %v
  161. %s Proposal: %v
  162. %s ProposalBlock: %v %v
  163. %s LockedRound: %v
  164. %s LockedBlock: %v %v
  165. %s ValidRound: %v
  166. %s ValidBlock: %v %v
  167. %s Votes: %v
  168. %s LastCommit: %v
  169. %s LastValidators:%v
  170. %s}`,
  171. indent, rs.Height, rs.Round, rs.Step,
  172. indent, rs.StartTime,
  173. indent, rs.CommitTime,
  174. indent, rs.Validators.StringIndented(indent+" "),
  175. indent, rs.Proposal,
  176. indent, rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort(),
  177. indent, rs.LockedRound,
  178. indent, rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort(),
  179. indent, rs.ValidRound,
  180. indent, rs.ValidBlockParts.StringShort(), rs.ValidBlock.StringShort(),
  181. indent, rs.Votes.StringIndented(indent+" "),
  182. indent, rs.LastCommit.StringShort(),
  183. indent, rs.LastValidators.StringIndented(indent+" "),
  184. indent)
  185. }
  186. // StringShort returns a string
  187. func (rs *RoundState) StringShort() string {
  188. return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
  189. rs.Height, rs.Round, rs.Step, rs.StartTime)
  190. }