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.

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