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.

1542 lines
53 KiB

10 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
10 years ago
8 years ago
8 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
8 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
9 years ago
8 years ago
10 years ago
8 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
8 years ago
10 years ago
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"
  7. "reflect"
  8. "sync"
  9. "time"
  10. fail "github.com/ebuchman/fail-test"
  11. wire "github.com/tendermint/go-wire"
  12. cfg "github.com/tendermint/tendermint/config"
  13. "github.com/tendermint/tendermint/proxy"
  14. sm "github.com/tendermint/tendermint/state"
  15. "github.com/tendermint/tendermint/types"
  16. cmn "github.com/tendermint/tmlibs/common"
  17. "github.com/tendermint/tmlibs/log"
  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. func (cs *ConsensusState) receiveRoutine(maxSteps int) {
  525. for {
  526. if maxSteps > 0 {
  527. if cs.nSteps >= maxSteps {
  528. cs.Logger.Info("reached max steps. exiting receive routine")
  529. cs.nSteps = 0
  530. return
  531. }
  532. }
  533. rs := cs.RoundState
  534. var mi msgInfo
  535. select {
  536. case mi = <-cs.peerMsgQueue:
  537. cs.wal.Save(mi)
  538. // handles proposals, block parts, votes
  539. // may generate internal events (votes, complete proposals, 2/3 majorities)
  540. cs.handleMsg(mi, rs)
  541. case mi = <-cs.internalMsgQueue:
  542. cs.wal.Save(mi)
  543. // handles proposals, block parts, votes
  544. cs.handleMsg(mi, rs)
  545. case ti := <-cs.timeoutTicker.Chan(): // tockChan:
  546. cs.wal.Save(ti)
  547. // if the timeout is relevant to the rs
  548. // go to the next step
  549. cs.handleTimeout(ti, rs)
  550. case <-cs.Quit:
  551. // NOTE: the internalMsgQueue may have signed messages from our
  552. // priv_val that haven't hit the WAL, but its ok because
  553. // priv_val tracks LastSig
  554. // close wal now that we're done writing to it
  555. if cs.wal != nil {
  556. cs.wal.Stop()
  557. }
  558. close(cs.done)
  559. return
  560. }
  561. }
  562. }
  563. // state transitions on complete-proposal, 2/3-any, 2/3-one
  564. func (cs *ConsensusState) handleMsg(mi msgInfo, rs RoundState) {
  565. cs.mtx.Lock()
  566. defer cs.mtx.Unlock()
  567. var err error
  568. msg, peerKey := mi.Msg, mi.PeerKey
  569. switch msg := msg.(type) {
  570. case *ProposalMessage:
  571. // will not cause transition.
  572. // once proposal is set, we can receive block parts
  573. err = cs.setProposal(msg.Proposal)
  574. case *BlockPartMessage:
  575. // if the proposal is complete, we'll enterPrevote or tryFinalizeCommit
  576. _, err = cs.addProposalBlockPart(msg.Height, msg.Part, peerKey != "")
  577. if err != nil && msg.Round != cs.Round {
  578. err = nil
  579. }
  580. case *VoteMessage:
  581. // attempt to add the vote and dupeout the validator if its a duplicate signature
  582. // if the vote gives us a 2/3-any or 2/3-one, we transition
  583. err := cs.tryAddVote(msg.Vote, peerKey)
  584. if err == ErrAddingVote {
  585. // TODO: punish peer
  586. }
  587. // NOTE: the vote is broadcast to peers by the reactor listening
  588. // for vote events
  589. // TODO: If rs.Height == vote.Height && rs.Round < vote.Round,
  590. // the peer is sending us CatchupCommit precommits.
  591. // We could make note of this and help filter in broadcastHasVoteMessage().
  592. default:
  593. cs.Logger.Error("Unknown msg type", reflect.TypeOf(msg))
  594. }
  595. if err != nil {
  596. cs.Logger.Error("Error with msg", "type", reflect.TypeOf(msg), "peer", peerKey, "err", err, "msg", msg)
  597. }
  598. }
  599. func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs RoundState) {
  600. cs.Logger.Debug("Received tock", "timeout", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step)
  601. // timeouts must be for current height, round, step
  602. if ti.Height != rs.Height || ti.Round < rs.Round || (ti.Round == rs.Round && ti.Step < rs.Step) {
  603. cs.Logger.Debug("Ignoring tock because we're ahead", "height", rs.Height, "round", rs.Round, "step", rs.Step)
  604. return
  605. }
  606. // the timeout will now cause a state transition
  607. cs.mtx.Lock()
  608. defer cs.mtx.Unlock()
  609. switch ti.Step {
  610. case RoundStepNewHeight:
  611. // NewRound event fired from enterNewRound.
  612. // XXX: should we fire timeout here (for timeout commit)?
  613. cs.enterNewRound(ti.Height, 0)
  614. case RoundStepPropose:
  615. types.FireEventTimeoutPropose(cs.evsw, cs.RoundStateEvent())
  616. cs.enterPrevote(ti.Height, ti.Round)
  617. case RoundStepPrevoteWait:
  618. types.FireEventTimeoutWait(cs.evsw, cs.RoundStateEvent())
  619. cs.enterPrecommit(ti.Height, ti.Round)
  620. case RoundStepPrecommitWait:
  621. types.FireEventTimeoutWait(cs.evsw, cs.RoundStateEvent())
  622. cs.enterNewRound(ti.Height, ti.Round+1)
  623. default:
  624. panic(cmn.Fmt("Invalid timeout step: %v", ti.Step))
  625. }
  626. }
  627. //-----------------------------------------------------------------------------
  628. // State functions
  629. // Used internally by handleTimeout and handleMsg to make state transitions
  630. // Enter: `timeoutNewHeight` by startTime (commitTime+timeoutCommit),
  631. // or, if SkipTimeout==true, after receiving all precommits from (height,round-1)
  632. // Enter: `timeoutPrecommits` after any +2/3 precommits from (height,round-1)
  633. // Enter: +2/3 precommits for nil at (height,round-1)
  634. // Enter: +2/3 prevotes any or +2/3 precommits for block or any from (height, round)
  635. // NOTE: cs.StartTime was already set for height.
  636. func (cs *ConsensusState) enterNewRound(height int, round int) {
  637. if cs.Height != height || round < cs.Round || (cs.Round == round && cs.Step != RoundStepNewHeight) {
  638. cs.Logger.Debug(cmn.Fmt("enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  639. return
  640. }
  641. if now := time.Now(); cs.StartTime.After(now) {
  642. cs.Logger.Info("Need to set a buffer and log message here for sanity.", "startTime", cs.StartTime, "now", now)
  643. }
  644. cs.Logger.Info(cmn.Fmt("enterNewRound(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  645. // Increment validators if necessary
  646. validators := cs.Validators
  647. if cs.Round < round {
  648. validators = validators.Copy()
  649. validators.IncrementAccum(round - cs.Round)
  650. }
  651. // Setup new round
  652. // we don't fire newStep for this step,
  653. // but we fire an event, so update the round step first
  654. cs.updateRoundStep(round, RoundStepNewRound)
  655. cs.Validators = validators
  656. if round == 0 {
  657. // We've already reset these upon new height,
  658. // and meanwhile we might have received a proposal
  659. // for round 0.
  660. } else {
  661. cs.Proposal = nil
  662. cs.ProposalBlock = nil
  663. cs.ProposalBlockParts = nil
  664. }
  665. cs.Votes.SetRound(round + 1) // also track next round (round+1) to allow round-skipping
  666. types.FireEventNewRound(cs.evsw, cs.RoundStateEvent())
  667. // Immediately go to enterPropose.
  668. cs.enterPropose(height, round)
  669. }
  670. // Enter: from enterNewRound(height,round).
  671. func (cs *ConsensusState) enterPropose(height int, round int) {
  672. if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPropose <= cs.Step) {
  673. cs.Logger.Debug(cmn.Fmt("enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  674. return
  675. }
  676. cs.Logger.Info(cmn.Fmt("enterPropose(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  677. defer func() {
  678. // Done enterPropose:
  679. cs.updateRoundStep(round, RoundStepPropose)
  680. cs.newStep()
  681. // If we have the whole proposal + POL, then goto Prevote now.
  682. // else, we'll enterPrevote when the rest of the proposal is received (in AddProposalBlockPart),
  683. // or else after timeoutPropose
  684. if cs.isProposalComplete() {
  685. cs.enterPrevote(height, cs.Round)
  686. }
  687. }()
  688. // If we don't get the proposal and all block parts quick enough, enterPrevote
  689. cs.scheduleTimeout(cs.config.Propose(round), height, round, RoundStepPropose)
  690. // Nothing more to do if we're not a validator
  691. if cs.privValidator == nil {
  692. cs.Logger.Debug("This node is not a validator")
  693. return
  694. }
  695. if !bytes.Equal(cs.Validators.GetProposer().Address, cs.privValidator.GetAddress()) {
  696. cs.Logger.Info("enterPropose: Not our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
  697. if cs.Validators.HasAddress(cs.privValidator.GetAddress()) {
  698. cs.Logger.Debug("This node is a validator")
  699. } else {
  700. cs.Logger.Debug("This node is not a validator")
  701. }
  702. } else {
  703. cs.Logger.Info("enterPropose: Our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
  704. cs.Logger.Debug("This node is a validator")
  705. cs.decideProposal(height, round)
  706. }
  707. }
  708. func (cs *ConsensusState) defaultDecideProposal(height, round int) {
  709. var block *types.Block
  710. var blockParts *types.PartSet
  711. // Decide on block
  712. if cs.LockedBlock != nil {
  713. // If we're locked onto a block, just choose that.
  714. block, blockParts = cs.LockedBlock, cs.LockedBlockParts
  715. } else {
  716. // Create a new proposal block from state/txs from the mempool.
  717. block, blockParts = cs.createProposalBlock()
  718. if block == nil { // on error
  719. return
  720. }
  721. }
  722. // Make proposal
  723. polRound, polBlockID := cs.Votes.POLInfo()
  724. proposal := types.NewProposal(height, round, blockParts.Header(), polRound, polBlockID)
  725. err := cs.privValidator.SignProposal(cs.state.ChainID, proposal)
  726. if err == nil {
  727. // Set fields
  728. /* fields set by setProposal and addBlockPart
  729. cs.Proposal = proposal
  730. cs.ProposalBlock = block
  731. cs.ProposalBlockParts = blockParts
  732. */
  733. // send proposal and block parts on internal msg queue
  734. cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""})
  735. for i := 0; i < blockParts.Total(); i++ {
  736. part := blockParts.GetPart(i)
  737. cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""})
  738. }
  739. cs.Logger.Info("Signed proposal", "height", height, "round", round, "proposal", proposal)
  740. cs.Logger.Debug(cmn.Fmt("Signed proposal block: %v", block))
  741. } else {
  742. if !cs.replayMode {
  743. cs.Logger.Error("enterPropose: Error signing proposal", "height", height, "round", round, "err", err)
  744. }
  745. }
  746. }
  747. // Returns true if the proposal block is complete &&
  748. // (if POLRound was proposed, we have +2/3 prevotes from there).
  749. func (cs *ConsensusState) isProposalComplete() bool {
  750. if cs.Proposal == nil || cs.ProposalBlock == nil {
  751. return false
  752. }
  753. // we have the proposal. if there's a POLRound,
  754. // make sure we have the prevotes from it too
  755. if cs.Proposal.POLRound < 0 {
  756. return true
  757. } else {
  758. // if this is false the proposer is lying or we haven't received the POL yet
  759. return cs.Votes.Prevotes(cs.Proposal.POLRound).HasTwoThirdsMajority()
  760. }
  761. }
  762. // Create the next block to propose and return it.
  763. // Returns nil block upon error.
  764. // NOTE: keep it side-effect free for clarity.
  765. func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts *types.PartSet) {
  766. var commit *types.Commit
  767. if cs.Height == 1 {
  768. // We're creating a proposal for the first block.
  769. // The commit is empty, but not nil.
  770. commit = &types.Commit{}
  771. } else if cs.LastCommit.HasTwoThirdsMajority() {
  772. // Make the commit from LastCommit
  773. commit = cs.LastCommit.MakeCommit()
  774. } else {
  775. // This shouldn't happen.
  776. cs.Logger.Error("enterPropose: Cannot propose anything: No commit for the previous block.")
  777. return
  778. }
  779. // Mempool validated transactions
  780. txs := cs.mempool.Reap(cs.config.MaxBlockSizeTxs)
  781. return types.MakeBlock(cs.Height, cs.state.ChainID, txs, commit,
  782. cs.state.LastBlockID, cs.state.Validators.Hash(), cs.state.AppHash, cs.config.BlockPartSize)
  783. }
  784. // Enter: `timeoutPropose` after entering Propose.
  785. // Enter: proposal block and POL is ready.
  786. // Enter: any +2/3 prevotes for future round.
  787. // Prevote for LockedBlock if we're locked, or ProposalBlock if valid.
  788. // Otherwise vote nil.
  789. func (cs *ConsensusState) enterPrevote(height int, round int) {
  790. if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrevote <= cs.Step) {
  791. cs.Logger.Debug(cmn.Fmt("enterPrevote(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  792. return
  793. }
  794. defer func() {
  795. // Done enterPrevote:
  796. cs.updateRoundStep(round, RoundStepPrevote)
  797. cs.newStep()
  798. }()
  799. // fire event for how we got here
  800. if cs.isProposalComplete() {
  801. types.FireEventCompleteProposal(cs.evsw, cs.RoundStateEvent())
  802. } else {
  803. // we received +2/3 prevotes for a future round
  804. // TODO: catchup event?
  805. }
  806. cs.Logger.Info(cmn.Fmt("enterPrevote(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  807. // Sign and broadcast vote as necessary
  808. cs.doPrevote(height, round)
  809. // Once `addVote` hits any +2/3 prevotes, we will go to PrevoteWait
  810. // (so we have more time to try and collect +2/3 prevotes for a single block)
  811. }
  812. func (cs *ConsensusState) defaultDoPrevote(height int, round int) {
  813. logger := cs.Logger.With("height", height, "round", round)
  814. // If a block is locked, prevote that.
  815. if cs.LockedBlock != nil {
  816. logger.Info("enterPrevote: Block was locked")
  817. cs.signAddVote(types.VoteTypePrevote, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header())
  818. return
  819. }
  820. // If ProposalBlock is nil, prevote nil.
  821. if cs.ProposalBlock == nil {
  822. logger.Info("enterPrevote: ProposalBlock is nil")
  823. cs.signAddVote(types.VoteTypePrevote, nil, types.PartSetHeader{})
  824. return
  825. }
  826. // Validate proposal block
  827. err := cs.state.ValidateBlock(cs.ProposalBlock)
  828. if err != nil {
  829. // ProposalBlock is invalid, prevote nil.
  830. logger.Error("enterPrevote: ProposalBlock is invalid", "err", err)
  831. cs.signAddVote(types.VoteTypePrevote, nil, types.PartSetHeader{})
  832. return
  833. }
  834. // Prevote cs.ProposalBlock
  835. // NOTE: the proposal signature is validated when it is received,
  836. // and the proposal block parts are validated as they are received (against the merkle hash in the proposal)
  837. logger.Info("enterPrevote: ProposalBlock is valid")
  838. cs.signAddVote(types.VoteTypePrevote, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header())
  839. }
  840. // Enter: any +2/3 prevotes at next round.
  841. func (cs *ConsensusState) enterPrevoteWait(height int, round int) {
  842. if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrevoteWait <= cs.Step) {
  843. cs.Logger.Debug(cmn.Fmt("enterPrevoteWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  844. return
  845. }
  846. if !cs.Votes.Prevotes(round).HasTwoThirdsAny() {
  847. cmn.PanicSanity(cmn.Fmt("enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes", height, round))
  848. }
  849. cs.Logger.Info(cmn.Fmt("enterPrevoteWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  850. defer func() {
  851. // Done enterPrevoteWait:
  852. cs.updateRoundStep(round, RoundStepPrevoteWait)
  853. cs.newStep()
  854. }()
  855. // Wait for some more prevotes; enterPrecommit
  856. cs.scheduleTimeout(cs.config.Prevote(round), height, round, RoundStepPrevoteWait)
  857. }
  858. // Enter: `timeoutPrevote` after any +2/3 prevotes.
  859. // Enter: +2/3 precomits for block or nil.
  860. // Enter: any +2/3 precommits for next round.
  861. // Lock & precommit the ProposalBlock if we have enough prevotes for it (a POL in this round)
  862. // else, unlock an existing lock and precommit nil if +2/3 of prevotes were nil,
  863. // else, precommit nil otherwise.
  864. func (cs *ConsensusState) enterPrecommit(height int, round int) {
  865. if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrecommit <= cs.Step) {
  866. cs.Logger.Debug(cmn.Fmt("enterPrecommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  867. return
  868. }
  869. cs.Logger.Info(cmn.Fmt("enterPrecommit(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  870. defer func() {
  871. // Done enterPrecommit:
  872. cs.updateRoundStep(round, RoundStepPrecommit)
  873. cs.newStep()
  874. }()
  875. blockID, ok := cs.Votes.Prevotes(round).TwoThirdsMajority()
  876. // If we don't have a polka, we must precommit nil
  877. if !ok {
  878. if cs.LockedBlock != nil {
  879. cs.Logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit while we're locked. Precommitting nil")
  880. } else {
  881. cs.Logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit. Precommitting nil.")
  882. }
  883. cs.signAddVote(types.VoteTypePrecommit, nil, types.PartSetHeader{})
  884. return
  885. }
  886. // At this point +2/3 prevoted for a particular block or nil
  887. types.FireEventPolka(cs.evsw, cs.RoundStateEvent())
  888. // the latest POLRound should be this round
  889. polRound, _ := cs.Votes.POLInfo()
  890. if polRound < round {
  891. cmn.PanicSanity(cmn.Fmt("This POLRound should be %v but got %", round, polRound))
  892. }
  893. // +2/3 prevoted nil. Unlock and precommit nil.
  894. if len(blockID.Hash) == 0 {
  895. if cs.LockedBlock == nil {
  896. cs.Logger.Info("enterPrecommit: +2/3 prevoted for nil.")
  897. } else {
  898. cs.Logger.Info("enterPrecommit: +2/3 prevoted for nil. Unlocking")
  899. cs.LockedRound = 0
  900. cs.LockedBlock = nil
  901. cs.LockedBlockParts = nil
  902. types.FireEventUnlock(cs.evsw, cs.RoundStateEvent())
  903. }
  904. cs.signAddVote(types.VoteTypePrecommit, nil, types.PartSetHeader{})
  905. return
  906. }
  907. // At this point, +2/3 prevoted for a particular block.
  908. // If we're already locked on that block, precommit it, and update the LockedRound
  909. if cs.LockedBlock.HashesTo(blockID.Hash) {
  910. cs.Logger.Info("enterPrecommit: +2/3 prevoted locked block. Relocking")
  911. cs.LockedRound = round
  912. types.FireEventRelock(cs.evsw, cs.RoundStateEvent())
  913. cs.signAddVote(types.VoteTypePrecommit, blockID.Hash, blockID.PartsHeader)
  914. return
  915. }
  916. // If +2/3 prevoted for proposal block, stage and precommit it
  917. if cs.ProposalBlock.HashesTo(blockID.Hash) {
  918. cs.Logger.Info("enterPrecommit: +2/3 prevoted proposal block. Locking", "hash", blockID.Hash)
  919. // Validate the block.
  920. if err := cs.state.ValidateBlock(cs.ProposalBlock); err != nil {
  921. cmn.PanicConsensus(cmn.Fmt("enterPrecommit: +2/3 prevoted for an invalid block: %v", err))
  922. }
  923. cs.LockedRound = round
  924. cs.LockedBlock = cs.ProposalBlock
  925. cs.LockedBlockParts = cs.ProposalBlockParts
  926. types.FireEventLock(cs.evsw, cs.RoundStateEvent())
  927. cs.signAddVote(types.VoteTypePrecommit, blockID.Hash, blockID.PartsHeader)
  928. return
  929. }
  930. // There was a polka in this round for a block we don't have.
  931. // Fetch that block, unlock, and precommit nil.
  932. // The +2/3 prevotes for this round is the POL for our unlock.
  933. // TODO: In the future save the POL prevotes for justification.
  934. cs.LockedRound = 0
  935. cs.LockedBlock = nil
  936. cs.LockedBlockParts = nil
  937. if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
  938. cs.ProposalBlock = nil
  939. cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
  940. }
  941. types.FireEventUnlock(cs.evsw, cs.RoundStateEvent())
  942. cs.signAddVote(types.VoteTypePrecommit, nil, types.PartSetHeader{})
  943. }
  944. // Enter: any +2/3 precommits for next round.
  945. func (cs *ConsensusState) enterPrecommitWait(height int, round int) {
  946. if cs.Height != height || round < cs.Round || (cs.Round == round && RoundStepPrecommitWait <= cs.Step) {
  947. cs.Logger.Debug(cmn.Fmt("enterPrecommitWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  948. return
  949. }
  950. if !cs.Votes.Precommits(round).HasTwoThirdsAny() {
  951. cmn.PanicSanity(cmn.Fmt("enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes", height, round))
  952. }
  953. cs.Logger.Info(cmn.Fmt("enterPrecommitWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  954. defer func() {
  955. // Done enterPrecommitWait:
  956. cs.updateRoundStep(round, RoundStepPrecommitWait)
  957. cs.newStep()
  958. }()
  959. // Wait for some more precommits; enterNewRound
  960. cs.scheduleTimeout(cs.config.Precommit(round), height, round, RoundStepPrecommitWait)
  961. }
  962. // Enter: +2/3 precommits for block
  963. func (cs *ConsensusState) enterCommit(height int, commitRound int) {
  964. if cs.Height != height || RoundStepCommit <= cs.Step {
  965. cs.Logger.Debug(cmn.Fmt("enterCommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step))
  966. return
  967. }
  968. cs.Logger.Info(cmn.Fmt("enterCommit(%v/%v). Current: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step))
  969. defer func() {
  970. // Done enterCommit:
  971. // keep cs.Round the same, commitRound points to the right Precommits set.
  972. cs.updateRoundStep(cs.Round, RoundStepCommit)
  973. cs.CommitRound = commitRound
  974. cs.CommitTime = time.Now()
  975. cs.newStep()
  976. // Maybe finalize immediately.
  977. cs.tryFinalizeCommit(height)
  978. }()
  979. blockID, ok := cs.Votes.Precommits(commitRound).TwoThirdsMajority()
  980. if !ok {
  981. cmn.PanicSanity("RunActionCommit() expects +2/3 precommits")
  982. }
  983. // The Locked* fields no longer matter.
  984. // Move them over to ProposalBlock if they match the commit hash,
  985. // otherwise they'll be cleared in updateToState.
  986. if cs.LockedBlock.HashesTo(blockID.Hash) {
  987. cs.ProposalBlock = cs.LockedBlock
  988. cs.ProposalBlockParts = cs.LockedBlockParts
  989. }
  990. // If we don't have the block being committed, set up to get it.
  991. if !cs.ProposalBlock.HashesTo(blockID.Hash) {
  992. if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
  993. // We're getting the wrong block.
  994. // Set up ProposalBlockParts and keep waiting.
  995. cs.ProposalBlock = nil
  996. cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
  997. } else {
  998. // We just need to keep waiting.
  999. }
  1000. }
  1001. }
  1002. // If we have the block AND +2/3 commits for it, finalize.
  1003. func (cs *ConsensusState) tryFinalizeCommit(height int) {
  1004. if cs.Height != height {
  1005. cmn.PanicSanity(cmn.Fmt("tryFinalizeCommit() cs.Height: %v vs height: %v", cs.Height, height))
  1006. }
  1007. blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority()
  1008. if !ok || len(blockID.Hash) == 0 {
  1009. cs.Logger.Error("Attempt to finalize failed. There was no +2/3 majority, or +2/3 was for <nil>.", "height", height)
  1010. return
  1011. }
  1012. if !cs.ProposalBlock.HashesTo(blockID.Hash) {
  1013. // TODO: this happens every time if we're not a validator (ugly logs)
  1014. // TODO: ^^ wait, why does it matter that we're a validator?
  1015. 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)
  1016. return
  1017. }
  1018. // go
  1019. cs.finalizeCommit(height)
  1020. }
  1021. // Increment height and goto RoundStepNewHeight
  1022. func (cs *ConsensusState) finalizeCommit(height int) {
  1023. if cs.Height != height || cs.Step != RoundStepCommit {
  1024. cs.Logger.Debug(cmn.Fmt("finalizeCommit(%v): Invalid args. Current step: %v/%v/%v", height, cs.Height, cs.Round, cs.Step))
  1025. return
  1026. }
  1027. blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority()
  1028. block, blockParts := cs.ProposalBlock, cs.ProposalBlockParts
  1029. if !ok {
  1030. cmn.PanicSanity(cmn.Fmt("Cannot finalizeCommit, commit does not have two thirds majority"))
  1031. }
  1032. if !blockParts.HasHeader(blockID.PartsHeader) {
  1033. cmn.PanicSanity(cmn.Fmt("Expected ProposalBlockParts header to be commit header"))
  1034. }
  1035. if !block.HashesTo(blockID.Hash) {
  1036. cmn.PanicSanity(cmn.Fmt("Cannot finalizeCommit, ProposalBlock does not hash to commit hash"))
  1037. }
  1038. if err := cs.state.ValidateBlock(block); err != nil {
  1039. cmn.PanicConsensus(cmn.Fmt("+2/3 committed an invalid block: %v", err))
  1040. }
  1041. cs.Logger.Info(cmn.Fmt("Finalizing commit of block with %d txs", block.NumTxs),
  1042. "height", block.Height, "hash", block.Hash(), "root", block.AppHash)
  1043. cs.Logger.Info(cmn.Fmt("%v", block))
  1044. fail.Fail() // XXX
  1045. // Save to blockStore.
  1046. if cs.blockStore.Height() < block.Height {
  1047. // NOTE: the seenCommit is local justification to commit this block,
  1048. // but may differ from the LastCommit included in the next block
  1049. precommits := cs.Votes.Precommits(cs.CommitRound)
  1050. seenCommit := precommits.MakeCommit()
  1051. cs.blockStore.SaveBlock(block, blockParts, seenCommit)
  1052. } else {
  1053. // Happens during replay if we already saved the block but didn't commit
  1054. cs.Logger.Info("Calling finalizeCommit on already stored block", "height", block.Height)
  1055. }
  1056. fail.Fail() // XXX
  1057. // Finish writing to the WAL for this height.
  1058. // NOTE: If we fail before writing this, we'll never write it,
  1059. // and just recover by running ApplyBlock in the Handshake.
  1060. // If we moved it before persisting the block, we'd have to allow
  1061. // WAL replay for blocks with an #ENDHEIGHT
  1062. // As is, ConsensusState should not be started again
  1063. // until we successfully call ApplyBlock (ie. here or in Handshake after restart)
  1064. if cs.wal != nil {
  1065. cs.wal.writeEndHeight(height)
  1066. }
  1067. fail.Fail() // XXX
  1068. // Create a copy of the state for staging
  1069. // and an event cache for txs
  1070. stateCopy := cs.state.Copy()
  1071. eventCache := types.NewEventCache(cs.evsw)
  1072. // Execute and commit the block, update and save the state, and update the mempool.
  1073. // All calls to the proxyAppConn come here.
  1074. // NOTE: the block.AppHash wont reflect these txs until the next block
  1075. err := stateCopy.ApplyBlock(eventCache, cs.proxyAppConn, block, blockParts.Header(), cs.mempool)
  1076. if err != nil {
  1077. cs.Logger.Error("Error on ApplyBlock. Did the application crash? Please restart tendermint", "err", err)
  1078. return
  1079. }
  1080. fail.Fail() // XXX
  1081. // Fire event for new block.
  1082. // NOTE: If we fail before firing, these events will never fire
  1083. //
  1084. // TODO: Either
  1085. // * Fire before persisting state, in ApplyBlock
  1086. // * Fire on start up if we haven't written any new WAL msgs
  1087. // Both options mean we may fire more than once. Is that fine ?
  1088. types.FireEventNewBlock(cs.evsw, types.EventDataNewBlock{block})
  1089. types.FireEventNewBlockHeader(cs.evsw, types.EventDataNewBlockHeader{block.Header})
  1090. eventCache.Flush()
  1091. fail.Fail() // XXX
  1092. // NewHeightStep!
  1093. cs.updateToState(stateCopy)
  1094. fail.Fail() // XXX
  1095. // cs.StartTime is already set.
  1096. // Schedule Round0 to start soon.
  1097. cs.scheduleRound0(&cs.RoundState)
  1098. // By here,
  1099. // * cs.Height has been increment to height+1
  1100. // * cs.Step is now RoundStepNewHeight
  1101. // * cs.StartTime is set to when we will start round0.
  1102. }
  1103. //-----------------------------------------------------------------------------
  1104. func (cs *ConsensusState) defaultSetProposal(proposal *types.Proposal) error {
  1105. // Already have one
  1106. // TODO: possibly catch double proposals
  1107. if cs.Proposal != nil {
  1108. return nil
  1109. }
  1110. // Does not apply
  1111. if proposal.Height != cs.Height || proposal.Round != cs.Round {
  1112. return nil
  1113. }
  1114. // We don't care about the proposal if we're already in RoundStepCommit.
  1115. if RoundStepCommit <= cs.Step {
  1116. return nil
  1117. }
  1118. // Verify POLRound, which must be -1 or between 0 and proposal.Round exclusive.
  1119. if proposal.POLRound != -1 &&
  1120. (proposal.POLRound < 0 || proposal.Round <= proposal.POLRound) {
  1121. return ErrInvalidProposalPOLRound
  1122. }
  1123. // Verify signature
  1124. if !cs.Validators.GetProposer().PubKey.VerifyBytes(types.SignBytes(cs.state.ChainID, proposal), proposal.Signature) {
  1125. return ErrInvalidProposalSignature
  1126. }
  1127. cs.Proposal = proposal
  1128. cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockPartsHeader)
  1129. return nil
  1130. }
  1131. // NOTE: block is not necessarily valid.
  1132. // Asynchronously triggers either enterPrevote (before we timeout of propose) or tryFinalizeCommit, once we have the full block.
  1133. func (cs *ConsensusState) addProposalBlockPart(height int, part *types.Part, verify bool) (added bool, err error) {
  1134. // Blocks might be reused, so round mismatch is OK
  1135. if cs.Height != height {
  1136. return false, nil
  1137. }
  1138. // We're not expecting a block part.
  1139. if cs.ProposalBlockParts == nil {
  1140. return false, nil // TODO: bad peer? Return error?
  1141. }
  1142. added, err = cs.ProposalBlockParts.AddPart(part, verify)
  1143. if err != nil {
  1144. return added, err
  1145. }
  1146. if added && cs.ProposalBlockParts.IsComplete() {
  1147. // Added and completed!
  1148. var n int
  1149. var err error
  1150. cs.ProposalBlock = wire.ReadBinary(&types.Block{}, cs.ProposalBlockParts.GetReader(), types.MaxBlockSize, &n, &err).(*types.Block)
  1151. // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal
  1152. cs.Logger.Info("Received complete proposal block", "height", cs.ProposalBlock.Height, "hash", cs.ProposalBlock.Hash())
  1153. if cs.Step == RoundStepPropose && cs.isProposalComplete() {
  1154. // Move onto the next step
  1155. cs.enterPrevote(height, cs.Round)
  1156. } else if cs.Step == RoundStepCommit {
  1157. // If we're waiting on the proposal block...
  1158. cs.tryFinalizeCommit(height)
  1159. }
  1160. return true, err
  1161. }
  1162. return added, nil
  1163. }
  1164. // Attempt to add the vote. if its a duplicate signature, dupeout the validator
  1165. func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerKey string) error {
  1166. _, err := cs.addVote(vote, peerKey)
  1167. if err != nil {
  1168. // If the vote height is off, we'll just ignore it,
  1169. // But if it's a conflicting sig, broadcast evidence tx for slashing.
  1170. // If it's otherwise invalid, punish peer.
  1171. if err == ErrVoteHeightMismatch {
  1172. return err
  1173. } else if _, ok := err.(*types.ErrVoteConflictingVotes); ok {
  1174. if bytes.Equal(vote.ValidatorAddress, cs.privValidator.GetAddress()) {
  1175. cs.Logger.Error("Found conflicting vote from ourselves. Did you unsafe_reset a validator?", "height", vote.Height, "round", vote.Round, "type", vote.Type)
  1176. return err
  1177. }
  1178. cs.Logger.Error("Found conflicting vote. Publish evidence (TODO)", "height", vote.Height, "round", vote.Round, "type", vote.Type, "valAddr", vote.ValidatorAddress, "valIndex", vote.ValidatorIndex)
  1179. // TODO: track evidence for inclusion in a block
  1180. return err
  1181. } else {
  1182. // Probably an invalid signature. Bad peer.
  1183. cs.Logger.Error("Error attempting to add vote", "err", err)
  1184. return ErrAddingVote
  1185. }
  1186. }
  1187. return nil
  1188. }
  1189. //-----------------------------------------------------------------------------
  1190. func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool, err error) {
  1191. cs.Logger.Debug("addVote", "voteHeight", vote.Height, "voteType", vote.Type, "valIndex", vote.ValidatorIndex, "csHeight", cs.Height)
  1192. // A precommit for the previous height?
  1193. // These come in while we wait timeoutCommit
  1194. if vote.Height+1 == cs.Height {
  1195. if !(cs.Step == RoundStepNewHeight && vote.Type == types.VoteTypePrecommit) {
  1196. // TODO: give the reason ..
  1197. // fmt.Errorf("tryAddVote: Wrong height, not a LastCommit straggler commit.")
  1198. return added, ErrVoteHeightMismatch
  1199. }
  1200. added, err = cs.LastCommit.AddVote(vote)
  1201. if added {
  1202. cs.Logger.Info(cmn.Fmt("Added to lastPrecommits: %v", cs.LastCommit.StringShort()))
  1203. types.FireEventVote(cs.evsw, types.EventDataVote{vote})
  1204. // if we can skip timeoutCommit and have all the votes now,
  1205. if cs.config.SkipTimeoutCommit && cs.LastCommit.HasAll() {
  1206. // go straight to new round (skip timeout commit)
  1207. // cs.scheduleTimeout(time.Duration(0), cs.Height, 0, RoundStepNewHeight)
  1208. cs.enterNewRound(cs.Height, 0)
  1209. }
  1210. }
  1211. return
  1212. }
  1213. // A prevote/precommit for this height?
  1214. if vote.Height == cs.Height {
  1215. height := cs.Height
  1216. added, err = cs.Votes.AddVote(vote, peerKey)
  1217. if added {
  1218. types.FireEventVote(cs.evsw, types.EventDataVote{vote})
  1219. switch vote.Type {
  1220. case types.VoteTypePrevote:
  1221. prevotes := cs.Votes.Prevotes(vote.Round)
  1222. cs.Logger.Info("Added to prevote", "vote", vote, "prevotes", prevotes.StringShort())
  1223. // First, unlock if prevotes is a valid POL.
  1224. // >> lockRound < POLRound <= unlockOrChangeLockRound (see spec)
  1225. // NOTE: If (lockRound < POLRound) but !(POLRound <= unlockOrChangeLockRound),
  1226. // we'll still enterNewRound(H,vote.R) and enterPrecommit(H,vote.R) to process it
  1227. // there.
  1228. if (cs.LockedBlock != nil) && (cs.LockedRound < vote.Round) && (vote.Round <= cs.Round) {
  1229. blockID, ok := prevotes.TwoThirdsMajority()
  1230. if ok && !cs.LockedBlock.HashesTo(blockID.Hash) {
  1231. cs.Logger.Info("Unlocking because of POL.", "lockedRound", cs.LockedRound, "POLRound", vote.Round)
  1232. cs.LockedRound = 0
  1233. cs.LockedBlock = nil
  1234. cs.LockedBlockParts = nil
  1235. types.FireEventUnlock(cs.evsw, cs.RoundStateEvent())
  1236. }
  1237. }
  1238. if cs.Round <= vote.Round && prevotes.HasTwoThirdsAny() {
  1239. // Round-skip over to PrevoteWait or goto Precommit.
  1240. cs.enterNewRound(height, vote.Round) // if the vote is ahead of us
  1241. if prevotes.HasTwoThirdsMajority() {
  1242. cs.enterPrecommit(height, vote.Round)
  1243. } else {
  1244. cs.enterPrevote(height, vote.Round) // if the vote is ahead of us
  1245. cs.enterPrevoteWait(height, vote.Round)
  1246. }
  1247. } else if cs.Proposal != nil && 0 <= cs.Proposal.POLRound && cs.Proposal.POLRound == vote.Round {
  1248. // If the proposal is now complete, enter prevote of cs.Round.
  1249. if cs.isProposalComplete() {
  1250. cs.enterPrevote(height, cs.Round)
  1251. }
  1252. }
  1253. case types.VoteTypePrecommit:
  1254. precommits := cs.Votes.Precommits(vote.Round)
  1255. cs.Logger.Info("Added to precommit", "vote", vote, "precommits", precommits.StringShort())
  1256. blockID, ok := precommits.TwoThirdsMajority()
  1257. if ok {
  1258. if len(blockID.Hash) == 0 {
  1259. cs.enterNewRound(height, vote.Round+1)
  1260. } else {
  1261. cs.enterNewRound(height, vote.Round)
  1262. cs.enterPrecommit(height, vote.Round)
  1263. cs.enterCommit(height, vote.Round)
  1264. if cs.config.SkipTimeoutCommit && precommits.HasAll() {
  1265. // if we have all the votes now,
  1266. // go straight to new round (skip timeout commit)
  1267. // cs.scheduleTimeout(time.Duration(0), cs.Height, 0, RoundStepNewHeight)
  1268. cs.enterNewRound(cs.Height, 0)
  1269. }
  1270. }
  1271. } else if cs.Round <= vote.Round && precommits.HasTwoThirdsAny() {
  1272. cs.enterNewRound(height, vote.Round)
  1273. cs.enterPrecommit(height, vote.Round)
  1274. cs.enterPrecommitWait(height, vote.Round)
  1275. }
  1276. default:
  1277. cmn.PanicSanity(cmn.Fmt("Unexpected vote type %X", vote.Type)) // Should not happen.
  1278. }
  1279. }
  1280. // Either duplicate, or error upon cs.Votes.AddByIndex()
  1281. return
  1282. } else {
  1283. err = ErrVoteHeightMismatch
  1284. }
  1285. // Height mismatch, bad peer?
  1286. cs.Logger.Info("Vote ignored and not added", "voteHeight", vote.Height, "csHeight", cs.Height, "err", err)
  1287. return
  1288. }
  1289. func (cs *ConsensusState) signVote(type_ byte, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
  1290. addr := cs.privValidator.GetAddress()
  1291. valIndex, _ := cs.Validators.GetByAddress(addr)
  1292. vote := &types.Vote{
  1293. ValidatorAddress: addr,
  1294. ValidatorIndex: valIndex,
  1295. Height: cs.Height,
  1296. Round: cs.Round,
  1297. Type: type_,
  1298. BlockID: types.BlockID{hash, header},
  1299. }
  1300. err := cs.privValidator.SignVote(cs.state.ChainID, vote)
  1301. return vote, err
  1302. }
  1303. // sign the vote and publish on internalMsgQueue
  1304. func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header types.PartSetHeader) *types.Vote {
  1305. // if we don't have a key or we're not in the validator set, do nothing
  1306. if cs.privValidator == nil || !cs.Validators.HasAddress(cs.privValidator.GetAddress()) {
  1307. return nil
  1308. }
  1309. vote, err := cs.signVote(type_, hash, header)
  1310. if err == nil {
  1311. cs.sendInternalMessage(msgInfo{&VoteMessage{vote}, ""})
  1312. cs.Logger.Info("Signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
  1313. return vote
  1314. } else {
  1315. //if !cs.replayMode {
  1316. cs.Logger.Error("Error signing vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
  1317. //}
  1318. return nil
  1319. }
  1320. }
  1321. //---------------------------------------------------------
  1322. func CompareHRS(h1, r1 int, s1 RoundStepType, h2, r2 int, s2 RoundStepType) int {
  1323. if h1 < h2 {
  1324. return -1
  1325. } else if h1 > h2 {
  1326. return 1
  1327. }
  1328. if r1 < r2 {
  1329. return -1
  1330. } else if r1 > r2 {
  1331. return 1
  1332. }
  1333. if s1 < s2 {
  1334. return -1
  1335. } else if s1 > s2 {
  1336. return 1
  1337. }
  1338. return 0
  1339. }