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.

134 lines
4.3 KiB

7 years ago
7 years ago
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. // 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. Evidence types.Evidences
  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. rs_ := *rs
  76. edrs := types.EventDataRoundState{
  77. Height: rs.Height,
  78. Round: rs.Round,
  79. Step: rs.Step.String(),
  80. RoundState: &rs_,
  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 Votes: %v
  100. %s LastCommit: %v
  101. %s LastValidators:%v
  102. %s Evidence: %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.Votes.StringIndented(indent+" "),
  113. indent, rs.LastCommit.StringShort(),
  114. indent, rs.LastValidators.StringIndented(indent+" "),
  115. indent, rs.Evidence.String(),
  116. indent)
  117. }
  118. // StringShort returns a string
  119. func (rs *RoundState) StringShort() string {
  120. return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
  121. rs.Height, rs.Round, rs.Step, rs.StartTime)
  122. }