2014-08-10 16:35:08 -07:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2014-12-09 18:49:04 -08:00
|
|
|
"bytes"
|
2014-10-18 01:42:33 -07:00
|
|
|
"fmt"
|
2015-12-10 11:41:18 -05:00
|
|
|
"reflect"
|
2017-10-02 15:24:30 -04:00
|
|
|
"runtime/debug"
|
2014-08-10 16:35:08 -07:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-12-22 06:36:45 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2018-07-01 22:36:49 -04:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
2018-10-30 10:34:51 -04:00
|
|
|
"github.com/tendermint/tendermint/libs/fail"
|
2018-07-01 22:36:49 -04:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-09-01 01:33:51 +02:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2018-04-05 17:54:26 +03:00
|
|
|
|
2017-05-04 20:07:08 +02:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2017-10-10 12:39:21 +04:00
|
|
|
cstypes "github.com/tendermint/tendermint/consensus/types"
|
2018-05-16 11:03:11 +04:00
|
|
|
tmevents "github.com/tendermint/tendermint/libs/events"
|
2018-01-01 21:27:38 -05:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
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
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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
|
|
|
)
|
|
|
|
|
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 {
|
2018-01-01 21:27:38 -05:00
|
|
|
Msg ConsensusMessage `json:"msg"`
|
|
|
|
PeerID p2p.ID `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 {
|
2017-10-10 12:39:21 +04:00
|
|
|
Duration time.Duration `json:"duration"`
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64 `json:"height"`
|
2017-10-10 12:39:21 +04:00
|
|
|
Round int `json:"round"`
|
|
|
|
Step cstypes.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
|
|
|
}
|
|
|
|
|
2019-01-17 21:46:40 -05:00
|
|
|
// interface to the mempool
|
|
|
|
type txNotifier interface {
|
|
|
|
TxsAvailable() <-chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface to the evidence pool
|
|
|
|
type evidencePool interface {
|
|
|
|
AddEvidence(types.Evidence) error
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// ConsensusState handles execution of the consensus algorithm.
|
|
|
|
// It processes votes and proposals, and upon reaching agreement,
|
|
|
|
// commits blocks to the chain and executes them against the application.
|
|
|
|
// The internal state machine receives input from peers, the internal validator, and from a timer.
|
2014-09-03 20:41:57 -07:00
|
|
|
type ConsensusState struct {
|
2017-04-25 14:50:20 -04:00
|
|
|
cmn.BaseService
|
2014-10-30 03:32:09 -07:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
// config details
|
2017-05-04 20:07:08 +02:00
|
|
|
config *cfg.ConsensusConfig
|
2017-09-18 18:12:31 -04:00
|
|
|
privValidator types.PrivValidator // for signing votes
|
2017-05-01 20:09:29 -04:00
|
|
|
|
2019-01-17 21:46:40 -05:00
|
|
|
// store blocks and commits
|
2018-06-04 13:46:34 -07:00
|
|
|
blockStore sm.BlockStore
|
2019-01-17 21:46:40 -05:00
|
|
|
|
|
|
|
// create and execute blocks
|
|
|
|
blockExec *sm.BlockExecutor
|
|
|
|
|
|
|
|
// notify us if txs are available
|
|
|
|
txNotifier txNotifier
|
|
|
|
|
|
|
|
// add evidence to the pool
|
|
|
|
// when it's detected
|
|
|
|
evpool evidencePool
|
2016-11-16 20:52:08 -05:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
// internal state
|
2018-07-19 10:49:12 +04:00
|
|
|
mtx sync.RWMutex
|
2017-10-10 12:39:21 +04:00
|
|
|
cstypes.RoundState
|
2019-01-28 14:13:17 +02:00
|
|
|
state sm.State // State until height-1.
|
2015-04-07 15:24:09 -05:00
|
|
|
|
2018-01-23 21:41:13 -05:00
|
|
|
// state changes may be triggered by: msgs from peers,
|
2017-05-01 20:09:29 -04:00
|
|
|
// msgs from ourself, or by timeouts
|
|
|
|
peerMsgQueue chan msgInfo
|
|
|
|
internalMsgQueue chan msgInfo
|
|
|
|
timeoutTicker TimeoutTicker
|
2015-12-05 14:58:12 -05:00
|
|
|
|
2018-09-21 20:36:48 +02:00
|
|
|
// information about about added votes and block parts are written on this channel
|
|
|
|
// so statistics can be computed by reactor
|
|
|
|
statsMsgQueue chan msgInfo
|
|
|
|
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
// we use eventBus to trigger msg broadcasts in the reactor,
|
2017-05-01 20:09:29 -04:00
|
|
|
// and to notify external subscribers, eg. through a websocket
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
eventBus *types.EventBus
|
2015-12-12 01:28:33 -05:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
// a Write-Ahead Log ensures we can recover from any kind of crash
|
|
|
|
// and helps us avoid signing conflicting votes
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
wal WAL
|
2017-10-26 18:29:23 -04:00
|
|
|
replayMode bool // so we don't log signing errors during replay
|
|
|
|
doWALCatchup bool // determines if we even try to do the catchup
|
2015-12-22 15:23:22 -05:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
// for tests where we want to limit the number of transitions the state makes
|
|
|
|
nSteps int
|
2016-06-26 15:33:11 -04:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
// some functions can be overwritten for testing
|
2017-12-01 19:04:53 -06:00
|
|
|
decideProposal func(height int64, round int)
|
|
|
|
doPrevote func(height int64, round int)
|
2016-06-26 15:33:11 -04:00
|
|
|
setProposal func(proposal *types.Proposal) error
|
2017-01-12 14:44:42 -05:00
|
|
|
|
2017-05-01 20:09:29 -04:00
|
|
|
// closed when we finish shutting down
|
2017-01-12 14:44:42 -05:00
|
|
|
done chan struct{}
|
2018-05-15 14:32:06 +04:00
|
|
|
|
2018-05-16 10:28:58 +04:00
|
|
|
// synchronous pubsub between consensus state and reactor.
|
2018-11-28 14:52:35 +01:00
|
|
|
// state only emits EventNewRoundStep and EventVote
|
2018-05-16 10:28:58 +04:00
|
|
|
evsw tmevents.EventSwitch
|
2018-06-11 17:14:42 +04:00
|
|
|
|
|
|
|
// for reporting metrics
|
|
|
|
metrics *Metrics
|
2018-05-15 14:32:06 +04:00
|
|
|
}
|
|
|
|
|
2018-09-25 04:14:38 -07:00
|
|
|
// StateOption sets an optional parameter on the ConsensusState.
|
|
|
|
type StateOption func(*ConsensusState)
|
2018-06-20 10:39:19 +04:00
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// NewConsensusState returns a new ConsensusState.
|
2018-06-20 10:39:19 +04:00
|
|
|
func NewConsensusState(
|
|
|
|
config *cfg.ConsensusConfig,
|
|
|
|
state sm.State,
|
|
|
|
blockExec *sm.BlockExecutor,
|
|
|
|
blockStore sm.BlockStore,
|
2019-01-17 21:46:40 -05:00
|
|
|
txNotifier txNotifier,
|
|
|
|
evpool evidencePool,
|
2018-09-25 04:14:38 -07:00
|
|
|
options ...StateOption,
|
2018-06-20 10:39:19 +04:00
|
|
|
) *ConsensusState {
|
2014-09-14 15:37:32 -07:00
|
|
|
cs := &ConsensusState{
|
2016-05-08 15:00:58 -07:00
|
|
|
config: config,
|
2017-12-27 22:09:48 -05:00
|
|
|
blockExec: blockExec,
|
2015-12-11 11:57:15 -05:00
|
|
|
blockStore: blockStore,
|
2019-01-17 21:46:40 -05:00
|
|
|
txNotifier: txNotifier,
|
2015-12-11 11:57:15 -05:00
|
|
|
peerMsgQueue: make(chan msgInfo, msgQueueSize),
|
|
|
|
internalMsgQueue: make(chan msgInfo, msgQueueSize),
|
2016-12-19 10:44:25 -05:00
|
|
|
timeoutTicker: NewTimeoutTicker(),
|
2018-09-21 20:36:48 +02:00
|
|
|
statsMsgQueue: make(chan msgInfo, msgQueueSize),
|
2017-01-12 14:44:42 -05:00
|
|
|
done: make(chan struct{}),
|
2017-10-26 18:29:23 -04:00
|
|
|
doWALCatchup: true,
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
wal: nilWAL{},
|
2017-11-19 02:02:58 +00:00
|
|
|
evpool: evpool,
|
2018-05-16 10:28:58 +04:00
|
|
|
evsw: tmevents.NewEventSwitch(),
|
2018-06-16 11:44:03 +04:00
|
|
|
metrics: NopMetrics(),
|
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)
|
2018-08-08 16:03:58 +04:00
|
|
|
|
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)
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.BaseService = *cmn.NewBaseService(nil, "ConsensusState", cs)
|
2018-06-16 11:44:03 +04:00
|
|
|
for _, option := range options {
|
|
|
|
option(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
|
|
|
|
|
2017-05-12 23:07:53 +02:00
|
|
|
// SetLogger implements Service.
|
|
|
|
func (cs *ConsensusState) SetLogger(l log.Logger) {
|
|
|
|
cs.BaseService.Logger = l
|
|
|
|
cs.timeoutTicker.SetLogger(l)
|
|
|
|
}
|
|
|
|
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
// SetEventBus sets event bus.
|
|
|
|
func (cs *ConsensusState) SetEventBus(b *types.EventBus) {
|
|
|
|
cs.eventBus = b
|
2017-12-28 19:35:56 -05:00
|
|
|
cs.blockExec.SetEventBus(b)
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
|
|
|
|
2018-09-25 04:14:38 -07:00
|
|
|
// StateMetrics sets the metrics.
|
|
|
|
func StateMetrics(metrics *Metrics) StateOption {
|
2018-06-20 10:39:19 +04:00
|
|
|
return func(cs *ConsensusState) { cs.metrics = metrics }
|
2018-06-16 11:44:03 +04:00
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// String returns a string.
|
2015-12-12 01:28:33 -05:00
|
|
|
func (cs *ConsensusState) String() string {
|
2016-07-11 23:07:21 -04:00
|
|
|
// better not to access shared variables
|
2018-08-10 00:25:57 -05:00
|
|
|
return fmt.Sprintf("ConsensusState") //(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
|
2015-06-04 13:36:47 -07:00
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// GetState returns a copy of the chain state.
|
2017-12-27 22:09:48 -05:00
|
|
|
func (cs *ConsensusState) GetState() sm.State {
|
2018-07-19 10:49:12 +04:00
|
|
|
cs.mtx.RLock()
|
|
|
|
defer cs.mtx.RUnlock()
|
2015-01-11 14:27:46 -08:00
|
|
|
return cs.state.Copy()
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:52:38 -07:00
|
|
|
// GetLastHeight returns the last height committed.
|
|
|
|
// If there were no blocks, returns 0.
|
|
|
|
func (cs *ConsensusState) GetLastHeight() int64 {
|
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748)
* use READ lock/unlock in ConsensusState#GetLastHeight
Refs #2721
* do not use defers when there's no need
* fix peer formatting (output its address instead of the pointer)
```
[54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout"
```
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581
* panic if peer has no state
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165
It's confusing that sometimes we check if peer has a state, but most of
the times we expect it to be there
1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138
2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited)
I will change everything to always assume peer has a state and panic
otherwise
that should help identify issues earlier
* abci/localclient: extend lock on app callback
App callback should be protected by lock as well (note this was already
done for InitChainAsync, why not for others???). Otherwise, when we
execute the block, tx might come in and call the callback in the same
time we're updating it in execBlockOnProxyApp => DATA RACE
Fixes #2721
Consensus state is locked
```
goroutine 113333 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*RWMutex).RLock(0xc001800090)
/usr/local/go/src/sync/rwmutex.go:50 +0x4e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248
9c0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236
```
because localClient is locked
```
goroutine 1899 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132
```
tx comes in and CheckTx is executed right when we execute the block
```
goroutine 111044 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0x0)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8
reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...)
/usr/local/go/src/reflect/value.go:447 +0x449
reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344)
/usr/local/go/src/reflect/value.go:308 +0xa4
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188
net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2361 +0x127
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394
net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2741 +0xab
net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0)
/usr/local/go/src/net/http/server.go:1847 +0x646
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2851 +0x2f5
```
* consensus: use read lock in Receive#VoteMessage
* use defer to unlock mutex because application might panic
* use defer in every method of the localClient
* add a changelog entry
* drain channels before Unsubscribe(All)
Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13
for the detailed explanation of the issue.
We'll need to fix it someday. Make sure to keep an eye on
https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md
* retry instead of panic when peer has no state in reactors other than consensus
in /dump_consensus_state RPC endpoint, skip a peer with no state
* rpc/core/mempool: simplify error messages
* rpc/core/mempool: use time.After instead of timer
also, do not log DeliverTx result (to be consistent with other memthods)
* unlock before calling the callback in reqRes#SetCallback
2018-11-13 20:32:51 +04:00
|
|
|
cs.mtx.RLock()
|
|
|
|
defer cs.mtx.RUnlock()
|
2018-06-26 16:52:38 -07:00
|
|
|
return cs.RoundState.Height - 1
|
|
|
|
}
|
|
|
|
|
2018-04-10 11:15:16 +02:00
|
|
|
// GetRoundState returns a shallow copy of the internal consensus state.
|
2017-10-10 12:39:21 +04:00
|
|
|
func (cs *ConsensusState) GetRoundState() *cstypes.RoundState {
|
2018-07-19 10:49:12 +04:00
|
|
|
cs.mtx.RLock()
|
2014-09-14 15:37:32 -07:00
|
|
|
rs := cs.RoundState // copy
|
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748)
* use READ lock/unlock in ConsensusState#GetLastHeight
Refs #2721
* do not use defers when there's no need
* fix peer formatting (output its address instead of the pointer)
```
[54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout"
```
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581
* panic if peer has no state
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165
It's confusing that sometimes we check if peer has a state, but most of
the times we expect it to be there
1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138
2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited)
I will change everything to always assume peer has a state and panic
otherwise
that should help identify issues earlier
* abci/localclient: extend lock on app callback
App callback should be protected by lock as well (note this was already
done for InitChainAsync, why not for others???). Otherwise, when we
execute the block, tx might come in and call the callback in the same
time we're updating it in execBlockOnProxyApp => DATA RACE
Fixes #2721
Consensus state is locked
```
goroutine 113333 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*RWMutex).RLock(0xc001800090)
/usr/local/go/src/sync/rwmutex.go:50 +0x4e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248
9c0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236
```
because localClient is locked
```
goroutine 1899 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132
```
tx comes in and CheckTx is executed right when we execute the block
```
goroutine 111044 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0x0)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8
reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...)
/usr/local/go/src/reflect/value.go:447 +0x449
reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344)
/usr/local/go/src/reflect/value.go:308 +0xa4
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188
net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2361 +0x127
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394
net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2741 +0xab
net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0)
/usr/local/go/src/net/http/server.go:1847 +0x646
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2851 +0x2f5
```
* consensus: use read lock in Receive#VoteMessage
* use defer to unlock mutex because application might panic
* use defer in every method of the localClient
* add a changelog entry
* drain channels before Unsubscribe(All)
Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13
for the detailed explanation of the issue.
We'll need to fix it someday. Make sure to keep an eye on
https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md
* retry instead of panic when peer has no state in reactors other than consensus
in /dump_consensus_state RPC endpoint, skip a peer with no state
* rpc/core/mempool: simplify error messages
* rpc/core/mempool: use time.After instead of timer
also, do not log DeliverTx result (to be consistent with other memthods)
* unlock before calling the callback in reqRes#SetCallback
2018-11-13 20:32:51 +04:00
|
|
|
cs.mtx.RUnlock()
|
2014-09-14 15:37:32 -07:00
|
|
|
return &rs
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
|
2018-04-10 11:15:16 +02:00
|
|
|
// GetRoundStateJSON returns a json of RoundState, marshalled using go-amino.
|
|
|
|
func (cs *ConsensusState) GetRoundStateJSON() ([]byte, error) {
|
2018-07-19 10:49:12 +04:00
|
|
|
cs.mtx.RLock()
|
|
|
|
defer cs.mtx.RUnlock()
|
2018-04-10 11:15:16 +02:00
|
|
|
return cdc.MarshalJSON(cs.RoundState)
|
|
|
|
}
|
|
|
|
|
2018-05-13 19:53:54 -04:00
|
|
|
// GetRoundStateSimpleJSON returns a json of RoundStateSimple, marshalled using go-amino.
|
|
|
|
func (cs *ConsensusState) GetRoundStateSimpleJSON() ([]byte, error) {
|
2018-07-19 10:49:12 +04:00
|
|
|
cs.mtx.RLock()
|
|
|
|
defer cs.mtx.RUnlock()
|
2018-05-13 19:53:54 -04:00
|
|
|
return cdc.MarshalJSON(cs.RoundState.RoundStateSimple())
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// GetValidators returns a copy of the current validators.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) GetValidators() (int64, []*types.Validator) {
|
2018-07-19 10:49:12 +04:00
|
|
|
cs.mtx.RLock()
|
|
|
|
defer cs.mtx.RUnlock()
|
2016-10-14 21:36:42 -04:00
|
|
|
return cs.state.LastBlockHeight, cs.state.Validators.Copy().Validators
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// SetPrivValidator sets the private validator account for signing votes.
|
2017-09-18 18:12:31 -04:00
|
|
|
func (cs *ConsensusState) SetPrivValidator(priv types.PrivValidator) {
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.mtx.Lock()
|
|
|
|
cs.privValidator = priv
|
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748)
* use READ lock/unlock in ConsensusState#GetLastHeight
Refs #2721
* do not use defers when there's no need
* fix peer formatting (output its address instead of the pointer)
```
[54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout"
```
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581
* panic if peer has no state
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165
It's confusing that sometimes we check if peer has a state, but most of
the times we expect it to be there
1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138
2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited)
I will change everything to always assume peer has a state and panic
otherwise
that should help identify issues earlier
* abci/localclient: extend lock on app callback
App callback should be protected by lock as well (note this was already
done for InitChainAsync, why not for others???). Otherwise, when we
execute the block, tx might come in and call the callback in the same
time we're updating it in execBlockOnProxyApp => DATA RACE
Fixes #2721
Consensus state is locked
```
goroutine 113333 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*RWMutex).RLock(0xc001800090)
/usr/local/go/src/sync/rwmutex.go:50 +0x4e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248
9c0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236
```
because localClient is locked
```
goroutine 1899 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132
```
tx comes in and CheckTx is executed right when we execute the block
```
goroutine 111044 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0x0)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8
reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...)
/usr/local/go/src/reflect/value.go:447 +0x449
reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344)
/usr/local/go/src/reflect/value.go:308 +0xa4
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188
net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2361 +0x127
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394
net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2741 +0xab
net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0)
/usr/local/go/src/net/http/server.go:1847 +0x646
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2851 +0x2f5
```
* consensus: use read lock in Receive#VoteMessage
* use defer to unlock mutex because application might panic
* use defer in every method of the localClient
* add a changelog entry
* drain channels before Unsubscribe(All)
Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13
for the detailed explanation of the issue.
We'll need to fix it someday. Make sure to keep an eye on
https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md
* retry instead of panic when peer has no state in reactors other than consensus
in /dump_consensus_state RPC endpoint, skip a peer with no state
* rpc/core/mempool: simplify error messages
* rpc/core/mempool: use time.After instead of timer
also, do not log DeliverTx result (to be consistent with other memthods)
* unlock before calling the callback in reqRes#SetCallback
2018-11-13 20:32:51 +04:00
|
|
|
cs.mtx.Unlock()
|
2016-11-16 20:52:08 -05:00
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// SetTimeoutTicker sets the local timer. It may be useful to overwrite for testing.
|
2016-12-19 10:44:25 -05:00
|
|
|
func (cs *ConsensusState) SetTimeoutTicker(timeoutTicker TimeoutTicker) {
|
|
|
|
cs.mtx.Lock()
|
|
|
|
cs.timeoutTicker = timeoutTicker
|
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748)
* use READ lock/unlock in ConsensusState#GetLastHeight
Refs #2721
* do not use defers when there's no need
* fix peer formatting (output its address instead of the pointer)
```
[54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout"
```
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581
* panic if peer has no state
https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165
It's confusing that sometimes we check if peer has a state, but most of
the times we expect it to be there
1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138
2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited)
I will change everything to always assume peer has a state and panic
otherwise
that should help identify issues earlier
* abci/localclient: extend lock on app callback
App callback should be protected by lock as well (note this was already
done for InitChainAsync, why not for others???). Otherwise, when we
execute the block, tx might come in and call the callback in the same
time we're updating it in execBlockOnProxyApp => DATA RACE
Fixes #2721
Consensus state is locked
```
goroutine 113333 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*RWMutex).RLock(0xc001800090)
/usr/local/go/src/sync/rwmutex.go:50 +0x4e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248
9c0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236
```
because localClient is locked
```
goroutine 1899 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670
created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132
```
tx comes in and CheckTx is executed right when we execute the block
```
goroutine 111044 [semacquire, 309 minutes]:
sync.runtime_SemacquireMutex(0xc00003363c, 0x0)
/usr/local/go/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xc000033638)
/usr/local/go/src/sync/mutex.go:134 +0xff
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8
reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...)
/usr/local/go/src/reflect/value.go:447 +0x449
reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344)
/usr/local/go/src/reflect/value.go:308 +0xa4
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188
net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2361 +0x127
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf
github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900)
/root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394
net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:1964 +0x44
net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900)
/usr/local/go/src/net/http/server.go:2741 +0xab
net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0)
/usr/local/go/src/net/http/server.go:1847 +0x646
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2851 +0x2f5
```
* consensus: use read lock in Receive#VoteMessage
* use defer to unlock mutex because application might panic
* use defer in every method of the localClient
* add a changelog entry
* drain channels before Unsubscribe(All)
Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13
for the detailed explanation of the issue.
We'll need to fix it someday. Make sure to keep an eye on
https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md
* retry instead of panic when peer has no state in reactors other than consensus
in /dump_consensus_state RPC endpoint, skip a peer with no state
* rpc/core/mempool: simplify error messages
* rpc/core/mempool: use time.After instead of timer
also, do not log DeliverTx result (to be consistent with other memthods)
* unlock before calling the callback in reqRes#SetCallback
2018-11-13 20:32:51 +04:00
|
|
|
cs.mtx.Unlock()
|
2016-12-19 10:44:25 -05:00
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// LoadCommit loads the commit for a given height.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) LoadCommit(height int64) *types.Commit {
|
2018-07-19 10:49:12 +04:00
|
|
|
cs.mtx.RLock()
|
|
|
|
defer cs.mtx.RUnlock()
|
2016-11-16 16:47:31 -05:00
|
|
|
if height == cs.blockStore.Height() {
|
|
|
|
return cs.blockStore.LoadSeenCommit(height)
|
|
|
|
}
|
|
|
|
return cs.blockStore.LoadBlockCommit(height)
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// OnStart implements cmn.Service.
|
|
|
|
// It loads the latest state via the WAL, and starts the timeout and receive routines.
|
2015-08-04 18:44:15 -07:00
|
|
|
func (cs *ConsensusState) OnStart() error {
|
2018-05-16 10:28:58 +04:00
|
|
|
if err := cs.evsw.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
// we may set the WAL in testing before calling Start,
|
|
|
|
// so only OpenWAL if its still the nilWAL
|
|
|
|
if _, ok := cs.wal.(nilWAL); ok {
|
|
|
|
walFile := cs.config.WalFile()
|
|
|
|
wal, err := cs.OpenWAL(walFile)
|
|
|
|
if err != nil {
|
|
|
|
cs.Logger.Error("Error loading ConsensusState wal", "err", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cs.wal = wal
|
2016-08-14 12:31:24 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 18:06:25 -04:00
|
|
|
// we need the timeoutRoutine for replay so
|
2017-09-06 13:11:47 -04:00
|
|
|
// we don't block on the tick chan.
|
2016-09-08 18:06:25 -04:00
|
|
|
// NOTE: we will get a build up of garbage go routines
|
2017-09-06 13:11:47 -04:00
|
|
|
// firing on the tockChan until the receiveRoutine is started
|
|
|
|
// to deal with them (by that point, at most one will be valid)
|
2018-05-16 10:28:58 +04:00
|
|
|
if err := cs.timeoutTicker.Start(); err != nil {
|
2017-09-06 13:11:47 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-10 23:31:05 -05:00
|
|
|
|
|
|
|
// we may have lost some votes if the process crashed
|
|
|
|
// reload from consensus log to catchup
|
2017-10-26 18:29:23 -04:00
|
|
|
if cs.doWALCatchup {
|
|
|
|
if err := cs.catchupReplay(cs.Height); err != nil {
|
2019-02-04 13:00:06 -05:00
|
|
|
// don't try to recover from data corruption error
|
|
|
|
if IsDataCorruptionError(err) {
|
|
|
|
cs.Logger.Error("Encountered corrupt WAL file", "err", err.Error())
|
|
|
|
cs.Logger.Error("Please repair the WAL file before restarting")
|
|
|
|
fmt.Println(`You can attempt to repair the WAL as follows:
|
|
|
|
|
|
|
|
----
|
|
|
|
WALFILE=~/.tendermint/data/cs.wal/wal
|
|
|
|
cp $WALFILE ${WALFILE}.bak # backup the file
|
|
|
|
go run scripts/wal2json/main.go $WALFILE > wal.json # this will panic, but can be ignored
|
|
|
|
rm $WALFILE # remove the corrupt file
|
|
|
|
go run scripts/json2wal/main.go wal.json $WALFILE # rebuild the file without corruption
|
|
|
|
----`)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-26 18:29:23 -04:00
|
|
|
cs.Logger.Error("Error on catchup replay. Proceeding to start ConsensusState anyway", "err", err.Error())
|
|
|
|
// NOTE: if we ever do return an error here,
|
|
|
|
// make sure to stop the timeoutTicker
|
|
|
|
}
|
2016-01-10 23:31:05 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-11-06 13:20:39 -05:00
|
|
|
err := cs.timeoutTicker.Start()
|
2017-09-06 13:11:47 -04:00
|
|
|
if err != nil {
|
2017-09-21 10:56:42 -04:00
|
|
|
cs.Logger.Error("Error starting timeout ticker", "err", err)
|
2017-10-03 18:12:17 -04:00
|
|
|
return
|
2017-09-06 13:11:47 -04:00
|
|
|
}
|
2015-12-12 17:22:48 -05:00
|
|
|
go cs.receiveRoutine(maxSteps)
|
2015-12-10 11:41:18 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 21:24:13 +04:00
|
|
|
// OnStop implements cmn.Service.
|
2015-07-21 18:31:01 -07:00
|
|
|
func (cs *ConsensusState) OnStop() {
|
2018-05-16 10:28:58 +04:00
|
|
|
cs.evsw.Stop()
|
2016-12-19 22:29:32 -05:00
|
|
|
cs.timeoutTicker.Stop()
|
2018-11-21 21:24:13 +04:00
|
|
|
// WAL is stopped in receiveRoutine.
|
2014-10-30 03:32:09 -07:00
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// Wait waits for the the main routine to return.
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// OpenWAL opens a file to log all consensus messages and timeouts for deterministic accountability
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
func (cs *ConsensusState) OpenWAL(walFile string) (WAL, error) {
|
2018-04-09 16:32:43 +02:00
|
|
|
wal, err := NewWAL(walFile)
|
2016-01-18 14:10:05 -05:00
|
|
|
if err != nil {
|
2017-10-30 11:12:01 -05:00
|
|
|
cs.Logger.Error("Failed to open WAL for consensus state", "wal", walFile, "err", err)
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
return nil, err
|
2016-01-10 23:31:05 -05:00
|
|
|
}
|
2017-05-12 23:07:53 +02:00
|
|
|
wal.SetLogger(cs.Logger.With("wal", walFile))
|
2017-11-06 13:20:39 -05:00
|
|
|
if err := wal.Start(); err != nil {
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
return nil, err
|
2017-05-12 23:07:53 +02:00
|
|
|
}
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
return wal, nil
|
2016-01-06 18:42:12 -05:00
|
|
|
}
|
|
|
|
|
2015-12-12 17:22:48 -05:00
|
|
|
//------------------------------------------------------------
|
2017-07-20 00:05:33 -04:00
|
|
|
// Public interface for passing messages into the consensus state, possibly causing a state transition.
|
2018-01-01 21:27:38 -05:00
|
|
|
// If peerID == "", the msg is considered internal.
|
2017-07-20 00:05:33 -04:00
|
|
|
// Messages are added to the appropriate queue (peer or internal).
|
|
|
|
// If the queue is full, the function may block.
|
2015-12-12 17:22:48 -05:00
|
|
|
// TODO: should these return anything or let callers just use events?
|
2015-12-12 01:28:33 -05:00
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// AddVote inputs a vote.
|
2018-01-01 21:27:38 -05:00
|
|
|
func (cs *ConsensusState) AddVote(vote *types.Vote, peerID p2p.ID) (added bool, err error) {
|
|
|
|
if peerID == "" {
|
2016-07-01 17:47:31 -04:00
|
|
|
cs.internalMsgQueue <- msgInfo{&VoteMessage{vote}, ""}
|
2015-12-12 01:28:33 -05:00
|
|
|
} else {
|
2018-01-01 21:27:38 -05:00
|
|
|
cs.peerMsgQueue <- msgInfo{&VoteMessage{vote}, peerID}
|
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
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// SetProposal inputs a proposal.
|
2018-01-01 21:27:38 -05:00
|
|
|
func (cs *ConsensusState) SetProposal(proposal *types.Proposal, peerID p2p.ID) error {
|
2015-12-12 01:28:33 -05:00
|
|
|
|
2018-01-01 21:27:38 -05:00
|
|
|
if peerID == "" {
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.internalMsgQueue <- msgInfo{&ProposalMessage{proposal}, ""}
|
|
|
|
} else {
|
2018-01-01 21:27:38 -05:00
|
|
|
cs.peerMsgQueue <- msgInfo{&ProposalMessage{proposal}, peerID}
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: wait for event?!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// AddProposalBlockPart inputs a part of the proposal block.
|
2018-01-01 21:27:38 -05:00
|
|
|
func (cs *ConsensusState) AddProposalBlockPart(height int64, round int, part *types.Part, peerID p2p.ID) error {
|
2015-12-12 01:28:33 -05:00
|
|
|
|
2018-01-01 21:27:38 -05:00
|
|
|
if peerID == "" {
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.internalMsgQueue <- msgInfo{&BlockPartMessage{height, round, part}, ""}
|
|
|
|
} else {
|
2018-01-01 21:27:38 -05:00
|
|
|
cs.peerMsgQueue <- msgInfo{&BlockPartMessage{height, round, part}, peerID}
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: wait for event?!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// SetProposalAndBlock inputs the proposal and all block parts.
|
2018-01-01 21:27:38 -05:00
|
|
|
func (cs *ConsensusState) SetProposalAndBlock(proposal *types.Proposal, block *types.Block, parts *types.PartSet, peerID p2p.ID) error {
|
|
|
|
if err := cs.SetProposal(proposal, peerID); err != nil {
|
2017-09-06 13:11:47 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-12-12 01:28:33 -05:00
|
|
|
for i := 0; i < parts.Total(); i++ {
|
|
|
|
part := parts.GetPart(i)
|
2018-01-01 21:27:38 -05:00
|
|
|
if err := cs.AddProposalBlockPart(proposal.Height, proposal.Round, part, peerID); err != nil {
|
2017-09-06 13:11:47 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
2017-09-06 13:11:47 -04:00
|
|
|
return nil
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
|
|
|
|
2015-12-12 17:22:48 -05:00
|
|
|
//------------------------------------------------------------
|
2015-12-12 01:28:33 -05:00
|
|
|
// internal functions for managing the state
|
|
|
|
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) updateHeight(height int64) {
|
2018-06-15 15:10:25 +04:00
|
|
|
cs.metrics.Height.Set(float64(height))
|
2018-06-13 22:40:55 +04:00
|
|
|
cs.Height = height
|
2015-12-08 16:00:59 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
func (cs *ConsensusState) updateRoundStep(round int, step cstypes.RoundStepType) {
|
2015-12-08 16:00:59 -05:00
|
|
|
cs.Round = round
|
|
|
|
cs.Step = step
|
|
|
|
}
|
|
|
|
|
2015-12-13 19:33:05 -05:00
|
|
|
// enterNewRound(height, 0) at cs.StartTime.
|
2017-10-10 12:39:21 +04:00
|
|
|
func (cs *ConsensusState) scheduleRound0(rs *cstypes.RoundState) {
|
2018-09-01 01:33:51 +02:00
|
|
|
//cs.Logger.Info("scheduleRound0", "now", tmtime.Now(), "startTime", cs.StartTime)
|
2019-02-06 18:23:25 +04:00
|
|
|
sleepDuration := rs.StartTime.Sub(tmtime.Now())
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.scheduleTimeout(sleepDuration, rs.Height, 0, cstypes.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)
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) scheduleTimeout(duration time.Duration, height int64, round int, step cstypes.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
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.Logger.Info("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)
|
2017-12-27 22:09:48 -05:00
|
|
|
func (cs *ConsensusState) reconstructLastCommit(state sm.State) {
|
2015-12-12 01:28:33 -05:00
|
|
|
if state.LastBlockHeight == 0 {
|
|
|
|
return
|
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
seenCommit := cs.blockStore.LoadSeenCommit(state.LastBlockHeight)
|
2018-10-13 01:21:46 +02:00
|
|
|
lastPrecommits := types.NewVoteSet(state.ChainID, state.LastBlockHeight, seenCommit.Round(), types.PrecommitType, 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
|
|
|
|
}
|
2019-02-04 13:01:59 -05:00
|
|
|
added, err := lastPrecommits.AddVote(seenCommit.ToVote(precommit))
|
2015-12-12 01:28:33 -05:00
|
|
|
if !added || err != nil {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("Failed to reconstruct LastCommit: %v", err))
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !lastPrecommits.HasTwoThirdsMajority() {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic("Failed to reconstruct LastCommit: Does not have +2/3 maj")
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
|
|
|
cs.LastCommit = lastPrecommits
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates ConsensusState and increments height to match that of state.
|
2017-10-10 12:39:21 +04:00
|
|
|
// The round becomes 0 and cs.Step becomes cstypes.RoundStepNewHeight.
|
2017-12-27 22:09:48 -05:00
|
|
|
func (cs *ConsensusState) updateToState(state sm.State) {
|
2015-12-12 01:28:33 -05:00
|
|
|
if cs.CommitRound > -1 && 0 < cs.Height && cs.Height != state.LastBlockHeight {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("updateToState() expected state height of %v but found %v",
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.Height, state.LastBlockHeight))
|
|
|
|
}
|
2017-12-27 22:09:48 -05:00
|
|
|
if !cs.state.IsEmpty() && cs.state.LastBlockHeight+1 != cs.Height {
|
2015-12-12 01:28:33 -05:00
|
|
|
// This might happen when someone else is mutating cs.state.
|
|
|
|
// Someone forgot to pass in state.Copy() somewhere?!
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v",
|
2015-12-12 01:28:33 -05:00
|
|
|
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.
|
2018-06-18 17:08:09 -07:00
|
|
|
// We don't want to reset e.g. the Votes, but we still want to
|
2019-01-17 21:46:40 -05:00
|
|
|
// signal the new round step, because other services (eg. txNotifier)
|
2018-06-18 17:08:09 -07:00
|
|
|
// depend on having an up-to-date peer state!
|
2017-12-27 22:09:48 -05:00
|
|
|
if !cs.state.IsEmpty() && (state.LastBlockHeight <= cs.state.LastBlockHeight) {
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.Logger.Info("Ignoring updateToState()", "newHeight", state.LastBlockHeight+1, "oldHeight", cs.state.LastBlockHeight+1)
|
2018-06-18 17:08:09 -07:00
|
|
|
cs.newStep()
|
2015-12-12 01:28:33 -05:00
|
|
|
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() {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic("updateToState(state) called but last Precommit round didn't have +2/3")
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
|
|
|
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)
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.updateRoundStep(0, cstypes.RoundStepNewHeight)
|
2015-12-12 01:28:33 -05:00
|
|
|
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)
|
2018-09-01 01:33:51 +02:00
|
|
|
cs.StartTime = cs.config.Commit(tmtime.Now())
|
2015-12-12 01:28:33 -05:00
|
|
|
} else {
|
2017-05-01 20:09:29 -04:00
|
|
|
cs.StartTime = cs.config.Commit(cs.CommitTime)
|
2015-12-12 01:28:33 -05:00
|
|
|
}
|
2018-06-18 17:08:09 -07:00
|
|
|
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.Validators = validators
|
|
|
|
cs.Proposal = nil
|
|
|
|
cs.ProposalBlock = nil
|
|
|
|
cs.ProposalBlockParts = nil
|
2018-10-15 22:05:13 +02:00
|
|
|
cs.LockedRound = -1
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.LockedBlock = nil
|
|
|
|
cs.LockedBlockParts = nil
|
2018-10-15 22:05:13 +02:00
|
|
|
cs.ValidRound = -1
|
2018-01-23 16:54:24 +01:00
|
|
|
cs.ValidBlock = nil
|
|
|
|
cs.ValidBlockParts = nil
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.Votes = cstypes.NewHeightVoteSet(state.ChainID, height, validators)
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.CommitRound = -1
|
|
|
|
cs.LastCommit = lastPrecommits
|
|
|
|
cs.LastValidators = state.LastValidators
|
2019-02-18 08:29:41 +01:00
|
|
|
cs.TriggeredTimeoutPrecommit = false
|
2015-12-12 01:28:33 -05:00
|
|
|
|
|
|
|
cs.state = state
|
|
|
|
|
|
|
|
// Finally, broadcast RoundState
|
|
|
|
cs.newStep()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ConsensusState) newStep() {
|
2015-12-22 15:23:22 -05:00
|
|
|
rs := cs.RoundStateEvent()
|
2018-05-20 14:40:01 -04:00
|
|
|
cs.wal.Write(rs)
|
2018-04-02 10:21:17 +02:00
|
|
|
cs.nSteps++
|
2018-06-18 17:08:09 -07:00
|
|
|
// newStep is called by updateToState in NewConsensusState before the eventBus is set!
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
if cs.eventBus != nil {
|
|
|
|
cs.eventBus.PublishEventNewRoundStep(rs)
|
2018-05-16 10:28:58 +04:00
|
|
|
cs.evsw.FireEvent(types.EventNewRoundStep, &cs.RoundState)
|
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.
|
2017-07-25 10:52:14 -04:00
|
|
|
// Updates (state transitions) happen on timeouts, complete proposals, and 2/3 majorities.
|
|
|
|
// ConsensusState must be locked before any internal state is updated.
|
2015-12-12 01:28:33 -05:00
|
|
|
func (cs *ConsensusState) receiveRoutine(maxSteps int) {
|
2018-08-03 11:24:55 +04:00
|
|
|
onExit := func(cs *ConsensusState) {
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// close wal now that we're done writing to it
|
|
|
|
cs.wal.Stop()
|
|
|
|
cs.wal.Wait()
|
|
|
|
|
|
|
|
close(cs.done)
|
|
|
|
}
|
|
|
|
|
2017-08-16 01:01:09 -04:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2017-10-02 15:24:30 -04:00
|
|
|
cs.Logger.Error("CONSENSUS FAILURE!!!", "err", r, "stack", string(debug.Stack()))
|
2018-08-03 11:24:55 +04:00
|
|
|
// stop gracefully
|
|
|
|
//
|
|
|
|
// NOTE: We most probably shouldn't be running any further when there is
|
|
|
|
// some unexpected panic. Some unknown error happened, and so we don't
|
|
|
|
// know if that will result in the validator signing an invalid thing. It
|
|
|
|
// might be worthwhile to explore a mechanism for manual resuming via
|
|
|
|
// some console or secure RPC system, but for now, halting the chain upon
|
|
|
|
// unexpected consensus bugs sounds like the better option.
|
|
|
|
onExit(cs)
|
2017-08-16 01:01:09 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-10 11:41:18 -05:00
|
|
|
for {
|
2015-12-12 01:28:33 -05:00
|
|
|
if maxSteps > 0 {
|
|
|
|
if cs.nSteps >= maxSteps {
|
2017-05-12 23:07:53 +02:00
|
|
|
cs.Logger.Info("reached max steps. exiting receive routine")
|
2015-12-12 01:28:33 -05:00
|
|
|
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 {
|
2019-01-17 21:46:40 -05:00
|
|
|
case <-cs.txNotifier.TxsAvailable():
|
2018-07-23 16:47:15 +04:00
|
|
|
cs.handleTxsAvailable()
|
2015-12-11 11:57:15 -05:00
|
|
|
case mi = <-cs.peerMsgQueue:
|
2018-05-20 14:40:01 -04:00
|
|
|
cs.wal.Write(mi)
|
2015-12-10 11:41:18 -05:00
|
|
|
// handles proposals, block parts, votes
|
|
|
|
// may generate internal events (votes, complete proposals, 2/3 majorities)
|
2017-07-25 10:52:14 -04:00
|
|
|
cs.handleMsg(mi)
|
2015-12-11 11:57:15 -05:00
|
|
|
case mi = <-cs.internalMsgQueue:
|
2018-05-20 14:40:01 -04:00
|
|
|
cs.wal.WriteSync(mi) // NOTE: fsync
|
2019-02-04 13:00:06 -05:00
|
|
|
|
|
|
|
if _, ok := mi.Msg.(*VoteMessage); ok {
|
|
|
|
// we actually want to simulate failing during
|
|
|
|
// the previous WriteSync, but this isn't easy to do.
|
|
|
|
// Equivalent would be to fail here and manually remove
|
|
|
|
// some bytes from the end of the wal.
|
|
|
|
fail.Fail() // XXX
|
|
|
|
}
|
|
|
|
|
2015-12-11 11:57:15 -05:00
|
|
|
// handles proposals, block parts, votes
|
2017-07-25 10:52:14 -04:00
|
|
|
cs.handleMsg(mi)
|
2016-12-19 22:29:32 -05:00
|
|
|
case ti := <-cs.timeoutTicker.Chan(): // tockChan:
|
2018-05-20 14:40:01 -04:00
|
|
|
cs.wal.Write(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)
|
2018-02-12 14:31:52 +04:00
|
|
|
case <-cs.Quit():
|
2018-08-03 11:24:55 +04:00
|
|
|
onExit(cs)
|
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
|
2017-07-25 10:52:14 -04:00
|
|
|
func (cs *ConsensusState) handleMsg(mi msgInfo) {
|
2015-12-12 01:28:33 -05:00
|
|
|
cs.mtx.Lock()
|
|
|
|
defer cs.mtx.Unlock()
|
|
|
|
|
2019-03-08 09:46:09 +04:00
|
|
|
var (
|
|
|
|
added bool
|
2019-03-11 22:52:09 +04:00
|
|
|
err error
|
2019-03-08 09:46:09 +04:00
|
|
|
)
|
2018-01-01 21:27:38 -05:00
|
|
|
msg, peerID := mi.Msg, mi.PeerID
|
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
|
2019-03-08 09:46:09 +04:00
|
|
|
added, err = cs.addProposalBlockPart(msg, peerID)
|
2018-09-21 20:36:48 +02:00
|
|
|
if added {
|
|
|
|
cs.statsMsgQueue <- mi
|
|
|
|
}
|
|
|
|
|
2016-03-02 21:38:05 +00:00
|
|
|
if err != nil && msg.Round != cs.Round {
|
2018-05-17 13:59:41 -04:00
|
|
|
cs.Logger.Debug("Received block part from wrong round", "height", cs.Height, "csRound", cs.Round, "blockRound", msg.Round)
|
2016-03-02 21:38:05 +00:00
|
|
|
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
|
2019-03-08 09:46:09 +04:00
|
|
|
added, err = cs.tryAddVote(msg.Vote, peerID)
|
2018-09-21 20:36:48 +02:00
|
|
|
if added {
|
|
|
|
cs.statsMsgQueue <- mi
|
|
|
|
}
|
|
|
|
|
2015-12-10 11:41:18 -05:00
|
|
|
if err == ErrAddingVote {
|
2018-03-05 15:26:36 +04:00
|
|
|
// TODO: punish peer
|
2018-03-06 13:40:09 +04:00
|
|
|
// We probably don't want to stop the peer here. The vote does not
|
|
|
|
// necessarily comes from a malicious peer but can be just broadcasted by
|
|
|
|
// a typical peer.
|
|
|
|
// https://github.com/tendermint/tendermint/issues/1281
|
2015-12-10 11:41:18 -05:00
|
|
|
}
|
|
|
|
|
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:
|
2019-03-11 22:52:09 +04:00
|
|
|
cs.Logger.Error("Unknown msg type", "type", reflect.TypeOf(msg))
|
|
|
|
return
|
2015-12-10 11:41:18 -05:00
|
|
|
}
|
2019-03-11 22:52:09 +04:00
|
|
|
|
2015-12-10 11:41:18 -05:00
|
|
|
if err != nil {
|
2019-03-11 22:52:09 +04:00
|
|
|
// Causes TestReactorValidatorSetChanges to timeout
|
|
|
|
// https://github.com/tendermint/tendermint/issues/3406
|
|
|
|
// cs.Logger.Error("Error with msg", "height", cs.Height, "round", cs.Round,
|
|
|
|
// "peer", peerID, "err", err, "msg", msg)
|
2015-12-10 11:41:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
func (cs *ConsensusState) handleTimeout(ti timeoutInfo, rs cstypes.RoundState) {
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.Logger.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) {
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.Logger.Debug("Ignoring tock because we're ahead", "height", rs.Height, "round", rs.Round, "step", rs.Step)
|
2015-12-10 11:41:18 -05:00
|
|
|
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 {
|
2017-10-10 12:39:21 +04:00
|
|
|
case cstypes.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)
|
2017-10-10 12:39:21 +04:00
|
|
|
case cstypes.RoundStepNewRound:
|
2017-08-04 21:46:17 -04:00
|
|
|
cs.enterPropose(ti.Height, 0)
|
2017-10-10 12:39:21 +04:00
|
|
|
case cstypes.RoundStepPropose:
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventTimeoutPropose(cs.RoundStateEvent())
|
2015-12-22 15:23:22 -05:00
|
|
|
cs.enterPrevote(ti.Height, ti.Round)
|
2017-10-10 12:39:21 +04:00
|
|
|
case cstypes.RoundStepPrevoteWait:
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent())
|
2015-12-22 15:23:22 -05:00
|
|
|
cs.enterPrecommit(ti.Height, ti.Round)
|
2017-10-10 12:39:21 +04:00
|
|
|
case cstypes.RoundStepPrecommitWait:
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent())
|
2018-10-12 22:13:01 +02:00
|
|
|
cs.enterPrecommit(ti.Height, ti.Round)
|
2015-12-22 15:23:22 -05:00
|
|
|
cs.enterNewRound(ti.Height, ti.Round+1)
|
2015-12-10 11:41:18 -05:00
|
|
|
default:
|
2018-08-10 00:25:57 -05:00
|
|
|
panic(fmt.Sprintf("Invalid timeout step: %v", ti.Step))
|
2015-12-10 11:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-23 13:34:45 +02:00
|
|
|
func (cs *ConsensusState) handleTxsAvailable() {
|
2017-07-25 13:57:11 -04:00
|
|
|
cs.mtx.Lock()
|
|
|
|
defer cs.mtx.Unlock()
|
|
|
|
// we only need to do this for round 0
|
2019-01-24 15:33:47 +01:00
|
|
|
cs.enterNewRound(cs.Height, 0)
|
2018-07-23 13:34:45 +02:00
|
|
|
cs.enterPropose(cs.Height, 0)
|
2017-07-25 13:57:11 -04: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
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// Enter: `timeoutNewHeight` by startTime (commitTime+timeoutCommit),
|
|
|
|
// or, if SkipTimeout==true, after receiving all precommits from (height,round-1)
|
2015-06-24 17:05:52 -07:00
|
|
|
// Enter: `timeoutPrecommits` after any +2/3 precommits from (height,round-1)
|
2017-07-20 00:05:33 -04:00
|
|
|
// Enter: +2/3 precommits for nil at (height,round-1)
|
|
|
|
// Enter: +2/3 prevotes any or +2/3 precommits for block or any from (height, round)
|
2015-06-05 14:15:40 -07:00
|
|
|
// NOTE: cs.StartTime was already set for height.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) enterNewRound(height int64, round int) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger := cs.Logger.With("height", height, "round", round)
|
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
if cs.Height != height || round < cs.Round || (cs.Round == round && cs.Step != cstypes.RoundStepNewHeight) {
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Debug(fmt.Sprintf("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
|
|
|
|
2018-09-01 01:33:51 +02:00
|
|
|
if now := tmtime.Now(); cs.StartTime.After(now) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("Need to set a buffer and log message here for sanity.", "startTime", cs.StartTime, "now", now)
|
2014-10-30 03:32:09 -07:00
|
|
|
}
|
2015-12-11 11:57:15 -05:00
|
|
|
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Info(fmt.Sprintf("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()
|
2018-11-28 21:35:09 +01:00
|
|
|
validators.IncrementProposerPriority(round - cs.Round)
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.updateRoundStep(round, cstypes.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 {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("Resetting Proposal info")
|
2015-07-05 15:35:26 -07:00
|
|
|
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
|
2019-01-24 15:33:47 +01:00
|
|
|
cs.TriggeredTimeoutPrecommit = false
|
2015-06-05 14:15:40 -07:00
|
|
|
|
2018-11-15 18:40:42 -05:00
|
|
|
cs.eventBus.PublishEventNewRound(cs.NewRoundEvent())
|
2018-06-15 15:10:25 +04:00
|
|
|
cs.metrics.Rounds.Set(float64(round))
|
2015-09-09 16:45:53 -04:00
|
|
|
|
2017-07-12 01:02:16 -04:00
|
|
|
// Wait for txs to be available in the mempool
|
2017-07-25 13:57:11 -04:00
|
|
|
// before we enterPropose in round 0. If the last block changed the app hash,
|
2017-07-20 14:43:16 -04:00
|
|
|
// we may need an empty "proof" block, and enterPropose immediately.
|
2017-08-04 21:46:17 -04:00
|
|
|
waitForTxs := cs.config.WaitForTxs() && round == 0 && !cs.needProofBlock(height)
|
2017-07-25 13:57:11 -04:00
|
|
|
if waitForTxs {
|
2017-08-04 21:46:17 -04:00
|
|
|
if cs.config.CreateEmptyBlocksInterval > 0 {
|
2018-10-12 22:13:01 +02:00
|
|
|
cs.scheduleTimeout(cs.config.CreateEmptyBlocksInterval, height, round,
|
|
|
|
cstypes.RoundStepNewRound)
|
2017-08-04 21:46:17 -04:00
|
|
|
}
|
2017-07-13 13:19:44 -04:00
|
|
|
} else {
|
|
|
|
cs.enterPropose(height, round)
|
|
|
|
}
|
2017-07-12 01:02:16 -04:00
|
|
|
}
|
|
|
|
|
2017-07-20 14:43:16 -04:00
|
|
|
// needProofBlock returns true on the first height (so the genesis app hash is signed right away)
|
|
|
|
// and where the last block (height-1) caused the app hash to change
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) needProofBlock(height int64) bool {
|
2017-07-20 14:43:16 -04:00
|
|
|
if height == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1)
|
2017-09-21 14:13:13 -04:00
|
|
|
return !bytes.Equal(cs.state.AppHash, lastBlockMeta.Header.AppHash)
|
2017-07-20 14:43:16 -04:00
|
|
|
}
|
|
|
|
|
2017-08-04 21:46:17 -04:00
|
|
|
// Enter (CreateEmptyBlocks): from enterNewRound(height,round)
|
|
|
|
// Enter (CreateEmptyBlocks, CreateEmptyBlocksInterval > 0 ): after enterNewRound(height,round), after timeout of CreateEmptyBlocksInterval
|
|
|
|
// Enter (!CreateEmptyBlocks) : after enterNewRound(height,round), once txs are in the mempool
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) enterPropose(height int64, round int) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger := cs.Logger.With("height", height, "round", round)
|
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPropose <= cs.Step) {
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Debug(fmt.Sprintf("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
|
|
|
|
}
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Info(fmt.Sprintf("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:
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.updateRoundStep(round, cstypes.RoundStepPropose)
|
2015-12-12 16:25:49 -05:00
|
|
|
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
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.scheduleTimeout(cs.config.Propose(round), height, round, cstypes.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 {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Debug("This node is not a validator")
|
2015-01-08 22:07:23 -08:00
|
|
|
return
|
|
|
|
}
|
2015-06-05 14:15:40 -07:00
|
|
|
|
2018-01-23 21:41:13 -05:00
|
|
|
// if not a validator, we're done
|
2018-12-22 06:36:45 +01:00
|
|
|
address := cs.privValidator.GetPubKey().Address()
|
|
|
|
if !cs.Validators.HasAddress(address) {
|
|
|
|
logger.Debug("This node is not a validator", "addr", address, "vals", cs.Validators)
|
2018-01-23 21:41:13 -05:00
|
|
|
return
|
2018-01-21 13:32:04 -05:00
|
|
|
}
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Debug("This node is a validator")
|
2018-01-21 13:32:04 -05:00
|
|
|
|
2018-12-22 06:36:45 +01:00
|
|
|
if cs.isProposer(address) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPropose: Our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
|
2015-08-26 18:56:34 -04:00
|
|
|
cs.decideProposal(height, round)
|
2018-01-21 13:32:04 -05:00
|
|
|
} else {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPropose: Not our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
|
2016-06-26 15:33:11 -04:00
|
|
|
}
|
2015-06-24 17:05:52 -07:00
|
|
|
}
|
2014-08-10 16:35:08 -07:00
|
|
|
|
2018-12-22 06:36:45 +01:00
|
|
|
func (cs *ConsensusState) isProposer(address []byte) bool {
|
|
|
|
return bytes.Equal(cs.Validators.GetProposer().Address, address)
|
2017-07-11 19:18:15 -04:00
|
|
|
}
|
|
|
|
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) defaultDecideProposal(height int64, 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
|
2018-10-15 22:05:13 +02:00
|
|
|
if cs.ValidBlock != nil {
|
2018-01-23 16:54:24 +01:00
|
|
|
// If there is valid block, choose that.
|
|
|
|
block, blockParts = cs.ValidBlock, cs.ValidBlockParts
|
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
|
|
|
}
|
|
|
|
|
2019-02-20 07:45:18 +02:00
|
|
|
// Flush the WAL. Otherwise, we may not recompute the same proposal to sign, and the privValidator will refuse to sign anything.
|
2019-02-25 09:11:07 +04:00
|
|
|
cs.wal.FlushAndSync()
|
2019-02-20 07:45:18 +02:00
|
|
|
|
2014-09-14 15:37:32 -07:00
|
|
|
// Make proposal
|
2019-02-11 16:31:34 +04:00
|
|
|
propBlockId := types.BlockID{Hash: block.Hash(), PartsHeader: blockParts.Header()}
|
2018-10-31 15:27:11 +01:00
|
|
|
proposal := types.NewProposal(height, round, cs.ValidRound, propBlockId)
|
2017-10-12 14:50:09 +04:00
|
|
|
if err := cs.privValidator.SignProposal(cs.state.ChainID, proposal); err == nil {
|
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}, ""})
|
|
|
|
}
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.Logger.Info("Signed proposal", "height", height, "round", round, "proposal", proposal)
|
2018-08-10 00:25:57 -05:00
|
|
|
cs.Logger.Debug(fmt.Sprintf("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 {
|
2017-06-14 12:50:49 +04:00
|
|
|
cs.Logger.Error("enterPropose: Error signing proposal", "height", height, "round", round, "err", err)
|
2016-09-08 18:06:25 -04:00
|
|
|
}
|
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
|
|
|
|
}
|
2018-04-02 10:21:17 +02:00
|
|
|
// if this is false the proposer is lying or we haven't received the POL yet
|
|
|
|
return cs.Votes.Prevotes(cs.Proposal.POLRound).HasTwoThirdsMajority()
|
|
|
|
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the next block to propose and return it.
|
2018-07-24 21:48:51 -04:00
|
|
|
// We really only need to return the parts, but the block
|
|
|
|
// is returned for convenience so we can log the proposal block.
|
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.
|
2019-02-08 18:40:41 -05:00
|
|
|
commit = types.NewCommit(types.BlockID{}, nil)
|
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.
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.Logger.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
|
|
|
|
2018-12-22 06:36:45 +01:00
|
|
|
proposerAddr := cs.privValidator.GetPubKey().Address()
|
2019-01-17 21:46:40 -05:00
|
|
|
return cs.blockExec.CreateProposalBlock(cs.Height, cs.state, commit, proposerAddr)
|
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
|
|
|
// Prevote for LockedBlock if we're locked, or ProposalBlock if valid.
|
2014-10-30 03:32:09 -07:00
|
|
|
// Otherwise vote nil.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) enterPrevote(height int64, round int) {
|
2017-10-10 12:39:21 +04:00
|
|
|
if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevote <= cs.Step) {
|
2018-08-10 00:25:57 -05:00
|
|
|
cs.Logger.Debug(fmt.Sprintf("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:
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.updateRoundStep(round, cstypes.RoundStepPrevote)
|
2015-12-12 16:25:49 -05:00
|
|
|
cs.newStep()
|
|
|
|
}()
|
|
|
|
|
2018-08-10 00:25:57 -05:00
|
|
|
cs.Logger.Info(fmt.Sprintf("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
|
|
|
}
|
|
|
|
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) defaultDoPrevote(height int64, round int) {
|
2017-07-07 16:58:16 -04:00
|
|
|
logger := cs.Logger.With("height", height, "round", round)
|
2018-10-12 22:13:01 +02:00
|
|
|
|
2014-10-21 01:18:46 -07:00
|
|
|
// If a block is locked, prevote that.
|
|
|
|
if cs.LockedBlock != nil {
|
2017-07-07 16:58:16 -04:00
|
|
|
logger.Info("enterPrevote: Block was locked")
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrevoteType, 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 {
|
2017-07-07 16:58:16 -04:00
|
|
|
logger.Info("enterPrevote: ProposalBlock is nil")
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrevoteType, 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
|
|
|
|
2017-07-20 00:05:33 -04:00
|
|
|
// Validate proposal block
|
2017-12-28 19:35:56 -05:00
|
|
|
err := cs.blockExec.ValidateBlock(cs.state, 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.
|
2017-07-07 16:58:16 -04:00
|
|
|
logger.Error("enterPrevote: ProposalBlock is invalid", "err", err)
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrevoteType, 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)
|
2017-07-09 18:01:25 -04:00
|
|
|
logger.Info("enterPrevote: ProposalBlock is valid")
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrevoteType, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header())
|
2014-10-21 01:18:46 -07:00
|
|
|
}
|
|
|
|
|
2015-06-24 18:51:14 -07:00
|
|
|
// Enter: any +2/3 prevotes at next round.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) enterPrevoteWait(height int64, round int) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger := cs.Logger.With("height", height, "round", round)
|
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevoteWait <= cs.Step) {
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Debug(fmt.Sprintf("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
|
|
|
|
}
|
2018-08-02 01:59:46 -07:00
|
|
|
if !cs.Votes.Prevotes(round).HasTwoThirdsAny() {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes", height, round))
|
2018-08-02 01:59:46 -07:00
|
|
|
}
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Info(fmt.Sprintf("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:
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.updateRoundStep(round, cstypes.RoundStepPrevoteWait)
|
2015-12-12 16:25:49 -05:00
|
|
|
cs.newStep()
|
|
|
|
}()
|
2015-06-05 14:15:40 -07:00
|
|
|
|
2016-02-29 16:15:23 -05:00
|
|
|
// Wait for some more prevotes; enterPrecommit
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.scheduleTimeout(cs.config.Prevote(round), height, round, cstypes.RoundStepPrevoteWait)
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enter: `timeoutPrevote` after any +2/3 prevotes.
|
2018-10-15 22:05:13 +02:00
|
|
|
// Enter: `timeoutPrecommit` after any +2/3 precommits.
|
2017-07-20 00:05:33 -04:00
|
|
|
// Enter: +2/3 precomits for block or nil.
|
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.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) enterPrecommit(height int64, round int) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger := cs.Logger.With("height", height, "round", round)
|
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrecommit <= cs.Step) {
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Debug(fmt.Sprintf("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
|
|
|
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Info(fmt.Sprintf("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:
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.updateRoundStep(round, cstypes.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
|
|
|
|
2018-04-27 23:00:09 -04:00
|
|
|
// check for a polka
|
2016-08-16 14:59:19 -07:00
|
|
|
blockID, ok := cs.Votes.Prevotes(round).TwoThirdsMajority()
|
2015-06-05 14:15:40 -07:00
|
|
|
|
2018-05-12 17:36:05 -07: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 {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit while we're locked. Precommitting nil")
|
2015-06-05 14:15:40 -07:00
|
|
|
} else {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPrecommit: No +2/3 prevotes during enterPrecommit. Precommitting nil.")
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrecommitType, 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
|
|
|
|
2018-05-12 17:36:05 -07:00
|
|
|
// At this point +2/3 prevoted for a particular block or nil.
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventPolka(cs.RoundStateEvent())
|
2015-09-09 16:45:53 -04:00
|
|
|
|
2018-05-12 17:36:05 -07:00
|
|
|
// the latest POLRound should be this round.
|
2016-08-20 15:08:26 -07:00
|
|
|
polRound, _ := cs.Votes.POLInfo()
|
|
|
|
if polRound < round {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("This POLRound should be %v but got %v", 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 {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPrecommit: +2/3 prevoted for nil.")
|
2015-05-04 10:15:58 -07:00
|
|
|
} else {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPrecommit: +2/3 prevoted for nil. Unlocking")
|
2018-10-15 22:05:13 +02:00
|
|
|
cs.LockedRound = -1
|
2015-05-04 10:15:58 -07:00
|
|
|
cs.LockedBlock = nil
|
|
|
|
cs.LockedBlockParts = nil
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
|
2015-05-04 10:15:58 -07:00
|
|
|
}
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrecommitType, 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) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPrecommit: +2/3 prevoted locked block. Relocking")
|
2015-08-12 14:00:23 -04:00
|
|
|
cs.LockedRound = round
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventRelock(cs.RoundStateEvent())
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrecommitType, 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) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("enterPrecommit: +2/3 prevoted proposal block. Locking", "hash", blockID.Hash)
|
2015-06-05 14:15:40 -07:00
|
|
|
// Validate the block.
|
2017-12-28 19:35:56 -05:00
|
|
|
if err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock); err != nil {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("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
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventLock(cs.RoundStateEvent())
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrecommitType, 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.
|
2018-10-15 22:05:13 +02:00
|
|
|
cs.LockedRound = -1
|
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
|
|
|
}
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
|
2018-10-13 01:21:46 +02:00
|
|
|
cs.signAddVote(types.PrecommitType, nil, types.PartSetHeader{})
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enter: any +2/3 precommits for next round.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) enterPrecommitWait(height int64, round int) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger := cs.Logger.With("height", height, "round", round)
|
|
|
|
|
2019-01-24 15:33:47 +01:00
|
|
|
if cs.Height != height || round < cs.Round || (cs.Round == round && cs.TriggeredTimeoutPrecommit) {
|
2018-10-12 22:13:01 +02:00
|
|
|
logger.Debug(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"enterPrecommitWait(%v/%v): Invalid args. "+
|
2019-01-24 15:33:47 +01:00
|
|
|
"Current state is Height/Round: %v/%v/, TriggeredTimeoutPrecommit:%v",
|
|
|
|
height, round, cs.Height, cs.Round, cs.TriggeredTimeoutPrecommit))
|
2015-05-04 10:15:58 -07:00
|
|
|
return
|
|
|
|
}
|
2015-06-05 14:15:40 -07:00
|
|
|
if !cs.Votes.Precommits(round).HasTwoThirdsAny() {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes", height, round))
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Info(fmt.Sprintf("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:
|
2019-01-24 15:33:47 +01:00
|
|
|
cs.TriggeredTimeoutPrecommit = true
|
2015-12-12 16:25:49 -05:00
|
|
|
cs.newStep()
|
|
|
|
}()
|
2015-06-05 14:15:40 -07:00
|
|
|
|
2016-02-29 16:15:23 -05:00
|
|
|
// Wait for some more precommits; enterNewRound
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.scheduleTimeout(cs.config.Precommit(round), height, round, cstypes.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
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) enterCommit(height int64, commitRound int) {
|
2018-05-17 14:06:58 -04:00
|
|
|
logger := cs.Logger.With("height", height, "commitRound", commitRound)
|
2018-05-17 13:59:41 -04:00
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
if cs.Height != height || cstypes.RoundStepCommit <= cs.Step {
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Debug(fmt.Sprintf("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
|
|
|
}
|
2018-08-10 00:25:57 -05:00
|
|
|
logger.Info(fmt.Sprintf("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.
|
2017-10-10 12:39:21 +04:00
|
|
|
cs.updateRoundStep(cs.Round, cstypes.RoundStepCommit)
|
2015-09-15 16:13:39 -04:00
|
|
|
cs.CommitRound = commitRound
|
2018-09-01 01:33:51 +02:00
|
|
|
cs.CommitTime = tmtime.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 {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic("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) {
|
2018-05-17 14:06:58 -04:00
|
|
|
logger.Info("Commit is for locked block. Set ProposalBlock=LockedBlock", "blockHash", 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) {
|
2018-05-17 14:06:58 -04:00
|
|
|
logger.Info("Commit is for a block we don't know about. Set ProposalBlock=nil", "proposal", cs.ProposalBlock.Hash(), "commit", blockID.Hash)
|
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)
|
2018-10-31 14:20:36 +01:00
|
|
|
cs.eventBus.PublishEventValidBlock(cs.RoundStateEvent())
|
|
|
|
cs.evsw.FireEvent(types.EventValidBlock, &cs.RoundState)
|
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.
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) tryFinalizeCommit(height int64) {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger := cs.Logger.With("height", height)
|
|
|
|
|
2015-06-24 14:04:40 -07:00
|
|
|
if cs.Height != height {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("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 {
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Error("Attempt to finalize failed. There was no +2/3 majority, or +2/3 was for <nil>.")
|
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?
|
2018-05-17 13:59:41 -04:00
|
|
|
logger.Info("Attempt to finalize failed. We don't have the commit block.", "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
|
|
|
}
|
2017-05-15 11:02:40 +02: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
|
|
|
}
|
|
|
|
|
2017-10-10 12:39:21 +04:00
|
|
|
// Increment height and goto cstypes.RoundStepNewHeight
|
2017-12-01 19:04:53 -06:00
|
|
|
func (cs *ConsensusState) finalizeCommit(height int64) {
|
2017-10-10 12:39:21 +04:00
|
|
|
if cs.Height != height || cs.Step != cstypes.RoundStepCommit {
|
2018-08-10 00:25:57 -05:00
|
|
|
cs.Logger.Debug(fmt.Sprintf("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 {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("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) {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("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) {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("Cannot finalizeCommit, ProposalBlock does not hash to commit hash"))
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
2017-12-28 19:35:56 -05:00
|
|
|
if err := cs.blockExec.ValidateBlock(cs.state, block); err != nil {
|
2019-04-26 06:23:43 -04:00
|
|
|
panic(fmt.Sprintf("+2/3 committed an invalid block: %v", err))
|
2014-10-30 03:32:09 -07:00
|
|
|
}
|
2015-06-05 14:15:40 -07:00
|
|
|
|
2018-08-10 00:25:57 -05:00
|
|
|
cs.Logger.Info(fmt.Sprintf("Finalizing commit of block with %d txs", block.NumTxs),
|
2016-08-25 01:39:03 -04:00
|
|
|
"height", block.Height, "hash", block.Hash(), "root", block.AppHash)
|
2018-08-10 00:25:57 -05:00
|
|
|
cs.Logger.Info(fmt.Sprintf("%v", block))
|
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
|
|
|
// 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-05-02 11:53:32 +04:00
|
|
|
cs.Logger.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
|
|
|
|
|
2018-05-15 08:48:27 -07:00
|
|
|
// Write EndHeightMessage{} for this height, implying that the blockstore
|
|
|
|
// has saved the block.
|
|
|
|
//
|
|
|
|
// If we crash before writing this EndHeightMessage{}, we will recover by
|
|
|
|
// running ApplyBlock during the ABCI handshake when we restart. If we
|
|
|
|
// didn't save the block to the blockstore before writing
|
|
|
|
// EndHeightMessage{}, we'd have to change WAL replay -- currently it
|
|
|
|
// complains about replaying for heights where an #ENDHEIGHT entry already
|
|
|
|
// exists.
|
|
|
|
//
|
|
|
|
// Either way, the ConsensusState should not be resumed until we
|
|
|
|
// successfully call ApplyBlock (ie. later here, or in Handshake after
|
|
|
|
// restart).
|
2018-05-20 14:40:01 -04:00
|
|
|
cs.wal.WriteSync(EndHeightMessage{height}) // NOTE: fsync
|
2017-04-14 20:30:15 -04:00
|
|
|
|
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2018-05-15 08:48:27 -07:00
|
|
|
// Create a copy of the state for staging and an event cache for txs.
|
2016-01-06 17:14:20 -08:00
|
|
|
stateCopy := cs.state.Copy()
|
2016-07-05 17:03:09 -04:00
|
|
|
|
2017-04-14 15:33:19 -04:00
|
|
|
// Execute and commit the block, update and save the state, and update the mempool.
|
2018-05-15 08:48:27 -07:00
|
|
|
// NOTE The block.AppHash wont reflect these txs until the next block.
|
2017-12-27 22:09:48 -05:00
|
|
|
var err error
|
2019-02-11 16:31:34 +04:00
|
|
|
stateCopy, err = cs.blockExec.ApplyBlock(stateCopy, types.BlockID{Hash: block.Hash(), PartsHeader: blockParts.Header()}, block)
|
2016-11-30 17:28:41 -05:00
|
|
|
if err != nil {
|
2017-06-14 12:50:49 +04:00
|
|
|
cs.Logger.Error("Error on ApplyBlock. Did the application crash? Please restart tendermint", "err", err)
|
2017-10-27 10:55:20 -04:00
|
|
|
err := cmn.Kill()
|
|
|
|
if err != nil {
|
|
|
|
cs.Logger.Error("Failed to kill this process - please do so manually", "err", 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
|
|
|
|
|
2018-06-15 15:10:25 +04:00
|
|
|
// must be called before we update state
|
2018-06-14 16:09:32 +04:00
|
|
|
cs.recordMetrics(height, block)
|
2018-06-13 20:38:19 +04:00
|
|
|
|
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
|
|
|
|
2017-04-15 01:33:30 -04:00
|
|
|
fail.Fail() // XXX
|
|
|
|
|
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
|
2017-10-10 12:39:21 +04:00
|
|
|
// * cs.Step is now cstypes.RoundStepNewHeight
|
2015-06-24 14:04:40 -07:00
|
|
|
// * cs.StartTime is set to when we will start round0.
|
2014-10-20 19:02:10 -07:00
|
|
|
}
|
|
|
|
|
2018-06-14 16:09:32 +04:00
|
|
|
func (cs *ConsensusState) recordMetrics(height int64, block *types.Block) {
|
2018-06-15 14:23:34 +04:00
|
|
|
cs.metrics.Validators.Set(float64(cs.Validators.Size()))
|
2018-06-15 14:35:36 +04:00
|
|
|
cs.metrics.ValidatorsPower.Set(float64(cs.Validators.TotalVotingPower()))
|
2018-06-13 20:38:19 +04:00
|
|
|
missingValidators := 0
|
2018-06-15 14:35:36 +04:00
|
|
|
missingValidatorsPower := int64(0)
|
|
|
|
for i, val := range cs.Validators.Validators {
|
2019-02-04 13:01:59 -05:00
|
|
|
var vote *types.CommitSig
|
2018-06-13 20:38:19 +04:00
|
|
|
if i < len(block.LastCommit.Precommits) {
|
|
|
|
vote = block.LastCommit.Precommits[i]
|
|
|
|
}
|
|
|
|
if vote == nil {
|
|
|
|
missingValidators++
|
2018-06-15 14:35:36 +04:00
|
|
|
missingValidatorsPower += val.VotingPower
|
2018-06-13 20:38:19 +04:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 14:23:34 +04:00
|
|
|
cs.metrics.MissingValidators.Set(float64(missingValidators))
|
2018-06-15 14:35:36 +04:00
|
|
|
cs.metrics.MissingValidatorsPower.Set(float64(missingValidatorsPower))
|
2018-06-15 14:23:34 +04:00
|
|
|
cs.metrics.ByzantineValidators.Set(float64(len(block.Evidence.Evidence)))
|
2018-06-15 14:35:36 +04:00
|
|
|
byzantineValidatorsPower := int64(0)
|
|
|
|
for _, ev := range block.Evidence.Evidence {
|
|
|
|
if _, val := cs.Validators.GetByAddress(ev.Address()); val != nil {
|
|
|
|
byzantineValidatorsPower += val.VotingPower
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cs.metrics.ByzantineValidatorsPower.Set(float64(byzantineValidatorsPower))
|
2018-06-15 14:23:34 +04:00
|
|
|
|
|
|
|
if height > 1 {
|
|
|
|
lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1)
|
2018-09-12 13:12:12 +04:00
|
|
|
cs.metrics.BlockIntervalSeconds.Set(
|
2018-06-20 10:39:19 +04:00
|
|
|
block.Time.Sub(lastBlockMeta.Header.Time).Seconds(),
|
|
|
|
)
|
2018-06-15 14:23:34 +04:00
|
|
|
}
|
2018-06-14 16:09:32 +04:00
|
|
|
|
2018-06-15 14:23:34 +04:00
|
|
|
cs.metrics.NumTxs.Set(float64(block.NumTxs))
|
|
|
|
cs.metrics.BlockSizeBytes.Set(float64(block.Size()))
|
2018-06-15 15:10:25 +04:00
|
|
|
cs.metrics.TotalTxs.Set(float64(block.TotalTxs))
|
2018-09-25 04:14:38 -07:00
|
|
|
cs.metrics.CommittedHeight.Set(float64(block.Height))
|
|
|
|
|
2018-06-13 20:38:19 +04: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
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:40:42 -05:00
|
|
|
// Verify POLRound, which must be -1 or in range [0, proposal.Round).
|
2018-10-25 16:40:20 +02:00
|
|
|
if proposal.POLRound < -1 ||
|
|
|
|
(proposal.POLRound >= 0 && proposal.POLRound >= proposal.Round) {
|
2015-06-22 19:04:31 -07:00
|
|
|
return ErrInvalidProposalPOLRound
|
|
|
|
}
|
|
|
|
|
2014-10-21 23:30:18 -07:00
|
|
|
// Verify signature
|
2018-03-02 01:50:17 -05:00
|
|
|
if !cs.Validators.GetProposer().PubKey.VerifyBytes(proposal.SignBytes(cs.state.ChainID), proposal.Signature) {
|
2014-10-21 23:30:18 -07:00
|
|
|
return ErrInvalidProposalSignature
|
|
|
|
}
|
|
|
|
|
|
|
|
cs.Proposal = proposal
|
2018-10-31 14:20:36 +01:00
|
|
|
// We don't update cs.ProposalBlockParts if it is already set.
|
|
|
|
// This happens if we're already in cstypes.RoundStepCommit or if there is a valid block in the current round.
|
|
|
|
// TODO: We can check if Proposal is for a different block as this is a sign of misbehavior!
|
|
|
|
if cs.ProposalBlockParts == nil {
|
2018-10-31 15:27:11 +01:00
|
|
|
cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockID.PartsHeader)
|
2018-10-31 14:20:36 +01:00
|
|
|
}
|
2018-05-13 19:17:25 -04:00
|
|
|
cs.Logger.Info("Received proposal", "proposal", proposal)
|
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.
|
2018-06-22 15:08:39 -04:00
|
|
|
func (cs *ConsensusState) addProposalBlockPart(msg *BlockPartMessage, peerID p2p.ID) (added bool, err error) {
|
|
|
|
height, round, part := msg.Height, msg.Round, msg.Part
|
|
|
|
|
2014-10-21 23:30:18 -07:00
|
|
|
// Blocks might be reused, so round mismatch is OK
|
|
|
|
if cs.Height != height {
|
2018-06-22 15:08:39 -04:00
|
|
|
cs.Logger.Debug("Received block part from wrong height", "height", height, "round", round)
|
2014-10-21 23:30:18 -07:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're not expecting a block part.
|
2014-10-30 03:32:09 -07:00
|
|
|
if cs.ProposalBlockParts == nil {
|
2018-06-22 15:08:39 -04:00
|
|
|
// NOTE: this can happen when we've gone to a higher round and
|
|
|
|
// then receive parts from the previous round - not necessarily a bad peer.
|
|
|
|
cs.Logger.Info("Received a block part when we're not expecting any",
|
|
|
|
"height", height, "round", round, "index", part.Index, "peer", peerID)
|
|
|
|
return false, nil
|
2014-10-21 23:30:18 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 13:17:50 -04:00
|
|
|
added, err = cs.ProposalBlockParts.AddPart(part)
|
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!
|
2018-10-25 03:34:01 +02:00
|
|
|
_, err = cdc.UnmarshalBinaryLengthPrefixedReader(
|
2018-08-08 16:03:58 +04:00
|
|
|
cs.ProposalBlockParts.GetReader(),
|
|
|
|
&cs.ProposalBlock,
|
2019-03-04 13:24:44 +04:00
|
|
|
int64(cs.state.ConsensusParams.Block.MaxBytes),
|
2018-08-08 16:03:58 +04:00
|
|
|
)
|
2018-04-03 07:03:08 -07:00
|
|
|
if err != nil {
|
2018-09-21 20:36:48 +02:00
|
|
|
return added, err
|
2018-04-03 07:03:08 -07:00
|
|
|
}
|
2015-12-22 23:24:15 -05:00
|
|
|
// NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal
|
2017-05-12 23:07:53 +02:00
|
|
|
cs.Logger.Info("Received complete proposal block", "height", cs.ProposalBlock.Height, "hash", cs.ProposalBlock.Hash())
|
2018-11-15 18:40:42 -05:00
|
|
|
cs.eventBus.PublishEventCompleteProposal(cs.CompleteProposalEvent())
|
2018-04-17 15:43:40 +02:00
|
|
|
|
2018-05-12 17:42:37 -07:00
|
|
|
// Update Valid* if we can.
|
2018-04-17 15:43:40 +02:00
|
|
|
prevotes := cs.Votes.Prevotes(cs.Round)
|
2018-05-12 17:42:37 -07:00
|
|
|
blockID, hasTwoThirds := prevotes.TwoThirdsMajority()
|
|
|
|
if hasTwoThirds && !blockID.IsZero() && (cs.ValidRound < cs.Round) {
|
2018-04-25 16:12:25 +02:00
|
|
|
if cs.ProposalBlock.HashesTo(blockID.Hash) {
|
2018-05-17 13:59:41 -04:00
|
|
|
cs.Logger.Info("Updating valid block to new proposal block",
|
|
|
|
"valid-round", cs.Round, "valid-block-hash", cs.ProposalBlock.Hash())
|
2018-04-17 15:43:40 +02:00
|
|
|
cs.ValidRound = cs.Round
|
|
|
|
cs.ValidBlock = cs.ProposalBlock
|
|
|
|
cs.ValidBlockParts = cs.ProposalBlockParts
|
|
|
|
}
|
2018-05-12 17:42:37 -07:00
|
|
|
// TODO: In case there is +2/3 majority in Prevotes set for some
|
|
|
|
// block and cs.ProposalBlock contains different block, either
|
|
|
|
// proposer is faulty or voting power of faulty processes is more
|
|
|
|
// than 1/3. We should trigger in the future accountability
|
|
|
|
// procedure at this point.
|
2018-04-17 15:43:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 15:08:39 -04:00
|
|
|
if cs.Step <= cstypes.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)
|
2018-10-12 22:13:01 +02:00
|
|
|
if hasTwoThirds { // this is optimisation as this will be triggered when prevote is added
|
|
|
|
cs.enterPrecommit(height, cs.Round)
|
|
|
|
}
|
2017-10-10 12:39:21 +04:00
|
|
|
} else if cs.Step == cstypes.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
|
|
|
}
|
2018-09-21 20:36:48 +02:00
|
|
|
return added, nil
|
2014-10-21 23:30:18 -07:00
|
|
|
}
|
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
|
2018-09-21 20:36:48 +02:00
|
|
|
func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerID p2p.ID) (bool, error) {
|
|
|
|
added, err := cs.addVote(vote, peerID)
|
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,
|
2017-11-19 00:57:55 +00:00
|
|
|
// But if it's a conflicting sig, add it to the cs.evpool.
|
2015-09-09 16:45:53 -04:00
|
|
|
// If it's otherwise invalid, punish peer.
|
2015-08-26 18:56:34 -04:00
|
|
|
if err == ErrVoteHeightMismatch {
|
2018-09-21 20:36:48 +02:00
|
|
|
return added, err
|
2017-07-09 14:10:00 -04:00
|
|
|
} else if voteErr, ok := err.(*types.ErrVoteConflictingVotes); ok {
|
2018-12-22 06:36:45 +01:00
|
|
|
addr := cs.privValidator.GetPubKey().Address()
|
|
|
|
if bytes.Equal(vote.ValidatorAddress, addr) {
|
2017-05-02 11:53:32 +04:00
|
|
|
cs.Logger.Error("Found conflicting vote from ourselves. Did you unsafe_reset a validator?", "height", vote.Height, "round", vote.Round, "type", vote.Type)
|
2018-09-21 20:36:48 +02:00
|
|
|
return added, err
|
2016-02-07 16:56:59 -08:00
|
|
|
}
|
2017-11-02 12:06:48 -06:00
|
|
|
cs.evpool.AddEvidence(voteErr.DuplicateVoteEvidence)
|
2018-09-21 20:36:48 +02:00
|
|
|
return added, err
|
2015-08-12 14:00:23 -04:00
|
|
|
} else {
|
2017-12-21 18:22:02 -05:00
|
|
|
// Probably an invalid signature / Bad peer.
|
|
|
|
// Seems this can also err sometimes with "Unexpected step" - perhaps not from a bad peer ?
|
2017-06-14 12:50:49 +04:00
|
|
|
cs.Logger.Error("Error attempting to add vote", "err", err)
|
2018-09-21 20:36:48 +02:00
|
|
|
return added, ErrAddingVote
|
2015-08-12 14:00:23 -04:00
|
|
|
}
|
|
|
|
}
|
2018-09-21 20:36:48 +02:00
|
|
|
return added, nil
|
2015-08-12 14:00:23 -04:00
|
|
|
}
|
|
|
|
|
2014-11-01 22:42:04 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-01-01 21:27:38 -05:00
|
|
|
func (cs *ConsensusState) addVote(vote *types.Vote, peerID p2p.ID) (added bool, err error) {
|
2017-06-28 11:12:45 -04:00
|
|
|
cs.Logger.Debug("addVote", "voteHeight", vote.Height, "voteType", vote.Type, "valIndex", vote.ValidatorIndex, "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 {
|
2018-10-13 01:21:46 +02:00
|
|
|
if !(cs.Step == cstypes.RoundStepNewHeight && vote.Type == types.PrecommitType) {
|
2015-08-26 18:56:34 -04:00
|
|
|
// 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)
|
2018-03-18 23:07:23 +01:00
|
|
|
if !added {
|
|
|
|
return added, err
|
|
|
|
}
|
|
|
|
|
2018-08-10 00:25:57 -05:00
|
|
|
cs.Logger.Info(fmt.Sprintf("Added to lastPrecommits: %v", cs.LastCommit.StringShort()))
|
2019-02-11 16:31:34 +04:00
|
|
|
cs.eventBus.PublishEventVote(types.EventDataVote{Vote: vote})
|
2018-05-16 10:28:58 +04:00
|
|
|
cs.evsw.FireEvent(types.EventVote, vote)
|
2018-03-18 23:07:23 +01:00
|
|
|
|
|
|
|
// if we can skip timeoutCommit and have all the votes now,
|
|
|
|
if cs.config.SkipTimeoutCommit && cs.LastCommit.HasAll() {
|
|
|
|
// go straight to new round (skip timeout commit)
|
|
|
|
// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
|
|
|
|
cs.enterNewRound(cs.Height, 0)
|
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
|
|
|
}
|
|
|
|
|
2018-03-18 23:07:23 +01:00
|
|
|
// Height mismatch is ignored.
|
|
|
|
// Not necessarily a bad peer, but not favourable behaviour.
|
|
|
|
if vote.Height != cs.Height {
|
|
|
|
err = ErrVoteHeightMismatch
|
2019-01-06 10:00:12 +01:00
|
|
|
cs.Logger.Info("Vote ignored and not added", "voteHeight", vote.Height, "csHeight", cs.Height, "peerID", peerID)
|
2018-03-18 23:07:23 +01:00
|
|
|
return
|
|
|
|
}
|
2018-02-01 10:57:19 +01:00
|
|
|
|
2018-03-18 23:07:23 +01:00
|
|
|
height := cs.Height
|
|
|
|
added, err = cs.Votes.AddVote(vote, peerID)
|
|
|
|
if !added {
|
|
|
|
// Either duplicate, or error upon cs.Votes.AddByIndex()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:31:34 +04:00
|
|
|
cs.eventBus.PublishEventVote(types.EventDataVote{Vote: vote})
|
2018-05-16 10:28:58 +04:00
|
|
|
cs.evsw.FireEvent(types.EventVote, vote)
|
2018-03-18 23:07:23 +01:00
|
|
|
|
|
|
|
switch vote.Type {
|
2018-10-13 01:21:46 +02:00
|
|
|
case types.PrevoteType:
|
2018-03-18 23:07:23 +01:00
|
|
|
prevotes := cs.Votes.Prevotes(vote.Round)
|
|
|
|
cs.Logger.Info("Added to prevote", "vote", vote, "prevotes", prevotes.StringShort())
|
2018-05-12 17:36:05 -07:00
|
|
|
|
|
|
|
// If +2/3 prevotes for a block or nil for *any* round:
|
|
|
|
if blockID, ok := prevotes.TwoThirdsMajority(); ok {
|
|
|
|
|
2018-05-14 16:32:19 -04:00
|
|
|
// There was a polka!
|
|
|
|
// If we're locked but this is a recent polka, unlock.
|
|
|
|
// If it matches our ProposalBlock, update the ValidBlock
|
|
|
|
|
|
|
|
// Unlock if `cs.LockedRound < vote.Round <= cs.Round`
|
|
|
|
// NOTE: If vote.Round > cs.Round, we'll deal with it when we get to vote.Round
|
2018-05-12 17:36:05 -07:00
|
|
|
if (cs.LockedBlock != nil) &&
|
|
|
|
(cs.LockedRound < vote.Round) &&
|
|
|
|
(vote.Round <= cs.Round) &&
|
|
|
|
!cs.LockedBlock.HashesTo(blockID.Hash) {
|
|
|
|
|
2018-03-18 23:07:23 +01:00
|
|
|
cs.Logger.Info("Unlocking because of POL.", "lockedRound", cs.LockedRound, "POLRound", vote.Round)
|
2018-10-15 22:05:13 +02:00
|
|
|
cs.LockedRound = -1
|
2018-03-18 23:07:23 +01:00
|
|
|
cs.LockedBlock = nil
|
|
|
|
cs.LockedBlockParts = nil
|
|
|
|
cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
|
|
|
|
}
|
2018-05-12 17:36:05 -07:00
|
|
|
|
|
|
|
// Update Valid* if we can.
|
2018-05-14 16:32:19 -04:00
|
|
|
// NOTE: our proposal block may be nil or not what received a polka..
|
2018-10-31 14:20:36 +01:00
|
|
|
if len(blockID.Hash) != 0 && (cs.ValidRound < vote.Round) && (vote.Round == cs.Round) {
|
|
|
|
|
|
|
|
if cs.ProposalBlock.HashesTo(blockID.Hash) {
|
|
|
|
cs.Logger.Info(
|
|
|
|
"Updating ValidBlock because of POL.", "validRound", cs.ValidRound, "POLRound", vote.Round)
|
|
|
|
cs.ValidRound = vote.Round
|
|
|
|
cs.ValidBlock = cs.ProposalBlock
|
|
|
|
cs.ValidBlockParts = cs.ProposalBlockParts
|
|
|
|
} else {
|
|
|
|
cs.Logger.Info(
|
|
|
|
"Valid block we don't know about. Set ProposalBlock=nil",
|
|
|
|
"proposal", cs.ProposalBlock.Hash(), "blockId", blockID.Hash)
|
|
|
|
// We're getting the wrong block.
|
|
|
|
cs.ProposalBlock = nil
|
|
|
|
}
|
|
|
|
if !cs.ProposalBlockParts.HasHeader(blockID.PartsHeader) {
|
|
|
|
cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
|
|
|
|
}
|
|
|
|
cs.evsw.FireEvent(types.EventValidBlock, &cs.RoundState)
|
|
|
|
cs.eventBus.PublishEventValidBlock(cs.RoundStateEvent())
|
2018-03-18 23:07:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 22:13:01 +02:00
|
|
|
// If +2/3 prevotes for *anything* for future round:
|
|
|
|
if cs.Round < vote.Round && prevotes.HasTwoThirdsAny() {
|
|
|
|
// Round-skip if there is any 2/3+ of votes ahead of us
|
|
|
|
cs.enterNewRound(height, vote.Round)
|
|
|
|
} else if cs.Round == vote.Round && cstypes.RoundStepPrevote <= cs.Step { // current round
|
2018-10-31 14:20:36 +01:00
|
|
|
blockID, ok := prevotes.TwoThirdsMajority()
|
|
|
|
if ok && (cs.isProposalComplete() || len(blockID.Hash) == 0) {
|
2018-03-18 23:07:23 +01:00
|
|
|
cs.enterPrecommit(height, vote.Round)
|
2018-10-12 22:13:01 +02:00
|
|
|
} else if prevotes.HasTwoThirdsAny() {
|
2018-03-18 23:07:23 +01:00
|
|
|
cs.enterPrevoteWait(height, vote.Round)
|
|
|
|
}
|
|
|
|
} else if cs.Proposal != nil && 0 <= cs.Proposal.POLRound && cs.Proposal.POLRound == vote.Round {
|
|
|
|
// If the proposal is now complete, enter prevote of cs.Round.
|
|
|
|
if cs.isProposalComplete() {
|
|
|
|
cs.enterPrevote(height, cs.Round)
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 17:36:05 -07:00
|
|
|
|
2018-10-13 01:21:46 +02:00
|
|
|
case types.PrecommitType:
|
2018-03-18 23:07:23 +01:00
|
|
|
precommits := cs.Votes.Precommits(vote.Round)
|
|
|
|
cs.Logger.Info("Added to precommit", "vote", vote, "precommits", precommits.StringShort())
|
2018-10-12 22:13:01 +02:00
|
|
|
|
2018-03-18 23:07:23 +01:00
|
|
|
blockID, ok := precommits.TwoThirdsMajority()
|
2018-10-12 22:13:01 +02:00
|
|
|
if ok {
|
2018-10-04 15:37:13 +02:00
|
|
|
// Executed as TwoThirdsMajority could be from a higher round
|
|
|
|
cs.enterNewRound(height, vote.Round)
|
|
|
|
cs.enterPrecommit(height, vote.Round)
|
2018-10-12 22:13:01 +02:00
|
|
|
if len(blockID.Hash) != 0 {
|
|
|
|
cs.enterCommit(height, vote.Round)
|
|
|
|
if cs.config.SkipTimeoutCommit && precommits.HasAll() {
|
|
|
|
cs.enterNewRound(cs.Height, 0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cs.enterPrecommitWait(height, vote.Round)
|
2014-11-01 22:42:04 -07:00
|
|
|
}
|
2018-03-18 23:07:23 +01:00
|
|
|
} else if cs.Round <= vote.Round && precommits.HasTwoThirdsAny() {
|
|
|
|
cs.enterNewRound(height, vote.Round)
|
|
|
|
cs.enterPrecommitWait(height, vote.Round)
|
2014-10-30 03:32:09 -07:00
|
|
|
}
|
2018-10-12 22:13:01 +02:00
|
|
|
|
2018-03-18 23:07:23 +01:00
|
|
|
default:
|
2018-08-10 00:25:57 -05:00
|
|
|
panic(fmt.Sprintf("Unexpected vote type %X", vote.Type)) // go-wire should prevent this.
|
2014-10-21 23:30:18 -07:00
|
|
|
}
|
2015-06-05 14:15:40 -07:00
|
|
|
|
|
|
|
return
|
2014-10-21 23:30:18 -07:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:21:46 +02:00
|
|
|
func (cs *ConsensusState) signVote(type_ types.SignedMsgType, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
|
2019-02-20 07:45:18 +02:00
|
|
|
// Flush the WAL. Otherwise, we may not recompute the same vote to sign, and the privValidator will refuse to sign anything.
|
2019-02-25 09:11:07 +04:00
|
|
|
cs.wal.FlushAndSync()
|
2019-02-20 07:45:18 +02:00
|
|
|
|
2018-12-22 06:36:45 +01:00
|
|
|
addr := cs.privValidator.GetPubKey().Address()
|
2016-12-02 00:12:06 -05:00
|
|
|
valIndex, _ := cs.Validators.GetByAddress(addr)
|
2018-09-01 01:33:51 +02:00
|
|
|
|
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,
|
2018-09-01 01:33:51 +02:00
|
|
|
Timestamp: cs.voteTime(),
|
2015-08-12 22:36:43 -07:00
|
|
|
Type: type_,
|
2019-02-11 16:31:34 +04:00
|
|
|
BlockID: types.BlockID{Hash: hash, PartsHeader: header},
|
2014-10-24 14:37:12 -07:00
|
|
|
}
|
2017-10-12 14:50:09 +04:00
|
|
|
err := cs.privValidator.SignVote(cs.state.ChainID, vote)
|
2015-08-12 14:00:23 -04:00
|
|
|
return vote, err
|
|
|
|
}
|
|
|
|
|
2018-09-01 01:33:51 +02:00
|
|
|
func (cs *ConsensusState) voteTime() time.Time {
|
|
|
|
now := tmtime.Now()
|
|
|
|
minVoteTime := now
|
|
|
|
// TODO: We should remove next line in case we don't vote for v in case cs.ProposalBlock == nil,
|
|
|
|
// even if cs.LockedBlock != nil. See https://github.com/tendermint/spec.
|
2019-03-04 13:24:44 +04:00
|
|
|
timeIotaMs := time.Duration(cs.state.ConsensusParams.Block.TimeIotaMs) * time.Millisecond
|
2018-09-01 01:33:51 +02:00
|
|
|
if cs.LockedBlock != nil {
|
2019-03-04 13:24:44 +04:00
|
|
|
// See the BFT time spec https://tendermint.com/docs/spec/consensus/bft-time.html
|
|
|
|
minVoteTime = cs.LockedBlock.Time.Add(timeIotaMs)
|
2018-09-01 01:33:51 +02:00
|
|
|
} else if cs.ProposalBlock != nil {
|
2019-03-04 13:24:44 +04:00
|
|
|
minVoteTime = cs.ProposalBlock.Time.Add(timeIotaMs)
|
2018-09-01 01:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if now.After(minVoteTime) {
|
|
|
|
return now
|
|
|
|
}
|
|
|
|
return minVoteTime
|
|
|
|
}
|
|
|
|
|
2016-09-08 18:06:25 -04:00
|
|
|
// sign the vote and publish on internalMsgQueue
|
2018-10-13 01:21:46 +02:00
|
|
|
func (cs *ConsensusState) signAddVote(type_ types.SignedMsgType, 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
|
2018-12-22 06:36:45 +01:00
|
|
|
if cs.privValidator == nil || !cs.Validators.HasAddress(cs.privValidator.GetPubKey().Address()) {
|
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}, ""})
|
2017-06-14 12:50:49 +04:00
|
|
|
cs.Logger.Info("Signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
|
2014-12-31 16:14:26 -08:00
|
|
|
return vote
|
|
|
|
}
|
2018-04-02 10:21:17 +02:00
|
|
|
//if !cs.replayMode {
|
|
|
|
cs.Logger.Error("Error signing vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
|
|
|
|
//}
|
|
|
|
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
|
|
|
|
2017-12-01 19:04:53 -06:00
|
|
|
func CompareHRS(h1 int64, r1 int, s1 cstypes.RoundStepType, h2 int64, r2 int, s2 cstypes.RoundStepType) int {
|
2015-12-01 20:12:01 -08:00
|
|
|
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
|
|
|
|
}
|