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.

131 lines
4.2 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. // NOTE: Not thread safe. Should only be manipulated by functions downstream
  51. // of the cs.receiveRoutine
  52. type RoundState struct {
  53. Height int64 // Height we are working on
  54. Round int
  55. Step RoundStepType
  56. StartTime time.Time
  57. CommitTime time.Time // Subjective time when +2/3 precommits for Block at Round were found
  58. Validators *types.ValidatorSet
  59. Proposal *types.Proposal
  60. ProposalBlock *types.Block
  61. ProposalBlockParts *types.PartSet
  62. LockedRound int
  63. LockedBlock *types.Block
  64. LockedBlockParts *types.PartSet
  65. Votes *HeightVoteSet
  66. CommitRound int //
  67. LastCommit *types.VoteSet // Last precommits at Height-1
  68. LastValidators *types.ValidatorSet
  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 Votes: %v
  99. %s LastCommit: %v
  100. %s LastValidators: %v
  101. %s}`,
  102. indent, rs.Height, rs.Round, rs.Step,
  103. indent, rs.StartTime,
  104. indent, rs.CommitTime,
  105. indent, rs.Validators.StringIndented(indent+" "),
  106. indent, rs.Proposal,
  107. indent, rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort(),
  108. indent, rs.LockedRound,
  109. indent, rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort(),
  110. indent, rs.Votes.StringIndented(indent+" "),
  111. indent, rs.LastCommit.StringShort(),
  112. indent, rs.LastValidators.StringIndented(indent+" "),
  113. indent)
  114. }
  115. // StringShort returns a string
  116. func (rs *RoundState) StringShort() string {
  117. return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
  118. rs.Height, rs.Round, rs.Step, rs.StartTime)
  119. }