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.

1620 lines
55 KiB

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