2016-04-11 15:08:28 -07:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2017-11-22 16:55:09 -08:00
|
|
|
"fmt"
|
2016-04-11 15:08:28 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-11-27 13:48:15 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2017-12-01 22:47:55 -08:00
|
|
|
"github.com/tendermint/abci/example/code"
|
2017-01-28 08:27:13 -08:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-10-04 13:40:45 -07:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2017-12-01 22:47:55 -08:00
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/types"
|
2016-04-11 15:08:28 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-05-01 21:43:49 -07:00
|
|
|
config = ResetConfig("consensus_mempool_test")
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
|
2018-01-18 17:38:19 -08:00
|
|
|
func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
|
2017-07-13 12:03:19 -07:00
|
|
|
config := ResetConfig("consensus_mempool_txs_available_test")
|
2017-08-04 18:46:17 -07:00
|
|
|
config.Consensus.CreateEmptyBlocks = false
|
2016-04-11 15:08:28 -07:00
|
|
|
state, privVals := randGenesisState(1, false, 10)
|
2017-07-13 12:03:19 -07:00
|
|
|
cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
|
2017-07-25 10:57:11 -07:00
|
|
|
cs.mempool.EnableTxsAvailable()
|
2016-04-11 15:08:28 -07:00
|
|
|
height, round := cs.Height, cs.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 08:00:30 -07:00
|
|
|
newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
|
2017-07-13 12:03:19 -07:00
|
|
|
startTestRound(cs, height, round)
|
2016-04-11 15:08:28 -07:00
|
|
|
|
2017-07-20 11:43:16 -07:00
|
|
|
ensureNewStep(newBlockCh) // first block gets committed
|
|
|
|
ensureNoNewStep(newBlockCh)
|
2017-11-09 15:54:02 -08:00
|
|
|
deliverTxsRange(cs, 0, 1)
|
2017-07-20 11:43:16 -07:00
|
|
|
ensureNewStep(newBlockCh) // commit txs
|
|
|
|
ensureNewStep(newBlockCh) // commit updated app hash
|
2017-07-13 12:03:19 -07:00
|
|
|
ensureNoNewStep(newBlockCh)
|
2017-07-28 20:39:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-18 17:38:19 -08:00
|
|
|
func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
|
2017-08-08 13:35:25 -07:00
|
|
|
config := ResetConfig("consensus_mempool_txs_available_test")
|
|
|
|
config.Consensus.CreateEmptyBlocksInterval = int(ensureTimeout.Seconds())
|
|
|
|
state, privVals := randGenesisState(1, false, 10)
|
|
|
|
cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
|
|
|
|
cs.mempool.EnableTxsAvailable()
|
|
|
|
height, round := cs.Height, cs.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 08:00:30 -07:00
|
|
|
newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
|
2017-08-08 13:35:25 -07:00
|
|
|
startTestRound(cs, height, round)
|
|
|
|
|
|
|
|
ensureNewStep(newBlockCh) // first block gets committed
|
|
|
|
ensureNoNewStep(newBlockCh) // then we dont make a block ...
|
|
|
|
ensureNewStep(newBlockCh) // until the CreateEmptyBlocksInterval has passed
|
|
|
|
}
|
|
|
|
|
2018-01-18 17:38:19 -08:00
|
|
|
func TestMempoolProgressInHigherRound(t *testing.T) {
|
2017-07-28 20:39:10 -07:00
|
|
|
config := ResetConfig("consensus_mempool_txs_available_test")
|
2017-08-04 18:46:17 -07:00
|
|
|
config.Consensus.CreateEmptyBlocks = false
|
2017-07-28 20:39:10 -07:00
|
|
|
state, privVals := randGenesisState(1, false, 10)
|
|
|
|
cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
|
|
|
|
cs.mempool.EnableTxsAvailable()
|
|
|
|
height, round := cs.Height, cs.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 08:00:30 -07:00
|
|
|
newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
|
|
|
|
newRoundCh := subscribe(cs.eventBus, types.EventQueryNewRound)
|
|
|
|
timeoutCh := subscribe(cs.eventBus, types.EventQueryTimeoutPropose)
|
2017-07-28 20:39:10 -07:00
|
|
|
cs.setProposal = func(proposal *types.Proposal) error {
|
|
|
|
if cs.Height == 2 && cs.Round == 0 {
|
|
|
|
// dont set the proposal in round 0 so we timeout and
|
|
|
|
// go to next round
|
|
|
|
cs.Logger.Info("Ignoring set proposal at height 2, round 0")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cs.defaultSetProposal(proposal)
|
|
|
|
}
|
|
|
|
startTestRound(cs, height, round)
|
|
|
|
|
|
|
|
ensureNewStep(newRoundCh) // first round at first height
|
|
|
|
ensureNewStep(newBlockCh) // first block gets committed
|
|
|
|
ensureNewStep(newRoundCh) // first round at next height
|
2017-11-09 15:54:02 -08:00
|
|
|
deliverTxsRange(cs, 0, 1) // we deliver txs, but dont set a proposal so we get the next round
|
2017-07-28 20:39:10 -07:00
|
|
|
<-timeoutCh
|
|
|
|
ensureNewStep(newRoundCh) // wait for the next round
|
|
|
|
ensureNewStep(newBlockCh) // now we can commit the block
|
2017-07-13 12:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func deliverTxsRange(cs *ConsensusState, start, end int) {
|
|
|
|
// Deliver some txs.
|
|
|
|
for i := start; i < end; i++ {
|
|
|
|
txBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(txBytes, uint64(i))
|
|
|
|
err := cs.mempool.CheckTx(txBytes, nil)
|
|
|
|
if err != nil {
|
2017-10-04 13:40:45 -07:00
|
|
|
panic(cmn.Fmt("Error after CheckTx: %v", err))
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 12:03:19 -07:00
|
|
|
}
|
|
|
|
|
2018-01-18 17:38:19 -08:00
|
|
|
func TestMempoolTxConcurrentWithCommit(t *testing.T) {
|
2017-07-13 12:03:19 -07:00
|
|
|
state, privVals := randGenesisState(1, false, 10)
|
|
|
|
cs := newConsensusState(state, privVals[0], NewCounterApplication())
|
|
|
|
height, round := cs.Height, cs.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 08:00:30 -07:00
|
|
|
newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
|
2016-04-11 15:08:28 -07:00
|
|
|
|
|
|
|
NTxs := 10000
|
2017-07-13 12:03:19 -07:00
|
|
|
go deliverTxsRange(cs, 0, NTxs)
|
2016-04-11 15:08:28 -07:00
|
|
|
|
|
|
|
startTestRound(cs, height, round)
|
|
|
|
for nTxs := 0; nTxs < NTxs; {
|
2017-12-16 16:16:08 -08:00
|
|
|
ticker := time.NewTicker(time.Second * 30)
|
2016-04-11 15:08:28 -07:00
|
|
|
select {
|
|
|
|
case b := <-newBlockCh:
|
2017-12-12 04:16:03 -08:00
|
|
|
evt := b.(types.TMEventData).Unwrap().(types.EventDataNewBlock)
|
|
|
|
nTxs += int(evt.Block.Header.NumTxs)
|
2016-04-11 15:08:28 -07:00
|
|
|
case <-ticker.C:
|
2016-07-11 18:10:05 -07:00
|
|
|
panic("Timed out waiting to commit blocks with transactions")
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 17:38:19 -08:00
|
|
|
func TestMempoolRmBadTx(t *testing.T) {
|
2016-10-10 14:05:50 -07:00
|
|
|
state, privVals := randGenesisState(1, false, 10)
|
|
|
|
app := NewCounterApplication()
|
|
|
|
cs := newConsensusState(state, privVals[0], app)
|
|
|
|
|
|
|
|
// increment the counter by 1
|
|
|
|
txBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(txBytes, uint64(0))
|
2017-09-21 08:42:44 -07:00
|
|
|
|
|
|
|
resDeliver := app.DeliverTx(txBytes)
|
2017-11-27 13:48:15 -08:00
|
|
|
assert.False(t, resDeliver.IsErr(), cmn.Fmt("expected no error. got %v", resDeliver))
|
2017-09-21 08:42:44 -07:00
|
|
|
|
|
|
|
resCommit := app.Commit()
|
2018-01-05 22:26:51 -08:00
|
|
|
assert.True(t, len(resCommit.Data) > 0)
|
2016-10-10 14:05:50 -07:00
|
|
|
|
2017-11-07 12:02:42 -08:00
|
|
|
emptyMempoolCh := make(chan struct{})
|
|
|
|
checkTxRespCh := make(chan struct{})
|
2016-10-10 14:05:50 -07:00
|
|
|
go func() {
|
|
|
|
// Try to send the tx through the mempool.
|
2017-01-12 12:53:32 -08:00
|
|
|
// CheckTx should not err, but the app should return a bad abci code
|
2016-10-10 14:05:50 -07:00
|
|
|
// and the tx should get removed from the pool
|
2017-01-12 12:53:32 -08:00
|
|
|
err := cs.mempool.CheckTx(txBytes, func(r *abci.Response) {
|
2017-12-01 22:47:55 -08:00
|
|
|
if r.GetCheckTx().Code != code.CodeTypeBadNonce {
|
2016-10-10 14:05:50 -07:00
|
|
|
t.Fatalf("expected checktx to return bad nonce, got %v", r)
|
|
|
|
}
|
2017-11-07 12:02:42 -08:00
|
|
|
checkTxRespCh <- struct{}{}
|
2016-10-10 14:05:50 -07:00
|
|
|
})
|
|
|
|
if err != nil {
|
2017-11-20 21:55:11 -08:00
|
|
|
t.Fatalf("Error after CheckTx: %v", err)
|
2016-10-10 14:05:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for the tx
|
2017-11-07 15:08:45 -08:00
|
|
|
for {
|
|
|
|
txs := cs.mempool.Reap(1)
|
|
|
|
if len(txs) == 0 {
|
|
|
|
emptyMempoolCh <- struct{}{}
|
2018-03-18 15:05:04 -07:00
|
|
|
return
|
2017-11-07 15:08:45 -08:00
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2016-10-10 14:05:50 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait until the tx returns
|
|
|
|
ticker := time.After(time.Second * 5)
|
|
|
|
select {
|
2017-11-07 12:02:42 -08:00
|
|
|
case <-checkTxRespCh:
|
2016-10-10 14:05:50 -07:00
|
|
|
// success
|
|
|
|
case <-ticker:
|
|
|
|
t.Fatalf("Timed out waiting for tx to return")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the tx is removed
|
|
|
|
ticker = time.After(time.Second * 5)
|
|
|
|
select {
|
2017-11-07 12:02:42 -08:00
|
|
|
case <-emptyMempoolCh:
|
2016-10-10 14:05:50 -07:00
|
|
|
// success
|
|
|
|
case <-ticker:
|
|
|
|
t.Fatalf("Timed out waiting for tx to be removed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:08:28 -07:00
|
|
|
// CounterApplication that maintains a mempool state and resets it upon commit
|
|
|
|
type CounterApplication struct {
|
2017-02-13 18:07:26 -08:00
|
|
|
abci.BaseApplication
|
|
|
|
|
2016-04-11 15:08:28 -07:00
|
|
|
txCount int
|
|
|
|
mempoolTxCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCounterApplication() *CounterApplication {
|
|
|
|
return &CounterApplication{}
|
|
|
|
}
|
|
|
|
|
2017-09-22 08:42:40 -07:00
|
|
|
func (app *CounterApplication) Info(req abci.RequestInfo) abci.ResponseInfo {
|
2017-10-04 13:40:45 -07:00
|
|
|
return abci.ResponseInfo{Data: cmn.Fmt("txs:%v", app.txCount)}
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:55:09 -08:00
|
|
|
func (app *CounterApplication) DeliverTx(tx []byte) abci.ResponseDeliverTx {
|
|
|
|
txValue := txAsUint64(tx)
|
|
|
|
if txValue != uint64(app.txCount) {
|
|
|
|
return abci.ResponseDeliverTx{
|
2017-12-01 22:47:55 -08:00
|
|
|
Code: code.CodeTypeBadNonce,
|
2017-11-22 16:55:09 -08:00
|
|
|
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
|
|
|
|
}
|
|
|
|
app.txCount += 1
|
2017-12-01 22:47:55 -08:00
|
|
|
return abci.ResponseDeliverTx{Code: code.CodeTypeOK}
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:55:09 -08:00
|
|
|
func (app *CounterApplication) CheckTx(tx []byte) abci.ResponseCheckTx {
|
|
|
|
txValue := txAsUint64(tx)
|
|
|
|
if txValue != uint64(app.mempoolTxCount) {
|
|
|
|
return abci.ResponseCheckTx{
|
2017-12-01 22:47:55 -08:00
|
|
|
Code: code.CodeTypeBadNonce,
|
2017-11-22 16:55:09 -08:00
|
|
|
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.mempoolTxCount, txValue)}
|
|
|
|
}
|
|
|
|
app.mempoolTxCount += 1
|
2017-12-01 22:47:55 -08:00
|
|
|
return abci.ResponseCheckTx{Code: code.CodeTypeOK}
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:55:09 -08:00
|
|
|
func txAsUint64(tx []byte) uint64 {
|
2016-04-11 15:08:28 -07:00
|
|
|
tx8 := make([]byte, 8)
|
|
|
|
copy(tx8[len(tx8)-len(tx):], tx)
|
2017-11-22 16:55:09 -08:00
|
|
|
return binary.BigEndian.Uint64(tx8)
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:55:09 -08:00
|
|
|
func (app *CounterApplication) Commit() abci.ResponseCommit {
|
2016-04-11 15:08:28 -07:00
|
|
|
app.mempoolTxCount = app.txCount
|
|
|
|
if app.txCount == 0 {
|
2018-01-05 22:26:51 -08:00
|
|
|
return abci.ResponseCommit{}
|
2016-04-11 15:08:28 -07:00
|
|
|
} else {
|
|
|
|
hash := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
|
2018-01-05 22:26:51 -08:00
|
|
|
return abci.ResponseCommit{Data: hash}
|
2016-04-11 15:08:28 -07:00
|
|
|
}
|
|
|
|
}
|