2015-03-28 23:44:07 -07:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-01-19 01:33:58 -08:00
|
|
|
"fmt"
|
2015-03-28 23:44:07 -07:00
|
|
|
|
2017-01-19 01:33:58 -08:00
|
|
|
fail "github.com/ebuchman/fail-test"
|
2017-02-14 12:33:14 -08:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-01-19 01:33:58 -08:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
2015-12-01 20:12:01 -08:00
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2017-04-18 16:29:02 -07:00
|
|
|
"github.com/tendermint/tendermint/state/txindex"
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2017-04-25 11:50:20 -07:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2017-05-02 00:53:32 -07:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
2015-03-28 23:44:07 -07:00
|
|
|
)
|
|
|
|
|
2017-04-14 12:33:19 -07:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Execute the block
|
|
|
|
|
2017-04-17 15:24:44 -07:00
|
|
|
// ValExecBlock executes the block, but does NOT mutate State.
|
2017-01-19 01:33:58 -08:00
|
|
|
// + validates the block
|
|
|
|
// + executes block.Txs on the proxyAppConn
|
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 08:00:30 -07:00
|
|
|
func (s *State) ValExecBlock(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus, block *types.Block) (*ABCIResponses, error) {
|
2015-12-01 20:12:01 -08:00
|
|
|
// Validate the block.
|
2016-11-19 16:32:35 -08:00
|
|
|
if err := s.validateBlock(block); err != nil {
|
2017-01-19 01:33:58 -08:00
|
|
|
return nil, ErrInvalidBlock(err)
|
2015-03-28 23:44:07 -07:00
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-01-06 17:14:20 -08:00
|
|
|
// Execute the block txs
|
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 08:00:30 -07:00
|
|
|
abciResponses, err := execBlockOnProxyApp(txEventPublisher, proxyAppConn, block, s.logger)
|
2015-12-01 20:12:01 -08:00
|
|
|
if err != nil {
|
2016-01-06 17:14:20 -08:00
|
|
|
// There was some error in proxyApp
|
|
|
|
// TODO Report error and wait for proxyApp to be available.
|
2017-01-19 01:33:58 -08:00
|
|
|
return nil, ErrProxyAppConn(err)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2017-04-14 12:33:19 -07:00
|
|
|
return abciResponses, nil
|
2015-03-28 23:44:07 -07:00
|
|
|
}
|
|
|
|
|
2016-01-06 17:14:20 -08:00
|
|
|
// Executes block's transactions on proxyAppConn.
|
2017-01-19 01:33:58 -08:00
|
|
|
// Returns a list of transaction results and updates to the validator set
|
2016-01-06 17:14:20 -08:00
|
|
|
// TODO: Generate a bitmap or otherwise store tx validity in state.
|
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 08:00:30 -07:00
|
|
|
func execBlockOnProxyApp(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus, block *types.Block, logger log.Logger) (*ABCIResponses, error) {
|
2016-01-06 17:14:20 -08:00
|
|
|
var validTxs, invalidTxs = 0, 0
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-04-10 11:41:07 -07:00
|
|
|
txIndex := 0
|
2017-04-14 12:33:19 -07:00
|
|
|
abciResponses := NewABCIResponses(block)
|
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
// Execute transactions and get hash
|
2017-01-12 12:53:32 -08:00
|
|
|
proxyCb := func(req *abci.Request, res *abci.Response) {
|
2016-05-14 09:33:27 -07:00
|
|
|
switch r := res.Value.(type) {
|
2017-01-12 12:55:03 -08:00
|
|
|
case *abci.Response_DeliverTx:
|
2016-01-25 14:34:08 -08:00
|
|
|
// TODO: make use of res.Log
|
2016-01-06 17:14:20 -08:00
|
|
|
// TODO: make use of this info
|
|
|
|
// Blocks may include invalid txs.
|
2017-01-12 12:55:03 -08:00
|
|
|
// reqDeliverTx := req.(abci.RequestDeliverTx)
|
2016-10-01 13:12:48 -07:00
|
|
|
txError := ""
|
2017-01-19 01:33:58 -08:00
|
|
|
txResult := r.DeliverTx
|
|
|
|
if txResult.Code == abci.CodeType_OK {
|
|
|
|
validTxs++
|
2016-01-06 17:14:20 -08:00
|
|
|
} else {
|
2017-05-02 00:53:32 -07:00
|
|
|
logger.Debug("Invalid tx", "code", txResult.Code, "log", txResult.Log)
|
2017-01-19 01:33:58 -08:00
|
|
|
invalidTxs++
|
|
|
|
txError = txResult.Code.String()
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:21:50 -07:00
|
|
|
abciResponses.DeliverTx[txIndex] = txResult
|
2017-04-10 11:41:07 -07:00
|
|
|
txIndex++
|
2017-01-19 01:33:58 -08:00
|
|
|
|
2016-06-27 17:43:09 -07:00
|
|
|
// NOTE: if we count we can access the tx from the block instead of
|
|
|
|
// pulling it from the req
|
2016-10-01 13:12:48 -07:00
|
|
|
event := types.EventDataTx{
|
2017-04-12 15:33:48 -07:00
|
|
|
Height: block.Height,
|
|
|
|
Tx: types.Tx(req.GetDeliverTx().Tx),
|
|
|
|
Data: txResult.Data,
|
|
|
|
Code: txResult.Code,
|
|
|
|
Log: txResult.Log,
|
|
|
|
Error: txError,
|
2016-10-01 13:12:48 -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 08:00:30 -07:00
|
|
|
txEventPublisher.PublishEventTx(event)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 17:14:20 -08:00
|
|
|
proxyAppConn.SetResponseCallback(proxyCb)
|
|
|
|
|
2016-11-03 16:51:22 -07:00
|
|
|
// Begin block
|
2017-09-22 08:42:40 -07:00
|
|
|
err := proxyAppConn.BeginBlockSync(abci.RequestBeginBlock{
|
|
|
|
block.Hash(),
|
|
|
|
types.TM2PB.Header(block.Header),
|
|
|
|
})
|
2016-11-03 16:51:22 -07:00
|
|
|
if err != nil {
|
2017-06-14 01:50:49 -07:00
|
|
|
logger.Error("Error in proxyAppConn.BeginBlock", "err", err)
|
2017-04-14 12:33:19 -07:00
|
|
|
return nil, err
|
2016-11-03 16:51:22 -07:00
|
|
|
}
|
2016-08-06 20:30:46 -07:00
|
|
|
|
2016-03-05 20:57:36 -08:00
|
|
|
// Run txs of block
|
2016-01-06 17:14:20 -08:00
|
|
|
for _, tx := range block.Txs {
|
2017-01-12 12:55:03 -08:00
|
|
|
proxyAppConn.DeliverTxAsync(tx)
|
2016-01-06 17:14:20 -08:00
|
|
|
if err := proxyAppConn.Error(); err != nil {
|
2017-04-14 12:33:19 -07:00
|
|
|
return nil, err
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 20:57:36 -08:00
|
|
|
|
|
|
|
// End block
|
2017-04-14 17:21:50 -07:00
|
|
|
abciResponses.EndBlock, err = proxyAppConn.EndBlockSync(uint64(block.Height))
|
2016-03-05 20:57:36 -08:00
|
|
|
if err != nil {
|
2017-06-14 01:50:49 -07:00
|
|
|
logger.Error("Error in proxyAppConn.EndBlock", "err", err)
|
2017-04-14 12:33:19 -07:00
|
|
|
return nil, err
|
2016-03-05 20:57:36 -08:00
|
|
|
}
|
2016-09-11 12:32:33 -07:00
|
|
|
|
2017-04-14 17:21:50 -07:00
|
|
|
valDiff := abciResponses.EndBlock.Diffs
|
|
|
|
|
2017-06-13 23:41:36 -07:00
|
|
|
logger.Info("Executed block", "height", block.Height, "validTxs", validTxs, "invalidTxs", invalidTxs)
|
2017-04-14 17:21:50 -07:00
|
|
|
if len(valDiff) > 0 {
|
2017-05-02 00:53:32 -07:00
|
|
|
logger.Info("Update to validator set", "updates", abci.ValidatorsString(valDiff))
|
2016-11-22 15:55:42 -08:00
|
|
|
}
|
2017-04-14 12:33:19 -07:00
|
|
|
|
|
|
|
return abciResponses, nil
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
|
2017-01-12 12:53:32 -08:00
|
|
|
func updateValidators(validators *types.ValidatorSet, changedValidators []*abci.Validator) error {
|
2016-11-19 16:32:35 -08:00
|
|
|
// TODO: prevent change of 1/3+ at once
|
|
|
|
|
|
|
|
for _, v := range changedValidators {
|
|
|
|
pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
|
|
|
|
if err != nil {
|
2016-12-01 21:12:06 -08:00
|
|
|
return err
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
address := pubkey.Address()
|
|
|
|
power := int64(v.Power)
|
|
|
|
// mind the overflow from uint64
|
|
|
|
if power < 0 {
|
2017-04-25 11:50:20 -07:00
|
|
|
return errors.New(cmn.Fmt("Power (%d) overflows int64", v.Power))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_, val := validators.GetByAddress(address)
|
|
|
|
if val == nil {
|
|
|
|
// add val
|
|
|
|
added := validators.Add(types.NewValidator(pubkey, power))
|
|
|
|
if !added {
|
2017-04-25 11:50:20 -07:00
|
|
|
return errors.New(cmn.Fmt("Failed to add new validator %X with voting power %d", address, power))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
} else if v.Power == 0 {
|
|
|
|
// remove val
|
|
|
|
_, removed := validators.Remove(address)
|
|
|
|
if !removed {
|
2017-04-25 11:50:20 -07:00
|
|
|
return errors.New(cmn.Fmt("Failed to remove validator %X)"))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// update val
|
|
|
|
val.VotingPower = power
|
|
|
|
updated := validators.Update(val)
|
|
|
|
if !updated {
|
2017-04-25 11:50:20 -07:00
|
|
|
return errors.New(cmn.Fmt("Failed to update validator %X with voting power %d", address, power))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 21:12:06 -08:00
|
|
|
return nil
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2017-09-21 05:49:19 -07:00
|
|
|
// return a bit array of validators that signed the last commit
|
|
|
|
// NOTE: assumes commits have already been authenticated
|
2017-09-21 10:54:34 -07:00
|
|
|
/* function is currently unused
|
2017-09-21 05:49:19 -07:00
|
|
|
func commitBitArrayFromBlock(block *types.Block) *cmn.BitArray {
|
|
|
|
signed := cmn.NewBitArray(len(block.LastCommit.Precommits))
|
|
|
|
for i, precommit := range block.LastCommit.Precommits {
|
|
|
|
if precommit != nil {
|
|
|
|
signed.SetIndex(i, true) // val_.LastCommitHeight = block.Height - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return signed
|
|
|
|
}
|
2017-09-21 10:54:34 -07:00
|
|
|
*/
|
2017-09-21 05:49:19 -07:00
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
//-----------------------------------------------------
|
|
|
|
// Validate block
|
2015-08-10 20:38:45 -07:00
|
|
|
|
2017-08-21 13:12:07 -07:00
|
|
|
// ValidateBlock validates the block against the state.
|
2016-09-11 10:16:23 -07:00
|
|
|
func (s *State) ValidateBlock(block *types.Block) error {
|
|
|
|
return s.validateBlock(block)
|
2015-08-10 20:38:45 -07:00
|
|
|
}
|
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
func (s *State) validateBlock(block *types.Block) error {
|
|
|
|
// Basic block validation.
|
|
|
|
err := block.ValidateBasic(s.ChainID, s.LastBlockHeight, s.LastBlockID, s.LastBlockTime, s.AppHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate block LastCommit.
|
|
|
|
if block.Height == 1 {
|
|
|
|
if len(block.LastCommit.Precommits) != 0 {
|
|
|
|
return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(block.LastCommit.Precommits) != s.LastValidators.Size() {
|
2017-04-25 11:50:20 -07:00
|
|
|
return errors.New(cmn.Fmt("Invalid block commit size. Expected %v, got %v",
|
2016-09-11 10:16:23 -07:00
|
|
|
s.LastValidators.Size(), len(block.LastCommit.Precommits)))
|
|
|
|
}
|
|
|
|
err := s.LastValidators.VerifyCommit(
|
|
|
|
s.ChainID, s.LastBlockID, block.Height-1, block.LastCommit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2016-08-23 18:44:07 -07:00
|
|
|
|
2017-04-14 12:33:19 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-04-17 15:24:44 -07:00
|
|
|
// ApplyBlock validates & executes the block, updates state w/ ABCI responses,
|
2017-04-14 12:33:19 -07:00
|
|
|
// then commits and updates the mempool atomically, then saves state.
|
|
|
|
// Transaction results are optionally indexed.
|
|
|
|
|
2017-08-21 13:12:07 -07:00
|
|
|
// ApplyBlock validates the block against the state, executes it against the app,
|
|
|
|
// commits it, and saves the block and state. It's the only function that needs to be called
|
|
|
|
// from outside this package to process and commit an entire block.
|
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 08:00:30 -07:00
|
|
|
func (s *State) ApplyBlock(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus,
|
2017-02-20 17:09:15 -08:00
|
|
|
block *types.Block, partsHeader types.PartSetHeader, mempool types.Mempool) error {
|
2016-09-11 10:16:23 -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 08:00:30 -07:00
|
|
|
abciResponses, err := s.ValExecBlock(txEventPublisher, proxyAppConn, block)
|
2016-09-11 10:16:23 -07:00
|
|
|
if err != nil {
|
2017-01-19 01:33:58 -08:00
|
|
|
return fmt.Errorf("Exec failed for application: %v", err)
|
2016-09-11 10:16:23 -07:00
|
|
|
}
|
|
|
|
|
2017-04-14 12:33:19 -07:00
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2017-04-14 17:30:15 -07:00
|
|
|
// index txs. This could run in the background
|
|
|
|
s.indexTxs(abciResponses)
|
|
|
|
|
2017-04-14 12:33:19 -07:00
|
|
|
// save the results before we commit
|
|
|
|
s.SaveABCIResponses(abciResponses)
|
|
|
|
|
|
|
|
fail.Fail() // XXX
|
|
|
|
|
|
|
|
// now update the block and validators
|
2017-04-14 17:21:50 -07:00
|
|
|
s.SetBlockAndValidators(block.Header, partsHeader, abciResponses)
|
2017-04-14 12:33:19 -07:00
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
// lock mempool, commit state, update mempoool
|
|
|
|
err = s.CommitStateUpdateMempool(proxyAppConn, block, mempool)
|
|
|
|
if err != nil {
|
2017-01-19 01:33:58 -08:00
|
|
|
return fmt.Errorf("Commit failed for application: %v", err)
|
2016-09-11 10:16:23 -07:00
|
|
|
}
|
2017-01-19 01:33:58 -08:00
|
|
|
|
2017-04-14 12:33:19 -07:00
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2017-09-04 15:27:04 -07:00
|
|
|
// save the state and the validators
|
2017-04-14 12:33:19 -07:00
|
|
|
s.Save()
|
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-23 18:44:07 -07:00
|
|
|
|
2017-08-21 13:12:07 -07:00
|
|
|
// CommitStateUpdateMempool locks the mempool, runs the ABCI Commit message, and updates the mempool.
|
|
|
|
// The Mempool must be locked during commit and update because state is typically reset on Commit and old txs must be replayed
|
|
|
|
// against committed state before new txs are run in the mempool, lest they be invalid.
|
2017-02-20 17:09:15 -08:00
|
|
|
func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, block *types.Block, mempool types.Mempool) error {
|
2016-08-24 21:18:03 -07:00
|
|
|
mempool.Lock()
|
|
|
|
defer mempool.Unlock()
|
|
|
|
|
|
|
|
// Commit block, get hash back
|
|
|
|
res := proxyAppConn.CommitSync()
|
|
|
|
if res.IsErr() {
|
2017-06-14 01:50:49 -07:00
|
|
|
s.logger.Error("Error in proxyAppConn.CommitSync", "err", res)
|
2016-08-24 21:18:03 -07:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res.Log != "" {
|
2017-05-02 00:53:32 -07:00
|
|
|
s.logger.Debug("Commit.Log: " + res.Log)
|
2016-08-24 21:18:03 -07:00
|
|
|
}
|
|
|
|
|
2017-05-26 08:57:41 -07:00
|
|
|
s.logger.Info("Committed state", "height", block.Height, "txs", block.NumTxs, "hash", res.Data)
|
2017-05-22 05:16:25 -07:00
|
|
|
|
2016-08-24 21:18:03 -07:00
|
|
|
// Set the state's new AppHash
|
|
|
|
s.AppHash = res.Data
|
|
|
|
|
|
|
|
// Update mempool.
|
2017-09-21 08:42:44 -07:00
|
|
|
return mempool.Update(block.Height, block.Txs)
|
2016-08-24 21:18:03 -07:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:30:15 -07:00
|
|
|
func (s *State) indexTxs(abciResponses *ABCIResponses) {
|
|
|
|
// save the tx results using the TxIndexer
|
|
|
|
// NOTE: these may be overwriting, but the values should be the same.
|
2017-04-18 18:35:00 -07:00
|
|
|
batch := txindex.NewBatch(len(abciResponses.DeliverTx))
|
2017-04-14 17:30:15 -07:00
|
|
|
for i, d := range abciResponses.DeliverTx {
|
|
|
|
tx := abciResponses.txs[i]
|
2017-09-06 08:50:43 -07:00
|
|
|
if err := batch.Add(types.TxResult{
|
2017-04-18 18:35:00 -07:00
|
|
|
Height: uint64(abciResponses.Height),
|
|
|
|
Index: uint32(i),
|
|
|
|
Tx: tx,
|
|
|
|
Result: *d,
|
2017-09-06 08:50:43 -07:00
|
|
|
}); err != nil {
|
2017-09-21 06:55:06 -07:00
|
|
|
s.logger.Error("Error with batch.Add", "err", err)
|
2017-09-06 08:50:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := s.TxIndexer.AddBatch(batch); err != nil {
|
2017-09-21 06:55:06 -07:00
|
|
|
s.logger.Error("Error adding batch", "err", err)
|
2017-04-14 17:30:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 13:12:07 -07:00
|
|
|
// ExecCommitBlock executes and commits a block on the proxyApp without validating or mutating the state.
|
|
|
|
// It returns the application root hash (result of abci.Commit).
|
2017-05-02 00:53:32 -07:00
|
|
|
func ExecCommitBlock(appConnConsensus proxy.AppConnConsensus, block *types.Block, logger log.Logger) ([]byte, error) {
|
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 08:00:30 -07:00
|
|
|
_, err := execBlockOnProxyApp(types.NopEventBus{}, appConnConsensus, block, logger)
|
2017-02-17 08:32:56 -08:00
|
|
|
if err != nil {
|
2017-05-02 00:53:32 -07:00
|
|
|
logger.Error("Error executing block on proxy app", "height", block.Height, "err", err)
|
2017-02-17 08:32:56 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Commit block, get hash back
|
|
|
|
res := appConnConsensus.CommitSync()
|
|
|
|
if res.IsErr() {
|
2017-06-14 01:50:49 -07:00
|
|
|
logger.Error("Error in proxyAppConn.CommitSync", "err", res)
|
2017-02-17 08:32:56 -08:00
|
|
|
return nil, res
|
|
|
|
}
|
|
|
|
if res.Log != "" {
|
2017-05-02 00:53:32 -07:00
|
|
|
logger.Info("Commit.Log: " + res.Log)
|
2017-02-17 08:32:56 -08:00
|
|
|
}
|
|
|
|
return res.Data, nil
|
|
|
|
}
|