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.

1511 lines
52 KiB

10 years ago
7 years ago
8 years ago
7 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
9 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
10 years ago
8 years ago
8 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
8 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
9 years ago
8 years ago
10 years ago
8 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
8 years ago
9 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
  1. package consensus
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "path/filepath"
  7. "reflect"
  8. "runtime/debug"
  9. "sync"
  10. "time"
  11. fail "github.com/ebuchman/fail-test"
  12. wire "github.com/tendermint/go-wire"
  13. cmn "github.com/tendermint/tmlibs/common"
  14. "github.com/tendermint/tmlibs/log"
  15. cfg "github.com/tendermint/tendermint/config"
  16. cstypes "github.com/tendermint/tendermint/consensus/types"
  17. "github.com/tendermint/tendermint/proxy"
  18. sm "github.com/tendermint/tendermint/state"
  19. "github.com/tendermint/tendermint/types"
  20. )
  21. //-----------------------------------------------------------------------------
  22. // Config
  23. const (
  24. proposalHeartbeatIntervalSeconds = 2
  25. )
  26. //-----------------------------------------------------------------------------
  27. // Errors
  28. var (
  29. ErrInvalidProposalSignature = errors.New("Error invalid proposal signature")
  30. ErrInvalidProposalPOLRound = errors.New("Error invalid proposal POL round")
  31. ErrAddingVote = errors.New("Error adding vote")
  32. ErrVoteHeightMismatch = errors.New("Error vote height mismatch")
  33. )
  34. //-----------------------------------------------------------------------------
  35. var (
  36. msgQueueSize = 1000
  37. )
  38. // msgs from the reactor which may update the state
  39. type msgInfo struct {
  40. Msg ConsensusMessage `json:"msg"`
  41. PeerKey string `json:"peer_key"`
  42. }
  43. // internally generated messages which may update the state
  44. type timeoutInfo struct {
  45. Duration time.Duration `json:"duration"`
  46. Height int `json:"height"`
  47. Round int `json:"round"`
  48. Step cstypes.RoundStepType `json:"step"`
  49. }
  50. func (ti *timeoutInfo) String() string {
  51. return fmt.Sprintf("%v ; %d/%d %v", ti.Duration, ti.Height, ti.Round, ti.Step)
  52. }
  53. // ConsensusState handles execution of the consensus algorithm.
  54. // It processes votes and proposals, and upon reaching agreement,
  55. // commits blocks to the chain and executes them against the application.
  56. // The internal state machine receives input from peers, the internal validator, and from a timer.
  57. type ConsensusState struct {
  58. cmn.BaseService
  59. // config details
  60. config *cfg.ConsensusConfig
  61. privValidator types.PrivValidator // for signing votes
  62. // services for creating and executing blocks
  63. proxyAppConn proxy.AppConnConsensus
  64. blockStore types.BlockStore
  65. mempool types.Mempool
  66. // internal state
  67. mtx sync.Mutex
  68. cstypes.RoundState
  69. state *sm.State // State until height-1.
  70. // state changes may be triggered by msgs from peers,
  71. // msgs from ourself, or by timeouts
  72. peerMsgQueue chan msgInfo
  73. internalMsgQueue chan msgInfo
  74. timeoutTicker TimeoutTicker
  75. // we use PubSub to trigger msg broadcasts in the reactor,
  76. // and to notify external subscribers, eg. through a websocket
  77. evsw types.EventSwitch
  78. // a Write-Ahead Log ensures we can recover from any kind of crash
  79. // and helps us avoid signing conflicting votes
  80. wal *WAL
  81. replayMode bool // so we don't log signing errors during replay
  82. doWALCatchup bool // determines if we even try to do the catchup
  83. // for tests where we want to limit the number of transitions the state makes
  84. nSteps int
  85. // some functions can be overwritten for testing
  86. decideProposal func(height, round int)
  87. doPrevote func(height, round int)
  88. setProposal func(proposal *types.Proposal) error
  89. // closed when we finish shutting down
  90. done chan struct{}
  91. }
  92. // NewConsensusState returns a new ConsensusState.
  93. func NewConsensusState(config *cfg.ConsensusConfig, state *sm.State, proxyAppConn proxy.AppConnConsensus, blockStore types.BlockStore, mempool types.Mempool) *ConsensusState {
  94. cs := &ConsensusState{
  95. config: config,
  96. proxyAppConn: proxyAppConn,
  97. blockStore: blockStore,
  98. mempool: mempool,
  99. peerMsgQueue: make(chan msgInfo, msgQueueSize),
  100. internalMsgQueue: make(chan msgInfo, msgQueueSize),
  101. timeoutTicker: NewTimeoutTicker(),
  102. done: make(chan struct{}),
  103. doWALCatchup: true,
  104. }
  105. // set function defaults (may be overwritten before calling Start)
  106. cs.decideProposal = cs.defaultDecideProposal
  107. cs.doPrevote = cs.defaultDoPrevote
  108. cs.setProposal = cs.defaultSetProposal
  109. cs.updateToState(state)
  110. // Don't call scheduleRound0 yet.
  111. // We do that upon Start().
  112. cs.reconstructLastCommit(state)
  113. cs.BaseService = *cmn.NewBaseService(nil, "ConsensusState", cs)
  114. return cs
  115. }
  116. //----------------------------------------
  117. // Public interface
  118. // SetLogger implements Service.
  119. func (cs *ConsensusState) SetLogger(l log.Logger) {
  120. cs.BaseService.Logger = l
  121. cs.timeoutTicker.SetLogger(l)
  122. }
  123. // SetEventSwitch implements events.Eventable
  124. func (cs *ConsensusState) SetEventSwitch(evsw types.EventSwitch) {
  125. cs.evsw = evsw
  126. }
  127. // String returns a string.
  128. func (cs *ConsensusState) String() string {
  129. // better not to access shared variables
  130. return cmn.Fmt("ConsensusState") //(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
  131. }
  132. // GetState returns a copy of the chain state.
  133. func (cs *ConsensusState) GetState() *sm.State {
  134. cs.mtx.Lock()
  135. defer cs.mtx.Unlock()
  136. return cs.state.Copy()
  137. }
  138. // GetRoundState returns a copy of the internal consensus state.
  139. func (cs *ConsensusState) GetRoundState() *cstypes.RoundState {
  140. cs.mtx.Lock()
  141. defer cs.mtx.Unlock()
  142. return cs.getRoundState()
  143. }
  144. func (cs *ConsensusState) getRoundState() *cstypes.RoundState {
  145. rs := cs.RoundState // copy
  146. return &rs
  147. }
  148. // GetValidators returns a copy of the current validators.
  149. func (cs *ConsensusState) GetValidators() (int, []*types.Validator) {
  150. cs.mtx.Lock()
  151. defer cs.mtx.Unlock()
  152. return cs.state.LastBlockHeight, cs.state.Validators.Copy().Validators
  153. }
  154. // SetPrivValidator sets the private validator account for signing votes.
  155. func (cs *ConsensusState) SetPrivValidator(priv types.PrivValidator) {
  156. cs.mtx.Lock()
  157. defer cs.mtx.Unlock()
  158. cs.privValidator = priv
  159. }
  160. // SetTimeoutTicker sets the local timer. It may be useful to overwrite for testing.
  161. func (cs *ConsensusState) SetTimeoutTicker(timeoutTicker TimeoutTicker) {
  162. cs.mtx.Lock()
  163. defer cs.mtx.Unlock()
  164. cs.timeoutTicker = timeoutTicker
  165. }
  166. // LoadCommit loads the commit for a given height.
  167. func (cs *ConsensusState) LoadCommit(height int) *types.Commit {
  168. cs.mtx.Lock()
  169. defer cs.mtx.Unlock()
  170. if height == cs.blockStore.Height() {
  171. return cs.blockStore.LoadSeenCommit(height)
  172. }
  173. return cs.blockStore.LoadBlockCommit(height)
  174. }
  175. // OnStart implements cmn.Service.
  176. // It loads the latest state via the WAL, and starts the timeout and receive routines.
  177. func (cs *ConsensusState) OnStart() error {
  178. walFile := cs.config.WalFile()
  179. if err := cs.OpenWAL(walFile); err != nil {
  180. cs.Logger.Error("Error loading ConsensusState wal", "err", err.Error())
  181. return err
  182. }
  183. // we need the timeoutRoutine for replay so
  184. // we don't block on the tick chan.
  185. // NOTE: we will get a build up of garbage go routines
  186. // firing on the tockChan until the receiveRoutine is started
  187. // to deal with them (by that point, at most one will be valid)
  188. cs.timeoutTicker.Start()
  189. // we may have lost some votes if the process crashed
  190. // reload from consensus log to catchup
  191. if cs.doWALCatchup {
  192. if err := cs.catchupReplay(cs.Height); err != nil {
  193. cs.Logger.Error("Error on catchup replay. Proceeding to start ConsensusState anyway", "err", err.Error())
  194. // NOTE: if we ever do return an error here,
  195. // make sure to stop the timeoutTicker
  196. }
  197. }
  198. // now start the receiveRoutine
  199. go cs.receiveRoutine(0)
  200. // schedule the first round!
  201. // use GetRoundState so we don't race the receiveRoutine for access
  202. cs.scheduleRound0(cs.GetRoundState())
  203. return nil
  204. }
  205. // timeoutRoutine: receive requests for timeouts on tickChan and fire timeouts on tockChan
  206. // receiveRoutine: serializes processing of proposoals, block parts, votes; coordinates state transitions
  207. func (cs *ConsensusState) startRoutines(maxSteps int) {
  208. cs.timeoutTicker.Start()
  209. go cs.receiveRoutine(maxSteps)
  210. }
  211. // OnStop implements cmn.Service. It stops all routines and waits for the WAL to finish.
  212. func (cs *ConsensusState) OnStop() {
  213. cs.BaseService.OnStop()
  214. cs.timeoutTicker.Stop()
  215. // Make BaseService.Wait() wait until cs.wal.Wait()
  216. if cs.wal != nil && cs.IsRunning() {
  217. cs.wal.Wait()
  218. }
  219. }
  220. // Wait waits for the the main routine to return.
  221. // NOTE: be sure to Stop() the event switch and drain
  222. // any event channels or this may deadlock
  223. func (cs *ConsensusState) Wait() {
  224. <-cs.done
  225. }
  226. // OpenWAL opens a file to log all consensus messages and timeouts for deterministic accountability
  227. func (cs *ConsensusState) OpenWAL(walFile string) (err error) {
  228. err = cmn.EnsureDir(filepath.Dir(walFile), 0700)
  229. if err != nil {
  230. cs.Logger.Error("Error ensuring ConsensusState wal dir", "err", err.Error())
  231. return err
  232. }
  233. cs.mtx.Lock()
  234. defer cs.mtx.Unlock()
  235. wal, err := NewWAL(walFile, cs.config.WalLight)
  236. if err != nil {
  237. return err
  238. }
  239. wal.SetLogger(cs.Logger.With("wal", walFile))
  240. if _, err := wal.Start(); err != nil {
  241. return err
  242. }
  243. cs.wal = wal
  244. return nil
  245. }
  246. //------------------------------------------------------------
  247. // Public interface for passing messages into the consensus state, possibly causing a state transition.
  248. // If peerKey == "", the msg is considered internal.
  249. // Messages are added to the appropriate queue (peer or internal).
  250. // If the queue is full, the function may block.
  251. // TODO: should these return anything or let callers just use events?
  252. // AddVote inputs a vote.
  253. func (cs *ConsensusState) AddVote(vote *types.Vote, peerKey string) (added bool, err error) {
  254. if peerKey == "" {
  255. cs.internalMsgQueue <- msgInfo{&VoteMessage{vote}, ""}
  256. } else {
  257. cs.peerMsgQueue <- msgInfo{&VoteMessage{vote}, peerKey}
  258. }
  259. // TODO: wait for event?!
  260. return false, nil
  261. }
  262. // SetProposal inputs a proposal.
  263. func (cs *ConsensusState) SetProposal(proposal *types.Proposal, peerKey string) error {
  264. if peerKey == "" {
  265. cs.internalMsgQueue <- msgInfo{&ProposalMessage{proposal}, ""}
  266. } else {
  267. cs.peerMsgQueue <- msgInfo{&ProposalMessage{proposal}, peerKey}
  268. }
  269. // TODO: wait for event?!
  270. return nil
  271. }
  272. // AddProposalBlockPart inputs a part of the proposal block.
  273. func (cs *ConsensusState) AddProposalBlockPart(height, round int, part *types.Part, peerKey string) error {
  274. if peerKey == "" {
  275. cs.internalMsgQueue <- msgInfo{&BlockPartMessage{height, round, part}, ""}
  276. } else {
  277. cs.peerMsgQueue <- msgInfo{&BlockPartMessage{height, round, part}, peerKey}
  278. }
  279. // TODO: wait for event?!
  280. return nil
  281. }
  282. // SetProposalAndBlock inputs the proposal and all block parts.
  283. func (cs *ConsensusState) SetProposalAndBlock(proposal *types.Proposal, block *types.Block, parts *types.PartSet, peerKey string) error {
  284. cs.SetProposal(proposal, peerKey)
  285. for i := 0; i < parts.Total(); i++ {
  286. part := parts.GetPart(i)
  287. cs.AddProposalBlockPart(proposal.Height, proposal.Round, part, peerKey)
  288. }
  289. return nil // TODO errors
  290. }
  291. //------------------------------------------------------------
  292. // internal functions for managing the state
  293. func (cs *ConsensusState) updateHeight(height int) {
  294. cs.Height = height
  295. }
  296. func (cs *ConsensusState) updateRoundStep(round int, step cstypes.RoundStepType) {
  297. cs.Round = round
  298. cs.Step = step
  299. }
  300. // enterNewRound(height, 0) at cs.StartTime.
  301. func (cs *ConsensusState) scheduleRound0(rs *cstypes.RoundState) {
  302. //cs.Logger.Info("scheduleRound0", "now", time.Now(), "startTime", cs.StartTime)
  303. sleepDuration := rs.StartTime.Sub(time.Now())
  304. cs.scheduleTimeout(sleepDuration, rs.Height, 0, cstypes.RoundStepNewHeight)
  305. }
  306. // Attempt to schedule a timeout (by sending timeoutInfo on the tickChan)
  307. func (cs *ConsensusState) scheduleTimeout(duration time.Duration, height, round int, step cstypes.RoundStepType) {
  308. cs.timeoutTicker.ScheduleTimeout(timeoutInfo{duration, height, round, step})
  309. }
  310. // send a msg into the receiveRoutine regarding our own proposal, block part, or vote
  311. func (cs *ConsensusState) sendInternalMessage(mi msgInfo) {
  312. select {
  313. case cs.internalMsgQueue <- mi:
  314. default:
  315. // NOTE: using the go-routine means our votes can
  316. // be processed out of order.
  317. // TODO: use CList here for strict determinism and
  318. // attempt push to internalMsgQueue in receiveRoutine
  319. cs.Logger.Info("Internal msg queue is full. Using a go-routine")
  320. go func() { cs.internalMsgQueue <- mi }()
  321. }
  322. }
  323. // Reconstruct LastCommit from SeenCommit, which we saved along with the block,
  324. // (which happens even before saving the state)
  325. func (cs *ConsensusState) reconstructLastCommit(state *sm.State) {
  326. if state.LastBlockHeight == 0 {
  327. return
  328. }
  329. seenCommit := cs.blockStore.LoadSeenCommit(state.LastBlockHeight)
  330. lastPrecommits := types.NewVoteSet(state.ChainID, state.LastBlockHeight, seenCommit.Round(), types.VoteTypePrecommit, state.LastValidators)
  331. for _, precommit := range seenCommit.Precommits {
  332. if precommit == nil {
  333. continue
  334. }
  335. added, err := lastPrecommits.AddVote(precommit)
  336. if !added || err != nil {
  337. cmn.PanicCrisis(cmn.Fmt("Failed to reconstruct LastCommit: %v", err))
  338. }
  339. }
  340. if !lastPrecommits.HasTwoThirdsMajority() {
  341. cmn.PanicSanity("Failed to reconstruct LastCommit: Does not have +2/3 maj")
  342. }
  343. cs.LastCommit = lastPrecommits
  344. }
  345. // Updates ConsensusState and increments height to match that of state.
  346. // The round becomes 0 and cs.Step becomes cstypes.RoundStepNewHeight.
  347. func (cs *ConsensusState) updateToState(state *sm.State) {
  348. if cs.CommitRound > -1 && 0 < cs.Height && cs.Height != state.LastBlockHeight {
  349. cmn.PanicSanity(cmn.Fmt("updateToState() expected state height of %v but found %v",
  350. cs.Height, state.LastBlockHeight))
  351. }
  352. if cs.state != nil && cs.state.LastBlockHeight+1 != cs.Height {
  353. // This might happen when someone else is mutating cs.state.
  354. // Someone forgot to pass in state.Copy() somewhere?!
  355. cmn.PanicSanity(cmn.Fmt("Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v",
  356. cs.state.LastBlockHeight+1, cs.Height))
  357. }
  358. // If state isn't further out than cs.state, just ignore.
  359. // This happens when SwitchToConsensus() is called in the reactor.
  360. // We don't want to reset e.g. the Votes.
  361. if cs.state != nil && (state.LastBlockHeight <= cs.state.LastBlockHeight) {
  362. cs.Logger.Info("Ignoring updateToState()", "newHeight", state.LastBlockHeight+1, "oldHeight", cs.state.LastBlockHeight+1)
  363. return
  364. }
  365. // Reset fields based on state.
  366. validators := state.Validators
  367. lastPrecommits := (*types.VoteSet)(nil)
  368. if cs.CommitRound > -1 && cs.Votes != nil {
  369. if !cs.Votes.Precommits(cs.CommitRound).HasTwoThirdsMajority() {
  370. cmn.PanicSanity("updateToState(state) called but last Precommit round didn't have +2/3")
  371. }
  372. lastPrecommits = cs.Votes.Precommits(cs.CommitRound)
  373. }
  374. // Next desired block height
  375. height := state.LastBlockHeight + 1
  376. // RoundState fields
  377. cs.updateHeight(height)
  378. cs.updateRoundStep(0, cstypes.RoundStepNewHeight)
  379. if cs.CommitTime.IsZero() {
  380. // "Now" makes it easier to sync up dev nodes.
  381. // We add timeoutCommit to allow transactions
  382. // to be gathered for the first block.
  383. // And alternative solution that relies on clocks:
  384. // cs.StartTime = state.LastBlockTime.Add(timeoutCommit)
  385. cs.StartTime = cs.config.Commit(time.Now())
  386. } else {
  387. cs.StartTime = cs.config.Commit(cs.CommitTime)
  388. }
  389. cs.Validators = validators
  390. cs.Proposal = nil
  391. cs.ProposalBlock = nil
  392. cs.ProposalBlockParts = nil
  393. cs.LockedRound = 0
  394. cs.LockedBlock = nil
  395. cs.LockedBlockParts = nil
  396. cs.Votes = cstypes.NewHeightVoteSet(state.ChainID, height, validators)
  397. cs.CommitRound = -1
  398. cs.LastCommit = lastPrecommits
  399. cs.LastValidators = state.LastValidators
  400. cs.state = state
  401. // Finally, broadcast RoundState
  402. cs.newStep()
  403. }
  404. func (cs *ConsensusState) newStep() {
  405. rs := cs.RoundStateEvent()
  406. cs.wal.Save(rs)
  407. cs.nSteps += 1
  408. // newStep is called by updateToStep in NewConsensusState before the evsw is set!
  409. if cs.evsw != nil {
  410. types.FireEventNewRoundStep(cs.evsw, rs)
  411. }
  412. }
  413. //-----------------------------------------
  414. // the main go routines
  415. // receiveRoutine handles messages which may cause state transitions.
  416. // it's argument (n) is the number of messages to process before exiting - use 0 to run forever
  417. // It keeps the RoundState and is the only thing that updates it.
  418. // Updates (state transitions) happen on timeouts, complete proposals, and 2/3 majorities.
  419. // ConsensusState must be locked before any internal state is updated.
  420. func (cs *ConsensusState) receiveRoutine(maxSteps int) {
  421. defer func() {
  422. if r := recover(); r != nil {
  423. cs.Logger.Error("CONSENSUS FAILURE!!!", "err", r, "stack", string(debug.Stack()))
  424. }
  425. }()
  426. for {
  427. if maxSteps > 0 {
  428. if cs.nSteps >= maxSteps {
  429. cs.Logger.Info("reached max steps. exiting receive routine")
  430. cs.nSteps = 0
  431. return
  432. }
  433. }
  434. rs := cs.RoundState
  435. var mi msgInfo
  436. select {
  437. case height := <-cs.mempool.TxsAvailable():
  438. cs.handleTxsAvailable(height)
  439. case mi = <-cs.peerMsgQueue:
  440. cs.wal.Save(mi)
  441. // handles proposals, block parts, votes
  442. // may generate internal events (votes, complete proposals, 2/3 majorities)
  443. cs.handleMsg(mi)
  444. case mi = <-cs.internalMsgQueue:
  445. cs.wal.Save(mi)
  446. // handles proposals, block parts, votes
  447. cs.handleMsg(mi)
  448. case ti := <-cs.timeoutTicker.Chan(): // tockChan:
  449. cs.wal.Save(ti)
  450. // if the timeout is relevant to the rs
  451. // go to the next step
  452. cs.handleTimeout(ti, rs)
  453. case <-cs.Quit:
  454. // NOTE: the internalMsgQueue may have signed messages from our
  455. // priv_val that haven't hit the WAL, but its ok because
  456. // priv_val tracks LastSig
  457. // close wal now that we're done writing to it
  458. if cs.wal != nil {
  459. cs.wal.Stop()
  460. }
  461. close(cs.done)
  462. return
  463. }
  464. }
  465. }
  466. // state transitions on complete-proposal, 2/3-any, 2/3-one
  467. func (cs *ConsensusState) handleMsg(mi msgInfo) {
  468. cs.mtx.Lock()
  469. defer cs.mtx.Unlock()
  470. var err error
  471. msg, peerKey := mi.Msg, mi.PeerKey
  472. switch msg := msg.(type) {
  473. case *ProposalMessage:
  474. // will not cause transition.
  475. // once proposal is set, we can receive block parts
  476. err = cs.setProposal(msg.Proposal)
  477. case *BlockPartMessage:
  478. // if the proposal is complete, we'll enterPrevote or tryFinalizeCommit
  479. _, err = cs.addProposalBlockPart(msg.Height, msg.Part, peerKey != "")
  480. if err != nil && msg.Round != cs.Round {
  481. err = nil
  482. }
  483. case *VoteMessage:
  484. // attempt to add the vote and dupeout the validator if its a duplicate signature
  485. // if the vote gives us a 2/3-any or 2/3-one, we transition
  486. err := cs.tryAddVote(msg.Vote, peerKey)
  487. if err == ErrAddingVote {
  488. // TODO: punish peer
  489. }
  490. // NOTE: the vote is broadcast to peers by the reactor listening
  491. // for vote events
  492. // TODO: If rs.Height == vote.Height && rs.Round < vote.Round,
  493. // the peer is sending us CatchupCommit precommits.
  494. // We could make note of this and help filter in broadcastHasVoteMessage().
  495. default:
  496. cs.Logger.Error("Unknown msg type", reflect.TypeOf(msg))
  497. }
  498. if err != nil {
  499. cs.Logger.Error("Error with msg", "type", reflect.TypeOf(msg), "peer", peerKey, "err", err, "msg", msg)
  500. }
  501. }
  502. func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs cstypes.RoundState) {
  503. cs.Logger.Debug("Received tock", "timeout", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step)
  504. // timeouts must be for current height, round, step
  505. if ti.Height != rs.Height || ti.Round < rs.Round || (ti.Round == rs.Round && ti.Step < rs.Step) {
  506. cs.Logger.Debug("Ignoring tock because we're ahead", "height", rs.Height, "round", rs.Round, "step", rs.Step)
  507. return
  508. }
  509. // the timeout will now cause a state transition
  510. cs.mtx.Lock()
  511. defer cs.mtx.Unlock()
  512. switch ti.Step {
  513. case cstypes.RoundStepNewHeight:
  514. // NewRound event fired from enterNewRound.
  515. // XXX: should we fire timeout here (for timeout commit)?
  516. cs.enterNewRound(ti.Height, 0)
  517. case cstypes.RoundStepNewRound:
  518. cs.enterPropose(ti.Height, 0)
  519. case cstypes.RoundStepPropose:
  520. types.FireEventTimeoutPropose(cs.evsw, cs.RoundStateEvent())
  521. cs.enterPrevote(ti.Height, ti.Round)
  522. case cstypes.RoundStepPrevoteWait:
  523. types.FireEventTimeoutWait(cs.evsw, cs.RoundStateEvent())
  524. cs.enterPrecommit(ti.Height, ti.Round)
  525. case cstypes.RoundStepPrecommitWait:
  526. types.FireEventTimeoutWait(cs.evsw, cs.RoundStateEvent())
  527. cs.enterNewRound(ti.Height, ti.Round+1)
  528. default:
  529. panic(cmn.Fmt("Invalid timeout step: %v", ti.Step))
  530. }
  531. }
  532. func (cs *ConsensusState) handleTxsAvailable(height int) {
  533. cs.mtx.Lock()
  534. defer cs.mtx.Unlock()
  535. // we only need to do this for round 0
  536. cs.enterPropose(height, 0)
  537. }
  538. //-----------------------------------------------------------------------------
  539. // State functions
  540. // Used internally by handleTimeout and handleMsg to make state transitions
  541. // Enter: `timeoutNewHeight` by startTime (commitTime+timeoutCommit),
  542. // or, if SkipTimeout==true, after receiving all precommits from (height,round-1)
  543. // Enter: `timeoutPrecommits` after any +2/3 precommits from (height,round-1)
  544. // Enter: +2/3 precommits for nil at (height,round-1)
  545. // Enter: +2/3 prevotes any or +2/3 precommits for block or any from (height, round)
  546. // NOTE: cs.StartTime was already set for height.
  547. func (cs *ConsensusState) enterNewRound(height int, round int) {
  548. if cs.Height != height || round < cs.Round || (cs.Round == round && cs.Step != cstypes.RoundStepNewHeight) {
  549. cs.Logger.Debug(cmn.Fmt("enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  550. return
  551. }
  552. if now := time.Now(); cs.StartTime.After(now) {
  553. cs.Logger.Info("Need to set a buffer and log message here for sanity.", "startTime", cs.StartTime, "now", now)
  554. }
  555. cs.Logger.Info(cmn.Fmt("enterNewRound(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  556. // Increment validators if necessary
  557. validators := cs.Validators
  558. if cs.Round < round {
  559. validators = validators.Copy()
  560. validators.IncrementAccum(round - cs.Round)
  561. }
  562. // Setup new round
  563. // we don't fire newStep for this step,
  564. // but we fire an event, so update the round step first
  565. cs.updateRoundStep(round, cstypes.RoundStepNewRound)
  566. cs.Validators = validators
  567. if round == 0 {
  568. // We've already reset these upon new height,
  569. // and meanwhile we might have received a proposal
  570. // for round 0.
  571. } else {
  572. cs.Proposal = nil
  573. cs.ProposalBlock = nil
  574. cs.ProposalBlockParts = nil
  575. }
  576. cs.Votes.SetRound(round + 1) // also track next round (round+1) to allow round-skipping
  577. types.FireEventNewRound(cs.evsw, cs.RoundStateEvent())
  578. // Wait for txs to be available in the mempool
  579. // before we enterPropose in round 0. If the last block changed the app hash,
  580. // we may need an empty "proof" block, and enterPropose immediately.
  581. waitForTxs := cs.config.WaitForTxs() && round == 0 && !cs.needProofBlock(height)
  582. if waitForTxs {
  583. if cs.config.CreateEmptyBlocksInterval > 0 {
  584. cs.scheduleTimeout(cs.config.EmptyBlocksInterval(), height, round, cstypes.RoundStepNewRound)
  585. }
  586. go cs.proposalHeartbeat(height, round)
  587. } else {
  588. cs.enterPropose(height, round)
  589. }
  590. }
  591. // needProofBlock returns true on the first height (so the genesis app hash is signed right away)
  592. // and where the last block (height-1) caused the app hash to change
  593. func (cs *ConsensusState) needProofBlock(height int) bool {
  594. if height == 1 {
  595. return true
  596. }
  597. lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1)
  598. if !bytes.Equal(cs.state.AppHash, lastBlockMeta.Header.AppHash) {
  599. return true
  600. }
  601. return false
  602. }
  603. func (cs *ConsensusState) proposalHeartbeat(height, round int) {
  604. counter := 0
  605. addr := cs.privValidator.GetAddress()
  606. valIndex, v := cs.Validators.GetByAddress(addr)
  607. if v == nil {
  608. // not a validator
  609. valIndex = -1
  610. }
  611. chainID := cs.state.ChainID
  612. for {
  613. rs := cs.GetRoundState()
  614. // if we've already moved on, no need to send more heartbeats
  615. if rs.Step > cstypes.RoundStepNewRound || rs.Round > round || rs.Height > height {
  616. return
  617. }
  618. heartbeat := &types.Heartbeat{
  619. Height: rs.Height,
  620. Round: rs.Round,
  621. Sequence: counter,
  622. ValidatorAddress: addr,
  623. ValidatorIndex: valIndex,
  624. }
  625. cs.privValidator.SignHeartbeat(chainID, heartbeat)
  626. heartbeatEvent := types.EventDataProposalHeartbeat{heartbeat}
  627. types.FireEventProposalHeartbeat(cs.evsw, heartbeatEvent)
  628. counter += 1
  629. time.Sleep(proposalHeartbeatIntervalSeconds * time.Second)
  630. }
  631. }
  632. // Enter (CreateEmptyBlocks): from enterNewRound(height,round)
  633. // Enter (CreateEmptyBlocks, CreateEmptyBlocksInterval > 0 ): after enterNewRound(height,round), after timeout of CreateEmptyBlocksInterval
  634. // Enter (!CreateEmptyBlocks) : after enterNewRound(height,round), once txs are in the mempool
  635. func (cs *ConsensusState) enterPropose(height int, round int) {
  636. if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPropose <= cs.Step) {
  637. cs.Logger.Debug(cmn.Fmt("enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  638. return
  639. }
  640. cs.Logger.Info(cmn.Fmt("enterPropose(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  641. defer func() {
  642. // Done enterPropose:
  643. cs.updateRoundStep(round, cstypes.RoundStepPropose)
  644. cs.newStep()
  645. // If we have the whole proposal + POL, then goto Prevote now.
  646. // else, we'll enterPrevote when the rest of the proposal is received (in AddProposalBlockPart),
  647. // or else after timeoutPropose
  648. if cs.isProposalComplete() {
  649. cs.enterPrevote(height, cs.Round)
  650. }
  651. }()
  652. // If we don't get the proposal and all block parts quick enough, enterPrevote
  653. cs.scheduleTimeout(cs.config.Propose(round), height, round, cstypes.RoundStepPropose)
  654. // Nothing more to do if we're not a validator
  655. if cs.privValidator == nil {
  656. cs.Logger.Debug("This node is not a validator")
  657. return
  658. }
  659. if !cs.isProposer() {
  660. cs.Logger.Info("enterPropose: Not our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
  661. if cs.Validators.HasAddress(cs.privValidator.GetAddress()) {
  662. cs.Logger.Debug("This node is a validator")
  663. } else {
  664. cs.Logger.Debug("This node is not a validator")
  665. }
  666. } else {
  667. cs.Logger.Info("enterPropose: Our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
  668. cs.Logger.Debug("This node is a validator")
  669. cs.decideProposal(height, round)
  670. }
  671. }
  672. func (cs *ConsensusState) isProposer() bool {
  673. return bytes.Equal(cs.Validators.GetProposer().Address, cs.privValidator.GetAddress())
  674. }
  675. func (cs *ConsensusState) defaultDecideProposal(height, round int) {
  676. var block *types.Block
  677. var blockParts *types.PartSet
  678. // Decide on block
  679. if cs.LockedBlock != nil {
  680. // If we're locked onto a block, just choose that.
  681. block, blockParts = cs.LockedBlock, cs.LockedBlockParts
  682. } else {
  683. // Create a new proposal block from state/txs from the mempool.
  684. block, blockParts = cs.createProposalBlock()
  685. if block == nil { // on error
  686. return
  687. }
  688. }
  689. // Make proposal
  690. polRound, polBlockID := cs.Votes.POLInfo()
  691. proposal := types.NewProposal(height, round, blockParts.Header(), polRound, polBlockID)
  692. if err := cs.privValidator.SignProposal(cs.state.ChainID, proposal); err == nil {
  693. // Set fields
  694. /* fields set by setProposal and addBlockPart
  695. cs.Proposal = proposal
  696. cs.ProposalBlock = block
  697. cs.ProposalBlockParts = blockParts
  698. */
  699. // send proposal and block parts on internal msg queue
  700. cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""})
  701. for i := 0; i < blockParts.Total(); i++ {
  702. part := blockParts.GetPart(i)
  703. cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""})
  704. }
  705. cs.Logger.Info("Signed proposal", "height", height, "round", round, "proposal", proposal)
  706. cs.Logger.Debug(cmn.Fmt("Signed proposal block: %v", block))
  707. } else {
  708. if !cs.replayMode {
  709. cs.Logger.Error("enterPropose: Error signing proposal", "height", height, "round", round, "err", err)
  710. }
  711. }
  712. }
  713. // Returns true if the proposal block is complete &&
  714. // (if POLRound was proposed, we have +2/3 prevotes from there).
  715. func (cs *ConsensusState) isProposalComplete() bool {
  716. if cs.Proposal == nil || cs.ProposalBlock == nil {
  717. return false
  718. }
  719. // we have the proposal. if there's a POLRound,
  720. // make sure we have the prevotes from it too
  721. if cs.Proposal.POLRound < 0 {
  722. return true
  723. } else {
  724. // if this is false the proposer is lying or we haven't received the POL yet
  725. return cs.Votes.Prevotes(cs.Proposal.POLRound).HasTwoThirdsMajority()
  726. }
  727. }
  728. // Create the next block to propose and return it.
  729. // Returns nil block upon error.
  730. // NOTE: keep it side-effect free for clarity.
  731. func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts *types.PartSet) {
  732. var commit *types.Commit
  733. if cs.Height == 1 {
  734. // We're creating a proposal for the first block.
  735. // The commit is empty, but not nil.
  736. commit = &types.Commit{}
  737. } else if cs.LastCommit.HasTwoThirdsMajority() {
  738. // Make the commit from LastCommit
  739. commit = cs.LastCommit.MakeCommit()
  740. } else {
  741. // This shouldn't happen.
  742. cs.Logger.Error("enterPropose: Cannot propose anything: No commit for the previous block.")
  743. return
  744. }
  745. // Mempool validated transactions
  746. txs := cs.mempool.Reap(cs.config.MaxBlockSizeTxs)
  747. return types.MakeBlock(cs.Height, cs.state.ChainID, txs, commit,
  748. cs.state.LastBlockID, cs.state.Validators.Hash(),
  749. cs.state.AppHash, cs.state.Params.BlockPartSizeBytes)
  750. }
  751. // Enter: `timeoutPropose` after entering Propose.
  752. // Enter: proposal block and POL is ready.
  753. // Enter: any +2/3 prevotes for future round.
  754. // Prevote for LockedBlock if we're locked, or ProposalBlock if valid.
  755. // Otherwise vote nil.
  756. func (cs *ConsensusState) enterPrevote(height int, round int) {
  757. if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevote <= cs.Step) {
  758. cs.Logger.Debug(cmn.Fmt("enterPrevote(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  759. return
  760. }
  761. defer func() {
  762. // Done enterPrevote:
  763. cs.updateRoundStep(round, cstypes.RoundStepPrevote)
  764. cs.newStep()
  765. }()
  766. // fire event for how we got here
  767. if cs.isProposalComplete() {
  768. types.FireEventCompleteProposal(cs.evsw, cs.RoundStateEvent())
  769. } else {
  770. // we received +2/3 prevotes for a future round
  771. // TODO: catchup event?
  772. }
  773. cs.Logger.Info(cmn.Fmt("enterPrevote(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  774. // Sign and broadcast vote as necessary
  775. cs.doPrevote(height, round)
  776. // Once `addVote` hits any +2/3 prevotes, we will go to PrevoteWait
  777. // (so we have more time to try and collect +2/3 prevotes for a single block)
  778. }
  779. func (cs *ConsensusState) defaultDoPrevote(height int, round int) {
  780. logger := cs.Logger.With("height", height, "round", round)
  781. // If a block is locked, prevote that.
  782. if cs.LockedBlock != nil {
  783. logger.Info("enterPrevote: Block was locked")
  784. cs.signAddVote(types.VoteTypePrevote, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header())
  785. return
  786. }
  787. // If ProposalBlock is nil, prevote nil.
  788. if cs.ProposalBlock == nil {
  789. logger.Info("enterPrevote: ProposalBlock is nil")
  790. cs.signAddVote(types.VoteTypePrevote, nil, types.PartSetHeader{})
  791. return
  792. }
  793. // Validate proposal block
  794. err := cs.state.ValidateBlock(cs.ProposalBlock)
  795. if err != nil {
  796. // ProposalBlock is invalid, prevote nil.
  797. logger.Error("enterPrevote: ProposalBlock is invalid", "err", err)
  798. cs.signAddVote(types.VoteTypePrevote, nil, types.PartSetHeader{})
  799. return
  800. }
  801. // Prevote cs.ProposalBlock
  802. // NOTE: the proposal signature is validated when it is received,
  803. // and the proposal block parts are validated as they are received (against the merkle hash in the proposal)
  804. logger.Info("enterPrevote: ProposalBlock is valid")
  805. cs.signAddVote(types.VoteTypePrevote, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header())
  806. }
  807. // Enter: any +2/3 prevotes at next round.
  808. func (cs *ConsensusState) enterPrevoteWait(height int, round int) {
  809. if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevoteWait <= cs.Step) {
  810. cs.Logger.Debug(cmn.Fmt("enterPrevoteWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  811. return
  812. }
  813. if !cs.Votes.Prevotes(round).HasTwoThirdsAny() {
  814. cmn.PanicSanity(cmn.Fmt("enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes", height, round))
  815. }
  816. cs.Logger.Info(cmn.Fmt("enterPrevoteWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  817. defer func() {
  818. // Done enterPrevoteWait:
  819. cs.updateRoundStep(round, cstypes.RoundStepPrevoteWait)
  820. cs.newStep()
  821. }()
  822. // Wait for some more prevotes; enterPrecommit
  823. cs.scheduleTimeout(cs.config.Prevote(round), height, round, cstypes.RoundStepPrevoteWait)
  824. }
  825. // Enter: `timeoutPrevote` after any +2/3 prevotes.
  826. // Enter: +2/3 precomits for block or nil.
  827. // Enter: any +2/3 precommits for next round.
  828. // Lock & precommit the ProposalBlock if we have enough prevotes for it (a POL in this round)
  829. // else, unlock an existing lock and precommit nil if +2/3 of prevotes were nil,
  830. // else, precommit nil otherwise.
  831. func (cs *ConsensusState) enterPrecommit(height int, round int) {
  832. if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrecommit <= cs.Step) {
  833. cs.Logger.Debug(cmn.Fmt("enterPrecommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  834. return
  835. }
  836. cs.Logger.Info(cmn.Fmt("enterPrecommit(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  837. defer func() {
  838. // Done enterPrecommit:
  839. cs.updateRoundStep(round, cstypes.RoundStepPrecommit)
  840. cs.newStep()
  841. }()
  842. blockID, ok := cs.Votes.Prevotes(round).TwoThirdsMajority()
  843. // If we don't have a polka, we must precommit nil
  844. if !ok {
  845. if cs.LockedBlock != nil {
  846. cs.Logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit while we're locked. Precommitting nil")
  847. } else {
  848. cs.Logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit. Precommitting nil.")
  849. }
  850. cs.signAddVote(types.VoteTypePrecommit, nil, types.PartSetHeader{})
  851. return
  852. }
  853. // At this point +2/3 prevoted for a particular block or nil
  854. types.FireEventPolka(cs.evsw, cs.RoundStateEvent())
  855. // the latest POLRound should be this round
  856. polRound, _ := cs.Votes.POLInfo()
  857. if polRound < round {
  858. cmn.PanicSanity(cmn.Fmt("This POLRound should be %v but got %", round, polRound))
  859. }
  860. // +2/3 prevoted nil. Unlock and precommit nil.
  861. if len(blockID.Hash) == 0 {
  862. if cs.LockedBlock == nil {
  863. cs.Logger.Info("enterPrecommit: +2/3 prevoted for nil.")
  864. } else {
  865. cs.Logger.Info("enterPrecommit: +2/3 prevoted for nil. Unlocking")
  866. cs.LockedRound = 0
  867. cs.LockedBlock = nil
  868. cs.LockedBlockParts = nil
  869. types.FireEventUnlock(cs.evsw, cs.RoundStateEvent())
  870. }
  871. cs.signAddVote(types.VoteTypePrecommit, nil, types.PartSetHeader{})
  872. return
  873. }
  874. // At this point, +2/3 prevoted for a particular block.
  875. // If we're already locked on that block, precommit it, and update the LockedRound
  876. if cs.LockedBlock.HashesTo(blockID.Hash) {
  877. cs.Logger.Info("enterPrecommit: +2/3 prevoted locked block. Relocking")
  878. cs.LockedRound = round
  879. types.FireEventRelock(cs.evsw, cs.RoundStateEvent())
  880. cs.signAddVote(types.VoteTypePrecommit, blockID.Hash, blockID.PartsHeader)
  881. return
  882. }
  883. // If +2/3 prevoted for proposal block, stage and precommit it
  884. if cs.ProposalBlock.HashesTo(blockID.Hash) {
  885. cs.Logger.Info("enterPrecommit: +2/3 prevoted proposal block. Locking", "hash", blockID.Hash)
  886. // Validate the block.
  887. if err := cs.state.ValidateBlock(cs.ProposalBlock); err != nil {
  888. cmn.PanicConsensus(cmn.Fmt("enterPrecommit: +2/3 prevoted for an invalid block: %v", err))
  889. }
  890. cs.LockedRound = round
  891. cs.LockedBlock = cs.ProposalBlock
  892. cs.LockedBlockParts = cs.ProposalBlockParts
  893. types.FireEventLock(cs.evsw, cs.RoundStateEvent())
  894. cs.signAddVote(types.VoteTypePrecommit, blockID.Hash, blockID.PartsHeader)
  895. return
  896. }
  897. // There was a polka in this round for a block we don't have.
  898. // Fetch that block, unlock, and precommit nil.
  899. // The +2/3 prevotes for this round is the POL for our unlock.
  900. // TODO: In the future save the POL prevotes for justification.
  901. cs.LockedRound = 0
  902. cs.LockedBlock = nil
  903. cs.LockedBlockParts = nil
  904. if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
  905. cs.ProposalBlock = nil
  906. cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
  907. }
  908. types.FireEventUnlock(cs.evsw, cs.RoundStateEvent())
  909. cs.signAddVote(types.VoteTypePrecommit, nil, types.PartSetHeader{})
  910. }
  911. // Enter: any +2/3 precommits for next round.
  912. func (cs *ConsensusState) enterPrecommitWait(height int, round int) {
  913. if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrecommitWait <= cs.Step) {
  914. cs.Logger.Debug(cmn.Fmt("enterPrecommitWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  915. return
  916. }
  917. if !cs.Votes.Precommits(round).HasTwoThirdsAny() {
  918. cmn.PanicSanity(cmn.Fmt("enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes", height, round))
  919. }
  920. cs.Logger.Info(cmn.Fmt("enterPrecommitWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  921. defer func() {
  922. // Done enterPrecommitWait:
  923. cs.updateRoundStep(round, cstypes.RoundStepPrecommitWait)
  924. cs.newStep()
  925. }()
  926. // Wait for some more precommits; enterNewRound
  927. cs.scheduleTimeout(cs.config.Precommit(round), height, round, cstypes.RoundStepPrecommitWait)
  928. }
  929. // Enter: +2/3 precommits for block
  930. func (cs *ConsensusState) enterCommit(height int, commitRound int) {
  931. if cs.Height != height || cstypes.RoundStepCommit <= cs.Step {
  932. cs.Logger.Debug(cmn.Fmt("enterCommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step))
  933. return
  934. }
  935. cs.Logger.Info(cmn.Fmt("enterCommit(%v/%v). Current: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step))
  936. defer func() {
  937. // Done enterCommit:
  938. // keep cs.Round the same, commitRound points to the right Precommits set.
  939. cs.updateRoundStep(cs.Round, cstypes.RoundStepCommit)
  940. cs.CommitRound = commitRound
  941. cs.CommitTime = time.Now()
  942. cs.newStep()
  943. // Maybe finalize immediately.
  944. cs.tryFinalizeCommit(height)
  945. }()
  946. blockID, ok := cs.Votes.Precommits(commitRound).TwoThirdsMajority()
  947. if !ok {
  948. cmn.PanicSanity("RunActionCommit() expects +2/3 precommits")
  949. }
  950. // The Locked* fields no longer matter.
  951. // Move them over to ProposalBlock if they match the commit hash,
  952. // otherwise they'll be cleared in updateToState.
  953. if cs.LockedBlock.HashesTo(blockID.Hash) {
  954. cs.ProposalBlock = cs.LockedBlock
  955. cs.ProposalBlockParts = cs.LockedBlockParts
  956. }
  957. // If we don't have the block being committed, set up to get it.
  958. if !cs.ProposalBlock.HashesTo(blockID.Hash) {
  959. if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
  960. // We're getting the wrong block.
  961. // Set up ProposalBlockParts and keep waiting.
  962. cs.ProposalBlock = nil
  963. cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
  964. } else {
  965. // We just need to keep waiting.
  966. }
  967. }
  968. }
  969. // If we have the block AND +2/3 commits for it, finalize.
  970. func (cs *ConsensusState) tryFinalizeCommit(height int) {
  971. if cs.Height != height {
  972. cmn.PanicSanity(cmn.Fmt("tryFinalizeCommit() cs.Height: %v vs height: %v", cs.Height, height))
  973. }
  974. blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority()
  975. if !ok || len(blockID.Hash) == 0 {
  976. cs.Logger.Error("Attempt to finalize failed. There was no +2/3 majority, or +2/3 was for <nil>.", "height", height)
  977. return
  978. }
  979. if !cs.ProposalBlock.HashesTo(blockID.Hash) {
  980. // TODO: this happens every time if we're not a validator (ugly logs)
  981. // TODO: ^^ wait, why does it matter that we're a validator?
  982. cs.Logger.Info("Attempt to finalize failed. We don't have the commit block.", "height", height, "proposal-block", cs.ProposalBlock.Hash(), "commit-block", blockID.Hash)
  983. return
  984. }
  985. // go
  986. cs.finalizeCommit(height)
  987. }
  988. // Increment height and goto cstypes.RoundStepNewHeight
  989. func (cs *ConsensusState) finalizeCommit(height int) {
  990. if cs.Height != height || cs.Step != cstypes.RoundStepCommit {
  991. cs.Logger.Debug(cmn.Fmt("finalizeCommit(%v): Invalid args. Current step: %v/%v/%v", height, cs.Height, cs.Round, cs.Step))
  992. return
  993. }
  994. blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority()
  995. block, blockParts := cs.ProposalBlock, cs.ProposalBlockParts
  996. if !ok {
  997. cmn.PanicSanity(cmn.Fmt("Cannot finalizeCommit, commit does not have two thirds majority"))
  998. }
  999. if !blockParts.HasHeader(blockID.PartsHeader) {
  1000. cmn.PanicSanity(cmn.Fmt("Expected ProposalBlockParts header to be commit header"))
  1001. }
  1002. if !block.HashesTo(blockID.Hash) {
  1003. cmn.PanicSanity(cmn.Fmt("Cannot finalizeCommit, ProposalBlock does not hash to commit hash"))
  1004. }
  1005. if err := cs.state.ValidateBlock(block); err != nil {
  1006. cmn.PanicConsensus(cmn.Fmt("+2/3 committed an invalid block: %v", err))
  1007. }
  1008. cs.Logger.Info(cmn.Fmt("Finalizing commit of block with %d txs", block.NumTxs),
  1009. "height", block.Height, "hash", block.Hash(), "root", block.AppHash)
  1010. cs.Logger.Info(cmn.Fmt("%v", block))
  1011. fail.Fail() // XXX
  1012. // Save to blockStore.
  1013. if cs.blockStore.Height() < block.Height {
  1014. // NOTE: the seenCommit is local justification to commit this block,
  1015. // but may differ from the LastCommit included in the next block
  1016. precommits := cs.Votes.Precommits(cs.CommitRound)
  1017. seenCommit := precommits.MakeCommit()
  1018. cs.blockStore.SaveBlock(block, blockParts, seenCommit)
  1019. } else {
  1020. // Happens during replay if we already saved the block but didn't commit
  1021. cs.Logger.Info("Calling finalizeCommit on already stored block", "height", block.Height)
  1022. }
  1023. fail.Fail() // XXX
  1024. // Finish writing to the WAL for this height.
  1025. // NOTE: If we fail before writing this, we'll never write it,
  1026. // and just recover by running ApplyBlock in the Handshake.
  1027. // If we moved it before persisting the block, we'd have to allow
  1028. // WAL replay for blocks with an #ENDHEIGHT
  1029. // As is, ConsensusState should not be started again
  1030. // until we successfully call ApplyBlock (ie. here or in Handshake after restart)
  1031. if cs.wal != nil {
  1032. cs.wal.Save(EndHeightMessage{uint64(height)})
  1033. }
  1034. fail.Fail() // XXX
  1035. // Create a copy of the state for staging
  1036. // and an event cache for txs
  1037. stateCopy := cs.state.Copy()
  1038. eventCache := types.NewEventCache(cs.evsw)
  1039. // Execute and commit the block, update and save the state, and update the mempool.
  1040. // All calls to the proxyAppConn come here.
  1041. // NOTE: the block.AppHash wont reflect these txs until the next block
  1042. err := stateCopy.ApplyBlock(eventCache, cs.proxyAppConn, block, blockParts.Header(), cs.mempool)
  1043. if err != nil {
  1044. cs.Logger.Error("Error on ApplyBlock. Did the application crash? Please restart tendermint", "err", err)
  1045. err := cmn.Kill()
  1046. if err != nil {
  1047. cs.Logger.Error("Failed to kill this process - please do so manually", "err", err)
  1048. }
  1049. return
  1050. }
  1051. fail.Fail() // XXX
  1052. // Fire event for new block.
  1053. // NOTE: If we fail before firing, these events will never fire
  1054. //
  1055. // TODO: Either
  1056. // * Fire before persisting state, in ApplyBlock
  1057. // * Fire on start up if we haven't written any new WAL msgs
  1058. // Both options mean we may fire more than once. Is that fine ?
  1059. types.FireEventNewBlock(cs.evsw, types.EventDataNewBlock{block})
  1060. types.FireEventNewBlockHeader(cs.evsw, types.EventDataNewBlockHeader{block.Header})
  1061. eventCache.Flush()
  1062. fail.Fail() // XXX
  1063. // NewHeightStep!
  1064. cs.updateToState(stateCopy)
  1065. fail.Fail() // XXX
  1066. // cs.StartTime is already set.
  1067. // Schedule Round0 to start soon.
  1068. cs.scheduleRound0(&cs.RoundState)
  1069. // By here,
  1070. // * cs.Height has been increment to height+1
  1071. // * cs.Step is now cstypes.RoundStepNewHeight
  1072. // * cs.StartTime is set to when we will start round0.
  1073. }
  1074. //-----------------------------------------------------------------------------
  1075. func (cs *ConsensusState) defaultSetProposal(proposal *types.Proposal) error {
  1076. // Already have one
  1077. // TODO: possibly catch double proposals
  1078. if cs.Proposal != nil {
  1079. return nil
  1080. }
  1081. // Does not apply
  1082. if proposal.Height != cs.Height || proposal.Round != cs.Round {
  1083. return nil
  1084. }
  1085. // We don't care about the proposal if we're already in cstypes.RoundStepCommit.
  1086. if cstypes.RoundStepCommit <= cs.Step {
  1087. return nil
  1088. }
  1089. // Verify POLRound, which must be -1 or between 0 and proposal.Round exclusive.
  1090. if proposal.POLRound != -1 &&
  1091. (proposal.POLRound < 0 || proposal.Round <= proposal.POLRound) {
  1092. return ErrInvalidProposalPOLRound
  1093. }
  1094. // Verify signature
  1095. if !cs.Validators.GetProposer().PubKey.VerifyBytes(types.SignBytes(cs.state.ChainID, proposal), proposal.Signature) {
  1096. return ErrInvalidProposalSignature
  1097. }
  1098. cs.Proposal = proposal
  1099. cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockPartsHeader)
  1100. return nil
  1101. }
  1102. // NOTE: block is not necessarily valid.
  1103. // Asynchronously triggers either enterPrevote (before we timeout of propose) or tryFinalizeCommit, once we have the full block.
  1104. func (cs *ConsensusState) addProposalBlockPart(height int, part *types.Part, verify bool) (added bool, err error) {
  1105. // Blocks might be reused, so round mismatch is OK
  1106. if cs.Height != height {
  1107. return false, nil
  1108. }
  1109. // We're not expecting a block part.
  1110. if cs.ProposalBlockParts == nil {
  1111. return false, nil // TODO: bad peer? Return error?
  1112. }
  1113. added, err = cs.ProposalBlockParts.AddPart(part, verify)
  1114. if err != nil {
  1115. return added, err
  1116. }
  1117. if added && cs.ProposalBlockParts.IsComplete() {
  1118. // Added and completed!
  1119. var n int
  1120. var err error
  1121. cs.ProposalBlock = wire.ReadBinary(&types.Block{}, cs.ProposalBlockParts.GetReader(),
  1122. cs.state.Params.BlockSizeParams.MaxBytes, &n, &err).(*types.Block)
  1123. // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal
  1124. cs.Logger.Info("Received complete proposal block", "height", cs.ProposalBlock.Height, "hash", cs.ProposalBlock.Hash())
  1125. if cs.Step == cstypes.RoundStepPropose && cs.isProposalComplete() {
  1126. // Move onto the next step
  1127. cs.enterPrevote(height, cs.Round)
  1128. } else if cs.Step == cstypes.RoundStepCommit {
  1129. // If we're waiting on the proposal block...
  1130. cs.tryFinalizeCommit(height)
  1131. }
  1132. return true, err
  1133. }
  1134. return added, nil
  1135. }
  1136. // Attempt to add the vote. if its a duplicate signature, dupeout the validator
  1137. func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerKey string) error {
  1138. _, err := cs.addVote(vote, peerKey)
  1139. if err != nil {
  1140. // If the vote height is off, we'll just ignore it,
  1141. // But if it's a conflicting sig, broadcast evidence tx for slashing.
  1142. // If it's otherwise invalid, punish peer.
  1143. if err == ErrVoteHeightMismatch {
  1144. return err
  1145. } else if _, ok := err.(*types.ErrVoteConflictingVotes); ok {
  1146. if bytes.Equal(vote.ValidatorAddress, cs.privValidator.GetAddress()) {
  1147. cs.Logger.Error("Found conflicting vote from ourselves. Did you unsafe_reset a validator?", "height", vote.Height, "round", vote.Round, "type", vote.Type)
  1148. return err
  1149. }
  1150. cs.Logger.Error("Found conflicting vote. Publish evidence (TODO)", "height", vote.Height, "round", vote.Round, "type", vote.Type, "valAddr", vote.ValidatorAddress, "valIndex", vote.ValidatorIndex)
  1151. // TODO: track evidence for inclusion in a block
  1152. return err
  1153. } else {
  1154. // Probably an invalid signature. Bad peer.
  1155. cs.Logger.Error("Error attempting to add vote", "err", err)
  1156. return ErrAddingVote
  1157. }
  1158. }
  1159. return nil
  1160. }
  1161. //-----------------------------------------------------------------------------
  1162. func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool, err error) {
  1163. cs.Logger.Debug("addVote", "voteHeight", vote.Height, "voteType", vote.Type, "valIndex", vote.ValidatorIndex, "csHeight", cs.Height)
  1164. // A precommit for the previous height?
  1165. // These come in while we wait timeoutCommit
  1166. if vote.Height+1 == cs.Height {
  1167. if !(cs.Step == cstypes.RoundStepNewHeight && vote.Type == types.VoteTypePrecommit) {
  1168. // TODO: give the reason ..
  1169. // fmt.Errorf("tryAddVote: Wrong height, not a LastCommit straggler commit.")
  1170. return added, ErrVoteHeightMismatch
  1171. }
  1172. added, err = cs.LastCommit.AddVote(vote)
  1173. if added {
  1174. cs.Logger.Info(cmn.Fmt("Added to lastPrecommits: %v", cs.LastCommit.StringShort()))
  1175. types.FireEventVote(cs.evsw, types.EventDataVote{vote})
  1176. // if we can skip timeoutCommit and have all the votes now,
  1177. if cs.config.SkipTimeoutCommit && cs.LastCommit.HasAll() {
  1178. // go straight to new round (skip timeout commit)
  1179. // cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
  1180. cs.enterNewRound(cs.Height, 0)
  1181. }
  1182. }
  1183. return
  1184. }
  1185. // A prevote/precommit for this height?
  1186. if vote.Height == cs.Height {
  1187. height := cs.Height
  1188. added, err = cs.Votes.AddVote(vote, peerKey)
  1189. if added {
  1190. types.FireEventVote(cs.evsw, types.EventDataVote{vote})
  1191. switch vote.Type {
  1192. case types.VoteTypePrevote:
  1193. prevotes := cs.Votes.Prevotes(vote.Round)
  1194. cs.Logger.Info("Added to prevote", "vote", vote, "prevotes", prevotes.StringShort())
  1195. // First, unlock if prevotes is a valid POL.
  1196. // >> lockRound < POLRound <= unlockOrChangeLockRound (see spec)
  1197. // NOTE: If (lockRound < POLRound) but !(POLRound <= unlockOrChangeLockRound),
  1198. // we'll still enterNewRound(H,vote.R) and enterPrecommit(H,vote.R) to process it
  1199. // there.
  1200. if (cs.LockedBlock != nil) && (cs.LockedRound < vote.Round) && (vote.Round <= cs.Round) {
  1201. blockID, ok := prevotes.TwoThirdsMajority()
  1202. if ok && !cs.LockedBlock.HashesTo(blockID.Hash) {
  1203. cs.Logger.Info("Unlocking because of POL.", "lockedRound", cs.LockedRound, "POLRound", vote.Round)
  1204. cs.LockedRound = 0
  1205. cs.LockedBlock = nil
  1206. cs.LockedBlockParts = nil
  1207. types.FireEventUnlock(cs.evsw, cs.RoundStateEvent())
  1208. }
  1209. }
  1210. if cs.Round <= vote.Round && prevotes.HasTwoThirdsAny() {
  1211. // Round-skip over to PrevoteWait or goto Precommit.
  1212. cs.enterNewRound(height, vote.Round) // if the vote is ahead of us
  1213. if prevotes.HasTwoThirdsMajority() {
  1214. cs.enterPrecommit(height, vote.Round)
  1215. } else {
  1216. cs.enterPrevote(height, vote.Round) // if the vote is ahead of us
  1217. cs.enterPrevoteWait(height, vote.Round)
  1218. }
  1219. } else if cs.Proposal != nil && 0 <= cs.Proposal.POLRound && cs.Proposal.POLRound == vote.Round {
  1220. // If the proposal is now complete, enter prevote of cs.Round.
  1221. if cs.isProposalComplete() {
  1222. cs.enterPrevote(height, cs.Round)
  1223. }
  1224. }
  1225. case types.VoteTypePrecommit:
  1226. precommits := cs.Votes.Precommits(vote.Round)
  1227. cs.Logger.Info("Added to precommit", "vote", vote, "precommits", precommits.StringShort())
  1228. blockID, ok := precommits.TwoThirdsMajority()
  1229. if ok {
  1230. if len(blockID.Hash) == 0 {
  1231. cs.enterNewRound(height, vote.Round+1)
  1232. } else {
  1233. cs.enterNewRound(height, vote.Round)
  1234. cs.enterPrecommit(height, vote.Round)
  1235. cs.enterCommit(height, vote.Round)
  1236. if cs.config.SkipTimeoutCommit && precommits.HasAll() {
  1237. // if we have all the votes now,
  1238. // go straight to new round (skip timeout commit)
  1239. // cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
  1240. cs.enterNewRound(cs.Height, 0)
  1241. }
  1242. }
  1243. } else if cs.Round <= vote.Round && precommits.HasTwoThirdsAny() {
  1244. cs.enterNewRound(height, vote.Round)
  1245. cs.enterPrecommit(height, vote.Round)
  1246. cs.enterPrecommitWait(height, vote.Round)
  1247. }
  1248. default:
  1249. cmn.PanicSanity(cmn.Fmt("Unexpected vote type %X", vote.Type)) // Should not happen.
  1250. }
  1251. }
  1252. // Either duplicate, or error upon cs.Votes.AddByIndex()
  1253. return
  1254. } else {
  1255. err = ErrVoteHeightMismatch
  1256. }
  1257. // Height mismatch, bad peer?
  1258. cs.Logger.Info("Vote ignored and not added", "voteHeight", vote.Height, "csHeight", cs.Height, "err", err)
  1259. return
  1260. }
  1261. func (cs *ConsensusState) signVote(type_ byte, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
  1262. addr := cs.privValidator.GetAddress()
  1263. valIndex, _ := cs.Validators.GetByAddress(addr)
  1264. vote := &types.Vote{
  1265. ValidatorAddress: addr,
  1266. ValidatorIndex: valIndex,
  1267. Height: cs.Height,
  1268. Round: cs.Round,
  1269. Type: type_,
  1270. BlockID: types.BlockID{hash, header},
  1271. }
  1272. err := cs.privValidator.SignVote(cs.state.ChainID, vote)
  1273. return vote, err
  1274. }
  1275. // sign the vote and publish on internalMsgQueue
  1276. func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header types.PartSetHeader) *types.Vote {
  1277. // if we don't have a key or we're not in the validator set, do nothing
  1278. if cs.privValidator == nil || !cs.Validators.HasAddress(cs.privValidator.GetAddress()) {
  1279. return nil
  1280. }
  1281. vote, err := cs.signVote(type_, hash, header)
  1282. if err == nil {
  1283. cs.sendInternalMessage(msgInfo{&VoteMessage{vote}, ""})
  1284. cs.Logger.Info("Signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
  1285. return vote
  1286. } else {
  1287. //if !cs.replayMode {
  1288. cs.Logger.Error("Error signing vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
  1289. //}
  1290. return nil
  1291. }
  1292. }
  1293. //---------------------------------------------------------
  1294. func CompareHRS(h1, r1 int, s1 cstypes.RoundStepType, h2, r2 int, s2 cstypes.RoundStepType) int {
  1295. if h1 < h2 {
  1296. return -1
  1297. } else if h1 > h2 {
  1298. return 1
  1299. }
  1300. if r1 < r2 {
  1301. return -1
  1302. } else if r1 > r2 {
  1303. return 1
  1304. }
  1305. if s1 < s2 {
  1306. return -1
  1307. } else if s1 > s2 {
  1308. return 1
  1309. }
  1310. return 0
  1311. }