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.

1583 lines
53 KiB

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