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.

1042 lines
32 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*
  2. Consensus State Machine Overview:
  3. * Propose, Prevote, Precommit represent state machine stages. (aka RoundStep, or step).
  4. Each take a predetermined amount of time depending on the round number.
  5. * The Commit step can be entered by two means:
  6. 1. After the Precommit step, +2/3 Precommits were found
  7. 2. At any time, +2/3 Commits were found
  8. * Once in the Commit stage, two conditions must both be satisfied
  9. before proceeding to the next height NewHeight.
  10. * The Propose step of the next height does not begin until
  11. at least Delta duration *after* +2/3 Commits were found.
  12. The step stays at NewHeight until this timeout occurs before
  13. proceeding to Propose.
  14. +-------------------------------------+
  15. | |
  16. v |(Wait til CommitTime + Delta)
  17. +-----------+ +-----+-----+
  18. +----------> | Propose +--------------+ | NewHeight |
  19. | +-----------+ | +-----------+
  20. | | ^
  21. | | |
  22. | | |
  23. |(Else) v |
  24. +-----+-----+ +-----------+ |
  25. | Precommit | <------------------------+ Prevote | |
  26. +-----+-----+ +-----------+ |
  27. |(If +2/3 Precommits found) |
  28. | |
  29. | + (When +2/3 Commits found) |
  30. | | |
  31. v v |
  32. +------------------------------------------------------------------------------+
  33. | Commit | |
  34. | | |
  35. | +----------------+ * Save Block | |
  36. | |Get Block Parts |---> * Stage Block +--+ + |
  37. | +----------------+ * Broadcast Commit | * Setup New Height |
  38. | | * Move Commits set to |
  39. | +--> LastCommits to continue |
  40. | | collecting commits |
  41. | +-----------------+ | * Broadcast New State |
  42. | |Get +2/3 Commits |--> * Set CommitTime +--+ |
  43. | +-----------------+ |
  44. | |
  45. +------------------------------------------------------------------------------+
  46. */
  47. package consensus
  48. import (
  49. "bytes"
  50. "errors"
  51. "fmt"
  52. "math"
  53. "sync"
  54. "sync/atomic"
  55. "time"
  56. . "github.com/tendermint/tendermint/account"
  57. . "github.com/tendermint/tendermint/binary"
  58. . "github.com/tendermint/tendermint/block"
  59. . "github.com/tendermint/tendermint/common"
  60. . "github.com/tendermint/tendermint/config"
  61. . "github.com/tendermint/tendermint/consensus/types"
  62. "github.com/tendermint/tendermint/mempool"
  63. "github.com/tendermint/tendermint/state"
  64. )
  65. type RoundStep uint8
  66. type RoundActionType uint8
  67. const (
  68. RoundStepNewHeight = RoundStep(0x00) // Round0 for new height started, wait til CommitTime + Delta
  69. RoundStepNewRound = RoundStep(0x01) // Pseudostep, immediately goes to RoundStepPropose
  70. RoundStepPropose = RoundStep(0x10) // Did propose, gossip proposal
  71. RoundStepPrevote = RoundStep(0x11) // Did prevote, gossip prevotes
  72. RoundStepPrecommit = RoundStep(0x12) // Did precommit, gossip precommits
  73. RoundStepCommit = RoundStep(0x20) // Entered commit state machine
  74. RoundActionPropose = RoundActionType(0xA0) // Propose and goto RoundStepPropose
  75. RoundActionPrevote = RoundActionType(0xA1) // Prevote and goto RoundStepPrevote
  76. RoundActionPrecommit = RoundActionType(0xA2) // Precommit and goto RoundStepPrecommit
  77. RoundActionTryCommit = RoundActionType(0xC0) // Goto RoundStepCommit, or RoundStepPropose for next round.
  78. RoundActionCommit = RoundActionType(0xC1) // Goto RoundStepCommit upon +2/3 commits
  79. RoundActionTryFinalize = RoundActionType(0xC2) // Maybe goto RoundStepPropose for next round.
  80. roundDuration0 = 60 * time.Second // The first round is 60 seconds long.
  81. roundDurationDelta = 15 * time.Second // Each successive round lasts 15 seconds longer.
  82. roundDeadlinePrevote = float64(1.0 / 3.0) // When the prevote is due.
  83. roundDeadlinePrecommit = float64(2.0 / 3.0) // When the precommit vote is due.
  84. newHeightDelta = roundDuration0 / 3 // The time to wait between commitTime and startTime of next consensus rounds.
  85. )
  86. var (
  87. ErrInvalidProposalSignature = errors.New("Error invalid proposal signature")
  88. )
  89. type RoundAction struct {
  90. Height uint // The block height for which consensus is reaching for.
  91. Round uint // The round number at given height.
  92. Action RoundActionType // Action to perform.
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Immutable when returned from ConsensusState.GetRoundState()
  96. type RoundState struct {
  97. Height uint // Height we are working on
  98. Round uint
  99. Step RoundStep
  100. StartTime time.Time
  101. CommitTime time.Time // Time when +2/3 commits were found
  102. Validators *state.ValidatorSet
  103. Proposal *Proposal
  104. ProposalBlock *Block
  105. ProposalBlockParts *PartSet
  106. ProposalPOL *POL
  107. ProposalPOLParts *PartSet
  108. LockedBlock *Block
  109. LockedBlockParts *PartSet
  110. LockedPOL *POL // Rarely needed, so no LockedPOLParts.
  111. Prevotes *VoteSet
  112. Precommits *VoteSet
  113. Commits *VoteSet
  114. LastCommits *VoteSet
  115. PrivValidator *state.PrivValidator
  116. }
  117. func (rs *RoundState) String() string {
  118. return rs.StringWithIndent("")
  119. }
  120. func (rs *RoundState) StringWithIndent(indent string) string {
  121. return fmt.Sprintf(`RoundState{
  122. %s H:%v R:%v S:%v
  123. %s StartTime: %v
  124. %s CommitTime: %v
  125. %s Validators: %v
  126. %s Proposal: %v
  127. %s ProposalBlock: %v %v
  128. %s ProposalPOL: %v %v
  129. %s LockedBlock: %v %v
  130. %s LockedPOL: %v
  131. %s Prevotes: %v
  132. %s Precommits: %v
  133. %s Commits: %v
  134. %s LastCommits: %v
  135. %s}`,
  136. indent, rs.Height, rs.Round, rs.Step,
  137. indent, rs.StartTime,
  138. indent, rs.CommitTime,
  139. indent, rs.Validators.StringWithIndent(indent+" "),
  140. indent, rs.Proposal,
  141. indent, rs.ProposalBlockParts.Description(), rs.ProposalBlock.Description(),
  142. indent, rs.ProposalPOLParts.Description(), rs.ProposalPOL.Description(),
  143. indent, rs.LockedBlockParts.Description(), rs.LockedBlock.Description(),
  144. indent, rs.LockedPOL.Description(),
  145. indent, rs.Prevotes.StringWithIndent(indent+" "),
  146. indent, rs.Precommits.StringWithIndent(indent+" "),
  147. indent, rs.Commits.StringWithIndent(indent+" "),
  148. indent, rs.LastCommits.Description(),
  149. indent)
  150. }
  151. func (rs *RoundState) Description() string {
  152. return fmt.Sprintf(`RS{%v/%v/%X %v}`,
  153. rs.Height, rs.Round, rs.Step, rs.StartTime)
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Tracks consensus state across block heights and rounds.
  157. type ConsensusState struct {
  158. started uint32
  159. stopped uint32
  160. quit chan struct{}
  161. blockStore *BlockStore
  162. mempoolReactor *mempool.MempoolReactor
  163. runActionCh chan RoundAction
  164. newStepCh chan *RoundState
  165. mtx sync.Mutex
  166. RoundState
  167. state *state.State // State until height-1.
  168. stagedBlock *Block // Cache last staged block.
  169. stagedState *state.State // Cache result of staged block.
  170. }
  171. func NewConsensusState(state *state.State, blockStore *BlockStore, mempoolReactor *mempool.MempoolReactor) *ConsensusState {
  172. cs := &ConsensusState{
  173. quit: make(chan struct{}),
  174. blockStore: blockStore,
  175. mempoolReactor: mempoolReactor,
  176. runActionCh: make(chan RoundAction, 1),
  177. newStepCh: make(chan *RoundState, 1),
  178. }
  179. cs.updateToState(state)
  180. return cs
  181. }
  182. func (cs *ConsensusState) GetRoundState() *RoundState {
  183. cs.mtx.Lock()
  184. defer cs.mtx.Unlock()
  185. return cs.getRoundState()
  186. }
  187. func (cs *ConsensusState) getRoundState() *RoundState {
  188. rs := cs.RoundState // copy
  189. return &rs
  190. }
  191. func (cs *ConsensusState) NewStepCh() chan *RoundState {
  192. return cs.newStepCh
  193. }
  194. func (cs *ConsensusState) Start() {
  195. if atomic.CompareAndSwapUint32(&cs.started, 0, 1) {
  196. log.Info("Starting ConsensusState")
  197. go cs.stepTransitionRoutine()
  198. }
  199. }
  200. func (cs *ConsensusState) Stop() {
  201. if atomic.CompareAndSwapUint32(&cs.stopped, 0, 1) {
  202. log.Info("Stopping ConsensusState")
  203. close(cs.quit)
  204. }
  205. }
  206. func (cs *ConsensusState) IsStopped() bool {
  207. return atomic.LoadUint32(&cs.stopped) == 1
  208. }
  209. func (cs *ConsensusState) queueAction(ra RoundAction) {
  210. go func() {
  211. cs.runActionCh <- ra
  212. }()
  213. }
  214. // Source of all round state transitions (and votes).
  215. func (cs *ConsensusState) stepTransitionRoutine() {
  216. // For clarity, all state transitions that happen after some timeout are here.
  217. // Schedule the next action by pushing a RoundAction{} to cs.runActionCh.
  218. scheduleNextAction := func() {
  219. go func() {
  220. // NOTE: We can push directly to runActionCh because
  221. // we're running in a separate goroutine, which avoids deadlocks.
  222. rs := cs.getRoundState()
  223. round, roundStartTime, roundDuration, _, elapsedRatio := calcRoundInfo(rs.StartTime)
  224. log.Debug("Called scheduleNextAction. round:%v roundStartTime:%v elapsedRatio:%v", round, roundStartTime, elapsedRatio)
  225. switch rs.Step {
  226. case RoundStepNewHeight:
  227. // We should run RoundActionPropose when rs.StartTime passes.
  228. if elapsedRatio < 0 {
  229. // startTime is in the future.
  230. time.Sleep(time.Duration((-1.0 * elapsedRatio) * float64(roundDuration)))
  231. }
  232. cs.runActionCh <- RoundAction{rs.Height, rs.Round, RoundActionPropose}
  233. case RoundStepNewRound:
  234. // Pseudostep: Immediately goto propose.
  235. cs.runActionCh <- RoundAction{rs.Height, rs.Round, RoundActionPropose}
  236. case RoundStepPropose:
  237. // Wake up when it's time to vote.
  238. time.Sleep(time.Duration((roundDeadlinePrevote - elapsedRatio) * float64(roundDuration)))
  239. cs.runActionCh <- RoundAction{rs.Height, rs.Round, RoundActionPrevote}
  240. case RoundStepPrevote:
  241. // Wake up when it's time to precommit.
  242. time.Sleep(time.Duration((roundDeadlinePrecommit - elapsedRatio) * float64(roundDuration)))
  243. cs.runActionCh <- RoundAction{rs.Height, rs.Round, RoundActionPrecommit}
  244. case RoundStepPrecommit:
  245. // Wake up when the round is over.
  246. time.Sleep(time.Duration((1.0 - elapsedRatio) * float64(roundDuration)))
  247. cs.runActionCh <- RoundAction{rs.Height, rs.Round, RoundActionTryCommit}
  248. case RoundStepCommit:
  249. // There's nothing to scheudle, we're waiting for
  250. // ProposalBlockParts.IsComplete() &&
  251. // Commits.HasTwoThirdsMajority()
  252. panic("The next action from RoundStepCommit is not scheduled by time")
  253. default:
  254. panic("Should not happen")
  255. }
  256. }()
  257. }
  258. scheduleNextAction()
  259. // NOTE: All ConsensusState.RunAction*() calls come from here.
  260. // Since only one routine calls them, it is safe to assume that
  261. // the RoundState Height/Round/Step won't change concurrently.
  262. // However, other fields like Proposal could change concurrent
  263. // due to gossip routines.
  264. ACTION_LOOP:
  265. for {
  266. var roundAction RoundAction
  267. select {
  268. case roundAction = <-cs.runActionCh:
  269. case <-cs.quit:
  270. return
  271. }
  272. height, round, action := roundAction.Height, roundAction.Round, roundAction.Action
  273. rs := cs.GetRoundState()
  274. log.Info("Running round action A:%X %v", action, rs.Description())
  275. // Continue if action is not relevant
  276. if height != rs.Height {
  277. continue
  278. }
  279. // If action <= RoundActionPrecommit, the round must match too.
  280. if action <= RoundActionPrecommit && round != rs.Round {
  281. continue
  282. }
  283. // Run action
  284. switch action {
  285. case RoundActionPropose:
  286. if rs.Step != RoundStepNewHeight && rs.Step != RoundStepNewRound {
  287. continue ACTION_LOOP
  288. }
  289. cs.RunActionPropose(rs.Height, rs.Round)
  290. scheduleNextAction()
  291. continue ACTION_LOOP
  292. case RoundActionPrevote:
  293. if rs.Step >= RoundStepPrevote {
  294. continue ACTION_LOOP
  295. }
  296. cs.RunActionPrevote(rs.Height, rs.Round)
  297. scheduleNextAction()
  298. continue ACTION_LOOP
  299. case RoundActionPrecommit:
  300. if rs.Step >= RoundStepPrecommit {
  301. continue ACTION_LOOP
  302. }
  303. cs.RunActionPrecommit(rs.Height, rs.Round)
  304. scheduleNextAction()
  305. continue ACTION_LOOP
  306. case RoundActionTryCommit:
  307. if rs.Step >= RoundStepCommit {
  308. continue ACTION_LOOP
  309. }
  310. if rs.Precommits.HasTwoThirdsMajority() {
  311. // Enter RoundStepCommit and commit.
  312. cs.RunActionCommit(rs.Height)
  313. continue ACTION_LOOP
  314. } else {
  315. // Could not commit, move onto next round.
  316. cs.SetupNewRound(rs.Height, rs.Round+1)
  317. // cs.Step is now at RoundStepNewRound
  318. scheduleNextAction()
  319. continue ACTION_LOOP
  320. }
  321. case RoundActionCommit:
  322. if rs.Step >= RoundStepCommit {
  323. continue ACTION_LOOP
  324. }
  325. // Enter RoundStepCommit and commit.
  326. cs.RunActionCommit(rs.Height)
  327. continue ACTION_LOOP
  328. case RoundActionTryFinalize:
  329. if cs.TryFinalizeCommit(rs.Height) {
  330. // Now at new height
  331. // cs.Step is at RoundStepNewHeight or RoundStepNewRound.
  332. scheduleNextAction()
  333. continue ACTION_LOOP
  334. } else {
  335. // do not schedule next action.
  336. continue ACTION_LOOP
  337. }
  338. default:
  339. panic("Unknown action")
  340. }
  341. // For clarity, ensure that all switch cases call "continue"
  342. panic("Should not happen.")
  343. }
  344. }
  345. // Updates ConsensusState and increments height to match that of state.
  346. // If calculated round is greater than 0 (based on BlockTime or calculated StartTime)
  347. // then also sets up the appropriate round, and cs.Step becomes RoundStepNewRound.
  348. // Otherwise the round is 0 and cs.Step becomes RoundStepNewHeight.
  349. func (cs *ConsensusState) updateToState(state *state.State) {
  350. // Sanity check state.
  351. if cs.Height > 0 && cs.Height != state.LastBlockHeight {
  352. Panicf("updateToState() expected state height of %v but found %v",
  353. cs.Height, state.LastBlockHeight)
  354. }
  355. // Reset fields based on state.
  356. validators := state.BondedValidators
  357. height := state.LastBlockHeight + 1 // next desired block height
  358. cs.Height = height
  359. cs.Round = 0
  360. cs.Step = RoundStepNewHeight
  361. if cs.CommitTime.IsZero() {
  362. cs.StartTime = state.LastBlockTime.Add(newHeightDelta)
  363. } else {
  364. cs.StartTime = cs.CommitTime.Add(newHeightDelta)
  365. }
  366. cs.CommitTime = time.Time{}
  367. cs.Validators = validators
  368. cs.Proposal = nil
  369. cs.ProposalBlock = nil
  370. cs.ProposalBlockParts = nil
  371. cs.ProposalPOL = nil
  372. cs.ProposalPOLParts = nil
  373. cs.LockedBlock = nil
  374. cs.LockedBlockParts = nil
  375. cs.LockedPOL = nil
  376. cs.Prevotes = NewVoteSet(height, 0, VoteTypePrevote, validators)
  377. cs.Precommits = NewVoteSet(height, 0, VoteTypePrecommit, validators)
  378. cs.LastCommits = cs.Commits
  379. cs.Commits = NewVoteSet(height, 0, VoteTypeCommit, validators)
  380. cs.state = state
  381. cs.stagedBlock = nil
  382. cs.stagedState = nil
  383. // Update the round if we need to.
  384. round := calcRound(cs.StartTime)
  385. if round > 0 {
  386. cs.setupNewRound(round)
  387. }
  388. // If we've timed out, then send rebond tx.
  389. if cs.PrivValidator != nil && cs.state.UnbondingValidators.HasAddress(cs.PrivValidator.Address) {
  390. rebondTx := &RebondTx{
  391. Address: cs.PrivValidator.Address,
  392. Height: cs.Height + 1,
  393. }
  394. rebondTx.Signature = cs.PrivValidator.SignRebondTx(rebondTx)
  395. cs.mempoolReactor.BroadcastTx(rebondTx)
  396. }
  397. }
  398. // After the call cs.Step becomes RoundStepNewRound.
  399. func (cs *ConsensusState) setupNewRound(round uint) {
  400. // Sanity check
  401. if round == 0 {
  402. panic("setupNewRound() should never be called for round 0")
  403. }
  404. // Increment all the way to round.
  405. validators := cs.Validators.Copy()
  406. validators.IncrementAccum(round - cs.Round)
  407. cs.Round = round
  408. cs.Step = RoundStepNewRound
  409. cs.Validators = validators
  410. cs.Proposal = nil
  411. cs.ProposalBlock = nil
  412. cs.ProposalBlockParts = nil
  413. cs.ProposalPOL = nil
  414. cs.ProposalPOLParts = nil
  415. cs.Prevotes = NewVoteSet(cs.Height, round, VoteTypePrevote, validators)
  416. cs.Prevotes.AddFromCommits(cs.Commits)
  417. cs.Precommits = NewVoteSet(cs.Height, round, VoteTypePrecommit, validators)
  418. cs.Precommits.AddFromCommits(cs.Commits)
  419. }
  420. func (cs *ConsensusState) SetPrivValidator(priv *state.PrivValidator) {
  421. cs.mtx.Lock()
  422. defer cs.mtx.Unlock()
  423. cs.PrivValidator = priv
  424. }
  425. //-----------------------------------------------------------------------------
  426. // Set up the round to desired round and set step to RoundStepNewRound
  427. func (cs *ConsensusState) SetupNewRound(height uint, desiredRound uint) bool {
  428. cs.mtx.Lock()
  429. defer cs.mtx.Unlock()
  430. if cs.Height != height {
  431. return false
  432. }
  433. if desiredRound <= cs.Round {
  434. return false
  435. }
  436. cs.setupNewRound(desiredRound)
  437. // c.Step is now RoundStepNewRound
  438. cs.newStepCh <- cs.getRoundState()
  439. return true
  440. }
  441. func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
  442. cs.mtx.Lock()
  443. defer cs.mtx.Unlock()
  444. if cs.Height != height || cs.Round != round {
  445. return
  446. }
  447. defer func() {
  448. cs.Step = RoundStepPropose
  449. cs.newStepCh <- cs.getRoundState()
  450. }()
  451. // Nothing to do if it's not our turn.
  452. if cs.PrivValidator == nil || !bytes.Equal(cs.Validators.Proposer().Address, cs.PrivValidator.Address) {
  453. return
  454. }
  455. var block *Block
  456. var blockParts *PartSet
  457. var pol *POL
  458. var polParts *PartSet
  459. // Decide on block and POL
  460. if cs.LockedBlock != nil {
  461. // If we're locked onto a block, just choose that.
  462. block = cs.LockedBlock
  463. blockParts = cs.LockedBlockParts
  464. pol = cs.LockedPOL
  465. } else {
  466. // Otherwise we should create a new proposal.
  467. var validation *Validation
  468. if cs.Height == 1 {
  469. // We're creating a proposal for the first block.
  470. // The validation is empty.
  471. validation = &Validation{}
  472. } else {
  473. // We need to create a proposal.
  474. // If we don't have enough commits from the last height,
  475. // we can't do anything.
  476. if !cs.LastCommits.HasTwoThirdsMajority() {
  477. return
  478. } else {
  479. validation = cs.LastCommits.MakeValidation()
  480. }
  481. }
  482. txs := cs.mempoolReactor.Mempool.GetProposalTxs()
  483. block = &Block{
  484. Header: &Header{
  485. Network: Config.Network,
  486. Height: cs.Height,
  487. Time: time.Now(),
  488. Fees: 0, // TODO fees
  489. LastBlockHash: cs.state.LastBlockHash,
  490. LastBlockParts: cs.state.LastBlockParts,
  491. StateHash: nil, // Will set afterwards.
  492. },
  493. Validation: validation,
  494. Data: &Data{
  495. Txs: txs,
  496. },
  497. }
  498. // Set the block.Header.StateHash.
  499. // TODO: we could cache the resulting state to cs.stagedState.
  500. cs.state.Copy().AppendBlock(block, PartSetHeader{}, false)
  501. blockParts = NewPartSetFromData(BinaryBytes(block))
  502. pol = cs.LockedPOL // If exists, is a PoUnlock.
  503. }
  504. if pol != nil {
  505. polParts = NewPartSetFromData(BinaryBytes(pol))
  506. }
  507. // Make proposal
  508. proposal := NewProposal(cs.Height, cs.Round, blockParts.Header(), polParts.Header())
  509. proposal.Signature = cs.PrivValidator.SignProposal(proposal)
  510. // Set fields
  511. cs.Proposal = proposal
  512. cs.ProposalBlock = block
  513. cs.ProposalBlockParts = blockParts
  514. cs.ProposalPOL = pol
  515. cs.ProposalPOLParts = polParts
  516. }
  517. // Prevote for LockedBlock if we're locked, or ProposealBlock if valid.
  518. // Otherwise vote nil.
  519. func (cs *ConsensusState) RunActionPrevote(height uint, round uint) {
  520. cs.mtx.Lock()
  521. defer cs.mtx.Unlock()
  522. if cs.Height != height || cs.Round != round {
  523. Panicf("RunActionPrevote(%v/%v), expected %v/%v", height, round, cs.Height, cs.Round)
  524. }
  525. defer func() {
  526. cs.Step = RoundStepPrevote
  527. cs.newStepCh <- cs.getRoundState()
  528. }()
  529. // If a block is locked, prevote that.
  530. if cs.LockedBlock != nil {
  531. cs.signAddVote(VoteTypePrevote, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header())
  532. return
  533. }
  534. // If ProposalBlock is nil, prevote nil.
  535. if cs.ProposalBlock == nil {
  536. log.Warning("ProposalBlock is nil")
  537. cs.signAddVote(VoteTypePrevote, nil, PartSetHeader{})
  538. return
  539. }
  540. // Try staging cs.ProposalBlock
  541. err := cs.stageBlock(cs.ProposalBlock, cs.ProposalBlockParts)
  542. if err != nil {
  543. // ProposalBlock is invalid, prevote nil.
  544. log.Warning("ProposalBlock is invalid: %v", err)
  545. cs.signAddVote(VoteTypePrevote, nil, PartSetHeader{})
  546. return
  547. }
  548. // Prevote cs.ProposalBlock
  549. cs.signAddVote(VoteTypePrevote, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header())
  550. return
  551. }
  552. // Lock & Precommit the ProposalBlock if we have enough prevotes for it,
  553. // or unlock an existing lock if +2/3 of prevotes were nil.
  554. func (cs *ConsensusState) RunActionPrecommit(height uint, round uint) {
  555. cs.mtx.Lock()
  556. defer cs.mtx.Unlock()
  557. if cs.Height != height || cs.Round != round {
  558. Panicf("RunActionPrecommit(%v/%v), expected %v/%v", height, round, cs.Height, cs.Round)
  559. }
  560. defer func() {
  561. cs.Step = RoundStepPrecommit
  562. cs.newStepCh <- cs.getRoundState()
  563. }()
  564. hash, partsHeader, ok := cs.Prevotes.TwoThirdsMajority()
  565. if !ok {
  566. // If we don't have two thirds of prevotes,
  567. // don't do anything at all.
  568. return
  569. }
  570. // Remember this POL. (hash may be nil)
  571. cs.LockedPOL = cs.Prevotes.MakePOL()
  572. // If +2/3 prevoted nil. Just unlock.
  573. if len(hash) == 0 {
  574. cs.LockedBlock = nil
  575. cs.LockedBlockParts = nil
  576. return
  577. }
  578. // If +2/3 prevoted for already locked block, precommit it.
  579. if cs.LockedBlock.HashesTo(hash) {
  580. cs.signAddVote(VoteTypePrecommit, hash, partsHeader)
  581. return
  582. }
  583. // If +2/3 prevoted for cs.ProposalBlock, lock it and precommit it.
  584. if cs.ProposalBlock.HashesTo(hash) {
  585. // Validate the block.
  586. if err := cs.stageBlock(cs.ProposalBlock, cs.ProposalBlockParts); err != nil {
  587. // Prevent zombies.
  588. log.Warning("+2/3 prevoted for an invalid block: %v", err)
  589. return
  590. }
  591. cs.LockedBlock = cs.ProposalBlock
  592. cs.LockedBlockParts = cs.ProposalBlockParts
  593. cs.signAddVote(VoteTypePrecommit, hash, partsHeader)
  594. return
  595. }
  596. // We don't have the block that validators prevoted.
  597. // Unlock if we're locked.
  598. cs.LockedBlock = nil
  599. cs.LockedBlockParts = nil
  600. return
  601. }
  602. // Enter commit step. See the diagram for details.
  603. // There are two ways to enter this step:
  604. // * After the Precommit step with +2/3 precommits, or,
  605. // * Upon +2/3 commits regardless of current step
  606. // Either way this action is run at most once per round.
  607. func (cs *ConsensusState) RunActionCommit(height uint) {
  608. cs.mtx.Lock()
  609. defer cs.mtx.Unlock()
  610. if cs.Height != height {
  611. Panicf("RunActionCommit(%v), expected %v", height, cs.Height)
  612. }
  613. defer func() {
  614. cs.Step = RoundStepCommit
  615. cs.newStepCh <- cs.getRoundState()
  616. }()
  617. // Sanity check.
  618. // There are two ways to enter:
  619. // 1. +2/3 precommits at the end of RoundStepPrecommit
  620. // 2. +2/3 commits at any time
  621. hash, partsHeader, ok := cs.Precommits.TwoThirdsMajority()
  622. if !ok {
  623. hash, partsHeader, ok = cs.Commits.TwoThirdsMajority()
  624. if !ok {
  625. panic("RunActionCommit() expects +2/3 precommits or commits")
  626. }
  627. }
  628. // Clear the Locked* fields and use cs.Proposed*
  629. if cs.LockedBlock.HashesTo(hash) {
  630. cs.ProposalBlock = cs.LockedBlock
  631. cs.ProposalBlockParts = cs.LockedBlockParts
  632. cs.LockedBlock = nil
  633. cs.LockedBlockParts = nil
  634. cs.LockedPOL = nil
  635. }
  636. // If we don't have the block being committed, set up to get it.
  637. if !cs.ProposalBlock.HashesTo(hash) {
  638. if !cs.ProposalBlockParts.HasHeader(partsHeader) {
  639. // We're getting the wrong block.
  640. // Set up ProposalBlockParts and keep waiting.
  641. cs.ProposalBlock = nil
  642. cs.ProposalBlockParts = NewPartSetFromHeader(partsHeader)
  643. } else {
  644. // We just need to keep waiting.
  645. }
  646. } else {
  647. // We have the block, so save/stage/sign-commit-vote.
  648. cs.saveCommitVoteBlock(cs.ProposalBlock, cs.ProposalBlockParts)
  649. }
  650. // If we have the block AND +2/3 commits, queue RoundActionTryFinalize.
  651. // Round will immediately become finalized.
  652. if cs.ProposalBlock.HashesTo(hash) && cs.Commits.HasTwoThirdsMajority() {
  653. cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionTryFinalize})
  654. }
  655. }
  656. // Returns true if Finalize happened, which increments height && sets
  657. // the step to RoundStepNewHeight (or RoundStepNewRound, but probably not).
  658. func (cs *ConsensusState) TryFinalizeCommit(height uint) bool {
  659. cs.mtx.Lock()
  660. defer cs.mtx.Unlock()
  661. if cs.Height != height {
  662. Panicf("TryFinalizeCommit(%v), expected %v", height, cs.Height)
  663. }
  664. if cs.Step == RoundStepCommit &&
  665. cs.Commits.HasTwoThirdsMajority() &&
  666. cs.ProposalBlockParts.IsComplete() {
  667. // Sanity check
  668. if cs.ProposalBlock == nil {
  669. Panicf("Expected ProposalBlock to exist")
  670. }
  671. hash, header, _ := cs.Commits.TwoThirdsMajority()
  672. if !cs.ProposalBlock.HashesTo(hash) {
  673. Panicf("Expected ProposalBlock to hash to commit hash")
  674. }
  675. if !cs.ProposalBlockParts.HasHeader(header) {
  676. Panicf("Expected ProposalBlockParts header to be commit header")
  677. }
  678. err := cs.stageBlock(cs.ProposalBlock, cs.ProposalBlockParts)
  679. if err == nil {
  680. log.Debug("Finalizing commit of block: %v", cs.ProposalBlock)
  681. // Increment height.
  682. cs.updateToState(cs.stagedState)
  683. // cs.Step is now RoundStepNewHeight or RoundStepNewRound
  684. cs.newStepCh <- cs.getRoundState()
  685. return true
  686. } else {
  687. // Prevent zombies.
  688. // TODO: Does this ever happen?
  689. Panicf("+2/3 committed an invalid block: %v", err)
  690. }
  691. }
  692. return false
  693. }
  694. //-----------------------------------------------------------------------------
  695. func (cs *ConsensusState) SetProposal(proposal *Proposal) error {
  696. cs.mtx.Lock()
  697. defer cs.mtx.Unlock()
  698. // Already have one
  699. if cs.Proposal != nil {
  700. return nil
  701. }
  702. // Does not apply
  703. if proposal.Height != cs.Height || proposal.Round != cs.Round {
  704. return nil
  705. }
  706. // We don't care about the proposal if we're already in RoundStepCommit.
  707. if cs.Step == RoundStepCommit {
  708. return nil
  709. }
  710. // Verify signature
  711. if !cs.Validators.Proposer().PubKey.VerifyBytes(SignBytes(proposal), proposal.Signature) {
  712. return ErrInvalidProposalSignature
  713. }
  714. cs.Proposal = proposal
  715. cs.ProposalBlockParts = NewPartSetFromHeader(proposal.BlockParts)
  716. cs.ProposalPOLParts = NewPartSetFromHeader(proposal.POLParts)
  717. return nil
  718. }
  719. // NOTE: block is not necessarily valid.
  720. // NOTE: This function may increment the height.
  721. func (cs *ConsensusState) AddProposalBlockPart(height uint, round uint, part *Part) (added bool, err error) {
  722. cs.mtx.Lock()
  723. defer cs.mtx.Unlock()
  724. // Blocks might be reused, so round mismatch is OK
  725. if cs.Height != height {
  726. return false, nil
  727. }
  728. // We're not expecting a block part.
  729. if cs.ProposalBlockParts == nil {
  730. return false, nil // TODO: bad peer? Return error?
  731. }
  732. added, err = cs.ProposalBlockParts.AddPart(part)
  733. if err != nil {
  734. return added, err
  735. }
  736. if added && cs.ProposalBlockParts.IsComplete() {
  737. var n int64
  738. var err error
  739. cs.ProposalBlock = ReadBinary(&Block{}, cs.ProposalBlockParts.GetReader(), &n, &err).(*Block)
  740. // If we're already in the commit step, try to finalize round.
  741. if cs.Step == RoundStepCommit {
  742. cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionTryFinalize})
  743. }
  744. // XXX If POL is valid, consider unlocking.
  745. return true, err
  746. }
  747. return true, nil
  748. }
  749. // NOTE: POL is not necessarily valid.
  750. func (cs *ConsensusState) AddProposalPOLPart(height uint, round uint, part *Part) (added bool, err error) {
  751. cs.mtx.Lock()
  752. defer cs.mtx.Unlock()
  753. if cs.Height != height || cs.Round != round {
  754. return false, nil
  755. }
  756. // We're not expecting a POL part.
  757. if cs.ProposalPOLParts == nil {
  758. return false, nil // TODO: bad peer? Return error?
  759. }
  760. added, err = cs.ProposalPOLParts.AddPart(part)
  761. if err != nil {
  762. return added, err
  763. }
  764. if added && cs.ProposalPOLParts.IsComplete() {
  765. var n int64
  766. var err error
  767. cs.ProposalPOL = ReadBinary(&POL{}, cs.ProposalPOLParts.GetReader(), &n, &err).(*POL)
  768. return true, err
  769. }
  770. return true, nil
  771. }
  772. func (cs *ConsensusState) AddVote(address []byte, vote *Vote) (added bool, index uint, err error) {
  773. cs.mtx.Lock()
  774. defer cs.mtx.Unlock()
  775. return cs.addVote(address, vote)
  776. }
  777. // TODO: Maybe move this out of here?
  778. func (cs *ConsensusState) LoadHeaderValidation(height uint) (*Header, *Validation) {
  779. meta := cs.blockStore.LoadBlockMeta(height)
  780. if meta == nil {
  781. return nil, nil
  782. }
  783. validation := cs.blockStore.LoadBlockValidation(height)
  784. return meta.Header, validation
  785. }
  786. //-----------------------------------------------------------------------------
  787. func (cs *ConsensusState) addVote(address []byte, vote *Vote) (added bool, index uint, err error) {
  788. switch vote.Type {
  789. case VoteTypePrevote:
  790. // Prevotes checks for height+round match.
  791. return cs.Prevotes.Add(address, vote)
  792. case VoteTypePrecommit:
  793. // Precommits checks for height+round match.
  794. return cs.Precommits.Add(address, vote)
  795. case VoteTypeCommit:
  796. if vote.Height == cs.Height {
  797. // No need to check if vote.Round < cs.Round ...
  798. // Prevotes && Precommits already checks that.
  799. cs.Prevotes.Add(address, vote)
  800. cs.Precommits.Add(address, vote)
  801. added, index, err = cs.Commits.Add(address, vote)
  802. if added && cs.Commits.HasTwoThirdsMajority() && cs.CommitTime.IsZero() {
  803. cs.CommitTime = time.Now()
  804. log.Debug("Set CommitTime to %v", cs.CommitTime)
  805. if cs.Step < RoundStepCommit {
  806. cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionCommit})
  807. } else {
  808. cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionTryFinalize})
  809. }
  810. }
  811. return added, index, err
  812. }
  813. if vote.Height+1 == cs.Height {
  814. return cs.LastCommits.Add(address, vote)
  815. }
  816. return false, 0, nil
  817. default:
  818. panic("Unknown vote type")
  819. }
  820. }
  821. func (cs *ConsensusState) stageBlock(block *Block, blockParts *PartSet) error {
  822. if block == nil {
  823. panic("Cannot stage nil block")
  824. }
  825. // Already staged?
  826. if cs.stagedBlock == block {
  827. return nil
  828. }
  829. // Create a copy of the state for staging
  830. stateCopy := cs.state.Copy()
  831. // Commit block onto the copied state.
  832. // NOTE: Basic validation is done in state.AppendBlock().
  833. err := stateCopy.AppendBlock(block, blockParts.Header(), true)
  834. if err != nil {
  835. return err
  836. } else {
  837. cs.stagedBlock = block
  838. cs.stagedState = stateCopy
  839. return nil
  840. }
  841. }
  842. func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header PartSetHeader) *Vote {
  843. if cs.PrivValidator == nil || !cs.Validators.HasAddress(cs.PrivValidator.Address) {
  844. return nil
  845. }
  846. vote := &Vote{
  847. Height: cs.Height,
  848. Round: cs.Round,
  849. Type: type_,
  850. BlockHash: hash,
  851. BlockParts: header,
  852. }
  853. vote.Signature = cs.PrivValidator.SignVote(vote)
  854. cs.addVote(cs.PrivValidator.Address, vote)
  855. return vote
  856. }
  857. func (cs *ConsensusState) saveCommitVoteBlock(block *Block, blockParts *PartSet) {
  858. // The proposal must be valid.
  859. if err := cs.stageBlock(block, blockParts); err != nil {
  860. // Prevent zombies.
  861. log.Warning("+2/3 precommitted an invalid block: %v", err)
  862. return
  863. }
  864. // Save to blockStore
  865. cs.blockStore.SaveBlock(block, blockParts)
  866. // Save the state
  867. cs.stagedState.Save()
  868. // Update mempool.
  869. cs.mempoolReactor.Mempool.ResetForBlockAndState(block, cs.stagedState)
  870. cs.signAddVote(VoteTypeCommit, block.Hash(), blockParts.Header())
  871. }
  872. //-----------------------------------------------------------------------------
  873. // total duration of given round
  874. func calcRoundDuration(round uint) time.Duration {
  875. return roundDuration0 + roundDurationDelta*time.Duration(round)
  876. }
  877. // startTime is when round zero started.
  878. func calcRoundStartTime(round uint, startTime time.Time) time.Time {
  879. return startTime.Add(roundDuration0*time.Duration(round) +
  880. roundDurationDelta*(time.Duration((int64(round)*int64(round)-int64(round))/2)))
  881. }
  882. // calculates the current round given startTime of round zero.
  883. // NOTE: round is zero if startTime is in the future.
  884. func calcRound(startTime time.Time) uint {
  885. now := time.Now()
  886. if now.Before(startTime) {
  887. return 0
  888. }
  889. // Start + D_0 * R + D_delta * (R^2 - R)/2 <= Now; find largest integer R.
  890. // D_delta * R^2 + (2D_0 - D_delta) * R + 2(Start - Now) <= 0.
  891. // AR^2 + BR + C <= 0; A = D_delta, B = (2_D0 - D_delta), C = 2(Start - Now).
  892. // R = Floor((-B + Sqrt(B^2 - 4AC))/2A)
  893. A := float64(roundDurationDelta)
  894. B := 2.0*float64(roundDuration0) - float64(roundDurationDelta)
  895. C := 2.0 * float64(startTime.Sub(now))
  896. R := math.Floor((-B + math.Sqrt(B*B-4.0*A*C)) / (2 * A))
  897. if math.IsNaN(R) {
  898. panic("Could not calc round, should not happen")
  899. }
  900. if R > math.MaxInt32 {
  901. Panicf("Could not calc round, round overflow: %v", R)
  902. }
  903. if R < 0 {
  904. return 0
  905. }
  906. return uint(R)
  907. }
  908. // convenience
  909. // NOTE: elapsedRatio can be negative if startTime is in the future.
  910. func calcRoundInfo(startTime time.Time) (round uint, roundStartTime time.Time, roundDuration time.Duration,
  911. roundElapsed time.Duration, elapsedRatio float64) {
  912. round = calcRound(startTime)
  913. roundStartTime = calcRoundStartTime(round, startTime)
  914. roundDuration = calcRoundDuration(round)
  915. roundElapsed = time.Now().Sub(roundStartTime)
  916. elapsedRatio = float64(roundElapsed) / float64(roundDuration)
  917. return
  918. }