github.com/evdatsion/aphelion-dpos-bft@v0.32.1/consensus/state.go (about)

     1  package consensus
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"runtime/debug"
     8  	"sync"
     9  	"time"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common"
    14  	"github.com/evdatsion/aphelion-dpos-bft/libs/fail"
    15  	"github.com/evdatsion/aphelion-dpos-bft/libs/log"
    16  	tmtime "github.com/evdatsion/aphelion-dpos-bft/types/time"
    17  
    18  	cfg "github.com/evdatsion/aphelion-dpos-bft/config"
    19  	cstypes "github.com/evdatsion/aphelion-dpos-bft/consensus/types"
    20  	tmevents "github.com/evdatsion/aphelion-dpos-bft/libs/events"
    21  	"github.com/evdatsion/aphelion-dpos-bft/p2p"
    22  	sm "github.com/evdatsion/aphelion-dpos-bft/state"
    23  	"github.com/evdatsion/aphelion-dpos-bft/types"
    24  )
    25  
    26  //-----------------------------------------------------------------------------
    27  // Errors
    28  
    29  var (
    30  	ErrInvalidProposalSignature = errors.New("Error invalid proposal signature")
    31  	ErrInvalidProposalPOLRound  = errors.New("Error invalid proposal POL round")
    32  	ErrAddingVote               = errors.New("Error adding vote")
    33  	ErrVoteHeightMismatch       = errors.New("Error vote height mismatch")
    34  )
    35  
    36  //-----------------------------------------------------------------------------
    37  
    38  var (
    39  	msgQueueSize = 1000
    40  )
    41  
    42  // msgs from the reactor which may update the state
    43  type msgInfo struct {
    44  	Msg    ConsensusMessage `json:"msg"`
    45  	PeerID p2p.ID           `json:"peer_key"`
    46  }
    47  
    48  // internally generated messages which may update the state
    49  type timeoutInfo struct {
    50  	Duration time.Duration         `json:"duration"`
    51  	Height   int64                 `json:"height"`
    52  	Round    int                   `json:"round"`
    53  	Step     cstypes.RoundStepType `json:"step"`
    54  }
    55  
    56  func (ti *timeoutInfo) String() string {
    57  	return fmt.Sprintf("%v ; %d/%d %v", ti.Duration, ti.Height, ti.Round, ti.Step)
    58  }
    59  
    60  // interface to the mempool
    61  type txNotifier interface {
    62  	TxsAvailable() <-chan struct{}
    63  }
    64  
    65  // interface to the evidence pool
    66  type evidencePool interface {
    67  	AddEvidence(types.Evidence) error
    68  }
    69  
    70  // ConsensusState handles execution of the consensus algorithm.
    71  // It processes votes and proposals, and upon reaching agreement,
    72  // commits blocks to the chain and executes them against the application.
    73  // The internal state machine receives input from peers, the internal validator, and from a timer.
    74  type ConsensusState struct {
    75  	cmn.BaseService
    76  
    77  	// config details
    78  	config        *cfg.ConsensusConfig
    79  	privValidator types.PrivValidator // for signing votes
    80  
    81  	// store blocks and commits
    82  	blockStore sm.BlockStore
    83  
    84  	// create and execute blocks
    85  	blockExec *sm.BlockExecutor
    86  
    87  	// notify us if txs are available
    88  	txNotifier txNotifier
    89  
    90  	// add evidence to the pool
    91  	// when it's detected
    92  	evpool evidencePool
    93  
    94  	// internal state
    95  	mtx sync.RWMutex
    96  	cstypes.RoundState
    97  	state sm.State // State until height-1.
    98  
    99  	// state changes may be triggered by: msgs from peers,
   100  	// msgs from ourself, or by timeouts
   101  	peerMsgQueue     chan msgInfo
   102  	internalMsgQueue chan msgInfo
   103  	timeoutTicker    TimeoutTicker
   104  
   105  	// information about about added votes and block parts are written on this channel
   106  	// so statistics can be computed by reactor
   107  	statsMsgQueue chan msgInfo
   108  
   109  	// we use eventBus to trigger msg broadcasts in the reactor,
   110  	// and to notify external subscribers, eg. through a websocket
   111  	eventBus *types.EventBus
   112  
   113  	// a Write-Ahead Log ensures we can recover from any kind of crash
   114  	// and helps us avoid signing conflicting votes
   115  	wal          WAL
   116  	replayMode   bool // so we don't log signing errors during replay
   117  	doWALCatchup bool // determines if we even try to do the catchup
   118  
   119  	// for tests where we want to limit the number of transitions the state makes
   120  	nSteps int
   121  
   122  	// some functions can be overwritten for testing
   123  	decideProposal func(height int64, round int)
   124  	doPrevote      func(height int64, round int)
   125  	setProposal    func(proposal *types.Proposal) error
   126  
   127  	// closed when we finish shutting down
   128  	done chan struct{}
   129  
   130  	// synchronous pubsub between consensus state and reactor.
   131  	// state only emits EventNewRoundStep and EventVote
   132  	evsw tmevents.EventSwitch
   133  
   134  	// for reporting metrics
   135  	metrics *Metrics
   136  }
   137  
   138  // StateOption sets an optional parameter on the ConsensusState.
   139  type StateOption func(*ConsensusState)
   140  
   141  // NewConsensusState returns a new ConsensusState.
   142  func NewConsensusState(
   143  	config *cfg.ConsensusConfig,
   144  	state sm.State,
   145  	blockExec *sm.BlockExecutor,
   146  	blockStore sm.BlockStore,
   147  	txNotifier txNotifier,
   148  	evpool evidencePool,
   149  	options ...StateOption,
   150  ) *ConsensusState {
   151  	cs := &ConsensusState{
   152  		config:           config,
   153  		blockExec:        blockExec,
   154  		blockStore:       blockStore,
   155  		txNotifier:       txNotifier,
   156  		peerMsgQueue:     make(chan msgInfo, msgQueueSize),
   157  		internalMsgQueue: make(chan msgInfo, msgQueueSize),
   158  		timeoutTicker:    NewTimeoutTicker(),
   159  		statsMsgQueue:    make(chan msgInfo, msgQueueSize),
   160  		done:             make(chan struct{}),
   161  		doWALCatchup:     true,
   162  		wal:              nilWAL{},
   163  		evpool:           evpool,
   164  		evsw:             tmevents.NewEventSwitch(),
   165  		metrics:          NopMetrics(),
   166  	}
   167  	// set function defaults (may be overwritten before calling Start)
   168  	cs.decideProposal = cs.defaultDecideProposal
   169  	cs.doPrevote = cs.defaultDoPrevote
   170  	cs.setProposal = cs.defaultSetProposal
   171  
   172  	cs.updateToState(state)
   173  
   174  	// Don't call scheduleRound0 yet.
   175  	// We do that upon Start().
   176  	cs.reconstructLastCommit(state)
   177  	cs.BaseService = *cmn.NewBaseService(nil, "ConsensusState", cs)
   178  	for _, option := range options {
   179  		option(cs)
   180  	}
   181  	return cs
   182  }
   183  
   184  //----------------------------------------
   185  // Public interface
   186  
   187  // SetLogger implements Service.
   188  func (cs *ConsensusState) SetLogger(l log.Logger) {
   189  	cs.BaseService.Logger = l
   190  	cs.timeoutTicker.SetLogger(l)
   191  }
   192  
   193  // SetEventBus sets event bus.
   194  func (cs *ConsensusState) SetEventBus(b *types.EventBus) {
   195  	cs.eventBus = b
   196  	cs.blockExec.SetEventBus(b)
   197  }
   198  
   199  // StateMetrics sets the metrics.
   200  func StateMetrics(metrics *Metrics) StateOption {
   201  	return func(cs *ConsensusState) { cs.metrics = metrics }
   202  }
   203  
   204  // String returns a string.
   205  func (cs *ConsensusState) String() string {
   206  	// better not to access shared variables
   207  	return fmt.Sprintf("ConsensusState") //(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
   208  }
   209  
   210  // GetState returns a copy of the chain state.
   211  func (cs *ConsensusState) GetState() sm.State {
   212  	cs.mtx.RLock()
   213  	defer cs.mtx.RUnlock()
   214  	return cs.state.Copy()
   215  }
   216  
   217  // GetLastHeight returns the last height committed.
   218  // If there were no blocks, returns 0.
   219  func (cs *ConsensusState) GetLastHeight() int64 {
   220  	cs.mtx.RLock()
   221  	defer cs.mtx.RUnlock()
   222  	return cs.RoundState.Height - 1
   223  }
   224  
   225  // GetRoundState returns a shallow copy of the internal consensus state.
   226  func (cs *ConsensusState) GetRoundState() *cstypes.RoundState {
   227  	cs.mtx.RLock()
   228  	rs := cs.RoundState // copy
   229  	cs.mtx.RUnlock()
   230  	return &rs
   231  }
   232  
   233  // GetRoundStateJSON returns a json of RoundState, marshalled using go-amino.
   234  func (cs *ConsensusState) GetRoundStateJSON() ([]byte, error) {
   235  	cs.mtx.RLock()
   236  	defer cs.mtx.RUnlock()
   237  	return cdc.MarshalJSON(cs.RoundState)
   238  }
   239  
   240  // GetRoundStateSimpleJSON returns a json of RoundStateSimple, marshalled using go-amino.
   241  func (cs *ConsensusState) GetRoundStateSimpleJSON() ([]byte, error) {
   242  	cs.mtx.RLock()
   243  	defer cs.mtx.RUnlock()
   244  	return cdc.MarshalJSON(cs.RoundState.RoundStateSimple())
   245  }
   246  
   247  // GetValidators returns a copy of the current validators.
   248  func (cs *ConsensusState) GetValidators() (int64, []*types.Validator) {
   249  	cs.mtx.RLock()
   250  	defer cs.mtx.RUnlock()
   251  	return cs.state.LastBlockHeight, cs.state.Validators.Copy().Validators
   252  }
   253  
   254  // SetPrivValidator sets the private validator account for signing votes.
   255  func (cs *ConsensusState) SetPrivValidator(priv types.PrivValidator) {
   256  	cs.mtx.Lock()
   257  	cs.privValidator = priv
   258  	cs.mtx.Unlock()
   259  }
   260  
   261  // SetTimeoutTicker sets the local timer. It may be useful to overwrite for testing.
   262  func (cs *ConsensusState) SetTimeoutTicker(timeoutTicker TimeoutTicker) {
   263  	cs.mtx.Lock()
   264  	cs.timeoutTicker = timeoutTicker
   265  	cs.mtx.Unlock()
   266  }
   267  
   268  // LoadCommit loads the commit for a given height.
   269  func (cs *ConsensusState) LoadCommit(height int64) *types.Commit {
   270  	cs.mtx.RLock()
   271  	defer cs.mtx.RUnlock()
   272  	if height == cs.blockStore.Height() {
   273  		return cs.blockStore.LoadSeenCommit(height)
   274  	}
   275  	return cs.blockStore.LoadBlockCommit(height)
   276  }
   277  
   278  // OnStart implements cmn.Service.
   279  // It loads the latest state via the WAL, and starts the timeout and receive routines.
   280  func (cs *ConsensusState) OnStart() error {
   281  	if err := cs.evsw.Start(); err != nil {
   282  		return err
   283  	}
   284  
   285  	// we may set the WAL in testing before calling Start,
   286  	// so only OpenWAL if its still the nilWAL
   287  	if _, ok := cs.wal.(nilWAL); ok {
   288  		walFile := cs.config.WalFile()
   289  		wal, err := cs.OpenWAL(walFile)
   290  		if err != nil {
   291  			cs.Logger.Error("Error loading ConsensusState wal", "err", err.Error())
   292  			return err
   293  		}
   294  		cs.wal = wal
   295  	}
   296  
   297  	// we need the timeoutRoutine for replay so
   298  	// we don't block on the tick chan.
   299  	// NOTE: we will get a build up of garbage go routines
   300  	// firing on the tockChan until the receiveRoutine is started
   301  	// to deal with them (by that point, at most one will be valid)
   302  	if err := cs.timeoutTicker.Start(); err != nil {
   303  		return err
   304  	}
   305  
   306  	// we may have lost some votes if the process crashed
   307  	// reload from consensus log to catchup
   308  	if cs.doWALCatchup {
   309  		if err := cs.catchupReplay(cs.Height); err != nil {
   310  			// don't try to recover from data corruption error
   311  			if IsDataCorruptionError(err) {
   312  				cs.Logger.Error("Encountered corrupt WAL file", "err", err.Error())
   313  				cs.Logger.Error("Please repair the WAL file before restarting")
   314  				fmt.Println(`You can attempt to repair the WAL as follows:
   315  
   316  ----
   317  WALFILE=~/.tendermint/data/cs.wal/wal
   318  cp $WALFILE ${WALFILE}.bak # backup the file
   319  go run scripts/wal2json/main.go $WALFILE > wal.json # this will panic, but can be ignored
   320  rm $WALFILE # remove the corrupt file
   321  go run scripts/json2wal/main.go wal.json $WALFILE # rebuild the file without corruption
   322  ----`)
   323  
   324  				return err
   325  			}
   326  
   327  			cs.Logger.Error("Error on catchup replay. Proceeding to start ConsensusState anyway", "err", err.Error())
   328  			// NOTE: if we ever do return an error here,
   329  			// make sure to stop the timeoutTicker
   330  		}
   331  	}
   332  
   333  	// now start the receiveRoutine
   334  	go cs.receiveRoutine(0)
   335  
   336  	// schedule the first round!
   337  	// use GetRoundState so we don't race the receiveRoutine for access
   338  	cs.scheduleRound0(cs.GetRoundState())
   339  
   340  	return nil
   341  }
   342  
   343  // timeoutRoutine: receive requests for timeouts on tickChan and fire timeouts on tockChan
   344  // receiveRoutine: serializes processing of proposoals, block parts, votes; coordinates state transitions
   345  func (cs *ConsensusState) startRoutines(maxSteps int) {
   346  	err := cs.timeoutTicker.Start()
   347  	if err != nil {
   348  		cs.Logger.Error("Error starting timeout ticker", "err", err)
   349  		return
   350  	}
   351  	go cs.receiveRoutine(maxSteps)
   352  }
   353  
   354  // OnStop implements cmn.Service.
   355  func (cs *ConsensusState) OnStop() {
   356  	cs.evsw.Stop()
   357  	cs.timeoutTicker.Stop()
   358  	// WAL is stopped in receiveRoutine.
   359  }
   360  
   361  // Wait waits for the the main routine to return.
   362  // NOTE: be sure to Stop() the event switch and drain
   363  // any event channels or this may deadlock
   364  func (cs *ConsensusState) Wait() {
   365  	<-cs.done
   366  }
   367  
   368  // OpenWAL opens a file to log all consensus messages and timeouts for deterministic accountability
   369  func (cs *ConsensusState) OpenWAL(walFile string) (WAL, error) {
   370  	wal, err := NewWAL(walFile)
   371  	if err != nil {
   372  		cs.Logger.Error("Failed to open WAL for consensus state", "wal", walFile, "err", err)
   373  		return nil, err
   374  	}
   375  	wal.SetLogger(cs.Logger.With("wal", walFile))
   376  	if err := wal.Start(); err != nil {
   377  		return nil, err
   378  	}
   379  	return wal, nil
   380  }
   381  
   382  //------------------------------------------------------------
   383  // Public interface for passing messages into the consensus state, possibly causing a state transition.
   384  // If peerID == "", the msg is considered internal.
   385  // Messages are added to the appropriate queue (peer or internal).
   386  // If the queue is full, the function may block.
   387  // TODO: should these return anything or let callers just use events?
   388  
   389  // AddVote inputs a vote.
   390  func (cs *ConsensusState) AddVote(vote *types.Vote, peerID p2p.ID) (added bool, err error) {
   391  	if peerID == "" {
   392  		cs.internalMsgQueue <- msgInfo{&VoteMessage{vote}, ""}
   393  	} else {
   394  		cs.peerMsgQueue <- msgInfo{&VoteMessage{vote}, peerID}
   395  	}
   396  
   397  	// TODO: wait for event?!
   398  	return false, nil
   399  }
   400  
   401  // SetProposal inputs a proposal.
   402  func (cs *ConsensusState) SetProposal(proposal *types.Proposal, peerID p2p.ID) error {
   403  
   404  	if peerID == "" {
   405  		cs.internalMsgQueue <- msgInfo{&ProposalMessage{proposal}, ""}
   406  	} else {
   407  		cs.peerMsgQueue <- msgInfo{&ProposalMessage{proposal}, peerID}
   408  	}
   409  
   410  	// TODO: wait for event?!
   411  	return nil
   412  }
   413  
   414  // AddProposalBlockPart inputs a part of the proposal block.
   415  func (cs *ConsensusState) AddProposalBlockPart(height int64, round int, part *types.Part, peerID p2p.ID) error {
   416  
   417  	if peerID == "" {
   418  		cs.internalMsgQueue <- msgInfo{&BlockPartMessage{height, round, part}, ""}
   419  	} else {
   420  		cs.peerMsgQueue <- msgInfo{&BlockPartMessage{height, round, part}, peerID}
   421  	}
   422  
   423  	// TODO: wait for event?!
   424  	return nil
   425  }
   426  
   427  // SetProposalAndBlock inputs the proposal and all block parts.
   428  func (cs *ConsensusState) SetProposalAndBlock(proposal *types.Proposal, block *types.Block, parts *types.PartSet, peerID p2p.ID) error {
   429  	if err := cs.SetProposal(proposal, peerID); err != nil {
   430  		return err
   431  	}
   432  	for i := 0; i < parts.Total(); i++ {
   433  		part := parts.GetPart(i)
   434  		if err := cs.AddProposalBlockPart(proposal.Height, proposal.Round, part, peerID); err != nil {
   435  			return err
   436  		}
   437  	}
   438  	return nil
   439  }
   440  
   441  //------------------------------------------------------------
   442  // internal functions for managing the state
   443  
   444  func (cs *ConsensusState) updateHeight(height int64) {
   445  	cs.metrics.Height.Set(float64(height))
   446  	cs.Height = height
   447  }
   448  
   449  func (cs *ConsensusState) updateRoundStep(round int, step cstypes.RoundStepType) {
   450  	cs.Round = round
   451  	cs.Step = step
   452  }
   453  
   454  // enterNewRound(height, 0) at cs.StartTime.
   455  func (cs *ConsensusState) scheduleRound0(rs *cstypes.RoundState) {
   456  	//cs.Logger.Info("scheduleRound0", "now", tmtime.Now(), "startTime", cs.StartTime)
   457  	sleepDuration := rs.StartTime.Sub(tmtime.Now())
   458  	cs.scheduleTimeout(sleepDuration, rs.Height, 0, cstypes.RoundStepNewHeight)
   459  }
   460  
   461  // Attempt to schedule a timeout (by sending timeoutInfo on the tickChan)
   462  func (cs *ConsensusState) scheduleTimeout(duration time.Duration, height int64, round int, step cstypes.RoundStepType) {
   463  	cs.timeoutTicker.ScheduleTimeout(timeoutInfo{duration, height, round, step})
   464  }
   465  
   466  // send a msg into the receiveRoutine regarding our own proposal, block part, or vote
   467  func (cs *ConsensusState) sendInternalMessage(mi msgInfo) {
   468  	select {
   469  	case cs.internalMsgQueue <- mi:
   470  	default:
   471  		// NOTE: using the go-routine means our votes can
   472  		// be processed out of order.
   473  		// TODO: use CList here for strict determinism and
   474  		// attempt push to internalMsgQueue in receiveRoutine
   475  		cs.Logger.Info("Internal msg queue is full. Using a go-routine")
   476  		go func() { cs.internalMsgQueue <- mi }()
   477  	}
   478  }
   479  
   480  // Reconstruct LastCommit from SeenCommit, which we saved along with the block,
   481  // (which happens even before saving the state)
   482  func (cs *ConsensusState) reconstructLastCommit(state sm.State) {
   483  	if state.LastBlockHeight == 0 {
   484  		return
   485  	}
   486  	seenCommit := cs.blockStore.LoadSeenCommit(state.LastBlockHeight)
   487  	lastPrecommits := types.CommitToVoteSet(state.ChainID, seenCommit, state.LastValidators)
   488  	if !lastPrecommits.HasTwoThirdsMajority() {
   489  		panic("Failed to reconstruct LastCommit: Does not have +2/3 maj")
   490  	}
   491  	cs.LastCommit = lastPrecommits
   492  }
   493  
   494  // Updates ConsensusState and increments height to match that of state.
   495  // The round becomes 0 and cs.Step becomes cstypes.RoundStepNewHeight.
   496  func (cs *ConsensusState) updateToState(state sm.State) {
   497  	if cs.CommitRound > -1 && 0 < cs.Height && cs.Height != state.LastBlockHeight {
   498  		panic(fmt.Sprintf("updateToState() expected state height of %v but found %v",
   499  			cs.Height, state.LastBlockHeight))
   500  	}
   501  	if !cs.state.IsEmpty() && cs.state.LastBlockHeight+1 != cs.Height {
   502  		// This might happen when someone else is mutating cs.state.
   503  		// Someone forgot to pass in state.Copy() somewhere?!
   504  		panic(fmt.Sprintf("Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v",
   505  			cs.state.LastBlockHeight+1, cs.Height))
   506  	}
   507  
   508  	// If state isn't further out than cs.state, just ignore.
   509  	// This happens when SwitchToConsensus() is called in the reactor.
   510  	// We don't want to reset e.g. the Votes, but we still want to
   511  	// signal the new round step, because other services (eg. txNotifier)
   512  	// depend on having an up-to-date peer state!
   513  	if !cs.state.IsEmpty() && (state.LastBlockHeight <= cs.state.LastBlockHeight) {
   514  		cs.Logger.Info("Ignoring updateToState()", "newHeight", state.LastBlockHeight+1, "oldHeight", cs.state.LastBlockHeight+1)
   515  		cs.newStep()
   516  		return
   517  	}
   518  
   519  	// Reset fields based on state.
   520  	validators := state.Validators
   521  	lastPrecommits := (*types.VoteSet)(nil)
   522  	if cs.CommitRound > -1 && cs.Votes != nil {
   523  		if !cs.Votes.Precommits(cs.CommitRound).HasTwoThirdsMajority() {
   524  			panic("updateToState(state) called but last Precommit round didn't have +2/3")
   525  		}
   526  		lastPrecommits = cs.Votes.Precommits(cs.CommitRound)
   527  	}
   528  
   529  	// Next desired block height
   530  	height := state.LastBlockHeight + 1
   531  
   532  	// RoundState fields
   533  	cs.updateHeight(height)
   534  	cs.updateRoundStep(0, cstypes.RoundStepNewHeight)
   535  	if cs.CommitTime.IsZero() {
   536  		// "Now" makes it easier to sync up dev nodes.
   537  		// We add timeoutCommit to allow transactions
   538  		// to be gathered for the first block.
   539  		// And alternative solution that relies on clocks:
   540  		//  cs.StartTime = state.LastBlockTime.Add(timeoutCommit)
   541  		cs.StartTime = cs.config.Commit(tmtime.Now())
   542  	} else {
   543  		cs.StartTime = cs.config.Commit(cs.CommitTime)
   544  	}
   545  
   546  	cs.Validators = validators
   547  	cs.Proposal = nil
   548  	cs.ProposalBlock = nil
   549  	cs.ProposalBlockParts = nil
   550  	cs.LockedRound = -1
   551  	cs.LockedBlock = nil
   552  	cs.LockedBlockParts = nil
   553  	cs.ValidRound = -1
   554  	cs.ValidBlock = nil
   555  	cs.ValidBlockParts = nil
   556  	cs.Votes = cstypes.NewHeightVoteSet(state.ChainID, height, validators)
   557  	cs.CommitRound = -1
   558  	cs.LastCommit = lastPrecommits
   559  	cs.LastValidators = state.LastValidators
   560  	cs.TriggeredTimeoutPrecommit = false
   561  
   562  	cs.state = state
   563  
   564  	// Finally, broadcast RoundState
   565  	cs.newStep()
   566  }
   567  
   568  func (cs *ConsensusState) newStep() {
   569  	rs := cs.RoundStateEvent()
   570  	cs.wal.Write(rs)
   571  	cs.nSteps++
   572  	// newStep is called by updateToState in NewConsensusState before the eventBus is set!
   573  	if cs.eventBus != nil {
   574  		cs.eventBus.PublishEventNewRoundStep(rs)
   575  		cs.evsw.FireEvent(types.EventNewRoundStep, &cs.RoundState)
   576  	}
   577  }
   578  
   579  //-----------------------------------------
   580  // the main go routines
   581  
   582  // receiveRoutine handles messages which may cause state transitions.
   583  // it's argument (n) is the number of messages to process before exiting - use 0 to run forever
   584  // It keeps the RoundState and is the only thing that updates it.
   585  // Updates (state transitions) happen on timeouts, complete proposals, and 2/3 majorities.
   586  // ConsensusState must be locked before any internal state is updated.
   587  func (cs *ConsensusState) receiveRoutine(maxSteps int) {
   588  	onExit := func(cs *ConsensusState) {
   589  		// NOTE: the internalMsgQueue may have signed messages from our
   590  		// priv_val that haven't hit the WAL, but its ok because
   591  		// priv_val tracks LastSig
   592  
   593  		// close wal now that we're done writing to it
   594  		cs.wal.Stop()
   595  		cs.wal.Wait()
   596  
   597  		close(cs.done)
   598  	}
   599  
   600  	defer func() {
   601  		if r := recover(); r != nil {
   602  			cs.Logger.Error("CONSENSUS FAILURE!!!", "err", r, "stack", string(debug.Stack()))
   603  			// stop gracefully
   604  			//
   605  			// NOTE: We most probably shouldn't be running any further when there is
   606  			// some unexpected panic. Some unknown error happened, and so we don't
   607  			// know if that will result in the validator signing an invalid thing. It
   608  			// might be worthwhile to explore a mechanism for manual resuming via
   609  			// some console or secure RPC system, but for now, halting the chain upon
   610  			// unexpected consensus bugs sounds like the better option.
   611  			onExit(cs)
   612  		}
   613  	}()
   614  
   615  	for {
   616  		if maxSteps > 0 {
   617  			if cs.nSteps >= maxSteps {
   618  				cs.Logger.Info("reached max steps. exiting receive routine")
   619  				cs.nSteps = 0
   620  				return
   621  			}
   622  		}
   623  		rs := cs.RoundState
   624  		var mi msgInfo
   625  
   626  		select {
   627  		case <-cs.txNotifier.TxsAvailable():
   628  			cs.handleTxsAvailable()
   629  		case mi = <-cs.peerMsgQueue:
   630  			cs.wal.Write(mi)
   631  			// handles proposals, block parts, votes
   632  			// may generate internal events (votes, complete proposals, 2/3 majorities)
   633  			cs.handleMsg(mi)
   634  		case mi = <-cs.internalMsgQueue:
   635  			cs.wal.WriteSync(mi) // NOTE: fsync
   636  
   637  			if _, ok := mi.Msg.(*VoteMessage); ok {
   638  				// we actually want to simulate failing during
   639  				// the previous WriteSync, but this isn't easy to do.
   640  				// Equivalent would be to fail here and manually remove
   641  				// some bytes from the end of the wal.
   642  				fail.Fail() // XXX
   643  			}
   644  
   645  			// handles proposals, block parts, votes
   646  			cs.handleMsg(mi)
   647  		case ti := <-cs.timeoutTicker.Chan(): // tockChan:
   648  			cs.wal.Write(ti)
   649  			// if the timeout is relevant to the rs
   650  			// go to the next step
   651  			cs.handleTimeout(ti, rs)
   652  		case <-cs.Quit():
   653  			onExit(cs)
   654  			return
   655  		}
   656  	}
   657  }
   658  
   659  // state transitions on complete-proposal, 2/3-any, 2/3-one
   660  func (cs *ConsensusState) handleMsg(mi msgInfo) {
   661  	cs.mtx.Lock()
   662  	defer cs.mtx.Unlock()
   663  
   664  	var (
   665  		added bool
   666  		err   error
   667  	)
   668  	msg, peerID := mi.Msg, mi.PeerID
   669  	switch msg := msg.(type) {
   670  	case *ProposalMessage:
   671  		// will not cause transition.
   672  		// once proposal is set, we can receive block parts
   673  		err = cs.setProposal(msg.Proposal)
   674  	case *BlockPartMessage:
   675  		// if the proposal is complete, we'll enterPrevote or tryFinalizeCommit
   676  		added, err = cs.addProposalBlockPart(msg, peerID)
   677  		if added {
   678  			cs.statsMsgQueue <- mi
   679  		}
   680  
   681  		if err != nil && msg.Round != cs.Round {
   682  			cs.Logger.Debug("Received block part from wrong round", "height", cs.Height, "csRound", cs.Round, "blockRound", msg.Round)
   683  			err = nil
   684  		}
   685  	case *VoteMessage:
   686  		// attempt to add the vote and dupeout the validator if its a duplicate signature
   687  		// if the vote gives us a 2/3-any or 2/3-one, we transition
   688  		added, err = cs.tryAddVote(msg.Vote, peerID)
   689  		if added {
   690  			cs.statsMsgQueue <- mi
   691  		}
   692  
   693  		if err == ErrAddingVote {
   694  			// TODO: punish peer
   695  			// We probably don't want to stop the peer here. The vote does not
   696  			// necessarily comes from a malicious peer but can be just broadcasted by
   697  			// a typical peer.
   698  			// https://github.com/evdatsion/aphelion-dpos-bft/issues/1281
   699  		}
   700  
   701  		// NOTE: the vote is broadcast to peers by the reactor listening
   702  		// for vote events
   703  
   704  		// TODO: If rs.Height == vote.Height && rs.Round < vote.Round,
   705  		// the peer is sending us CatchupCommit precommits.
   706  		// We could make note of this and help filter in broadcastHasVoteMessage().
   707  	default:
   708  		cs.Logger.Error("Unknown msg type", "type", reflect.TypeOf(msg))
   709  		return
   710  	}
   711  
   712  	if err != nil {
   713  		// Causes TestReactorValidatorSetChanges to timeout
   714  		// https://github.com/evdatsion/aphelion-dpos-bft/issues/3406
   715  		// cs.Logger.Error("Error with msg", "height", cs.Height, "round", cs.Round,
   716  		// 	"peer", peerID, "err", err, "msg", msg)
   717  	}
   718  }
   719  
   720  func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs cstypes.RoundState) {
   721  	cs.Logger.Debug("Received tock", "timeout", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step)
   722  
   723  	// timeouts must be for current height, round, step
   724  	if ti.Height != rs.Height || ti.Round < rs.Round || (ti.Round == rs.Round && ti.Step < rs.Step) {
   725  		cs.Logger.Debug("Ignoring tock because we're ahead", "height", rs.Height, "round", rs.Round, "step", rs.Step)
   726  		return
   727  	}
   728  
   729  	// the timeout will now cause a state transition
   730  	cs.mtx.Lock()
   731  	defer cs.mtx.Unlock()
   732  
   733  	switch ti.Step {
   734  	case cstypes.RoundStepNewHeight:
   735  		// NewRound event fired from enterNewRound.
   736  		// XXX: should we fire timeout here (for timeout commit)?
   737  		cs.enterNewRound(ti.Height, 0)
   738  	case cstypes.RoundStepNewRound:
   739  		cs.enterPropose(ti.Height, 0)
   740  	case cstypes.RoundStepPropose:
   741  		cs.eventBus.PublishEventTimeoutPropose(cs.RoundStateEvent())
   742  		cs.enterPrevote(ti.Height, ti.Round)
   743  	case cstypes.RoundStepPrevoteWait:
   744  		cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent())
   745  		cs.enterPrecommit(ti.Height, ti.Round)
   746  	case cstypes.RoundStepPrecommitWait:
   747  		cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent())
   748  		cs.enterPrecommit(ti.Height, ti.Round)
   749  		cs.enterNewRound(ti.Height, ti.Round+1)
   750  	default:
   751  		panic(fmt.Sprintf("Invalid timeout step: %v", ti.Step))
   752  	}
   753  
   754  }
   755  
   756  func (cs *ConsensusState) handleTxsAvailable() {
   757  	cs.mtx.Lock()
   758  	defer cs.mtx.Unlock()
   759  	// we only need to do this for round 0
   760  	cs.enterNewRound(cs.Height, 0)
   761  	cs.enterPropose(cs.Height, 0)
   762  }
   763  
   764  //-----------------------------------------------------------------------------
   765  // State functions
   766  // Used internally by handleTimeout and handleMsg to make state transitions
   767  
   768  // Enter: `timeoutNewHeight` by startTime (commitTime+timeoutCommit),
   769  // 	or, if SkipTimeout==true, after receiving all precommits from (height,round-1)
   770  // Enter: `timeoutPrecommits` after any +2/3 precommits from (height,round-1)
   771  // Enter: +2/3 precommits for nil at (height,round-1)
   772  // Enter: +2/3 prevotes any or +2/3 precommits for block or any from (height, round)
   773  // NOTE: cs.StartTime was already set for height.
   774  func (cs *ConsensusState) enterNewRound(height int64, round int) {
   775  	logger := cs.Logger.With("height", height, "round", round)
   776  
   777  	if cs.Height != height || round < cs.Round || (cs.Round == round && cs.Step != cstypes.RoundStepNewHeight) {
   778  		logger.Debug(fmt.Sprintf("enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
   779  		return
   780  	}
   781  
   782  	if now := tmtime.Now(); cs.StartTime.After(now) {
   783  		logger.Info("Need to set a buffer and log message here for sanity.", "startTime", cs.StartTime, "now", now)
   784  	}
   785  
   786  	logger.Info(fmt.Sprintf("enterNewRound(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
   787  
   788  	// Increment validators if necessary
   789  	validators := cs.Validators
   790  	if cs.Round < round {
   791  		validators = validators.Copy()
   792  		validators.IncrementProposerPriority(round - cs.Round)
   793  	}
   794  
   795  	// Setup new round
   796  	// we don't fire newStep for this step,
   797  	// but we fire an event, so update the round step first
   798  	cs.updateRoundStep(round, cstypes.RoundStepNewRound)
   799  	cs.Validators = validators
   800  	if round == 0 {
   801  		// We've already reset these upon new height,
   802  		// and meanwhile we might have received a proposal
   803  		// for round 0.
   804  	} else {
   805  		logger.Info("Resetting Proposal info")
   806  		cs.Proposal = nil
   807  		cs.ProposalBlock = nil
   808  		cs.ProposalBlockParts = nil
   809  	}
   810  	cs.Votes.SetRound(round + 1) // also track next round (round+1) to allow round-skipping
   811  	cs.TriggeredTimeoutPrecommit = false
   812  
   813  	cs.eventBus.PublishEventNewRound(cs.NewRoundEvent())
   814  	cs.metrics.Rounds.Set(float64(round))
   815  
   816  	// Wait for txs to be available in the mempool
   817  	// before we enterPropose in round 0. If the last block changed the app hash,
   818  	// we may need an empty "proof" block, and enterPropose immediately.
   819  	waitForTxs := cs.config.WaitForTxs() && round == 0 && !cs.needProofBlock(height)
   820  	if waitForTxs {
   821  		if cs.config.CreateEmptyBlocksInterval > 0 {
   822  			cs.scheduleTimeout(cs.config.CreateEmptyBlocksInterval, height, round,
   823  				cstypes.RoundStepNewRound)
   824  		}
   825  	} else {
   826  		cs.enterPropose(height, round)
   827  	}
   828  }
   829  
   830  // needProofBlock returns true on the first height (so the genesis app hash is signed right away)
   831  // and where the last block (height-1) caused the app hash to change
   832  func (cs *ConsensusState) needProofBlock(height int64) bool {
   833  	if height == 1 {
   834  		return true
   835  	}
   836  
   837  	lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1)
   838  	return !bytes.Equal(cs.state.AppHash, lastBlockMeta.Header.AppHash)
   839  }
   840  
   841  // Enter (CreateEmptyBlocks): from enterNewRound(height,round)
   842  // Enter (CreateEmptyBlocks, CreateEmptyBlocksInterval > 0 ): after enterNewRound(height,round), after timeout of CreateEmptyBlocksInterval
   843  // Enter (!CreateEmptyBlocks) : after enterNewRound(height,round), once txs are in the mempool
   844  func (cs *ConsensusState) enterPropose(height int64, round int) {
   845  	logger := cs.Logger.With("height", height, "round", round)
   846  
   847  	if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPropose <= cs.Step) {
   848  		logger.Debug(fmt.Sprintf("enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
   849  		return
   850  	}
   851  	logger.Info(fmt.Sprintf("enterPropose(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
   852  
   853  	defer func() {
   854  		// Done enterPropose:
   855  		cs.updateRoundStep(round, cstypes.RoundStepPropose)
   856  		cs.newStep()
   857  
   858  		// If we have the whole proposal + POL, then goto Prevote now.
   859  		// else, we'll enterPrevote when the rest of the proposal is received (in AddProposalBlockPart),
   860  		// or else after timeoutPropose
   861  		if cs.isProposalComplete() {
   862  			cs.enterPrevote(height, cs.Round)
   863  		}
   864  	}()
   865  
   866  	// If we don't get the proposal and all block parts quick enough, enterPrevote
   867  	cs.scheduleTimeout(cs.config.Propose(round), height, round, cstypes.RoundStepPropose)
   868  
   869  	// Nothing more to do if we're not a validator
   870  	if cs.privValidator == nil {
   871  		logger.Debug("This node is not a validator")
   872  		return
   873  	}
   874  
   875  	// if not a validator, we're done
   876  	address := cs.privValidator.GetPubKey().Address()
   877  	if !cs.Validators.HasAddress(address) {
   878  		logger.Debug("This node is not a validator", "addr", address, "vals", cs.Validators)
   879  		return
   880  	}
   881  	logger.Debug("This node is a validator")
   882  
   883  	if cs.isProposer(address) {
   884  		logger.Info("enterPropose: Our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
   885  		cs.decideProposal(height, round)
   886  	} else {
   887  		logger.Info("enterPropose: Not our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
   888  	}
   889  }
   890  
   891  func (cs *ConsensusState) isProposer(address []byte) bool {
   892  	return bytes.Equal(cs.Validators.GetProposer().Address, address)
   893  }
   894  
   895  func (cs *ConsensusState) defaultDecideProposal(height int64, round int) {
   896  	var block *types.Block
   897  	var blockParts *types.PartSet
   898  
   899  	// Decide on block
   900  	if cs.ValidBlock != nil {
   901  		// If there is valid block, choose that.
   902  		block, blockParts = cs.ValidBlock, cs.ValidBlockParts
   903  	} else {
   904  		// Create a new proposal block from state/txs from the mempool.
   905  		block, blockParts = cs.createProposalBlock()
   906  		if block == nil { // on error
   907  			return
   908  		}
   909  	}
   910  
   911  	// Flush the WAL. Otherwise, we may not recompute the same proposal to sign, and the privValidator will refuse to sign anything.
   912  	cs.wal.FlushAndSync()
   913  
   914  	// Make proposal
   915  	propBlockId := types.BlockID{Hash: block.Hash(), PartsHeader: blockParts.Header()}
   916  	proposal := types.NewProposal(height, round, cs.ValidRound, propBlockId)
   917  	if err := cs.privValidator.SignProposal(cs.state.ChainID, proposal); err == nil {
   918  
   919  		// send proposal and block parts on internal msg queue
   920  		cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""})
   921  		for i := 0; i < blockParts.Total(); i++ {
   922  			part := blockParts.GetPart(i)
   923  			cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""})
   924  		}
   925  		cs.Logger.Info("Signed proposal", "height", height, "round", round, "proposal", proposal)
   926  		cs.Logger.Debug(fmt.Sprintf("Signed proposal block: %v", block))
   927  	} else {
   928  		if !cs.replayMode {
   929  			cs.Logger.Error("enterPropose: Error signing proposal", "height", height, "round", round, "err", err)
   930  		}
   931  	}
   932  }
   933  
   934  // Returns true if the proposal block is complete &&
   935  // (if POLRound was proposed, we have +2/3 prevotes from there).
   936  func (cs *ConsensusState) isProposalComplete() bool {
   937  	if cs.Proposal == nil || cs.ProposalBlock == nil {
   938  		return false
   939  	}
   940  	// we have the proposal. if there's a POLRound,
   941  	// make sure we have the prevotes from it too
   942  	if cs.Proposal.POLRound < 0 {
   943  		return true
   944  	}
   945  	// if this is false the proposer is lying or we haven't received the POL yet
   946  	return cs.Votes.Prevotes(cs.Proposal.POLRound).HasTwoThirdsMajority()
   947  
   948  }
   949  
   950  // Create the next block to propose and return it.
   951  // We really only need to return the parts, but the block
   952  // is returned for convenience so we can log the proposal block.
   953  // Returns nil block upon error.
   954  // NOTE: keep it side-effect free for clarity.
   955  func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts *types.PartSet) {
   956  	var commit *types.Commit
   957  	if cs.Height == 1 {
   958  		// We're creating a proposal for the first block.
   959  		// The commit is empty, but not nil.
   960  		commit = types.NewCommit(types.BlockID{}, nil)
   961  	} else if cs.LastCommit.HasTwoThirdsMajority() {
   962  		// Make the commit from LastCommit
   963  		commit = cs.LastCommit.MakeCommit()
   964  	} else {
   965  		// This shouldn't happen.
   966  		cs.Logger.Error("enterPropose: Cannot propose anything: No commit for the previous block.")
   967  		return
   968  	}
   969  
   970  	proposerAddr := cs.privValidator.GetPubKey().Address()
   971  	return cs.blockExec.CreateProposalBlock(cs.Height, cs.state, commit, proposerAddr)
   972  }
   973  
   974  // Enter: `timeoutPropose` after entering Propose.
   975  // Enter: proposal block and POL is ready.
   976  // Prevote for LockedBlock if we're locked, or ProposalBlock if valid.
   977  // Otherwise vote nil.
   978  func (cs *ConsensusState) enterPrevote(height int64, round int) {
   979  	if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevote <= cs.Step) {
   980  		cs.Logger.Debug(fmt.Sprintf("enterPrevote(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
   981  		return
   982  	}
   983  
   984  	defer func() {
   985  		// Done enterPrevote:
   986  		cs.updateRoundStep(round, cstypes.RoundStepPrevote)
   987  		cs.newStep()
   988  	}()
   989  
   990  	cs.Logger.Info(fmt.Sprintf("enterPrevote(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
   991  
   992  	// Sign and broadcast vote as necessary
   993  	cs.doPrevote(height, round)
   994  
   995  	// Once `addVote` hits any +2/3 prevotes, we will go to PrevoteWait
   996  	// (so we have more time to try and collect +2/3 prevotes for a single block)
   997  }
   998  
   999  func (cs *ConsensusState) defaultDoPrevote(height int64, round int) {
  1000  	logger := cs.Logger.With("height", height, "round", round)
  1001  
  1002  	// If a block is locked, prevote that.
  1003  	if cs.LockedBlock != nil {
  1004  		logger.Info("enterPrevote: Block was locked")
  1005  		cs.signAddVote(types.PrevoteType, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header())
  1006  		return
  1007  	}
  1008  
  1009  	// If ProposalBlock is nil, prevote nil.
  1010  	if cs.ProposalBlock == nil {
  1011  		logger.Info("enterPrevote: ProposalBlock is nil")
  1012  		cs.signAddVote(types.PrevoteType, nil, types.PartSetHeader{})
  1013  		return
  1014  	}
  1015  
  1016  	// Validate proposal block
  1017  	err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock)
  1018  	if err != nil {
  1019  		// ProposalBlock is invalid, prevote nil.
  1020  		logger.Error("enterPrevote: ProposalBlock is invalid", "err", err)
  1021  		cs.signAddVote(types.PrevoteType, nil, types.PartSetHeader{})
  1022  		return
  1023  	}
  1024  
  1025  	// Prevote cs.ProposalBlock
  1026  	// NOTE: the proposal signature is validated when it is received,
  1027  	// and the proposal block parts are validated as they are received (against the merkle hash in the proposal)
  1028  	logger.Info("enterPrevote: ProposalBlock is valid")
  1029  	cs.signAddVote(types.PrevoteType, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header())
  1030  }
  1031  
  1032  // Enter: any +2/3 prevotes at next round.
  1033  func (cs *ConsensusState) enterPrevoteWait(height int64, round int) {
  1034  	logger := cs.Logger.With("height", height, "round", round)
  1035  
  1036  	if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevoteWait <= cs.Step) {
  1037  		logger.Debug(fmt.Sprintf("enterPrevoteWait(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  1038  		return
  1039  	}
  1040  	if !cs.Votes.Prevotes(round).HasTwoThirdsAny() {
  1041  		panic(fmt.Sprintf("enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes", height, round))
  1042  	}
  1043  	logger.Info(fmt.Sprintf("enterPrevoteWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  1044  
  1045  	defer func() {
  1046  		// Done enterPrevoteWait:
  1047  		cs.updateRoundStep(round, cstypes.RoundStepPrevoteWait)
  1048  		cs.newStep()
  1049  	}()
  1050  
  1051  	// Wait for some more prevotes; enterPrecommit
  1052  	cs.scheduleTimeout(cs.config.Prevote(round), height, round, cstypes.RoundStepPrevoteWait)
  1053  }
  1054  
  1055  // Enter: `timeoutPrevote` after any +2/3 prevotes.
  1056  // Enter: `timeoutPrecommit` after any +2/3 precommits.
  1057  // Enter: +2/3 precomits for block or nil.
  1058  // Lock & precommit the ProposalBlock if we have enough prevotes for it (a POL in this round)
  1059  // else, unlock an existing lock and precommit nil if +2/3 of prevotes were nil,
  1060  // else, precommit nil otherwise.
  1061  func (cs *ConsensusState) enterPrecommit(height int64, round int) {
  1062  	logger := cs.Logger.With("height", height, "round", round)
  1063  
  1064  	if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrecommit <= cs.Step) {
  1065  		logger.Debug(fmt.Sprintf("enterPrecommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  1066  		return
  1067  	}
  1068  
  1069  	logger.Info(fmt.Sprintf("enterPrecommit(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  1070  
  1071  	defer func() {
  1072  		// Done enterPrecommit:
  1073  		cs.updateRoundStep(round, cstypes.RoundStepPrecommit)
  1074  		cs.newStep()
  1075  	}()
  1076  
  1077  	// check for a polka
  1078  	blockID, ok := cs.Votes.Prevotes(round).TwoThirdsMajority()
  1079  
  1080  	// If we don't have a polka, we must precommit nil.
  1081  	if !ok {
  1082  		if cs.LockedBlock != nil {
  1083  			logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit while we're locked. Precommitting nil")
  1084  		} else {
  1085  			logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit. Precommitting nil.")
  1086  		}
  1087  		cs.signAddVote(types.PrecommitType, nil, types.PartSetHeader{})
  1088  		return
  1089  	}
  1090  
  1091  	// At this point +2/3 prevoted for a particular block or nil.
  1092  	cs.eventBus.PublishEventPolka(cs.RoundStateEvent())
  1093  
  1094  	// the latest POLRound should be this round.
  1095  	polRound, _ := cs.Votes.POLInfo()
  1096  	if polRound < round {
  1097  		panic(fmt.Sprintf("This POLRound should be %v but got %v", round, polRound))
  1098  	}
  1099  
  1100  	// +2/3 prevoted nil. Unlock and precommit nil.
  1101  	if len(blockID.Hash) == 0 {
  1102  		if cs.LockedBlock == nil {
  1103  			logger.Info("enterPrecommit: +2/3 prevoted for nil.")
  1104  		} else {
  1105  			logger.Info("enterPrecommit: +2/3 prevoted for nil. Unlocking")
  1106  			cs.LockedRound = -1
  1107  			cs.LockedBlock = nil
  1108  			cs.LockedBlockParts = nil
  1109  			cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
  1110  		}
  1111  		cs.signAddVote(types.PrecommitType, nil, types.PartSetHeader{})
  1112  		return
  1113  	}
  1114  
  1115  	// At this point, +2/3 prevoted for a particular block.
  1116  
  1117  	// If we're already locked on that block, precommit it, and update the LockedRound
  1118  	if cs.LockedBlock.HashesTo(blockID.Hash) {
  1119  		logger.Info("enterPrecommit: +2/3 prevoted locked block. Relocking")
  1120  		cs.LockedRound = round
  1121  		cs.eventBus.PublishEventRelock(cs.RoundStateEvent())
  1122  		cs.signAddVote(types.PrecommitType, blockID.Hash, blockID.PartsHeader)
  1123  		return
  1124  	}
  1125  
  1126  	// If +2/3 prevoted for proposal block, stage and precommit it
  1127  	if cs.ProposalBlock.HashesTo(blockID.Hash) {
  1128  		logger.Info("enterPrecommit: +2/3 prevoted proposal block. Locking", "hash", blockID.Hash)
  1129  		// Validate the block.
  1130  		if err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock); err != nil {
  1131  			panic(fmt.Sprintf("enterPrecommit: +2/3 prevoted for an invalid block: %v", err))
  1132  		}
  1133  		cs.LockedRound = round
  1134  		cs.LockedBlock = cs.ProposalBlock
  1135  		cs.LockedBlockParts = cs.ProposalBlockParts
  1136  		cs.eventBus.PublishEventLock(cs.RoundStateEvent())
  1137  		cs.signAddVote(types.PrecommitType, blockID.Hash, blockID.PartsHeader)
  1138  		return
  1139  	}
  1140  
  1141  	// There was a polka in this round for a block we don't have.
  1142  	// Fetch that block, unlock, and precommit nil.
  1143  	// The +2/3 prevotes for this round is the POL for our unlock.
  1144  	// TODO: In the future save the POL prevotes for justification.
  1145  	cs.LockedRound = -1
  1146  	cs.LockedBlock = nil
  1147  	cs.LockedBlockParts = nil
  1148  	if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
  1149  		cs.ProposalBlock = nil
  1150  		cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
  1151  	}
  1152  	cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
  1153  	cs.signAddVote(types.PrecommitType, nil, types.PartSetHeader{})
  1154  }
  1155  
  1156  // Enter: any +2/3 precommits for next round.
  1157  func (cs *ConsensusState) enterPrecommitWait(height int64, round int) {
  1158  	logger := cs.Logger.With("height", height, "round", round)
  1159  
  1160  	if cs.Height != height || round < cs.Round || (cs.Round == round && cs.TriggeredTimeoutPrecommit) {
  1161  		logger.Debug(
  1162  			fmt.Sprintf(
  1163  				"enterPrecommitWait(%v/%v): Invalid args. "+
  1164  					"Current state is Height/Round: %v/%v/, TriggeredTimeoutPrecommit:%v",
  1165  				height, round, cs.Height, cs.Round, cs.TriggeredTimeoutPrecommit))
  1166  		return
  1167  	}
  1168  	if !cs.Votes.Precommits(round).HasTwoThirdsAny() {
  1169  		panic(fmt.Sprintf("enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes", height, round))
  1170  	}
  1171  	logger.Info(fmt.Sprintf("enterPrecommitWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
  1172  
  1173  	defer func() {
  1174  		// Done enterPrecommitWait:
  1175  		cs.TriggeredTimeoutPrecommit = true
  1176  		cs.newStep()
  1177  	}()
  1178  
  1179  	// Wait for some more precommits; enterNewRound
  1180  	cs.scheduleTimeout(cs.config.Precommit(round), height, round, cstypes.RoundStepPrecommitWait)
  1181  
  1182  }
  1183  
  1184  // Enter: +2/3 precommits for block
  1185  func (cs *ConsensusState) enterCommit(height int64, commitRound int) {
  1186  	logger := cs.Logger.With("height", height, "commitRound", commitRound)
  1187  
  1188  	if cs.Height != height || cstypes.RoundStepCommit <= cs.Step {
  1189  		logger.Debug(fmt.Sprintf("enterCommit(%v/%v): Invalid args. Current step: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step))
  1190  		return
  1191  	}
  1192  	logger.Info(fmt.Sprintf("enterCommit(%v/%v). Current: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step))
  1193  
  1194  	defer func() {
  1195  		// Done enterCommit:
  1196  		// keep cs.Round the same, commitRound points to the right Precommits set.
  1197  		cs.updateRoundStep(cs.Round, cstypes.RoundStepCommit)
  1198  		cs.CommitRound = commitRound
  1199  		cs.CommitTime = tmtime.Now()
  1200  		cs.newStep()
  1201  
  1202  		// Maybe finalize immediately.
  1203  		cs.tryFinalizeCommit(height)
  1204  	}()
  1205  
  1206  	blockID, ok := cs.Votes.Precommits(commitRound).TwoThirdsMajority()
  1207  	if !ok {
  1208  		panic("RunActionCommit() expects +2/3 precommits")
  1209  	}
  1210  
  1211  	// The Locked* fields no longer matter.
  1212  	// Move them over to ProposalBlock if they match the commit hash,
  1213  	// otherwise they'll be cleared in updateToState.
  1214  	if cs.LockedBlock.HashesTo(blockID.Hash) {
  1215  		logger.Info("Commit is for locked block. Set ProposalBlock=LockedBlock", "blockHash", blockID.Hash)
  1216  		cs.ProposalBlock = cs.LockedBlock
  1217  		cs.ProposalBlockParts = cs.LockedBlockParts
  1218  	}
  1219  
  1220  	// If we don't have the block being committed, set up to get it.
  1221  	if !cs.ProposalBlock.HashesTo(blockID.Hash) {
  1222  		if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
  1223  			logger.Info("Commit is for a block we don't know about. Set ProposalBlock=nil", "proposal", cs.ProposalBlock.Hash(), "commit", blockID.Hash)
  1224  			// We're getting the wrong block.
  1225  			// Set up ProposalBlockParts and keep waiting.
  1226  			cs.ProposalBlock = nil
  1227  			cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
  1228  			cs.eventBus.PublishEventValidBlock(cs.RoundStateEvent())
  1229  			cs.evsw.FireEvent(types.EventValidBlock, &cs.RoundState)
  1230  		} else {
  1231  			// We just need to keep waiting.
  1232  		}
  1233  	}
  1234  }
  1235  
  1236  // If we have the block AND +2/3 commits for it, finalize.
  1237  func (cs *ConsensusState) tryFinalizeCommit(height int64) {
  1238  	logger := cs.Logger.With("height", height)
  1239  
  1240  	if cs.Height != height {
  1241  		panic(fmt.Sprintf("tryFinalizeCommit() cs.Height: %v vs height: %v", cs.Height, height))
  1242  	}
  1243  
  1244  	blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority()
  1245  	if !ok || len(blockID.Hash) == 0 {
  1246  		logger.Error("Attempt to finalize failed. There was no +2/3 majority, or +2/3 was for <nil>.")
  1247  		return
  1248  	}
  1249  	if !cs.ProposalBlock.HashesTo(blockID.Hash) {
  1250  		// TODO: this happens every time if we're not a validator (ugly logs)
  1251  		// TODO: ^^ wait, why does it matter that we're a validator?
  1252  		logger.Info("Attempt to finalize failed. We don't have the commit block.", "proposal-block", cs.ProposalBlock.Hash(), "commit-block", blockID.Hash)
  1253  		return
  1254  	}
  1255  
  1256  	//	go
  1257  	cs.finalizeCommit(height)
  1258  }
  1259  
  1260  // Increment height and goto cstypes.RoundStepNewHeight
  1261  func (cs *ConsensusState) finalizeCommit(height int64) {
  1262  	if cs.Height != height || cs.Step != cstypes.RoundStepCommit {
  1263  		cs.Logger.Debug(fmt.Sprintf("finalizeCommit(%v): Invalid args. Current step: %v/%v/%v", height, cs.Height, cs.Round, cs.Step))
  1264  		return
  1265  	}
  1266  
  1267  	blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority()
  1268  	block, blockParts := cs.ProposalBlock, cs.ProposalBlockParts
  1269  
  1270  	if !ok {
  1271  		panic(fmt.Sprintf("Cannot finalizeCommit, commit does not have two thirds majority"))
  1272  	}
  1273  	if !blockParts.HasHeader(blockID.PartsHeader) {
  1274  		panic(fmt.Sprintf("Expected ProposalBlockParts header to be commit header"))
  1275  	}
  1276  	if !block.HashesTo(blockID.Hash) {
  1277  		panic(fmt.Sprintf("Cannot finalizeCommit, ProposalBlock does not hash to commit hash"))
  1278  	}
  1279  	if err := cs.blockExec.ValidateBlock(cs.state, block); err != nil {
  1280  		panic(fmt.Sprintf("+2/3 committed an invalid block: %v", err))
  1281  	}
  1282  
  1283  	cs.Logger.Info(fmt.Sprintf("Finalizing commit of block with %d txs", block.NumTxs),
  1284  		"height", block.Height, "hash", block.Hash(), "root", block.AppHash)
  1285  	cs.Logger.Info(fmt.Sprintf("%v", block))
  1286  
  1287  	fail.Fail() // XXX
  1288  
  1289  	// Save to blockStore.
  1290  	if cs.blockStore.Height() < block.Height {
  1291  		// NOTE: the seenCommit is local justification to commit this block,
  1292  		// but may differ from the LastCommit included in the next block
  1293  		precommits := cs.Votes.Precommits(cs.CommitRound)
  1294  		seenCommit := precommits.MakeCommit()
  1295  		cs.blockStore.SaveBlock(block, blockParts, seenCommit)
  1296  	} else {
  1297  		// Happens during replay if we already saved the block but didn't commit
  1298  		cs.Logger.Info("Calling finalizeCommit on already stored block", "height", block.Height)
  1299  	}
  1300  
  1301  	fail.Fail() // XXX
  1302  
  1303  	// Write EndHeightMessage{} for this height, implying that the blockstore
  1304  	// has saved the block.
  1305  	//
  1306  	// If we crash before writing this EndHeightMessage{}, we will recover by
  1307  	// running ApplyBlock during the ABCI handshake when we restart.  If we
  1308  	// didn't save the block to the blockstore before writing
  1309  	// EndHeightMessage{}, we'd have to change WAL replay -- currently it
  1310  	// complains about replaying for heights where an #ENDHEIGHT entry already
  1311  	// exists.
  1312  	//
  1313  	// Either way, the ConsensusState should not be resumed until we
  1314  	// successfully call ApplyBlock (ie. later here, or in Handshake after
  1315  	// restart).
  1316  	cs.wal.WriteSync(EndHeightMessage{height}) // NOTE: fsync
  1317  
  1318  	fail.Fail() // XXX
  1319  
  1320  	// Create a copy of the state for staging and an event cache for txs.
  1321  	stateCopy := cs.state.Copy()
  1322  
  1323  	// Execute and commit the block, update and save the state, and update the mempool.
  1324  	// NOTE The block.AppHash wont reflect these txs until the next block.
  1325  	var err error
  1326  	stateCopy, err = cs.blockExec.ApplyBlock(stateCopy, types.BlockID{Hash: block.Hash(), PartsHeader: blockParts.Header()}, block)
  1327  	if err != nil {
  1328  		cs.Logger.Error("Error on ApplyBlock. Did the application crash? Please restart tendermint", "err", err)
  1329  		err := cmn.Kill()
  1330  		if err != nil {
  1331  			cs.Logger.Error("Failed to kill this process - please do so manually", "err", err)
  1332  		}
  1333  		return
  1334  	}
  1335  
  1336  	fail.Fail() // XXX
  1337  
  1338  	// must be called before we update state
  1339  	cs.recordMetrics(height, block)
  1340  
  1341  	// NewHeightStep!
  1342  	cs.updateToState(stateCopy)
  1343  
  1344  	fail.Fail() // XXX
  1345  
  1346  	// cs.StartTime is already set.
  1347  	// Schedule Round0 to start soon.
  1348  	cs.scheduleRound0(&cs.RoundState)
  1349  
  1350  	// By here,
  1351  	// * cs.Height has been increment to height+1
  1352  	// * cs.Step is now cstypes.RoundStepNewHeight
  1353  	// * cs.StartTime is set to when we will start round0.
  1354  }
  1355  
  1356  func (cs *ConsensusState) recordMetrics(height int64, block *types.Block) {
  1357  	cs.metrics.Validators.Set(float64(cs.Validators.Size()))
  1358  	cs.metrics.ValidatorsPower.Set(float64(cs.Validators.TotalVotingPower()))
  1359  	missingValidators := 0
  1360  	missingValidatorsPower := int64(0)
  1361  	for i, val := range cs.Validators.Validators {
  1362  		var vote *types.CommitSig
  1363  		if i < len(block.LastCommit.Precommits) {
  1364  			vote = block.LastCommit.Precommits[i]
  1365  		}
  1366  		if vote == nil {
  1367  			missingValidators++
  1368  			missingValidatorsPower += val.VotingPower
  1369  		}
  1370  	}
  1371  	cs.metrics.MissingValidators.Set(float64(missingValidators))
  1372  	cs.metrics.MissingValidatorsPower.Set(float64(missingValidatorsPower))
  1373  	cs.metrics.ByzantineValidators.Set(float64(len(block.Evidence.Evidence)))
  1374  	byzantineValidatorsPower := int64(0)
  1375  	for _, ev := range block.Evidence.Evidence {
  1376  		if _, val := cs.Validators.GetByAddress(ev.Address()); val != nil {
  1377  			byzantineValidatorsPower += val.VotingPower
  1378  		}
  1379  	}
  1380  	cs.metrics.ByzantineValidatorsPower.Set(float64(byzantineValidatorsPower))
  1381  
  1382  	if height > 1 {
  1383  		lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1)
  1384  		cs.metrics.BlockIntervalSeconds.Set(
  1385  			block.Time.Sub(lastBlockMeta.Header.Time).Seconds(),
  1386  		)
  1387  	}
  1388  
  1389  	cs.metrics.NumTxs.Set(float64(block.NumTxs))
  1390  	cs.metrics.BlockSizeBytes.Set(float64(block.Size()))
  1391  	cs.metrics.TotalTxs.Set(float64(block.TotalTxs))
  1392  	cs.metrics.CommittedHeight.Set(float64(block.Height))
  1393  
  1394  }
  1395  
  1396  //-----------------------------------------------------------------------------
  1397  
  1398  func (cs *ConsensusState) defaultSetProposal(proposal *types.Proposal) error {
  1399  	// Already have one
  1400  	// TODO: possibly catch double proposals
  1401  	if cs.Proposal != nil {
  1402  		return nil
  1403  	}
  1404  
  1405  	// Does not apply
  1406  	if proposal.Height != cs.Height || proposal.Round != cs.Round {
  1407  		return nil
  1408  	}
  1409  
  1410  	// Verify POLRound, which must be -1 or in range [0, proposal.Round).
  1411  	if proposal.POLRound < -1 ||
  1412  		(proposal.POLRound >= 0 && proposal.POLRound >= proposal.Round) {
  1413  		return ErrInvalidProposalPOLRound
  1414  	}
  1415  
  1416  	// Verify signature
  1417  	if !cs.Validators.GetProposer().PubKey.VerifyBytes(proposal.SignBytes(cs.state.ChainID), proposal.Signature) {
  1418  		return ErrInvalidProposalSignature
  1419  	}
  1420  
  1421  	cs.Proposal = proposal
  1422  	// We don't update cs.ProposalBlockParts if it is already set.
  1423  	// This happens if we're already in cstypes.RoundStepCommit or if there is a valid block in the current round.
  1424  	// TODO: We can check if Proposal is for a different block as this is a sign of misbehavior!
  1425  	if cs.ProposalBlockParts == nil {
  1426  		cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockID.PartsHeader)
  1427  	}
  1428  	cs.Logger.Info("Received proposal", "proposal", proposal)
  1429  	return nil
  1430  }
  1431  
  1432  // NOTE: block is not necessarily valid.
  1433  // Asynchronously triggers either enterPrevote (before we timeout of propose) or tryFinalizeCommit, once we have the full block.
  1434  func (cs *ConsensusState) addProposalBlockPart(msg *BlockPartMessage, peerID p2p.ID) (added bool, err error) {
  1435  	height, round, part := msg.Height, msg.Round, msg.Part
  1436  
  1437  	// Blocks might be reused, so round mismatch is OK
  1438  	if cs.Height != height {
  1439  		cs.Logger.Debug("Received block part from wrong height", "height", height, "round", round)
  1440  		return false, nil
  1441  	}
  1442  
  1443  	// We're not expecting a block part.
  1444  	if cs.ProposalBlockParts == nil {
  1445  		// NOTE: this can happen when we've gone to a higher round and
  1446  		// then receive parts from the previous round - not necessarily a bad peer.
  1447  		cs.Logger.Info("Received a block part when we're not expecting any",
  1448  			"height", height, "round", round, "index", part.Index, "peer", peerID)
  1449  		return false, nil
  1450  	}
  1451  
  1452  	added, err = cs.ProposalBlockParts.AddPart(part)
  1453  	if err != nil {
  1454  		return added, err
  1455  	}
  1456  	if added && cs.ProposalBlockParts.IsComplete() {
  1457  		// Added and completed!
  1458  		_, err = cdc.UnmarshalBinaryLengthPrefixedReader(
  1459  			cs.ProposalBlockParts.GetReader(),
  1460  			&cs.ProposalBlock,
  1461  			int64(cs.state.ConsensusParams.Block.MaxBytes),
  1462  		)
  1463  		if err != nil {
  1464  			return added, err
  1465  		}
  1466  		// NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal
  1467  		cs.Logger.Info("Received complete proposal block", "height", cs.ProposalBlock.Height, "hash", cs.ProposalBlock.Hash())
  1468  		cs.eventBus.PublishEventCompleteProposal(cs.CompleteProposalEvent())
  1469  
  1470  		// Update Valid* if we can.
  1471  		prevotes := cs.Votes.Prevotes(cs.Round)
  1472  		blockID, hasTwoThirds := prevotes.TwoThirdsMajority()
  1473  		if hasTwoThirds && !blockID.IsZero() && (cs.ValidRound < cs.Round) {
  1474  			if cs.ProposalBlock.HashesTo(blockID.Hash) {
  1475  				cs.Logger.Info("Updating valid block to new proposal block",
  1476  					"valid-round", cs.Round, "valid-block-hash", cs.ProposalBlock.Hash())
  1477  				cs.ValidRound = cs.Round
  1478  				cs.ValidBlock = cs.ProposalBlock
  1479  				cs.ValidBlockParts = cs.ProposalBlockParts
  1480  			}
  1481  			// TODO: In case there is +2/3 majority in Prevotes set for some
  1482  			// block and cs.ProposalBlock contains different block, either
  1483  			// proposer is faulty or voting power of faulty processes is more
  1484  			// than 1/3. We should trigger in the future accountability
  1485  			// procedure at this point.
  1486  		}
  1487  
  1488  		if cs.Step <= cstypes.RoundStepPropose && cs.isProposalComplete() {
  1489  			// Move onto the next step
  1490  			cs.enterPrevote(height, cs.Round)
  1491  			if hasTwoThirds { // this is optimisation as this will be triggered when prevote is added
  1492  				cs.enterPrecommit(height, cs.Round)
  1493  			}
  1494  		} else if cs.Step == cstypes.RoundStepCommit {
  1495  			// If we're waiting on the proposal block...
  1496  			cs.tryFinalizeCommit(height)
  1497  		}
  1498  		return added, nil
  1499  	}
  1500  	return added, nil
  1501  }
  1502  
  1503  // Attempt to add the vote. if its a duplicate signature, dupeout the validator
  1504  func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerID p2p.ID) (bool, error) {
  1505  	added, err := cs.addVote(vote, peerID)
  1506  	if err != nil {
  1507  		// If the vote height is off, we'll just ignore it,
  1508  		// But if it's a conflicting sig, add it to the cs.evpool.
  1509  		// If it's otherwise invalid, punish peer.
  1510  		if err == ErrVoteHeightMismatch {
  1511  			return added, err
  1512  		} else if voteErr, ok := err.(*types.ErrVoteConflictingVotes); ok {
  1513  			addr := cs.privValidator.GetPubKey().Address()
  1514  			if bytes.Equal(vote.ValidatorAddress, addr) {
  1515  				cs.Logger.Error("Found conflicting vote from ourselves. Did you unsafe_reset a validator?", "height", vote.Height, "round", vote.Round, "type", vote.Type)
  1516  				return added, err
  1517  			}
  1518  			cs.evpool.AddEvidence(voteErr.DuplicateVoteEvidence)
  1519  			return added, err
  1520  		} else {
  1521  			// Probably an invalid signature / Bad peer.
  1522  			// Seems this can also err sometimes with "Unexpected step" - perhaps not from a bad peer ?
  1523  			cs.Logger.Error("Error attempting to add vote", "err", err)
  1524  			return added, ErrAddingVote
  1525  		}
  1526  	}
  1527  	return added, nil
  1528  }
  1529  
  1530  //-----------------------------------------------------------------------------
  1531  
  1532  func (cs *ConsensusState) addVote(vote *types.Vote, peerID p2p.ID) (added bool, err error) {
  1533  	cs.Logger.Debug("addVote", "voteHeight", vote.Height, "voteType", vote.Type, "valIndex", vote.ValidatorIndex, "csHeight", cs.Height)
  1534  
  1535  	// A precommit for the previous height?
  1536  	// These come in while we wait timeoutCommit
  1537  	if vote.Height+1 == cs.Height {
  1538  		if !(cs.Step == cstypes.RoundStepNewHeight && vote.Type == types.PrecommitType) {
  1539  			// TODO: give the reason ..
  1540  			// fmt.Errorf("tryAddVote: Wrong height, not a LastCommit straggler commit.")
  1541  			return added, ErrVoteHeightMismatch
  1542  		}
  1543  		added, err = cs.LastCommit.AddVote(vote)
  1544  		if !added {
  1545  			return added, err
  1546  		}
  1547  
  1548  		cs.Logger.Info(fmt.Sprintf("Added to lastPrecommits: %v", cs.LastCommit.StringShort()))
  1549  		cs.eventBus.PublishEventVote(types.EventDataVote{Vote: vote})
  1550  		cs.evsw.FireEvent(types.EventVote, vote)
  1551  
  1552  		// if we can skip timeoutCommit and have all the votes now,
  1553  		if cs.config.SkipTimeoutCommit && cs.LastCommit.HasAll() {
  1554  			// go straight to new round (skip timeout commit)
  1555  			// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
  1556  			cs.enterNewRound(cs.Height, 0)
  1557  		}
  1558  
  1559  		return
  1560  	}
  1561  
  1562  	// Height mismatch is ignored.
  1563  	// Not necessarily a bad peer, but not favourable behaviour.
  1564  	if vote.Height != cs.Height {
  1565  		err = ErrVoteHeightMismatch
  1566  		cs.Logger.Info("Vote ignored and not added", "voteHeight", vote.Height, "csHeight", cs.Height, "peerID", peerID)
  1567  		return
  1568  	}
  1569  
  1570  	height := cs.Height
  1571  	added, err = cs.Votes.AddVote(vote, peerID)
  1572  	if !added {
  1573  		// Either duplicate, or error upon cs.Votes.AddByIndex()
  1574  		return
  1575  	}
  1576  
  1577  	cs.eventBus.PublishEventVote(types.EventDataVote{Vote: vote})
  1578  	cs.evsw.FireEvent(types.EventVote, vote)
  1579  
  1580  	switch vote.Type {
  1581  	case types.PrevoteType:
  1582  		prevotes := cs.Votes.Prevotes(vote.Round)
  1583  		cs.Logger.Info("Added to prevote", "vote", vote, "prevotes", prevotes.StringShort())
  1584  
  1585  		// If +2/3 prevotes for a block or nil for *any* round:
  1586  		if blockID, ok := prevotes.TwoThirdsMajority(); ok {
  1587  
  1588  			// There was a polka!
  1589  			// If we're locked but this is a recent polka, unlock.
  1590  			// If it matches our ProposalBlock, update the ValidBlock
  1591  
  1592  			// Unlock if `cs.LockedRound < vote.Round <= cs.Round`
  1593  			// NOTE: If vote.Round > cs.Round, we'll deal with it when we get to vote.Round
  1594  			if (cs.LockedBlock != nil) &&
  1595  				(cs.LockedRound < vote.Round) &&
  1596  				(vote.Round <= cs.Round) &&
  1597  				!cs.LockedBlock.HashesTo(blockID.Hash) {
  1598  
  1599  				cs.Logger.Info("Unlocking because of POL.", "lockedRound", cs.LockedRound, "POLRound", vote.Round)
  1600  				cs.LockedRound = -1
  1601  				cs.LockedBlock = nil
  1602  				cs.LockedBlockParts = nil
  1603  				cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
  1604  			}
  1605  
  1606  			// Update Valid* if we can.
  1607  			// NOTE: our proposal block may be nil or not what received a polka..
  1608  			if len(blockID.Hash) != 0 && (cs.ValidRound < vote.Round) && (vote.Round == cs.Round) {
  1609  
  1610  				if cs.ProposalBlock.HashesTo(blockID.Hash) {
  1611  					cs.Logger.Info(
  1612  						"Updating ValidBlock because of POL.", "validRound", cs.ValidRound, "POLRound", vote.Round)
  1613  					cs.ValidRound = vote.Round
  1614  					cs.ValidBlock = cs.ProposalBlock
  1615  					cs.ValidBlockParts = cs.ProposalBlockParts
  1616  				} else {
  1617  					cs.Logger.Info(
  1618  						"Valid block we don't know about. Set ProposalBlock=nil",
  1619  						"proposal", cs.ProposalBlock.Hash(), "blockId", blockID.Hash)
  1620  					// We're getting the wrong block.
  1621  					cs.ProposalBlock = nil
  1622  				}
  1623  				if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
  1624  					cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
  1625  				}
  1626  				cs.evsw.FireEvent(types.EventValidBlock, &cs.RoundState)
  1627  				cs.eventBus.PublishEventValidBlock(cs.RoundStateEvent())
  1628  			}
  1629  		}
  1630  
  1631  		// If +2/3 prevotes for *anything* for future round:
  1632  		if cs.Round < vote.Round && prevotes.HasTwoThirdsAny() {
  1633  			// Round-skip if there is any 2/3+ of votes ahead of us
  1634  			cs.enterNewRound(height, vote.Round)
  1635  		} else if cs.Round == vote.Round && cstypes.RoundStepPrevote <= cs.Step { // current round
  1636  			blockID, ok := prevotes.TwoThirdsMajority()
  1637  			if ok && (cs.isProposalComplete() || len(blockID.Hash) == 0) {
  1638  				cs.enterPrecommit(height, vote.Round)
  1639  			} else if prevotes.HasTwoThirdsAny() {
  1640  				cs.enterPrevoteWait(height, vote.Round)
  1641  			}
  1642  		} else if cs.Proposal != nil && 0 <= cs.Proposal.POLRound && cs.Proposal.POLRound == vote.Round {
  1643  			// If the proposal is now complete, enter prevote of cs.Round.
  1644  			if cs.isProposalComplete() {
  1645  				cs.enterPrevote(height, cs.Round)
  1646  			}
  1647  		}
  1648  
  1649  	case types.PrecommitType:
  1650  		precommits := cs.Votes.Precommits(vote.Round)
  1651  		cs.Logger.Info("Added to precommit", "vote", vote, "precommits", precommits.StringShort())
  1652  
  1653  		blockID, ok := precommits.TwoThirdsMajority()
  1654  		if ok {
  1655  			// Executed as TwoThirdsMajority could be from a higher round
  1656  			cs.enterNewRound(height, vote.Round)
  1657  			cs.enterPrecommit(height, vote.Round)
  1658  			if len(blockID.Hash) != 0 {
  1659  				cs.enterCommit(height, vote.Round)
  1660  				if cs.config.SkipTimeoutCommit && precommits.HasAll() {
  1661  					cs.enterNewRound(cs.Height, 0)
  1662  				}
  1663  			} else {
  1664  				cs.enterPrecommitWait(height, vote.Round)
  1665  			}
  1666  		} else if cs.Round <= vote.Round && precommits.HasTwoThirdsAny() {
  1667  			cs.enterNewRound(height, vote.Round)
  1668  			cs.enterPrecommitWait(height, vote.Round)
  1669  		}
  1670  
  1671  	default:
  1672  		panic(fmt.Sprintf("Unexpected vote type %X", vote.Type)) // go-wire should prevent this.
  1673  	}
  1674  
  1675  	return
  1676  }
  1677  
  1678  func (cs *ConsensusState) signVote(type_ types.SignedMsgType, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
  1679  	// Flush the WAL. Otherwise, we may not recompute the same vote to sign, and the privValidator will refuse to sign anything.
  1680  	cs.wal.FlushAndSync()
  1681  
  1682  	addr := cs.privValidator.GetPubKey().Address()
  1683  	valIndex, _ := cs.Validators.GetByAddress(addr)
  1684  
  1685  	vote := &types.Vote{
  1686  		ValidatorAddress: addr,
  1687  		ValidatorIndex:   valIndex,
  1688  		Height:           cs.Height,
  1689  		Round:            cs.Round,
  1690  		Timestamp:        cs.voteTime(),
  1691  		Type:             type_,
  1692  		BlockID:          types.BlockID{Hash: hash, PartsHeader: header},
  1693  	}
  1694  	err := cs.privValidator.SignVote(cs.state.ChainID, vote)
  1695  	return vote, err
  1696  }
  1697  
  1698  func (cs *ConsensusState) voteTime() time.Time {
  1699  	now := tmtime.Now()
  1700  	minVoteTime := now
  1701  	// TODO: We should remove next line in case we don't vote for v in case cs.ProposalBlock == nil,
  1702  	// even if cs.LockedBlock != nil. See https://github.com/evdatsion/spec.
  1703  	timeIotaMs := time.Duration(cs.state.ConsensusParams.Block.TimeIotaMs) * time.Millisecond
  1704  	if cs.LockedBlock != nil {
  1705  		// See the BFT time spec https://tendermint.com/docs/spec/consensus/bft-time.html
  1706  		minVoteTime = cs.LockedBlock.Time.Add(timeIotaMs)
  1707  	} else if cs.ProposalBlock != nil {
  1708  		minVoteTime = cs.ProposalBlock.Time.Add(timeIotaMs)
  1709  	}
  1710  
  1711  	if now.After(minVoteTime) {
  1712  		return now
  1713  	}
  1714  	return minVoteTime
  1715  }
  1716  
  1717  // sign the vote and publish on internalMsgQueue
  1718  func (cs *ConsensusState) signAddVote(type_ types.SignedMsgType, hash []byte, header types.PartSetHeader) *types.Vote {
  1719  	// if we don't have a key or we're not in the validator set, do nothing
  1720  	if cs.privValidator == nil || !cs.Validators.HasAddress(cs.privValidator.GetPubKey().Address()) {
  1721  		return nil
  1722  	}
  1723  	vote, err := cs.signVote(type_, hash, header)
  1724  	if err == nil {
  1725  		cs.sendInternalMessage(msgInfo{&VoteMessage{vote}, ""})
  1726  		cs.Logger.Info("Signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
  1727  		return vote
  1728  	}
  1729  	//if !cs.replayMode {
  1730  	cs.Logger.Error("Error signing vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
  1731  	//}
  1732  	return nil
  1733  }
  1734  
  1735  //---------------------------------------------------------
  1736  
  1737  func CompareHRS(h1 int64, r1 int, s1 cstypes.RoundStepType, h2 int64, r2 int, s2 cstypes.RoundStepType) int {
  1738  	if h1 < h2 {
  1739  		return -1
  1740  	} else if h1 > h2 {
  1741  		return 1
  1742  	}
  1743  	if r1 < r2 {
  1744  		return -1
  1745  	} else if r1 > r2 {
  1746  		return 1
  1747  	}
  1748  	if s1 < s2 {
  1749  		return -1
  1750  	} else if s1 > s2 {
  1751  		return 1
  1752  	}
  1753  	return 0
  1754  }