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.

1591 lines
54 KiB

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