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.

138 lines
4.5 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. // 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. ValidRound int
  66. ValidBlock *types.Block
  67. ValidBlockParts *types.PartSet
  68. Votes *HeightVoteSet
  69. CommitRound int //
  70. LastCommit *types.VoteSet // Last precommits at Height-1
  71. LastValidators *types.ValidatorSet
  72. }
  73. // RoundStateEvent returns the H/R/S of the RoundState as an event.
  74. func (rs *RoundState) RoundStateEvent() types.EventDataRoundState {
  75. // XXX: copy the RoundState
  76. // if we want to avoid this, we may need synchronous events after all
  77. rs_ := *rs
  78. edrs := types.EventDataRoundState{
  79. Height: rs.Height,
  80. Round: rs.Round,
  81. Step: rs.Step.String(),
  82. RoundState: &rs_,
  83. }
  84. return edrs
  85. }
  86. // String returns a string
  87. func (rs *RoundState) String() string {
  88. return rs.StringIndented("")
  89. }
  90. // StringIndented returns a string
  91. func (rs *RoundState) StringIndented(indent string) string {
  92. return fmt.Sprintf(`RoundState{
  93. %s H:%v R:%v S:%v
  94. %s StartTime: %v
  95. %s CommitTime: %v
  96. %s Validators: %v
  97. %s Proposal: %v
  98. %s ProposalBlock: %v %v
  99. %s LockedRound: %v
  100. %s LockedBlock: %v %v
  101. %s ValidRound: %v
  102. %s ValidBlock: %v %v
  103. %s Votes: %v
  104. %s LastCommit: %v
  105. %s LastValidators:%v
  106. %s}`,
  107. indent, rs.Height, rs.Round, rs.Step,
  108. indent, rs.StartTime,
  109. indent, rs.CommitTime,
  110. indent, rs.Validators.StringIndented(indent+" "),
  111. indent, rs.Proposal,
  112. indent, rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort(),
  113. indent, rs.LockedRound,
  114. indent, rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort(),
  115. indent, rs.ValidRound,
  116. indent, rs.ValidBlockParts.StringShort(), rs.ValidBlock.StringShort(),
  117. indent, rs.Votes.StringIndented(indent+" "),
  118. indent, rs.LastCommit.StringShort(),
  119. indent, rs.LastValidators.StringIndented(indent+" "),
  120. indent)
  121. }
  122. // StringShort returns a string
  123. func (rs *RoundState) StringShort() string {
  124. return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
  125. rs.Height, rs.Round, rs.Step, rs.StartTime)
  126. }