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.

135 lines
4.8 KiB

7 years ago
  1. package types
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. //-----------------------------------------------------------------------------
  8. // RoundStepType enum type
  9. // RoundStepType enumerates the state of the consensus state machine
  10. type RoundStepType uint8 // These must be numeric, ordered.
  11. const (
  12. RoundStepNewHeight = RoundStepType(0x01) // Wait til CommitTime + timeoutCommit
  13. RoundStepNewRound = RoundStepType(0x02) // Setup new round and go to RoundStepPropose
  14. RoundStepPropose = RoundStepType(0x03) // Did propose, gossip proposal
  15. RoundStepPrevote = RoundStepType(0x04) // Did prevote, gossip prevotes
  16. RoundStepPrevoteWait = RoundStepType(0x05) // Did receive any +2/3 prevotes, start timeout
  17. RoundStepPrecommit = RoundStepType(0x06) // Did precommit, gossip precommits
  18. RoundStepPrecommitWait = RoundStepType(0x07) // Did receive any +2/3 precommits, start timeout
  19. RoundStepCommit = RoundStepType(0x08) // Entered commit state machine
  20. // NOTE: RoundStepNewHeight acts as RoundStepCommitWait.
  21. )
  22. // String returns a string
  23. func (rs RoundStepType) String() string {
  24. switch rs {
  25. case RoundStepNewHeight:
  26. return "RoundStepNewHeight"
  27. case RoundStepNewRound:
  28. return "RoundStepNewRound"
  29. case RoundStepPropose:
  30. return "RoundStepPropose"
  31. case RoundStepPrevote:
  32. return "RoundStepPrevote"
  33. case RoundStepPrevoteWait:
  34. return "RoundStepPrevoteWait"
  35. case RoundStepPrecommit:
  36. return "RoundStepPrecommit"
  37. case RoundStepPrecommitWait:
  38. return "RoundStepPrecommitWait"
  39. case RoundStepCommit:
  40. return "RoundStepCommit"
  41. default:
  42. return "RoundStepUnknown" // Cannot panic.
  43. }
  44. }
  45. //-----------------------------------------------------------------------------
  46. // RoundState defines the internal consensus state.
  47. // NOTE: Not thread safe. Should only be manipulated by functions downstream
  48. // of the cs.receiveRoutine
  49. type RoundState struct {
  50. Height int64 `json:"height"` // Height we are working on
  51. Round int `json:"round"`
  52. Step RoundStepType `json:"step"`
  53. StartTime time.Time `json:"start_time"`
  54. CommitTime time.Time `json:"commit_time"` // Subjective time when +2/3 precommits for Block at Round were found
  55. Validators *types.ValidatorSet `json:"validators"`
  56. Proposal *types.Proposal `json:"proposal"`
  57. ProposalBlock *types.Block `json:"proposal_block"`
  58. ProposalBlockParts *types.PartSet `json:"proposal_block_parts"`
  59. LockedRound int `json:"locked_round"`
  60. LockedBlock *types.Block `json:"locked_block"`
  61. LockedBlockParts *types.PartSet `json:"locked_block_parts"`
  62. ValidRound int `json:"valid_round"`
  63. ValidBlock *types.Block `json:"valid_block"`
  64. ValidBlockParts *types.PartSet `json:"valid_block_parts"`
  65. Votes *HeightVoteSet `json:"votes"`
  66. CommitRound int `json:"commit_round"` //
  67. LastCommit *types.VoteSet `json:"last_commit"` // Last precommits at Height-1
  68. LastValidators *types.ValidatorSet `json:"last_validators"`
  69. }
  70. // RoundStateEvent returns the H/R/S of the RoundState as an event.
  71. func (rs *RoundState) RoundStateEvent() types.EventDataRoundState {
  72. // XXX: copy the RoundState
  73. // if we want to avoid this, we may need synchronous events after all
  74. rs_ := *rs
  75. edrs := types.EventDataRoundState{
  76. Height: rs.Height,
  77. Round: rs.Round,
  78. Step: rs.Step.String(),
  79. RoundState: &rs_,
  80. }
  81. return edrs
  82. }
  83. // String returns a string
  84. func (rs *RoundState) String() string {
  85. return rs.StringIndented("")
  86. }
  87. // StringIndented returns a string
  88. func (rs *RoundState) StringIndented(indent string) string {
  89. return fmt.Sprintf(`RoundState{
  90. %s H:%v R:%v S:%v
  91. %s StartTime: %v
  92. %s CommitTime: %v
  93. %s Validators: %v
  94. %s Proposal: %v
  95. %s ProposalBlock: %v %v
  96. %s LockedRound: %v
  97. %s LockedBlock: %v %v
  98. %s ValidRound: %v
  99. %s ValidBlock: %v %v
  100. %s Votes: %v
  101. %s LastCommit: %v
  102. %s LastValidators:%v
  103. %s}`,
  104. indent, rs.Height, rs.Round, rs.Step,
  105. indent, rs.StartTime,
  106. indent, rs.CommitTime,
  107. indent, rs.Validators.StringIndented(indent+" "),
  108. indent, rs.Proposal,
  109. indent, rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort(),
  110. indent, rs.LockedRound,
  111. indent, rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort(),
  112. indent, rs.ValidRound,
  113. indent, rs.ValidBlockParts.StringShort(), rs.ValidBlock.StringShort(),
  114. indent, rs.Votes.StringIndented(indent+" "),
  115. indent, rs.LastCommit.StringShort(),
  116. indent, rs.LastValidators.StringIndented(indent+" "),
  117. indent)
  118. }
  119. // StringShort returns a string
  120. func (rs *RoundState) StringShort() string {
  121. return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
  122. rs.Height, rs.Round, rs.Step, rs.StartTime)
  123. }