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.

126 lines
4.0 KiB

  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. // It is Immutable when returned from ConsensusState.GetRoundState()
  48. // TODO: Actually, only the top pointer is copied,
  49. // so access to field pointers is still racey
  50. type RoundState struct {
  51. Height int // Height we are working on
  52. Round int
  53. Step RoundStepType
  54. StartTime time.Time
  55. CommitTime time.Time // Subjective time when +2/3 precommits for Block at Round were found
  56. Validators *types.ValidatorSet
  57. Proposal *types.Proposal
  58. ProposalBlock *types.Block
  59. ProposalBlockParts *types.PartSet
  60. LockedRound int
  61. LockedBlock *types.Block
  62. LockedBlockParts *types.PartSet
  63. Votes *HeightVoteSet
  64. CommitRound int //
  65. LastCommit *types.VoteSet // Last precommits at Height-1
  66. LastValidators *types.ValidatorSet
  67. }
  68. // RoundStateEvent returns the H/R/S of the RoundState as an event.
  69. func (rs *RoundState) RoundStateEvent() types.EventDataRoundState {
  70. edrs := types.EventDataRoundState{
  71. Height: rs.Height,
  72. Round: rs.Round,
  73. Step: rs.Step.String(),
  74. RoundState: rs,
  75. }
  76. return edrs
  77. }
  78. // String returns a string
  79. func (rs *RoundState) String() string {
  80. return rs.StringIndented("")
  81. }
  82. // StringIndented returns a string
  83. func (rs *RoundState) StringIndented(indent string) string {
  84. return fmt.Sprintf(`RoundState{
  85. %s H:%v R:%v S:%v
  86. %s StartTime: %v
  87. %s CommitTime: %v
  88. %s Validators: %v
  89. %s Proposal: %v
  90. %s ProposalBlock: %v %v
  91. %s LockedRound: %v
  92. %s LockedBlock: %v %v
  93. %s Votes: %v
  94. %s LastCommit: %v
  95. %s LastValidators: %v
  96. %s}`,
  97. indent, rs.Height, rs.Round, rs.Step,
  98. indent, rs.StartTime,
  99. indent, rs.CommitTime,
  100. indent, rs.Validators.StringIndented(indent+" "),
  101. indent, rs.Proposal,
  102. indent, rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort(),
  103. indent, rs.LockedRound,
  104. indent, rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort(),
  105. indent, rs.Votes.StringIndented(indent+" "),
  106. indent, rs.LastCommit.StringShort(),
  107. indent, rs.LastValidators.StringIndented(indent+" "),
  108. indent)
  109. }
  110. // StringShort returns a string
  111. func (rs *RoundState) StringShort() string {
  112. return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
  113. rs.Height, rs.Round, rs.Step, rs.StartTime)
  114. }