2014-08-10 16:35:08 -07:00
package consensus
import (
2014-12-09 18:49:04 -08:00
"bytes"
2014-09-14 15:37:32 -07:00
"errors"
2014-10-18 01:42:33 -07:00
"fmt"
2017-02-17 19:12:05 -05:00
"path"
2015-12-10 11:41:18 -05:00
"reflect"
2014-08-10 16:35:08 -07:00
"sync"
"time"
2016-09-11 15:32:33 -04:00
"github.com/ebuchman/fail-test"
2015-10-22 17:39:06 -07:00
. "github.com/tendermint/go-common"
2016-05-08 15:00:58 -07:00
cfg "github.com/tendermint/go-config"
2015-11-01 11:34:08 -08:00
"github.com/tendermint/go-wire"
2015-12-01 20:12:01 -08:00
"github.com/tendermint/tendermint/proxy"
2015-04-01 17:30:16 -07:00
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
2014-08-10 16:35:08 -07:00
)
2016-02-29 16:15:23 -05:00
//-----------------------------------------------------------------------------
// Timeout Parameters
2017-01-04 01:50:02 +04:00
// TimeoutParams holds timeouts and deltas for each round step.
// All timeouts and deltas in milliseconds.
2016-02-29 16:15:23 -05:00
type TimeoutParams struct {
2017-01-04 01:50:02 +04:00
Propose0 int
ProposeDelta int
Prevote0 int
PrevoteDelta int
Precommit0 int
PrecommitDelta int
Commit0 int
SkipTimeoutCommit bool
2016-02-29 16:15:23 -05:00
}
2015-12-10 11:41:18 -05:00
2016-02-29 16:15:23 -05:00
// Wait this long for a proposal
func ( tp * TimeoutParams ) Propose ( round int ) time . Duration {
return time . Duration ( tp . Propose0 + tp . ProposeDelta * round ) * time . Millisecond
}
// After receiving any +2/3 prevote, wait this long for stragglers
func ( tp * TimeoutParams ) Prevote ( round int ) time . Duration {
return time . Duration ( tp . Prevote0 + tp . PrevoteDelta * round ) * time . Millisecond
}
// After receiving any +2/3 precommits, wait this long for stragglers
func ( tp * TimeoutParams ) Precommit ( round int ) time . Duration {
return time . Duration ( tp . Precommit0 + tp . PrecommitDelta * round ) * time . Millisecond
}
// After receiving +2/3 precommits for a single block (a commit), wait this long for stragglers in the next height's RoundStepNewHeight
func ( tp * TimeoutParams ) Commit ( t time . Time ) time . Time {
return t . Add ( time . Duration ( tp . Commit0 ) * time . Millisecond )
}
2017-01-04 01:50:02 +04:00
// InitTimeoutParamsFromConfig initializes parameters from config
2016-05-08 15:00:58 -07:00
func InitTimeoutParamsFromConfig ( config cfg . Config ) * TimeoutParams {
2016-02-29 16:15:23 -05:00
return & TimeoutParams {
2017-01-04 01:50:02 +04:00
Propose0 : config . GetInt ( "timeout_propose" ) ,
ProposeDelta : config . GetInt ( "timeout_propose_delta" ) ,
Prevote0 : config . GetInt ( "timeout_prevote" ) ,
PrevoteDelta : config . GetInt ( "timeout_prevote_delta" ) ,
Precommit0 : config . GetInt ( "timeout_precommit" ) ,
PrecommitDelta : config . GetInt ( "timeout_precommit_delta" ) ,
Commit0 : config . GetInt ( "timeout_commit" ) ,
SkipTimeoutCommit : config . GetBool ( "skip_timeout_commit" ) ,
2016-02-29 16:15:23 -05:00
}
}
//-----------------------------------------------------------------------------
// Errors
2014-12-30 17:14:54 -08:00
var (
ErrInvalidProposalSignature = errors . New ( "Error invalid proposal signature" )
2015-06-22 19:04:31 -07:00
ErrInvalidProposalPOLRound = errors . New ( "Error invalid proposal POL round" )
2015-08-12 14:00:23 -04:00
ErrAddingVote = errors . New ( "Error adding vote" )
2015-08-26 18:56:34 -04:00
ErrVoteHeightMismatch = errors . New ( "Error vote height mismatch" )
2014-12-30 17:14:54 -08:00
)
//-----------------------------------------------------------------------------
2015-04-20 23:59:52 -07:00
// RoundStepType enum type
2014-12-30 17:14:54 -08:00
2015-04-20 23:59:52 -07:00
type RoundStepType uint8 // These must be numeric, ordered.
2014-10-20 19:02:10 -07:00
2014-09-14 15:37:32 -07:00
const (
2015-06-05 14:15:40 -07:00
RoundStepNewHeight = RoundStepType ( 0x01 ) // Wait til CommitTime + timeoutCommit
RoundStepNewRound = RoundStepType ( 0x02 ) // Setup new round and go to RoundStepPropose
RoundStepPropose = RoundStepType ( 0x03 ) // Did propose, gossip proposal
RoundStepPrevote = RoundStepType ( 0x04 ) // Did prevote, gossip prevotes
RoundStepPrevoteWait = RoundStepType ( 0x05 ) // Did receive any +2/3 prevotes, start timeout
RoundStepPrecommit = RoundStepType ( 0x06 ) // Did precommit, gossip precommits
RoundStepPrecommitWait = RoundStepType ( 0x07 ) // Did receive any +2/3 precommits, start timeout
RoundStepCommit = RoundStepType ( 0x08 ) // Entered commit state machine
// NOTE: RoundStepNewHeight acts as RoundStepCommitWait.
2014-12-30 17:14:54 -08:00
)
2015-04-20 23:59:52 -07:00
func ( rs RoundStepType ) String ( ) string {
2014-12-30 17:14:54 -08:00
switch rs {
case RoundStepNewHeight :
return "RoundStepNewHeight"
case RoundStepNewRound :
return "RoundStepNewRound"
case RoundStepPropose :
return "RoundStepPropose"
case RoundStepPrevote :
return "RoundStepPrevote"
2015-06-05 14:15:40 -07:00
case RoundStepPrevoteWait :
return "RoundStepPrevoteWait"
2014-12-30 17:14:54 -08:00
case RoundStepPrecommit :
return "RoundStepPrecommit"
2015-06-05 14:15:40 -07:00
case RoundStepPrecommitWait :
return "RoundStepPrecommitWait"
2014-12-30 17:14:54 -08:00
case RoundStepCommit :
return "RoundStepCommit"
default :
2015-07-05 15:42:37 -07:00
return "RoundStepUnknown" // Cannot panic.
2014-12-30 17:14:54 -08:00
}
}
2014-10-30 03:32:09 -07:00
//-----------------------------------------------------------------------------
2014-09-14 15:37:32 -07:00
// Immutable when returned from ConsensusState.GetRoundState()
2017-03-02 20:47:07 -05:00
// TODO: Actually, only the top pointer is copied,
// so access to field pointers is still racey
2014-09-14 15:37:32 -07:00
type RoundState struct {
2015-06-25 20:28:34 -07:00
Height int // Height we are working on
Round int
2015-04-20 23:59:52 -07:00
Step RoundStepType
2014-10-26 13:26:27 -07:00
StartTime time . Time
2015-06-05 14:15:40 -07:00
CommitTime time . Time // Subjective time when +2/3 precommits for Block at Round were found
2015-08-10 20:38:45 -07:00
Validators * types . ValidatorSet
Proposal * types . Proposal
2015-03-22 19:00:08 -07:00
ProposalBlock * types . Block
ProposalBlockParts * types . PartSet
2015-06-25 20:28:34 -07:00
LockedRound int
2015-03-22 19:00:08 -07:00
LockedBlock * types . Block
LockedBlockParts * types . PartSet
2015-06-05 14:15:40 -07:00
Votes * HeightVoteSet
2015-09-15 16:13:39 -04:00
CommitRound int //
2015-08-10 20:38:45 -07:00
LastCommit * types . VoteSet // Last precommits at Height-1
LastValidators * types . ValidatorSet
2014-09-14 15:37:32 -07:00
}
2016-01-28 19:44:44 -08:00
func ( rs * RoundState ) RoundStateEvent ( ) types . EventDataRoundState {
edrs := types . EventDataRoundState {
2016-01-14 11:07:31 -08:00
Height : rs . Height ,
Round : rs . Round ,
Step : rs . Step . String ( ) ,
RoundState : rs ,
2015-09-09 16:45:53 -04:00
}
2015-12-14 00:38:19 -05:00
return edrs
2015-09-09 16:45:53 -04:00
}
2014-10-18 01:42:33 -07:00
func ( rs * RoundState ) String ( ) string {
2014-12-23 01:35:54 -08:00
return rs . StringIndented ( "" )
2014-10-18 01:42:33 -07:00
}
2014-12-23 01:35:54 -08:00
func ( rs * RoundState ) StringIndented ( indent string ) string {
2014-10-18 01:42:33 -07:00
return fmt . Sprintf ( ` RoundState {
% s H : % v R : % v S : % v
% s StartTime : % v
2014-10-21 18:30:03 -07:00
% s CommitTime : % v
2014-10-18 01:42:33 -07:00
% s Validators : % v
% s Proposal : % v
% s ProposalBlock : % v % v
2015-06-24 18:51:14 -07:00
% s LockedRound : % v
2014-10-21 18:30:03 -07:00
% s LockedBlock : % v % v
2015-06-05 14:15:40 -07:00
% s Votes : % v
2015-06-19 15:30:10 -07:00
% s LastCommit : % v
2015-06-26 17:48:24 -07:00
% s LastValidators : % v
2014-10-18 01:42:33 -07:00
% s } ` ,
indent , rs . Height , rs . Round , rs . Step ,
indent , rs . StartTime ,
2014-10-21 18:30:03 -07:00
indent , rs . CommitTime ,
2014-12-23 01:35:54 -08:00
indent , rs . Validators . StringIndented ( indent + " " ) ,
2014-10-18 01:42:33 -07:00
indent , rs . Proposal ,
2014-12-23 01:35:54 -08:00
indent , rs . ProposalBlockParts . StringShort ( ) , rs . ProposalBlock . StringShort ( ) ,
2015-06-24 18:51:14 -07:00
indent , rs . LockedRound ,
2014-12-23 01:35:54 -08:00
indent , rs . LockedBlockParts . StringShort ( ) , rs . LockedBlock . StringShort ( ) ,
2015-06-05 14:15:40 -07:00
indent , rs . Votes . StringIndented ( indent + " " ) ,
2015-06-19 15:30:10 -07:00
indent , rs . LastCommit . StringShort ( ) ,
2015-06-26 17:48:24 -07:00
indent , rs . LastValidators . StringIndented ( indent + " " ) ,
2014-10-18 01:42:33 -07:00
indent )
}
2014-12-23 01:35:54 -08:00
func ( rs * RoundState ) StringShort ( ) string {
2014-12-30 17:14:54 -08:00
return fmt . Sprintf ( ` RoundState { H:%v R:%v S:%v ST:%v} ` ,
2014-10-24 14:37:12 -07:00
rs . Height , rs . Round , rs . Step , rs . StartTime )
}
2014-10-30 03:32:09 -07:00
//-----------------------------------------------------------------------------
2014-09-14 15:37:32 -07:00
2015-12-05 14:58:12 -05:00
var (
2016-12-23 11:11:22 -05:00
msgQueueSize = 1000
2015-12-05 14:58:12 -05:00
)
2015-12-12 01:28:33 -05:00
// msgs from the reactor which may update the state
2015-12-08 16:00:59 -05:00
type msgInfo struct {
2015-12-22 15:23:22 -05:00
Msg ConsensusMessage ` json:"msg" `
PeerKey string ` json:"peer_key" `
2015-12-08 16:00:59 -05:00
}
2015-12-12 01:28:33 -05:00
// internally generated messages which may update the state
2015-12-08 16:00:59 -05:00
type timeoutInfo struct {
2015-12-22 15:23:22 -05:00
Duration time . Duration ` json:"duration" `
Height int ` json:"height" `
Round int ` json:"round" `
Step RoundStepType ` json:"step" `
2015-12-08 16:00:59 -05:00
}
func ( ti * timeoutInfo ) String ( ) string {
2015-12-22 15:23:22 -05:00
return fmt . Sprintf ( "%v ; %d/%d %v" , ti . Duration , ti . Height , ti . Round , ti . Step )
2015-12-08 16:00:59 -05:00
}
2016-06-26 15:33:11 -04:00
type PrivValidator interface {
GetAddress ( ) [ ] byte
SignVote ( chainID string , vote * types . Vote ) error
SignProposal ( chainID string , proposal * types . Proposal ) error
}
2014-09-03 20:41:57 -07:00
// Tracks consensus state across block heights and rounds.
type ConsensusState struct {
2016-10-28 12:14:24 -07:00
BaseService
2014-10-30 03:32:09 -07:00
2016-11-16 20:52:08 -05:00
config cfg . Config
proxyAppConn proxy . AppConnConsensus
2017-02-20 20:09:15 -05:00
blockStore types . BlockStore
mempool types . Mempool
2016-11-16 20:52:08 -05:00
2016-12-02 00:12:06 -05:00
privValidator PrivValidator // for signing votes
2014-09-14 15:37:32 -07:00
2014-10-07 01:05:54 -07:00
mtx sync . Mutex
RoundState
2016-01-06 17:14:20 -08:00
state * sm . State // State until height-1.
2015-04-07 15:24:09 -05:00
2016-12-19 22:29:32 -05:00
peerMsgQueue chan msgInfo // serializes msgs affecting state (proposals, block parts, votes)
internalMsgQueue chan msgInfo // like peerMsgQueue but for our own proposals, parts, votes
timeoutTicker TimeoutTicker // ticker for timeouts
timeoutParams * TimeoutParams // parameters and functions for timeout intervals
2015-12-05 14:58:12 -05:00
2016-10-10 02:58:13 -04:00
evsw types . EventSwitch
2015-12-12 01:28:33 -05:00
2016-09-08 18:06:25 -04:00
wal * WAL
replayMode bool // so we don't log signing errors during replay
2015-12-22 15:23:22 -05:00
2015-12-12 01:28:33 -05:00
nSteps int // used for testing to limit the number of transitions the state makes
2016-06-26 15:33:11 -04:00
// allow certain function to be overwritten for testing
decideProposal func ( height , round int )
doPrevote func ( height , round int )
setProposal func ( proposal * types . Proposal ) error
2017-01-12 14:44:42 -05:00
done chan struct { }
2014-09-14 15:37:32 -07:00
}
2017-02-20 20:09:15 -05:00
func NewConsensusState ( config cfg . Config , state * sm . State , proxyAppConn proxy . AppConnConsensus , blockStore types . BlockStore , mempool types . Mempool ) * ConsensusState {
2014-09-14 15:37:32 -07:00
cs := & ConsensusState {
2016-05-08 15:00:58 -07:00
config : config ,
2016-01-06 17:14:20 -08:00
proxyAppConn : proxyAppConn ,
2015-12-11 11:57:15 -05:00
blockStore : blockStore ,
mempool : mempool ,
peerMsgQueue : make ( chan msgInfo , msgQueueSize ) ,
internalMsgQueue : make ( chan msgInfo , msgQueueSize ) ,
2016-12-19 10:44:25 -05:00
timeoutTicker : NewTimeoutTicker ( ) ,
2016-05-08 15:00:58 -07:00
timeoutParams : InitTimeoutParamsFromConfig ( config ) ,
2017-01-12 14:44:42 -05:00
done : make ( chan struct { } ) ,
2014-09-14 15:37:32 -07:00
}
2016-06-26 15:33:11 -04:00
// set function defaults (may be overwritten before calling Start)
cs . decideProposal = cs . defaultDecideProposal
cs . doPrevote = cs . defaultDoPrevote
cs . setProposal = cs . defaultSetProposal
2015-09-15 16:13:39 -04:00
cs . updateToState ( state )
2015-06-24 14:04:40 -07:00
// Don't call scheduleRound0 yet.
// We do that upon Start().
2015-06-19 15:30:10 -07:00
cs . reconstructLastCommit ( state )
2016-10-28 12:14:24 -07:00
cs . BaseService = * NewBaseService ( log , "ConsensusState" , cs )
2014-09-03 20:41:57 -07:00
return cs
2014-08-10 16:35:08 -07:00
}
2015-12-12 01:28:33 -05:00
//----------------------------------------
// Public interface
// implements events.Eventable
2016-10-10 02:58:13 -04:00
func ( cs * ConsensusState ) SetEventSwitch ( evsw types . EventSwitch ) {
2015-12-12 01:28:33 -05:00
cs . evsw = evsw
}
func ( cs * ConsensusState ) String ( ) string {
2016-07-11 23:07:21 -04:00
// better not to access shared variables
return Fmt ( "ConsensusState" ) //(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
2015-06-04 13:36:47 -07:00
}
2015-01-14 20:34:53 -08:00
func ( cs * ConsensusState ) GetState ( ) * sm . State {
2015-01-11 14:27:46 -08:00
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
return cs . state . Copy ( )
}
2014-09-14 15:37:32 -07:00
func ( cs * ConsensusState ) GetRoundState ( ) * RoundState {
2014-09-03 20:41:57 -07:00
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
2014-10-30 03:32:09 -07:00
return cs . getRoundState ( )
}
func ( cs * ConsensusState ) getRoundState ( ) * RoundState {
2014-09-14 15:37:32 -07:00
rs := cs . RoundState // copy
return & rs
2014-08-10 16:35:08 -07:00
}
2016-10-14 21:36:42 -04:00
func ( cs * ConsensusState ) GetValidators ( ) ( int , [ ] * types . Validator ) {
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
return cs . state . LastBlockHeight , cs . state . Validators . Copy ( ) . Validators
}
2016-11-16 20:52:08 -05:00
// Sets our private validator account for signing votes.
2016-06-26 15:33:11 -04:00
func ( cs * ConsensusState ) SetPrivValidator ( priv PrivValidator ) {
2015-12-12 01:28:33 -05:00
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
cs . privValidator = priv
2016-11-16 20:52:08 -05:00
}
2016-12-19 10:44:25 -05:00
// Set the local timer
func ( cs * ConsensusState ) SetTimeoutTicker ( timeoutTicker TimeoutTicker ) {
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
cs . timeoutTicker = timeoutTicker
}
2016-11-16 16:47:31 -05:00
func ( cs * ConsensusState ) LoadCommit ( height int ) * types . Commit {
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
if height == cs . blockStore . Height ( ) {
return cs . blockStore . LoadSeenCommit ( height )
}
return cs . blockStore . LoadBlockCommit ( height )
}
2015-08-04 18:44:15 -07:00
func ( cs * ConsensusState ) OnStart ( ) error {
2016-10-28 12:14:24 -07:00
cs . BaseService . OnStart ( )
2015-12-10 11:41:18 -05:00
2017-02-17 19:12:05 -05:00
walFile := cs . config . GetString ( "cs_wal_file" )
err := EnsureDir ( path . Dir ( walFile ) , 0700 )
2016-08-14 12:31:24 -04:00
if err != nil {
2016-11-05 09:15:34 -07:00
log . Error ( "Error ensuring ConsensusState wal dir" , "error" , err . Error ( ) )
return err
}
2017-02-17 19:12:05 -05:00
err = cs . OpenWAL ( walFile )
2016-11-05 09:15:34 -07:00
if err != nil {
log . Error ( "Error loading ConsensusState wal" , "error" , err . Error ( ) )
2016-08-14 12:31:24 -04:00
return err
}
2016-09-08 18:06:25 -04:00
// we need the timeoutRoutine for replay so
// we don't block on the tick chan.
// NOTE: we will get a build up of garbage go routines
// firing on the tockChan until the receiveRoutine is started
// to deal with them (by that point, at most one will be valid)
2016-12-19 22:29:32 -05:00
cs . timeoutTicker . Start ( )
2016-01-10 23:31:05 -05:00
// we may have lost some votes if the process crashed
// reload from consensus log to catchup
if err := cs . catchupReplay ( cs . Height ) ; err != nil {
log . Error ( "Error on catchup replay" , "error" , err . Error ( ) )
// let's go for it anyways, maybe we're fine
}
2016-09-09 23:10:23 -04:00
// now start the receiveRoutine
2016-09-08 18:06:25 -04:00
go cs . receiveRoutine ( 0 )
2016-09-09 23:10:23 -04:00
// schedule the first round!
// use GetRoundState so we don't race the receiveRoutine for access
cs . scheduleRound0 ( cs . GetRoundState ( ) )
2015-08-04 18:44:15 -07:00
return nil
2015-07-20 14:40:41 -07:00
}
2015-12-12 17:22:48 -05:00
// timeoutRoutine: receive requests for timeouts on tickChan and fire timeouts on tockChan
// receiveRoutine: serializes processing of proposoals, block parts, votes; coordinates state transitions
2015-12-12 01:28:33 -05:00
func ( cs * ConsensusState ) startRoutines ( maxSteps int ) {
2016-12-19 22:29:32 -05:00
cs . timeoutTicker . Start ( )
2015-12-12 17:22:48 -05:00
go cs . receiveRoutine ( maxSteps )
2015-12-10 11:41:18 -05:00
}
2015-07-21 18:31:01 -07:00
func ( cs * ConsensusState ) OnStop ( ) {
2016-10-28 12:14:24 -07:00
cs . BaseService . OnStop ( )
2016-07-11 20:54:32 -04:00
2016-12-19 22:29:32 -05:00
cs . timeoutTicker . Stop ( )
2016-10-28 15:01:14 -07:00
// Make BaseService.Wait() wait until cs.wal.Wait()
2016-03-03 06:18:32 +00:00
if cs . wal != nil && cs . IsRunning ( ) {
2016-02-29 18:02:22 -05:00
cs . wal . Wait ( )
}
2014-10-30 03:32:09 -07:00
}
2017-01-12 14:44:42 -05:00
// NOTE: be sure to Stop() the event switch and drain
// any event channels or this may deadlock
func ( cs * ConsensusState ) Wait ( ) {
<- cs . done
}
2016-01-06 18:42:12 -05:00
// Open file to log all consensus messages and timeouts for deterministic accountability
2017-02-17 19:12:05 -05:00
func ( cs * ConsensusState ) OpenWAL ( walFile string ) ( err error ) {
2016-01-06 18:42:12 -05:00
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
2017-02-17 19:12:05 -05:00
wal , err := NewWAL ( walFile , cs . config . GetBool ( "cs_wal_light" ) )
2016-01-18 14:10:05 -05:00
if err != nil {
return err
2016-01-10 23:31:05 -05:00
}
2016-01-18 14:10:05 -05:00
cs . wal = wal
return nil
2016-01-06 18:42:12 -05:00
}
2015-12-12 17:22:48 -05:00
//------------------------------------------------------------
// Public interface for passing messages into the consensus state,
// possibly causing a state transition
// TODO: should these return anything or let callers just use events?
2015-12-12 01:28:33 -05:00
// May block on send if queue is full.
2016-07-01 17:47:31 -04:00
func ( cs * ConsensusState ) AddVote ( vote * types . Vote , peerKey string ) ( added bool , err error ) {
2015-12-12 01:28:33 -05:00
if peerKey == "" {
2016-07-01 17:47:31 -04:00
cs . internalMsgQueue <- msgInfo { & VoteMessage { vote } , "" }
2015-12-12 01:28:33 -05:00
} else {
2016-07-01 17:47:31 -04:00
cs . peerMsgQueue <- msgInfo { & VoteMessage { vote } , peerKey }
2015-12-12 01:28:33 -05:00
}
// TODO: wait for event?!
2016-07-01 17:47:31 -04:00
return false , nil
2015-12-12 01:28:33 -05:00
}
2015-12-12 16:25:49 -05:00
// May block on send if queue is full.
2015-12-12 01:28:33 -05:00
func ( cs * ConsensusState ) SetProposal ( proposal * types . Proposal , peerKey string ) error {
if peerKey == "" {
cs . internalMsgQueue <- msgInfo { & ProposalMessage { proposal } , "" }
} else {
cs . peerMsgQueue <- msgInfo { & ProposalMessage { proposal } , peerKey }
}
// TODO: wait for event?!
return nil
}
2015-12-12 16:25:49 -05:00
// May block on send if queue is full.
2015-12-12 01:28:33 -05:00
func ( cs * ConsensusState ) AddProposalBlockPart ( height , round int , part * types . Part , peerKey string ) error {
if peerKey == "" {
cs . internalMsgQueue <- msgInfo { & BlockPartMessage { height , round , part } , "" }
} else {
cs . peerMsgQueue <- msgInfo { & BlockPartMessage { height , round , part } , peerKey }
}
// TODO: wait for event?!
return nil
}
2015-12-15 14:44:58 -05:00
// May block on send if queue is full.
2015-12-12 01:28:33 -05:00
func ( cs * ConsensusState ) SetProposalAndBlock ( proposal * types . Proposal , block * types . Block , parts * types . PartSet , peerKey string ) error {
cs . SetProposal ( proposal , peerKey )
for i := 0 ; i < parts . Total ( ) ; i ++ {
part := parts . GetPart ( i )
2015-12-13 14:56:05 -05:00
cs . AddProposalBlockPart ( proposal . Height , proposal . Round , part , peerKey )
2015-12-12 01:28:33 -05:00
}
return nil // TODO errors
}
2015-12-12 17:22:48 -05:00
//------------------------------------------------------------
2015-12-12 01:28:33 -05:00
// internal functions for managing the state
2015-12-08 16:00:59 -05:00
func ( cs * ConsensusState ) updateHeight ( height int ) {
cs . Height = height
}
func ( cs * ConsensusState ) updateRoundStep ( round int , step RoundStepType ) {
cs . Round = round
cs . Step = step
}
2015-12-13 19:33:05 -05:00
// enterNewRound(height, 0) at cs.StartTime.
2016-09-09 23:10:23 -04:00
func ( cs * ConsensusState ) scheduleRound0 ( rs * RoundState ) {
2015-07-19 21:49:13 +00:00
//log.Info("scheduleRound0", "now", time.Now(), "startTime", cs.StartTime)
2016-09-09 23:10:23 -04:00
sleepDuration := rs . StartTime . Sub ( time . Now ( ) )
cs . scheduleTimeout ( sleepDuration , rs . Height , 0 , RoundStepNewHeight )
2015-12-08 16:00:59 -05:00
}
2016-12-19 22:29:32 -05:00
// Attempt to schedule a timeout (by sending timeoutInfo on the tickChan)
2015-12-10 11:41:18 -05:00
func ( cs * ConsensusState ) scheduleTimeout ( duration time . Duration , height , round int , step RoundStepType ) {
2016-12-19 22:29:32 -05:00
cs . timeoutTicker . ScheduleTimeout ( timeoutInfo { duration , height , round , step } )
2015-06-05 14:15:40 -07:00
}
2015-12-12 01:28:33 -05:00
// send a msg into the receiveRoutine regarding our own proposal, block part, or vote
func ( cs * ConsensusState ) sendInternalMessage ( mi msgInfo ) {
select {
case cs . internalMsgQueue <- mi :
2015-12-13 19:33:05 -05:00
default :
// NOTE: using the go-routine means our votes can
// be processed out of order.
// TODO: use CList here for strict determinism and
// attempt push to internalMsgQueue in receiveRoutine
2016-08-09 20:06:19 -04:00
log . Warn ( "Internal msg queue is full. Using a go-routine" )
2015-12-12 01:28:33 -05:00
go func ( ) { cs . internalMsgQueue <- mi } ( )
}
}
2016-04-02 09:10:16 -07:00
// Reconstruct LastCommit from SeenCommit, which we saved along with the block,
2015-12-12 01:28:33 -05:00
// (which happens even before saving the state)
func ( cs * ConsensusState ) reconstructLastCommit ( state * sm . State ) {
if state . LastBlockHeight == 0 {
return
}
2016-04-02 09:10:16 -07:00
seenCommit := cs . blockStore . LoadSeenCommit ( state . LastBlockHeight )
2016-05-08 15:00:58 -07:00
lastPrecommits := types . NewVoteSet ( cs . config . GetString ( "chain_id" ) , state . LastBlockHeight , seenCommit . Round ( ) , types . VoteTypePrecommit , state . LastValidators )
2016-07-01 17:47:31 -04:00
for _ , precommit := range seenCommit . Precommits {
2015-12-12 01:28:33 -05:00
if precommit == nil {
continue
}
2016-07-01 17:47:31 -04:00
added , err := lastPrecommits . AddVote ( precommit )
2015-12-12 01:28:33 -05:00
if ! added || err != nil {
PanicCrisis ( Fmt ( "Failed to reconstruct LastCommit: %v" , err ) )
}
}
if ! lastPrecommits . HasTwoThirdsMajority ( ) {
PanicSanity ( "Failed to reconstruct LastCommit: Does not have +2/3 maj" )
}
cs . LastCommit = lastPrecommits
}
// Updates ConsensusState and increments height to match that of state.
// The round becomes 0 and cs.Step becomes RoundStepNewHeight.
func ( cs * ConsensusState ) updateToState ( state * sm . State ) {
if cs . CommitRound > - 1 && 0 < cs . Height && cs . Height != state . LastBlockHeight {
PanicSanity ( Fmt ( "updateToState() expected state height of %v but found %v" ,
cs . Height , state . LastBlockHeight ) )
}
if cs . state != nil && cs . state . LastBlockHeight + 1 != cs . Height {
// This might happen when someone else is mutating cs.state.
// Someone forgot to pass in state.Copy() somewhere?!
PanicSanity ( Fmt ( "Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v" ,
cs . state . LastBlockHeight + 1 , cs . Height ) )
}
// If state isn't further out than cs.state, just ignore.
// This happens when SwitchToConsensus() is called in the reactor.
// We don't want to reset e.g. the Votes.
if cs . state != nil && ( state . LastBlockHeight <= cs . state . LastBlockHeight ) {
log . Notice ( "Ignoring updateToState()" , "newHeight" , state . LastBlockHeight + 1 , "oldHeight" , cs . state . LastBlockHeight + 1 )
return
}
// Reset fields based on state.
validators := state . Validators
lastPrecommits := ( * types . VoteSet ) ( nil )
if cs . CommitRound > - 1 && cs . Votes != nil {
if ! cs . Votes . Precommits ( cs . CommitRound ) . HasTwoThirdsMajority ( ) {
PanicSanity ( "updateToState(state) called but last Precommit round didn't have +2/3" )
}
lastPrecommits = cs . Votes . Precommits ( cs . CommitRound )
}
2017-02-17 10:51:05 -05:00
// Next desired block height
height := state . LastBlockHeight + 1
2015-12-12 01:28:33 -05:00
// RoundState fields
cs . updateHeight ( height )
cs . updateRoundStep ( 0 , RoundStepNewHeight )
if cs . CommitTime . IsZero ( ) {
// "Now" makes it easier to sync up dev nodes.
// We add timeoutCommit to allow transactions
// to be gathered for the first block.
// And alternative solution that relies on clocks:
// cs.StartTime = state.LastBlockTime.Add(timeoutCommit)
2016-02-29 16:15:23 -05:00
cs . StartTime = cs . timeoutParams . Commit ( time . Now ( ) )
2015-12-12 01:28:33 -05:00
} else {
2016-02-29 16:15:23 -05:00
cs . StartTime = cs . timeoutParams . Commit ( cs . CommitTime )
2015-12-12 01:28:33 -05:00
}
cs . Validators = validators
cs . Proposal = nil
cs . ProposalBlock = nil
cs . ProposalBlockParts = nil
cs . LockedRound = 0
cs . LockedBlock = nil
cs . LockedBlockParts = nil
2016-05-08 15:00:58 -07:00
cs . Votes = NewHeightVoteSet ( cs . config . GetString ( "chain_id" ) , height , validators )
2015-12-12 01:28:33 -05:00
cs . CommitRound = - 1
cs . LastCommit = lastPrecommits
cs . LastValidators = state . LastValidators
cs . state = state
// Finally, broadcast RoundState
cs . newStep ( )
}
func ( cs * ConsensusState ) newStep ( ) {
2015-12-22 15:23:22 -05:00
rs := cs . RoundStateEvent ( )
2016-01-18 14:10:05 -05:00
cs . wal . Save ( rs )
2015-12-12 01:28:33 -05:00
cs . nSteps += 1
2015-12-13 19:30:15 -05:00
// newStep is called by updateToStep in NewConsensusState before the evsw is set!
if cs . evsw != nil {
2016-10-10 02:58:13 -04:00
types . FireEventNewRoundStep ( cs . evsw , rs )
2015-12-13 19:30:15 -05:00
}
2015-12-12 01:28:33 -05:00
}
//-----------------------------------------
// the main go routines
2015-12-11 11:57:15 -05:00
// receiveRoutine handles messages which may cause state transitions.
2015-12-12 01:28:33 -05:00
// it's argument (n) is the number of messages to process before exiting - use 0 to run forever
2015-12-11 11:57:15 -05:00
// It keeps the RoundState and is the only thing that updates it.
2015-12-13 19:30:15 -05:00
// Updates (state transitions) happen on timeouts, complete proposals, and 2/3 majorities
2015-12-12 01:28:33 -05:00
func ( cs * ConsensusState ) receiveRoutine ( maxSteps int ) {
2015-12-10 11:41:18 -05:00
for {
2015-12-12 01:28:33 -05:00
if maxSteps > 0 {
if cs . nSteps >= maxSteps {
log . Warn ( "reached max steps. exiting receive routine" )
cs . nSteps = 0
return
}
}
2015-12-11 11:57:15 -05:00
rs := cs . RoundState
2015-12-10 11:41:18 -05:00
var mi msgInfo
select {
2015-12-11 11:57:15 -05:00
case mi = <- cs . peerMsgQueue :
2016-01-18 14:10:05 -05:00
cs . wal . Save ( mi )
2015-12-10 11:41:18 -05:00
// handles proposals, block parts, votes
// may generate internal events (votes, complete proposals, 2/3 majorities)
cs . handleMsg ( mi , rs )
2015-12-11 11:57:15 -05:00
case mi = <- cs . internalMsgQueue :
2016-01-18 14:10:05 -05:00
cs . wal . Save ( mi )
2015-12-11 11:57:15 -05:00
// handles proposals, block parts, votes
cs . handleMsg ( mi , rs )
2016-12-19 22:29:32 -05:00
case ti := <- cs . timeoutTicker . Chan ( ) : // tockChan:
2016-01-18 14:10:05 -05:00
cs . wal . Save ( ti )
2015-12-10 11:41:18 -05:00
// if the timeout is relevant to the rs
// go to the next step
cs . handleTimeout ( ti , rs )
case <- cs . Quit :
2016-02-29 18:02:22 -05:00
2016-12-19 10:44:25 -05:00
// NOTE: the internalMsgQueue may have signed messages from our
// priv_val that haven't hit the WAL, but its ok because
// priv_val tracks LastSig
2016-02-29 18:02:22 -05:00
2016-01-18 14:10:05 -05:00
// close wal now that we're done writing to it
if cs . wal != nil {
2016-10-28 15:01:14 -07:00
cs . wal . Stop ( )
2016-01-18 14:10:05 -05:00
}
2017-01-12 14:44:42 -05:00
close ( cs . done )
2015-12-10 11:41:18 -05:00
return
}
}
}
2015-12-11 11:57:15 -05:00
// state transitions on complete-proposal, 2/3-any, 2/3-one
2015-12-10 11:41:18 -05:00
func ( cs * ConsensusState ) handleMsg ( mi msgInfo , rs RoundState ) {
2015-12-12 01:28:33 -05:00
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
2015-12-10 11:41:18 -05:00
var err error
2015-12-22 15:23:22 -05:00
msg , peerKey := mi . Msg , mi . PeerKey
2015-12-10 11:41:18 -05:00
switch msg := msg . ( type ) {
case * ProposalMessage :
2015-12-11 11:57:15 -05:00
// will not cause transition.
// once proposal is set, we can receive block parts
2015-12-12 01:28:33 -05:00
err = cs . setProposal ( msg . Proposal )
2015-12-10 11:41:18 -05:00
case * BlockPartMessage :
2015-12-13 19:33:05 -05:00
// if the proposal is complete, we'll enterPrevote or tryFinalizeCommit
2016-03-11 21:38:15 -05:00
_ , err = cs . addProposalBlockPart ( msg . Height , msg . Part , peerKey != "" )
2016-03-02 21:38:05 +00:00
if err != nil && msg . Round != cs . Round {
err = nil
}
2015-12-10 11:41:18 -05:00
case * VoteMessage :
// attempt to add the vote and dupeout the validator if its a duplicate signature
2015-12-11 11:57:15 -05:00
// if the vote gives us a 2/3-any or 2/3-one, we transition
2016-07-01 17:47:31 -04:00
err := cs . tryAddVote ( msg . Vote , peerKey )
2015-12-10 11:41:18 -05:00
if err == ErrAddingVote {
// TODO: punish peer
}
2015-12-13 19:30:15 -05:00
// NOTE: the vote is broadcast to peers by the reactor listening
// for vote events
2015-12-10 11:41:18 -05:00
2015-12-13 19:30:15 -05:00
// TODO: If rs.Height == vote.Height && rs.Round < vote.Round,
// the peer is sending us CatchupCommit precommits.
// We could make note of this and help filter in broadcastHasVoteMessage().
2015-12-10 11:41:18 -05:00
default :
log . Warn ( "Unknown msg type" , reflect . TypeOf ( msg ) )
}
if err != nil {
2016-03-01 16:04:19 -05:00
log . Error ( "Error with msg" , "type" , reflect . TypeOf ( msg ) , "peer" , peerKey , "error" , err , "msg" , msg )
2015-12-10 11:41:18 -05:00
}
}
func ( cs * ConsensusState ) handleTimeout ( ti timeoutInfo , rs RoundState ) {
2015-12-22 15:23:22 -05:00
log . Debug ( "Received tock" , "timeout" , ti . Duration , "height" , ti . Height , "round" , ti . Round , "step" , ti . Step )
2015-12-12 01:28:33 -05:00
2015-12-10 11:41:18 -05:00
// timeouts must be for current height, round, step
2015-12-22 15:23:22 -05:00
if ti . Height != rs . Height || ti . Round < rs . Round || ( ti . Round == rs . Round && ti . Step < rs . Step ) {
2015-12-10 11:41:18 -05:00
log . Debug ( "Ignoring tock because we're ahead" , "height" , rs . Height , "round" , rs . Round , "step" , rs . Step )
return
}
2015-12-11 11:57:15 -05:00
// the timeout will now cause a state transition
2015-12-12 01:28:33 -05:00
cs . mtx . Lock ( )
defer cs . mtx . Unlock ( )
2015-12-11 11:57:15 -05:00
2015-12-22 15:23:22 -05:00
switch ti . Step {
2015-12-12 17:22:48 -05:00
case RoundStepNewHeight :
2015-12-13 19:33:05 -05:00
// NewRound event fired from enterNewRound.
2016-12-19 10:44:25 -05:00
// XXX: should we fire timeout here (for timeout commit)?
2015-12-22 15:23:22 -05:00
cs . enterNewRound ( ti . Height , 0 )
2015-12-10 11:41:18 -05:00
case RoundStepPropose :
2016-10-10 02:58:13 -04:00
types . FireEventTimeoutPropose ( cs . evsw , cs . RoundStateEvent ( ) )
2015-12-22 15:23:22 -05:00
cs . enterPrevote ( ti . Height , ti . Round )
2015-12-10 11:41:18 -05:00
case RoundStepPrevoteWait :
2016-10-10 02:58:13 -04:00
types . FireEventTimeoutWait ( cs . evsw , cs . RoundStateEvent ( ) )
2015-12-22 15:23:22 -05:00
cs . enterPrecommit ( ti . Height , ti . Round )
2015-12-10 11:41:18 -05:00
case RoundStepPrecommitWait :
2016-10-10 02:58:13 -04:00
types . FireEventTimeoutWait ( cs . evsw , cs . RoundStateEvent ( ) )
2015-12-22 15:23:22 -05:00
cs . enterNewRound ( ti . Height , ti . Round + 1 )
2015-12-10 11:41:18 -05:00
default :
2015-12-22 15:23:22 -05:00
panic ( Fmt ( "Invalid timeout step: %v" , ti . Step ) )
2015-12-10 11:41:18 -05:00
}
}
2014-10-21 23:30:18 -07:00
//-----------------------------------------------------------------------------
2015-12-12 01:28:33 -05:00
// State functions
2015-12-14 00:38:19 -05:00
// Used internally by handleTimeout and handleMsg to make state transitions
2014-08-10 16:35:08 -07:00
2015-06-24 18:51:14 -07:00
// Enter: +2/3 precommits for nil at (height,round-1)
2015-06-24 17:05:52 -07:00
// Enter: `timeoutPrecommits` after any +2/3 precommits from (height,round-1)
// Enter: `startTime = commitTime+timeoutCommit` from NewHeight(height)
2015-06-05 14:15:40 -07:00
// NOTE: cs.StartTime was already set for height.
2015-12-13 19:33:05 -05:00
func ( cs * ConsensusState ) enterNewRound ( height int , round int ) {
2015-06-24 14:04:40 -07:00
if cs . Height != height || round < cs . Round || ( cs . Round == round && cs . Step != RoundStepNewHeight ) {
2015-12-13 19:33:05 -05:00
log . Debug ( Fmt ( "enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
return
2014-10-30 03:32:09 -07:00
}
2015-09-09 16:45:53 -04:00
2015-06-05 14:15:40 -07:00
if now := time . Now ( ) ; cs . StartTime . After ( now ) {
log . Warn ( "Need to set a buffer and log.Warn() here for sanity." , "startTime" , cs . StartTime , "now" , now )
2014-10-30 03:32:09 -07:00
}
2015-12-11 11:57:15 -05:00
2015-12-13 19:33:05 -05:00
log . Notice ( Fmt ( "enterNewRound(%v/%v). Current: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
// Increment validators if necessary
2015-06-24 14:04:40 -07:00
validators := cs . Validators
2015-06-05 14:15:40 -07:00
if cs . Round < round {
2015-06-24 14:04:40 -07:00
validators = validators . Copy ( )
2015-06-05 14:15:40 -07:00
validators . IncrementAccum ( round - cs . Round )
}
// Setup new round
2015-12-12 16:25:49 -05:00
// we don't fire newStep for this step,
// but we fire an event, so update the round step first
2015-12-08 16:00:59 -05:00
cs . updateRoundStep ( round , RoundStepNewRound )
2015-06-05 14:15:40 -07:00
cs . Validators = validators
2015-07-05 15:35:26 -07:00
if round == 0 {
// We've already reset these upon new height,
// and meanwhile we might have received a proposal
// for round 0.
} else {
cs . Proposal = nil
cs . ProposalBlock = nil
cs . ProposalBlockParts = nil
}
2015-06-24 17:05:52 -07:00
cs . Votes . SetRound ( round + 1 ) // also track next round (round+1) to allow round-skipping
2015-06-05 14:15:40 -07:00
2016-10-10 02:58:13 -04:00
types . FireEventNewRound ( cs . evsw , cs . RoundStateEvent ( ) )
2015-09-09 16:45:53 -04:00
2015-12-13 19:33:05 -05:00
// Immediately go to enterPropose.
cs . enterPropose ( height , round )
2014-10-30 03:32:09 -07:00
}
2015-06-24 17:05:52 -07:00
// Enter: from NewRound(height,round).
2015-12-13 19:33:05 -05:00
func ( cs * ConsensusState ) enterPropose ( height int , round int ) {
2015-06-24 17:05:52 -07:00
if cs . Height != height || round < cs . Round || ( cs . Round == round && RoundStepPropose <= cs . Step ) {
2015-12-13 19:33:05 -05:00
log . Debug ( Fmt ( "enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2014-10-21 01:18:46 -07:00
return
}
2015-12-13 19:33:05 -05:00
log . Info ( Fmt ( "enterPropose(%v/%v). Current: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
2015-12-12 16:25:49 -05:00
defer func ( ) {
2015-12-13 19:33:05 -05:00
// Done enterPropose:
2015-12-12 16:25:49 -05:00
cs . updateRoundStep ( round , RoundStepPropose )
cs . newStep ( )
2015-12-14 00:38:19 -05:00
// If we have the whole proposal + POL, then goto Prevote now.
// else, we'll enterPrevote when the rest of the proposal is received (in AddProposalBlockPart),
// or else after timeoutPropose
if cs . isProposalComplete ( ) {
cs . enterPrevote ( height , cs . Round )
}
2015-12-12 16:25:49 -05:00
} ( )
2016-02-29 16:15:23 -05:00
// If we don't get the proposal and all block parts quick enough, enterPrevote
cs . scheduleTimeout ( cs . timeoutParams . Propose ( round ) , height , round , RoundStepPropose )
2014-09-14 15:37:32 -07:00
2015-06-24 17:05:52 -07:00
// Nothing more to do if we're not a validator
2015-04-20 20:39:42 -07:00
if cs . privValidator == nil {
2015-01-08 22:07:23 -08:00
return
}
2015-06-05 14:15:40 -07:00
2016-06-26 15:33:11 -04:00
if ! bytes . Equal ( cs . Validators . Proposer ( ) . Address , cs . privValidator . GetAddress ( ) ) {
2015-12-13 19:33:05 -05:00
log . Info ( "enterPropose: Not our turn to propose" , "proposer" , cs . Validators . Proposer ( ) . Address , "privValidator" , cs . privValidator )
2015-01-08 22:07:23 -08:00
} else {
2015-12-13 19:33:05 -05:00
log . Info ( "enterPropose: Our turn to propose" , "proposer" , cs . Validators . Proposer ( ) . Address , "privValidator" , cs . privValidator )
2015-08-26 18:56:34 -04:00
cs . decideProposal ( height , round )
2015-12-10 11:41:18 -05:00
2016-06-26 15:33:11 -04:00
}
2015-06-24 17:05:52 -07:00
}
2014-08-10 16:35:08 -07:00
2016-06-26 15:33:11 -04:00
func ( cs * ConsensusState ) defaultDecideProposal ( height , round int ) {
2015-03-22 19:00:08 -07:00
var block * types . Block
var blockParts * types . PartSet
2014-09-14 15:37:32 -07:00
2015-06-05 14:15:40 -07:00
// Decide on block
2014-09-14 15:37:32 -07:00
if cs . LockedBlock != nil {
// If we're locked onto a block, just choose that.
2015-06-05 14:15:40 -07:00
block , blockParts = cs . LockedBlock , cs . LockedBlockParts
2014-08-10 16:35:08 -07:00
} else {
2015-06-05 14:15:40 -07:00
// Create a new proposal block from state/txs from the mempool.
block , blockParts = cs . createProposalBlock ( )
2015-12-01 20:12:01 -08:00
if block == nil { // on error
return
}
2014-08-10 16:35:08 -07:00
}
2014-09-14 15:37:32 -07:00
// Make proposal
2016-08-20 15:08:26 -07:00
polRound , polBlockID := cs . Votes . POLInfo ( )
proposal := types . NewProposal ( height , round , blockParts . Header ( ) , polRound , polBlockID )
2015-05-29 17:53:57 -04:00
err := cs . privValidator . SignProposal ( cs . state . ChainID , proposal )
2014-12-31 16:14:26 -08:00
if err == nil {
// Set fields
2015-12-12 01:28:33 -05:00
/ * fields set by setProposal and addBlockPart
2014-12-31 16:14:26 -08:00
cs . Proposal = proposal
2015-01-15 22:43:15 -08:00
cs . ProposalBlock = block
2014-12-31 16:14:26 -08:00
cs . ProposalBlockParts = blockParts
2015-12-12 01:28:33 -05:00
* /
2015-12-05 14:58:12 -05:00
2015-12-12 01:28:33 -05:00
// send proposal and block parts on internal msg queue
2015-12-11 11:57:15 -05:00
cs . sendInternalMessage ( msgInfo { & ProposalMessage { proposal } , "" } )
for i := 0 ; i < blockParts . Total ( ) ; i ++ {
part := blockParts . GetPart ( i )
cs . sendInternalMessage ( msgInfo { & BlockPartMessage { cs . Height , cs . Round , part } , "" } )
}
2016-03-10 20:20:07 -05:00
log . Info ( "Signed proposal" , "height" , height , "round" , round , "proposal" , proposal )
log . Debug ( Fmt ( "Signed proposal block: %v" , block ) )
2014-12-31 16:14:26 -08:00
} else {
2016-09-08 18:06:25 -04:00
if ! cs . replayMode {
log . Warn ( "enterPropose: Error signing proposal" , "height" , height , "round" , round , "error" , err )
}
2015-06-05 14:15:40 -07:00
}
}
2015-06-24 17:05:52 -07:00
// Returns true if the proposal block is complete &&
// (if POLRound was proposed, we have +2/3 prevotes from there).
2015-06-05 14:15:40 -07:00
func ( cs * ConsensusState ) isProposalComplete ( ) bool {
if cs . Proposal == nil || cs . ProposalBlock == nil {
return false
}
2015-08-12 14:00:23 -04:00
// we have the proposal. if there's a POLRound,
// make sure we have the prevotes from it too
2015-06-24 14:04:40 -07:00
if cs . Proposal . POLRound < 0 {
return true
} else {
2015-08-12 14:00:23 -04:00
// if this is false the proposer is lying or we haven't received the POL yet
2015-06-25 20:28:34 -07:00
return cs . Votes . Prevotes ( cs . Proposal . POLRound ) . HasTwoThirdsMajority ( )
2015-06-24 14:04:40 -07:00
}
2015-06-05 14:15:40 -07:00
}
// Create the next block to propose and return it.
2015-12-01 20:12:01 -08:00
// Returns nil block upon error.
2015-08-26 18:56:34 -04:00
// NOTE: keep it side-effect free for clarity.
2015-06-24 14:04:40 -07:00
func ( cs * ConsensusState ) createProposalBlock ( ) ( block * types . Block , blockParts * types . PartSet ) {
2016-04-02 09:10:16 -07:00
var commit * types . Commit
2015-06-05 14:15:40 -07:00
if cs . Height == 1 {
// We're creating a proposal for the first block.
2016-04-02 09:10:16 -07:00
// The commit is empty, but not nil.
commit = & types . Commit { }
2015-06-19 15:30:10 -07:00
} else if cs . LastCommit . HasTwoThirdsMajority ( ) {
2016-04-02 09:10:16 -07:00
// Make the commit from LastCommit
commit = cs . LastCommit . MakeCommit ( )
2015-06-05 14:15:40 -07:00
} else {
// This shouldn't happen.
2016-04-02 09:10:16 -07:00
log . Error ( "enterPropose: Cannot propose anything: No commit for the previous block." )
2015-06-05 14:15:40 -07:00
return
}
2015-12-01 20:12:01 -08:00
2016-01-06 17:14:20 -08:00
// Mempool validated transactions
2016-05-08 15:00:58 -07:00
txs := cs . mempool . Reap ( cs . config . GetInt ( "block_size" ) )
2016-02-29 16:15:23 -05:00
2016-11-06 01:48:39 +00:00
return types . MakeBlock ( cs . Height , cs . state . ChainID , txs , commit ,
2016-11-16 16:13:17 -05:00
cs . state . LastBlockID , cs . state . Validators . Hash ( ) , cs . state . AppHash , cs . config . GetInt ( "block_part_size" ) )
2014-08-10 16:35:08 -07:00
}
2015-06-24 17:05:52 -07:00
// Enter: `timeoutPropose` after entering Propose.
2015-06-05 14:15:40 -07:00
// Enter: proposal block and POL is ready.
2015-06-24 17:05:52 -07:00
// Enter: any +2/3 prevotes for future round.
// Prevote for LockedBlock if we're locked, or ProposalBlock if valid.
2014-10-30 03:32:09 -07:00
// Otherwise vote nil.
2015-12-13 19:33:05 -05:00
func ( cs * ConsensusState ) enterPrevote ( height int , round int ) {
2015-06-24 17:05:52 -07:00
if cs . Height != height || round < cs . Round || ( cs . Round == round && RoundStepPrevote <= cs . Step ) {
2015-12-13 19:33:05 -05:00
log . Debug ( Fmt ( "enterPrevote(%v/%v): Invalid args. Current step: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
return
2014-10-21 01:18:46 -07:00
}
2015-09-09 16:45:53 -04:00
2015-12-12 16:25:49 -05:00
defer func ( ) {
2015-12-13 19:33:05 -05:00
// Done enterPrevote:
2015-12-12 16:25:49 -05:00
cs . updateRoundStep ( round , RoundStepPrevote )
cs . newStep ( )
} ( )
2015-09-09 16:45:53 -04:00
// fire event for how we got here
2015-12-11 11:57:15 -05:00
if cs . isProposalComplete ( ) {
2016-10-10 02:58:13 -04:00
types . FireEventCompleteProposal ( cs . evsw , cs . RoundStateEvent ( ) )
2015-09-09 16:45:53 -04:00
} else {
// we received +2/3 prevotes for a future round
// TODO: catchup event?
}
2015-12-13 19:33:05 -05:00
log . Info ( Fmt ( "enterPrevote(%v/%v). Current: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
// Sign and broadcast vote as necessary
cs . doPrevote ( height , round )
2015-06-24 17:05:52 -07:00
2015-09-09 16:45:53 -04:00
// Once `addVote` hits any +2/3 prevotes, we will go to PrevoteWait
// (so we have more time to try and collect +2/3 prevotes for a single block)
2015-06-05 14:15:40 -07:00
}
2016-06-26 15:33:11 -04:00
func ( cs * ConsensusState ) defaultDoPrevote ( height int , round int ) {
2014-10-21 01:18:46 -07:00
// If a block is locked, prevote that.
if cs . LockedBlock != nil {
2016-06-26 15:33:11 -04:00
log . Notice ( "enterPrevote: Block was locked" )
2015-03-22 19:00:08 -07:00
cs . signAddVote ( types . VoteTypePrevote , cs . LockedBlock . Hash ( ) , cs . LockedBlockParts . Header ( ) )
2014-10-30 03:32:09 -07:00
return
2014-10-24 14:37:12 -07:00
}
2014-10-30 03:32:09 -07:00
2014-10-24 14:37:12 -07:00
// If ProposalBlock is nil, prevote nil.
if cs . ProposalBlock == nil {
2015-12-13 19:33:05 -05:00
log . Warn ( "enterPrevote: ProposalBlock is nil" )
2015-03-22 19:00:08 -07:00
cs . signAddVote ( types . VoteTypePrevote , nil , types . PartSetHeader { } )
2014-10-30 03:32:09 -07:00
return
2014-10-21 01:18:46 -07:00
}
2014-10-30 03:32:09 -07:00
2016-01-06 17:14:20 -08:00
// Valdiate proposal block
err := cs . state . ValidateBlock ( cs . ProposalBlock )
2014-10-21 01:18:46 -07:00
if err != nil {
2014-10-30 03:32:09 -07:00
// ProposalBlock is invalid, prevote nil.
2015-12-13 19:33:05 -05:00
log . Warn ( "enterPrevote: ProposalBlock is invalid" , "error" , err )
2015-03-22 19:00:08 -07:00
cs . signAddVote ( types . VoteTypePrevote , nil , types . PartSetHeader { } )
2014-10-30 03:32:09 -07:00
return
2014-10-21 01:18:46 -07:00
}
2014-10-30 03:32:09 -07:00
// Prevote cs.ProposalBlock
2015-08-12 14:00:23 -04:00
// NOTE: the proposal signature is validated when it is received,
// and the proposal block parts are validated as they are received (against the merkle hash in the proposal)
2015-03-22 19:00:08 -07:00
cs . signAddVote ( types . VoteTypePrevote , cs . ProposalBlock . Hash ( ) , cs . ProposalBlockParts . Header ( ) )
2014-10-30 03:32:09 -07:00
return
2014-10-21 01:18:46 -07:00
}
2015-06-24 18:51:14 -07:00
// Enter: any +2/3 prevotes at next round.
2015-12-13 19:33:05 -05:00
func ( cs * ConsensusState ) enterPrevoteWait ( height int , round int ) {
2015-06-24 17:05:52 -07:00
if cs . Height != height || round < cs . Round || ( cs . Round == round && RoundStepPrevoteWait <= cs . Step ) {
2015-12-13 19:33:05 -05:00
log . Debug ( Fmt ( "enterPrevoteWait(%v/%v): Invalid args. Current step: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
return
}
if ! cs . Votes . Prevotes ( round ) . HasTwoThirdsAny ( ) {
2015-12-13 19:33:05 -05:00
PanicSanity ( Fmt ( "enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes" , height , round ) )
2015-06-05 14:15:40 -07:00
}
2015-12-13 19:33:05 -05:00
log . Info ( Fmt ( "enterPrevoteWait(%v/%v). Current: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
2015-12-12 16:25:49 -05:00
defer func ( ) {
2015-12-13 19:33:05 -05:00
// Done enterPrevoteWait:
2015-12-12 16:25:49 -05:00
cs . updateRoundStep ( round , RoundStepPrevoteWait )
cs . newStep ( )
} ( )
2015-06-05 14:15:40 -07:00
2016-02-29 16:15:23 -05:00
// Wait for some more prevotes; enterPrecommit
cs . scheduleTimeout ( cs . timeoutParams . Prevote ( round ) , height , round , RoundStepPrevoteWait )
2015-06-05 14:15:40 -07:00
}
// Enter: +2/3 precomits for block or nil.
// Enter: `timeoutPrevote` after any +2/3 prevotes.
// Enter: any +2/3 precommits for next round.
2015-08-26 18:56:34 -04:00
// Lock & precommit the ProposalBlock if we have enough prevotes for it (a POL in this round)
2015-06-05 14:15:40 -07:00
// else, unlock an existing lock and precommit nil if +2/3 of prevotes were nil,
2015-08-12 14:00:23 -04:00
// else, precommit nil otherwise.
2015-12-13 19:33:05 -05:00
func ( cs * ConsensusState ) enterPrecommit ( height int , round int ) {
2015-06-24 17:05:52 -07:00
if cs . Height != height || round < cs . Round || ( cs . Round == round && RoundStepPrecommit <= cs . Step ) {
2015-12-13 19:33:05 -05:00
log . Debug ( Fmt ( "enterPrecommit(%v/%v): Invalid args. Current step: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
return
2014-09-14 15:37:32 -07:00
}
2015-09-09 16:45:53 -04:00
2015-12-13 19:33:05 -05:00
log . Info ( Fmt ( "enterPrecommit(%v/%v). Current: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
2014-11-01 04:04:58 -07:00
defer func ( ) {
2015-12-13 19:33:05 -05:00
// Done enterPrecommit:
2015-12-12 16:25:49 -05:00
cs . updateRoundStep ( round , RoundStepPrecommit )
2015-12-12 01:28:33 -05:00
cs . newStep ( )
2014-11-01 04:04:58 -07:00
} ( )
2014-09-14 15:37:32 -07:00
2016-08-16 14:59:19 -07:00
blockID , ok := cs . Votes . Prevotes ( round ) . TwoThirdsMajority ( )
2015-06-05 14:15:40 -07:00
2015-09-09 16:45:53 -04:00
// If we don't have a polka, we must precommit nil
2014-10-30 03:32:09 -07:00
if ! ok {
2015-06-05 14:15:40 -07:00
if cs . LockedBlock != nil {
2016-06-26 15:33:11 -04:00
log . Notice ( "enterPrecommit: No +2/3 prevotes during enterPrecommit while we're locked. Precommitting nil" )
2015-06-05 14:15:40 -07:00
} else {
2016-06-26 15:33:11 -04:00
log . Notice ( "enterPrecommit: No +2/3 prevotes during enterPrecommit. Precommitting nil." )
2015-06-05 14:15:40 -07:00
}
2015-08-12 14:00:23 -04:00
cs . signAddVote ( types . VoteTypePrecommit , nil , types . PartSetHeader { } )
2014-10-30 03:32:09 -07:00
return
2014-09-14 15:37:32 -07:00
}
2014-08-10 16:35:08 -07:00
2015-08-12 14:00:23 -04:00
// At this point +2/3 prevoted for a particular block or nil
2016-10-10 02:58:13 -04:00
types . FireEventPolka ( cs . evsw , cs . RoundStateEvent ( ) )
2015-09-09 16:45:53 -04:00
// the latest POLRound should be this round
2016-08-20 15:08:26 -07:00
polRound , _ := cs . Votes . POLInfo ( )
if polRound < round {
PanicSanity ( Fmt ( "This POLRound should be %v but got %" , round , polRound ) )
2015-09-09 16:45:53 -04:00
}
2015-08-12 14:00:23 -04:00
2015-06-05 14:15:40 -07:00
// +2/3 prevoted nil. Unlock and precommit nil.
2016-08-16 14:59:19 -07:00
if len ( blockID . Hash ) == 0 {
2015-05-04 10:15:58 -07:00
if cs . LockedBlock == nil {
2016-03-02 21:38:05 +00:00
log . Notice ( "enterPrecommit: +2/3 prevoted for nil." )
2015-05-04 10:15:58 -07:00
} else {
2016-03-02 21:38:05 +00:00
log . Notice ( "enterPrecommit: +2/3 prevoted for nil. Unlocking" )
2015-08-26 18:56:34 -04:00
cs . LockedRound = 0
2015-05-04 10:15:58 -07:00
cs . LockedBlock = nil
cs . LockedBlockParts = nil
2016-10-10 02:58:13 -04:00
types . FireEventUnlock ( cs . evsw , cs . RoundStateEvent ( ) )
2015-05-04 10:15:58 -07:00
}
2015-06-05 14:15:40 -07:00
cs . signAddVote ( types . VoteTypePrecommit , nil , types . PartSetHeader { } )
2014-10-30 03:32:09 -07:00
return
}
2014-09-14 15:37:32 -07:00
2015-06-05 14:15:40 -07:00
// At this point, +2/3 prevoted for a particular block.
2015-08-12 14:00:23 -04:00
// If we're already locked on that block, precommit it, and update the LockedRound
2016-08-16 14:59:19 -07:00
if cs . LockedBlock . HashesTo ( blockID . Hash ) {
2016-03-02 21:38:05 +00:00
log . Notice ( "enterPrecommit: +2/3 prevoted locked block. Relocking" )
2015-08-12 14:00:23 -04:00
cs . LockedRound = round
2016-10-10 02:58:13 -04:00
types . FireEventRelock ( cs . evsw , cs . RoundStateEvent ( ) )
2016-08-16 14:59:19 -07:00
cs . signAddVote ( types . VoteTypePrecommit , blockID . Hash , blockID . PartsHeader )
2014-10-30 03:32:09 -07:00
return
}
2014-09-14 15:37:32 -07:00
2015-06-05 14:15:40 -07:00
// If +2/3 prevoted for proposal block, stage and precommit it
2016-08-16 14:59:19 -07:00
if cs . ProposalBlock . HashesTo ( blockID . Hash ) {
log . Notice ( "enterPrecommit: +2/3 prevoted proposal block. Locking" , "hash" , blockID . Hash )
2015-06-05 14:15:40 -07:00
// Validate the block.
2016-01-06 17:14:20 -08:00
if err := cs . state . ValidateBlock ( cs . ProposalBlock ) ; err != nil {
2015-12-13 19:33:05 -05:00
PanicConsensus ( Fmt ( "enterPrecommit: +2/3 prevoted for an invalid block: %v" , err ) )
2015-06-05 14:15:40 -07:00
}
2015-06-24 18:51:14 -07:00
cs . LockedRound = round
2015-06-05 14:15:40 -07:00
cs . LockedBlock = cs . ProposalBlock
cs . LockedBlockParts = cs . ProposalBlockParts
2016-10-10 02:58:13 -04:00
types . FireEventLock ( cs . evsw , cs . RoundStateEvent ( ) )
2016-08-16 14:59:19 -07:00
cs . signAddVote ( types . VoteTypePrecommit , blockID . Hash , blockID . PartsHeader )
2014-10-30 03:32:09 -07:00
return
2014-09-14 15:37:32 -07:00
}
2015-09-09 16:45:53 -04:00
// There was a polka in this round for a block we don't have.
// Fetch that block, unlock, and precommit nil.
2015-06-24 17:05:52 -07:00
// The +2/3 prevotes for this round is the POL for our unlock.
2015-08-19 16:11:52 -04:00
// TODO: In the future save the POL prevotes for justification.
cs . LockedRound = 0
2015-06-05 14:15:40 -07:00
cs . LockedBlock = nil
cs . LockedBlockParts = nil
2016-08-16 14:59:19 -07:00
if ! cs . ProposalBlockParts . HasHeader ( blockID . PartsHeader ) {
2015-06-05 14:15:40 -07:00
cs . ProposalBlock = nil
2016-08-16 14:59:19 -07:00
cs . ProposalBlockParts = types . NewPartSetFromHeader ( blockID . PartsHeader )
2015-06-05 14:15:40 -07:00
}
2016-10-10 02:58:13 -04:00
types . FireEventUnlock ( cs . evsw , cs . RoundStateEvent ( ) )
2015-06-24 14:04:40 -07:00
cs . signAddVote ( types . VoteTypePrecommit , nil , types . PartSetHeader { } )
2015-06-05 14:15:40 -07:00
return
}
// Enter: any +2/3 precommits for next round.
2015-12-13 19:33:05 -05:00
func ( cs * ConsensusState ) enterPrecommitWait ( height int , round int ) {
2015-06-24 17:05:52 -07:00
if cs . Height != height || round < cs . Round || ( cs . Round == round && RoundStepPrecommitWait <= cs . Step ) {
2015-12-13 19:33:05 -05:00
log . Debug ( Fmt ( "enterPrecommitWait(%v/%v): Invalid args. Current step: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-05-04 10:15:58 -07:00
return
}
2015-06-05 14:15:40 -07:00
if ! cs . Votes . Precommits ( round ) . HasTwoThirdsAny ( ) {
2015-12-13 19:33:05 -05:00
PanicSanity ( Fmt ( "enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes" , height , round ) )
2015-06-05 14:15:40 -07:00
}
2015-12-13 19:33:05 -05:00
log . Info ( Fmt ( "enterPrecommitWait(%v/%v). Current: %v/%v/%v" , height , round , cs . Height , cs . Round , cs . Step ) )
2015-05-04 10:15:58 -07:00
2015-12-12 16:25:49 -05:00
defer func ( ) {
2015-12-13 19:33:05 -05:00
// Done enterPrecommitWait:
2015-12-12 16:25:49 -05:00
cs . updateRoundStep ( round , RoundStepPrecommitWait )
cs . newStep ( )
} ( )
2015-06-05 14:15:40 -07:00
2016-02-29 16:15:23 -05:00
// Wait for some more precommits; enterNewRound
cs . scheduleTimeout ( cs . timeoutParams . Precommit ( round ) , height , round , RoundStepPrecommitWait )
2015-12-08 16:00:59 -05:00
2014-08-10 16:35:08 -07:00
}
2015-06-05 14:15:40 -07:00
// Enter: +2/3 precommits for block
2015-12-13 19:33:05 -05:00
func ( cs * ConsensusState ) enterCommit ( height int , commitRound int ) {
2015-06-24 17:05:52 -07:00
if cs . Height != height || RoundStepCommit <= cs . Step {
2015-12-13 19:33:05 -05:00
log . Debug ( Fmt ( "enterCommit(%v/%v): Invalid args. Current step: %v/%v/%v" , height , commitRound , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
return
2014-10-30 03:32:09 -07:00
}
2015-12-13 19:33:05 -05:00
log . Info ( Fmt ( "enterCommit(%v/%v). Current: %v/%v/%v" , height , commitRound , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
2014-11-01 04:04:58 -07:00
defer func ( ) {
2015-12-13 19:33:05 -05:00
// Done enterCommit:
2016-03-05 01:18:14 -05:00
// keep cs.Round the same, commitRound points to the right Precommits set.
2015-12-12 16:25:49 -05:00
cs . updateRoundStep ( cs . Round , RoundStepCommit )
2015-09-15 16:13:39 -04:00
cs . CommitRound = commitRound
2016-12-19 10:44:25 -05:00
cs . CommitTime = time . Now ( )
2015-12-12 01:28:33 -05:00
cs . newStep ( )
2015-06-05 14:15:40 -07:00
// Maybe finalize immediately.
2015-09-15 16:13:39 -04:00
cs . tryFinalizeCommit ( height )
2014-11-01 04:04:58 -07:00
} ( )
2016-08-16 14:59:19 -07:00
blockID , ok := cs . Votes . Precommits ( commitRound ) . TwoThirdsMajority ( )
2014-10-30 03:32:09 -07:00
if ! ok {
2015-07-19 23:42:52 +00:00
PanicSanity ( "RunActionCommit() expects +2/3 precommits" )
2014-10-30 03:32:09 -07:00
}
2015-06-05 14:15:40 -07:00
// The Locked* fields no longer matter.
// Move them over to ProposalBlock if they match the commit hash,
2015-08-26 18:56:34 -04:00
// otherwise they'll be cleared in updateToState.
2016-08-16 14:59:19 -07:00
if cs . LockedBlock . HashesTo ( blockID . Hash ) {
2014-10-30 03:32:09 -07:00
cs . ProposalBlock = cs . LockedBlock
cs . ProposalBlockParts = cs . LockedBlockParts
}
// If we don't have the block being committed, set up to get it.
2016-08-16 14:59:19 -07:00
if ! cs . ProposalBlock . HashesTo ( blockID . Hash ) {
if ! cs . ProposalBlockParts . HasHeader ( blockID . PartsHeader ) {
2014-10-30 03:32:09 -07:00
// We're getting the wrong block.
// Set up ProposalBlockParts and keep waiting.
cs . ProposalBlock = nil
2016-08-16 14:59:19 -07:00
cs . ProposalBlockParts = types . NewPartSetFromHeader ( blockID . PartsHeader )
2014-10-30 03:32:09 -07:00
} else {
// We just need to keep waiting.
}
2014-12-09 18:49:04 -08:00
}
2015-06-05 14:15:40 -07:00
}
2014-11-01 04:04:58 -07:00
2015-06-05 14:15:40 -07:00
// If we have the block AND +2/3 commits for it, finalize.
2015-09-15 16:13:39 -04:00
func ( cs * ConsensusState ) tryFinalizeCommit ( height int ) {
2015-06-24 14:04:40 -07:00
if cs . Height != height {
2015-07-19 23:42:52 +00:00
PanicSanity ( Fmt ( "tryFinalizeCommit() cs.Height: %v vs height: %v" , cs . Height , height ) )
2014-10-21 18:30:03 -07:00
}
2015-06-24 14:04:40 -07:00
2016-08-16 14:59:19 -07:00
blockID , ok := cs . Votes . Precommits ( cs . CommitRound ) . TwoThirdsMajority ( )
if ! ok || len ( blockID . Hash ) == 0 {
2017-03-05 02:04:09 -05:00
log . Warn ( "Attempt to finalize failed. There was no +2/3 majority, or +2/3 was for <nil>." , "height" , height )
2015-08-19 16:11:52 -04:00
return
2015-06-24 14:04:40 -07:00
}
2016-08-16 14:59:19 -07:00
if ! cs . ProposalBlock . HashesTo ( blockID . Hash ) {
2016-01-20 13:12:42 -05:00
// TODO: this happens every time if we're not a validator (ugly logs)
2016-10-17 11:29:43 -07:00
// TODO: ^^ wait, why does it matter that we're a validator?
2017-03-05 02:04:09 -05:00
log . Warn ( "Attempt to finalize failed. We don't have the commit block." , "height" , height , "proposal-block" , cs . ProposalBlock . Hash ( ) , "commit-block" , blockID . Hash )
2015-08-19 16:11:52 -04:00
return
2015-06-24 14:04:40 -07:00
}
2015-12-10 11:41:18 -05:00
// go
2015-12-14 00:38:19 -05:00
cs . finalizeCommit ( height )
2014-10-21 18:30:03 -07:00
}
2015-06-05 14:15:40 -07:00
// Increment height and goto RoundStepNewHeight
2015-12-14 00:38:19 -05:00
func ( cs * ConsensusState ) finalizeCommit ( height int ) {
2015-06-05 14:15:40 -07:00
if cs . Height != height || cs . Step != RoundStepCommit {
2015-12-14 00:38:19 -05:00
log . Debug ( Fmt ( "finalizeCommit(%v): Invalid args. Current step: %v/%v/%v" , height , cs . Height , cs . Round , cs . Step ) )
2015-06-05 14:15:40 -07:00
return
2014-10-21 01:18:46 -07:00
}
2016-08-16 14:59:19 -07:00
blockID , ok := cs . Votes . Precommits ( cs . CommitRound ) . TwoThirdsMajority ( )
2016-01-06 17:14:20 -08:00
block , blockParts := cs . ProposalBlock , cs . ProposalBlockParts
2014-10-30 03:32:09 -07:00
2015-06-05 14:15:40 -07:00
if ! ok {
2015-12-14 00:38:19 -05:00
PanicSanity ( Fmt ( "Cannot finalizeCommit, commit does not have two thirds majority" ) )
2015-06-05 14:15:40 -07:00
}
2016-08-16 14:59:19 -07:00
if ! blockParts . HasHeader ( blockID . PartsHeader ) {
2015-07-19 23:42:52 +00:00
PanicSanity ( Fmt ( "Expected ProposalBlockParts header to be commit header" ) )
2015-06-05 14:15:40 -07:00
}
2016-08-16 14:59:19 -07:00
if ! block . HashesTo ( blockID . Hash ) {
2015-12-14 00:38:19 -05:00
PanicSanity ( Fmt ( "Cannot finalizeCommit, ProposalBlock does not hash to commit hash" ) )
2015-06-05 14:15:40 -07:00
}
2016-01-06 17:14:20 -08:00
if err := cs . state . ValidateBlock ( block ) ; err != nil {
2015-07-19 23:42:52 +00:00
PanicConsensus ( Fmt ( "+2/3 committed an invalid block: %v" , err ) )
2014-10-30 03:32:09 -07:00
}
2015-06-05 14:15:40 -07:00
2016-08-25 01:39:03 -04:00
log . Notice ( Fmt ( "Finalizing commit of block with %d txs" , block . NumTxs ) ,
"height" , block . Height , "hash" , block . Hash ( ) , "root" , block . AppHash )
2016-01-06 17:14:20 -08:00
log . Info ( Fmt ( "%v" , block ) )
2016-09-11 15:32:33 -04:00
fail . Fail ( ) // XXX
2016-09-11 13:16:23 -04:00
// Save to blockStore.
if cs . blockStore . Height ( ) < block . Height {
2016-11-19 19:32:35 -05:00
// NOTE: the seenCommit is local justification to commit this block,
// but may differ from the LastCommit included in the next block
2016-09-11 13:16:23 -04:00
precommits := cs . Votes . Precommits ( cs . CommitRound )
seenCommit := precommits . MakeCommit ( )
cs . blockStore . SaveBlock ( block , blockParts , seenCommit )
} else {
2017-02-17 10:57:09 -05:00
// Happens during replay if we already saved the block but didn't commit
2017-02-20 16:24:35 -05:00
log . Info ( "Calling finalizeCommit on already stored block" , "height" , block . Height )
2016-09-11 13:16:23 -04:00
}
2016-01-06 17:14:20 -08:00
2016-09-11 15:32:33 -04:00
fail . Fail ( ) // XXX
2016-01-06 17:14:20 -08:00
// Create a copy of the state for staging
2016-09-11 13:16:23 -04:00
// and an event cache for txs
2016-01-06 17:14:20 -08:00
stateCopy := cs . state . Copy ( )
2016-10-10 02:58:13 -04:00
eventCache := types . NewEventCache ( cs . evsw )
2016-07-05 17:03:09 -04:00
2016-09-11 13:16:23 -04:00
// Execute and commit the block, and update the mempool.
// All calls to the proxyAppConn should come here.
// NOTE: the block.AppHash wont reflect these txs until the next block
2016-11-30 17:28:41 -05:00
err := stateCopy . ApplyBlock ( eventCache , cs . proxyAppConn , block , blockParts . Header ( ) , cs . mempool )
if err != nil {
2017-02-20 16:24:35 -05:00
log . Error ( "Error on ApplyBlock. Did the application crash? Please restart tendermint" , "error" , err )
2017-02-17 10:57:09 -05:00
return
2016-11-30 17:28:41 -05:00
}
2016-01-06 17:14:20 -08:00
2016-09-11 15:32:33 -04:00
fail . Fail ( ) // XXX
2016-09-11 13:16:23 -04:00
// Fire off event for new block.
// TODO: Handle app failure. See #177
types . FireEventNewBlock ( cs . evsw , types . EventDataNewBlock { block } )
types . FireEventNewBlockHeader ( cs . evsw , types . EventDataNewBlockHeader { block . Header } )
2016-07-05 17:03:09 -04:00
eventCache . Flush ( )
2016-01-06 17:14:20 -08:00
// Save the state.
stateCopy . Save ( )
2016-09-11 15:32:33 -04:00
fail . Fail ( ) // XXX
2015-12-12 17:22:48 -05:00
// NewHeightStep!
2016-01-06 17:14:20 -08:00
cs . updateToState ( stateCopy )
2015-12-12 01:28:33 -05:00
2015-06-24 14:04:40 -07:00
// cs.StartTime is already set.
// Schedule Round0 to start soon.
2016-09-09 23:10:23 -04:00
cs . scheduleRound0 ( & cs . RoundState )
2015-06-05 14:15:40 -07:00
// By here,
// * cs.Height has been increment to height+1
// * cs.Step is now RoundStepNewHeight
2015-06-24 14:04:40 -07:00
// * cs.StartTime is set to when we will start round0.
2015-06-05 14:15:40 -07:00
return
2014-10-20 19:02:10 -07:00
}
2014-10-21 23:30:18 -07:00
//-----------------------------------------------------------------------------
2016-06-26 15:33:11 -04:00
func ( cs * ConsensusState ) defaultSetProposal ( proposal * types . Proposal ) error {
2014-10-21 23:30:18 -07:00
// Already have one
2016-06-26 15:33:11 -04:00
// TODO: possibly catch double proposals
2014-10-21 23:30:18 -07:00
if cs . Proposal != nil {
return nil
}
2014-10-30 03:32:09 -07:00
// Does not apply
2014-10-21 23:30:18 -07:00
if proposal . Height != cs . Height || proposal . Round != cs . Round {
return nil
}
2014-10-30 03:32:09 -07:00
// We don't care about the proposal if we're already in RoundStepCommit.
2015-06-24 17:05:52 -07:00
if RoundStepCommit <= cs . Step {
2014-10-30 03:32:09 -07:00
return nil
}
2015-06-22 19:04:31 -07:00
// Verify POLRound, which must be -1 or between 0 and proposal.Round exclusive.
if proposal . POLRound != - 1 &&
2015-06-25 20:28:34 -07:00
( proposal . POLRound < 0 || proposal . Round <= proposal . POLRound ) {
2015-06-22 19:04:31 -07:00
return ErrInvalidProposalPOLRound
}
2014-10-21 23:30:18 -07:00
// Verify signature
2015-11-01 11:34:08 -08:00
if ! cs . Validators . Proposer ( ) . PubKey . VerifyBytes ( types . SignBytes ( cs . state . ChainID , proposal ) , proposal . Signature ) {
2014-10-21 23:30:18 -07:00
return ErrInvalidProposalSignature
}
cs . Proposal = proposal
2015-06-24 14:04:40 -07:00
cs . ProposalBlockParts = types . NewPartSetFromHeader ( proposal . BlockPartsHeader )
2014-10-21 23:30:18 -07:00
return nil
}
// NOTE: block is not necessarily valid.
2016-03-02 21:38:05 +00:00
// Asynchronously triggers either enterPrevote (before we timeout of propose) or tryFinalizeCommit, once we have the full block.
2016-03-11 21:38:15 -05:00
func ( cs * ConsensusState ) addProposalBlockPart ( height int , part * types . Part , verify bool ) ( added bool , err error ) {
2014-10-21 23:30:18 -07:00
// Blocks might be reused, so round mismatch is OK
if cs . Height != height {
return false , nil
}
// We're not expecting a block part.
2014-10-30 03:32:09 -07:00
if cs . ProposalBlockParts == nil {
2014-10-21 23:30:18 -07:00
return false , nil // TODO: bad peer? Return error?
}
2016-03-11 21:38:15 -05:00
added , err = cs . ProposalBlockParts . AddPart ( part , verify )
2014-10-21 23:30:18 -07:00
if err != nil {
return added , err
}
2014-10-26 13:26:27 -07:00
if added && cs . ProposalBlockParts . IsComplete ( ) {
2015-06-24 18:51:14 -07:00
// Added and completed!
2015-11-10 13:10:43 -08:00
var n int
2014-10-21 23:30:18 -07:00
var err error
2015-11-10 13:10:43 -08:00
cs . ProposalBlock = wire . ReadBinary ( & types . Block { } , cs . ProposalBlockParts . GetReader ( ) , types . MaxBlockSize , & n , & err ) . ( * types . Block )
2015-12-22 23:24:15 -05:00
// NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal
log . Info ( "Received complete proposal block" , "height" , cs . ProposalBlock . Height , "hash" , cs . ProposalBlock . Hash ( ) )
2015-06-12 20:24:08 -07:00
if cs . Step == RoundStepPropose && cs . isProposalComplete ( ) {
2015-06-24 18:51:14 -07:00
// Move onto the next step
2015-12-13 19:33:05 -05:00
cs . enterPrevote ( height , cs . Round )
2015-06-05 14:15:40 -07:00
} else if cs . Step == RoundStepCommit {
2015-06-24 18:51:14 -07:00
// If we're waiting on the proposal block...
2015-09-15 16:13:39 -04:00
cs . tryFinalizeCommit ( height )
2014-12-09 18:49:04 -08:00
}
2014-10-21 23:30:18 -07:00
return true , err
}
2015-06-24 18:51:14 -07:00
return added , nil
2014-10-21 23:30:18 -07:00
}
2015-08-12 14:00:23 -04:00
// Attempt to add the vote. if its a duplicate signature, dupeout the validator
2016-07-01 17:47:31 -04:00
func ( cs * ConsensusState ) tryAddVote ( vote * types . Vote , peerKey string ) error {
_ , err := cs . addVote ( vote , peerKey )
2015-08-12 14:00:23 -04:00
if err != nil {
2015-08-26 18:56:34 -04:00
// If the vote height is off, we'll just ignore it,
2015-09-09 16:45:53 -04:00
// But if it's a conflicting sig, broadcast evidence tx for slashing.
// If it's otherwise invalid, punish peer.
2015-08-26 18:56:34 -04:00
if err == ErrVoteHeightMismatch {
2015-12-13 19:30:15 -05:00
return err
2016-07-24 14:32:08 -07:00
} else if _ , ok := err . ( * types . ErrVoteConflictingVotes ) ; ok {
2016-02-07 16:56:59 -08:00
if peerKey == "" {
log . Warn ( "Found conflicting vote from ourselves. Did you unsafe_reset a validator?" , "height" , vote . Height , "round" , vote . Round , "type" , vote . Type )
return err
}
log . Warn ( "Found conflicting vote. Publish evidence (TODO)" )
2015-12-01 20:12:01 -08:00
/ * TODO
2015-08-12 14:00:23 -04:00
evidenceTx := & types . DupeoutTx {
Address : address ,
VoteA : * errDupe . VoteA ,
VoteB : * errDupe . VoteB ,
}
2015-12-21 15:18:16 -08:00
cs . mempool . BroadcastTx ( struct { ? ? ? } { evidenceTx } ) // shouldn't need to check returned err
2015-11-01 11:34:08 -08:00
* /
2015-12-13 19:30:15 -05:00
return err
2015-08-12 14:00:23 -04:00
} else {
// Probably an invalid signature. Bad peer.
log . Warn ( "Error attempting to add vote" , "error" , err )
2015-12-13 19:30:15 -05:00
return ErrAddingVote
2015-08-12 14:00:23 -04:00
}
}
2015-12-13 19:30:15 -05:00
return nil
2015-08-12 14:00:23 -04:00
}
2014-11-01 22:42:04 -07:00
//-----------------------------------------------------------------------------
2016-07-01 17:47:31 -04:00
func ( cs * ConsensusState ) addVote ( vote * types . Vote , peerKey string ) ( added bool , err error ) {
2015-08-12 14:00:23 -04:00
log . Debug ( "addVote" , "voteHeight" , vote . Height , "voteType" , vote . Type , "csHeight" , cs . Height )
2015-07-05 21:01:59 -07:00
2015-06-05 14:15:40 -07:00
// A precommit for the previous height?
2016-12-19 10:44:25 -05:00
// These come in while we wait timeoutCommit
2015-08-26 18:56:34 -04:00
if vote . Height + 1 == cs . Height {
if ! ( cs . Step == RoundStepNewHeight && vote . Type == types . VoteTypePrecommit ) {
// TODO: give the reason ..
2015-12-11 11:57:15 -05:00
// fmt.Errorf("tryAddVote: Wrong height, not a LastCommit straggler commit.")
2016-07-01 17:47:31 -04:00
return added , ErrVoteHeightMismatch
2015-08-26 18:56:34 -04:00
}
2016-07-01 17:47:31 -04:00
added , err = cs . LastCommit . AddVote ( vote )
2015-05-04 11:18:21 -07:00
if added {
2015-07-19 21:49:13 +00:00
log . Info ( Fmt ( "Added to lastPrecommits: %v" , cs . LastCommit . StringShort ( ) ) )
2016-07-01 17:47:31 -04:00
types . FireEventVote ( cs . evsw , types . EventDataVote { vote } )
2016-12-19 10:44:25 -05:00
2017-01-04 01:50:02 +04:00
// if we can skip timeoutCommit and have all the votes now,
if cs . timeoutParams . SkipTimeoutCommit && cs . LastCommit . HasAll ( ) {
2017-01-11 15:32:03 -05:00
// go straight to new round (skip timeout commit)
2016-12-19 20:12:37 -05:00
// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, RoundStepNewHeight)
cs . enterNewRound ( cs . Height , 0 )
2016-12-19 10:44:25 -05:00
}
2015-05-04 11:18:21 -07:00
}
2016-12-19 10:44:25 -05:00
2015-05-04 11:18:21 -07:00
return
2015-06-05 14:15:40 -07:00
}
// A prevote/precommit for this height?
if vote . Height == cs . Height {
2015-06-24 14:04:40 -07:00
height := cs . Height
2016-07-01 17:47:31 -04:00
added , err = cs . Votes . AddVote ( vote , peerKey )
2015-05-04 11:18:21 -07:00
if added {
2016-07-01 17:47:31 -04:00
types . FireEventVote ( cs . evsw , types . EventDataVote { vote } )
2015-12-12 01:28:33 -05:00
2015-06-05 14:15:40 -07:00
switch vote . Type {
case types . VoteTypePrevote :
2015-06-24 18:51:14 -07:00
prevotes := cs . Votes . Prevotes ( vote . Round )
2015-12-01 20:12:01 -08:00
log . Info ( "Added to prevote" , "vote" , vote , "prevotes" , prevotes . StringShort ( ) )
2015-06-24 18:51:14 -07:00
// First, unlock if prevotes is a valid POL.
2015-06-25 12:52:16 -07:00
// >> lockRound < POLRound <= unlockOrChangeLockRound (see spec)
// NOTE: If (lockRound < POLRound) but !(POLRound <= unlockOrChangeLockRound),
2015-12-13 19:33:05 -05:00
// we'll still enterNewRound(H,vote.R) and enterPrecommit(H,vote.R) to process it
2015-06-25 12:52:16 -07:00
// there.
if ( cs . LockedBlock != nil ) && ( cs . LockedRound < vote . Round ) && ( vote . Round <= cs . Round ) {
2016-08-16 14:59:19 -07:00
blockID , ok := prevotes . TwoThirdsMajority ( )
if ok && ! cs . LockedBlock . HashesTo ( blockID . Hash ) {
2015-07-19 21:49:13 +00:00
log . Notice ( "Unlocking because of POL." , "lockedRound" , cs . LockedRound , "POLRound" , vote . Round )
2015-08-26 18:56:34 -04:00
cs . LockedRound = 0
2015-06-24 18:51:14 -07:00
cs . LockedBlock = nil
cs . LockedBlockParts = nil
2016-10-10 02:58:13 -04:00
types . FireEventUnlock ( cs . evsw , cs . RoundStateEvent ( ) )
2015-06-24 18:51:14 -07:00
}
}
if cs . Round <= vote . Round && prevotes . HasTwoThirdsAny ( ) {
// Round-skip over to PrevoteWait or goto Precommit.
2015-12-13 19:33:05 -05:00
cs . enterNewRound ( height , vote . Round ) // if the vote is ahead of us
2015-12-10 11:41:18 -05:00
if prevotes . HasTwoThirdsMajority ( ) {
2015-12-13 19:33:05 -05:00
cs . enterPrecommit ( height , vote . Round )
2015-12-10 11:41:18 -05:00
} else {
2015-12-13 19:33:05 -05:00
cs . enterPrevote ( height , vote . Round ) // if the vote is ahead of us
cs . enterPrevoteWait ( height , vote . Round )
2015-12-10 11:41:18 -05:00
}
2015-06-25 20:28:34 -07:00
} else if cs . Proposal != nil && 0 <= cs . Proposal . POLRound && cs . Proposal . POLRound == vote . Round {
2015-06-24 18:51:14 -07:00
// If the proposal is now complete, enter prevote of cs.Round.
2015-06-12 20:24:08 -07:00
if cs . isProposalComplete ( ) {
2015-12-13 19:33:05 -05:00
cs . enterPrevote ( height , cs . Round )
2015-06-12 20:24:08 -07:00
}
2015-06-05 14:15:40 -07:00
}
case types . VoteTypePrecommit :
2015-06-24 18:51:14 -07:00
precommits := cs . Votes . Precommits ( vote . Round )
2015-12-01 20:12:01 -08:00
log . Info ( "Added to precommit" , "vote" , vote , "precommits" , precommits . StringShort ( ) )
2016-08-16 14:59:19 -07:00
blockID , ok := precommits . TwoThirdsMajority ( )
2015-08-19 16:11:52 -04:00
if ok {
2016-08-16 14:59:19 -07:00
if len ( blockID . Hash ) == 0 {
2015-12-13 19:33:05 -05:00
cs . enterNewRound ( height , vote . Round + 1 )
2015-12-10 11:41:18 -05:00
} else {
2015-12-13 19:33:05 -05:00
cs . enterNewRound ( height , vote . Round )
cs . enterPrecommit ( height , vote . Round )
cs . enterCommit ( height , vote . Round )
2017-01-11 15:32:03 -05:00
2017-01-11 18:37:36 -05:00
if cs . timeoutParams . SkipTimeoutCommit && precommits . HasAll ( ) {
2017-01-11 15:32:03 -05:00
// if we have all the votes now,
// go straight to new round (skip timeout commit)
// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, RoundStepNewHeight)
cs . enterNewRound ( cs . Height , 0 )
}
2015-12-10 11:41:18 -05:00
}
} else if cs . Round <= vote . Round && precommits . HasTwoThirdsAny ( ) {
2015-12-13 19:33:05 -05:00
cs . enterNewRound ( height , vote . Round )
cs . enterPrecommit ( height , vote . Round )
cs . enterPrecommitWait ( height , vote . Round )
2015-06-05 14:15:40 -07:00
}
default :
2015-07-19 23:42:52 +00:00
PanicSanity ( Fmt ( "Unexpected vote type %X" , vote . Type ) ) // Should not happen.
2014-11-01 22:42:04 -07:00
}
2014-10-30 03:32:09 -07:00
}
2015-08-26 18:56:34 -04:00
// Either duplicate, or error upon cs.Votes.AddByIndex()
2015-06-05 14:15:40 -07:00
return
2015-08-26 18:56:34 -04:00
} else {
err = ErrVoteHeightMismatch
2014-10-21 23:30:18 -07:00
}
2015-06-05 14:15:40 -07:00
2015-07-05 21:01:59 -07:00
// Height mismatch, bad peer?
2016-10-11 11:44:07 -04:00
log . Info ( "Vote ignored and not added" , "voteHeight" , vote . Height , "csHeight" , cs . Height , "err" , err )
2015-06-05 14:15:40 -07:00
return
2014-10-21 23:30:18 -07:00
}
2015-08-12 14:00:23 -04:00
func ( cs * ConsensusState ) signVote ( type_ byte , hash [ ] byte , header types . PartSetHeader ) ( * types . Vote , error ) {
2016-12-02 00:12:06 -05:00
addr := cs . privValidator . GetAddress ( )
valIndex , _ := cs . Validators . GetByAddress ( addr )
2015-03-22 19:00:08 -07:00
vote := & types . Vote {
2016-12-02 00:12:06 -05:00
ValidatorAddress : addr ,
ValidatorIndex : valIndex ,
2015-08-12 22:36:43 -07:00
Height : cs . Height ,
Round : cs . Round ,
Type : type_ ,
2016-08-16 14:59:19 -07:00
BlockID : types . BlockID { hash , header } ,
2014-10-24 14:37:12 -07:00
}
2015-05-29 17:53:57 -04:00
err := cs . privValidator . SignVote ( cs . state . ChainID , vote )
2015-08-12 14:00:23 -04:00
return vote , err
}
2016-09-08 18:06:25 -04:00
// sign the vote and publish on internalMsgQueue
2015-08-12 14:00:23 -04:00
func ( cs * ConsensusState ) signAddVote ( type_ byte , hash [ ] byte , header types . PartSetHeader ) * types . Vote {
2016-11-16 20:58:53 -05:00
// if we don't have a key or we're not in the validator set, do nothing
2016-12-02 00:12:06 -05:00
if cs . privValidator == nil || ! cs . Validators . HasAddress ( cs . privValidator . GetAddress ( ) ) {
2015-08-12 14:00:23 -04:00
return nil
}
vote , err := cs . signVote ( type_ , hash , header )
2014-12-31 16:14:26 -08:00
if err == nil {
2016-07-01 17:47:31 -04:00
cs . sendInternalMessage ( msgInfo { & VoteMessage { vote } , "" } )
2015-12-15 14:44:58 -05:00
log . Info ( "Signed and pushed vote" , "height" , cs . Height , "round" , cs . Round , "vote" , vote , "error" , err )
2014-12-31 16:14:26 -08:00
return vote
} else {
2016-06-26 15:33:11 -04:00
//if !cs.replayMode {
log . Warn ( "Error signing vote" , "height" , cs . Height , "round" , cs . Round , "vote" , vote , "error" , err )
//}
2014-12-31 16:14:26 -08:00
return nil
}
2014-10-24 14:37:12 -07:00
}
2014-10-30 03:32:09 -07:00
2015-12-12 01:28:33 -05:00
//---------------------------------------------------------
2015-12-01 20:12:01 -08:00
func CompareHRS ( h1 , r1 int , s1 RoundStepType , h2 , r2 int , s2 RoundStepType ) int {
if h1 < h2 {
return - 1
} else if h1 > h2 {
return 1
}
if r1 < r2 {
return - 1
} else if r1 > r2 {
return 1
}
if s1 < s2 {
return - 1
} else if s1 > s2 {
return 1
}
return 0
}