You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
8.1 KiB

mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
5 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
5 years ago
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
7 years ago
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
7 years ago
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
7 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
5 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
5 years ago
7 years ago
7 years ago
  1. package consensus
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/tendermint/tendermint/abci/example/code"
  10. abci "github.com/tendermint/tendermint/abci/types"
  11. mempl "github.com/tendermint/tendermint/mempool"
  12. sm "github.com/tendermint/tendermint/state"
  13. "github.com/tendermint/tendermint/types"
  14. dbm "github.com/tendermint/tm-db"
  15. )
  16. // for testing
  17. func assertMempool(txn txNotifier) mempl.Mempool {
  18. return txn.(mempl.Mempool)
  19. }
  20. func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
  21. config := ResetConfig("consensus_mempool_txs_available_test")
  22. defer os.RemoveAll(config.RootDir)
  23. config.Consensus.CreateEmptyBlocks = false
  24. state, privVals := randGenesisState(1, false, 10)
  25. cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
  26. assertMempool(cs.txNotifier).EnableTxsAvailable()
  27. height, round := cs.Height, cs.Round
  28. newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
  29. startTestRound(cs, height, round)
  30. ensureNewEventOnChannel(newBlockCh) // first block gets committed
  31. ensureNoNewEventOnChannel(newBlockCh)
  32. deliverTxsRange(cs, 0, 1)
  33. ensureNewEventOnChannel(newBlockCh) // commit txs
  34. ensureNewEventOnChannel(newBlockCh) // commit updated app hash
  35. ensureNoNewEventOnChannel(newBlockCh)
  36. }
  37. func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
  38. config := ResetConfig("consensus_mempool_txs_available_test")
  39. defer os.RemoveAll(config.RootDir)
  40. config.Consensus.CreateEmptyBlocksInterval = ensureTimeout
  41. state, privVals := randGenesisState(1, false, 10)
  42. cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
  43. assertMempool(cs.txNotifier).EnableTxsAvailable()
  44. height, round := cs.Height, cs.Round
  45. newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
  46. startTestRound(cs, height, round)
  47. ensureNewEventOnChannel(newBlockCh) // first block gets committed
  48. ensureNoNewEventOnChannel(newBlockCh) // then we dont make a block ...
  49. ensureNewEventOnChannel(newBlockCh) // until the CreateEmptyBlocksInterval has passed
  50. }
  51. func TestMempoolProgressInHigherRound(t *testing.T) {
  52. config := ResetConfig("consensus_mempool_txs_available_test")
  53. defer os.RemoveAll(config.RootDir)
  54. config.Consensus.CreateEmptyBlocks = false
  55. state, privVals := randGenesisState(1, false, 10)
  56. cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
  57. assertMempool(cs.txNotifier).EnableTxsAvailable()
  58. height, round := cs.Height, cs.Round
  59. newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
  60. newRoundCh := subscribe(cs.eventBus, types.EventQueryNewRound)
  61. timeoutCh := subscribe(cs.eventBus, types.EventQueryTimeoutPropose)
  62. cs.setProposal = func(proposal *types.Proposal) error {
  63. if cs.Height == 2 && cs.Round == 0 {
  64. // dont set the proposal in round 0 so we timeout and
  65. // go to next round
  66. cs.Logger.Info("Ignoring set proposal at height 2, round 0")
  67. return nil
  68. }
  69. return cs.defaultSetProposal(proposal)
  70. }
  71. startTestRound(cs, height, round)
  72. ensureNewRound(newRoundCh, height, round) // first round at first height
  73. ensureNewEventOnChannel(newBlockCh) // first block gets committed
  74. height++ // moving to the next height
  75. round = 0
  76. ensureNewRound(newRoundCh, height, round) // first round at next height
  77. deliverTxsRange(cs, 0, 1) // we deliver txs, but dont set a proposal so we get the next round
  78. ensureNewTimeout(timeoutCh, height, round, cs.config.TimeoutPropose.Nanoseconds())
  79. round++ // moving to the next round
  80. ensureNewRound(newRoundCh, height, round) // wait for the next round
  81. ensureNewEventOnChannel(newBlockCh) // now we can commit the block
  82. }
  83. func deliverTxsRange(cs *ConsensusState, start, end int) {
  84. // Deliver some txs.
  85. for i := start; i < end; i++ {
  86. txBytes := make([]byte, 8)
  87. binary.BigEndian.PutUint64(txBytes, uint64(i))
  88. err := assertMempool(cs.txNotifier).CheckTx(txBytes, nil, mempl.TxInfo{})
  89. if err != nil {
  90. panic(fmt.Sprintf("Error after CheckTx: %v", err))
  91. }
  92. }
  93. }
  94. func TestMempoolTxConcurrentWithCommit(t *testing.T) {
  95. state, privVals := randGenesisState(1, false, 10)
  96. blockDB := dbm.NewMemDB()
  97. cs := newConsensusStateWithConfigAndBlockStore(config, state, privVals[0], NewCounterApplication(), blockDB)
  98. sm.SaveState(blockDB, state)
  99. newBlockHeaderCh := subscribe(cs.eventBus, types.EventQueryNewBlockHeader)
  100. const numTxs int64 = 3000
  101. go deliverTxsRange(cs, 0, int(numTxs))
  102. startTestRound(cs, cs.Height, cs.Round)
  103. for n := int64(0); n < numTxs; {
  104. select {
  105. case msg := <-newBlockHeaderCh:
  106. headerEvent := msg.Data().(types.EventDataNewBlockHeader)
  107. n += headerEvent.NumTxs
  108. case <-time.After(30 * time.Second):
  109. t.Fatal("Timed out waiting 30s to commit blocks with transactions")
  110. }
  111. }
  112. }
  113. func TestMempoolRmBadTx(t *testing.T) {
  114. state, privVals := randGenesisState(1, false, 10)
  115. app := NewCounterApplication()
  116. blockDB := dbm.NewMemDB()
  117. cs := newConsensusStateWithConfigAndBlockStore(config, state, privVals[0], app, blockDB)
  118. sm.SaveState(blockDB, state)
  119. // increment the counter by 1
  120. txBytes := make([]byte, 8)
  121. binary.BigEndian.PutUint64(txBytes, uint64(0))
  122. resDeliver := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
  123. assert.False(t, resDeliver.IsErr(), fmt.Sprintf("expected no error. got %v", resDeliver))
  124. resCommit := app.Commit()
  125. assert.True(t, len(resCommit.Data) > 0)
  126. emptyMempoolCh := make(chan struct{})
  127. checkTxRespCh := make(chan struct{})
  128. go func() {
  129. // Try to send the tx through the mempool.
  130. // CheckTx should not err, but the app should return a bad abci code
  131. // and the tx should get removed from the pool
  132. err := assertMempool(cs.txNotifier).CheckTx(txBytes, func(r *abci.Response) {
  133. if r.GetCheckTx().Code != code.CodeTypeBadNonce {
  134. t.Errorf("expected checktx to return bad nonce, got %v", r)
  135. return
  136. }
  137. checkTxRespCh <- struct{}{}
  138. }, mempl.TxInfo{})
  139. if err != nil {
  140. t.Errorf("error after CheckTx: %v", err)
  141. return
  142. }
  143. // check for the tx
  144. for {
  145. txs := assertMempool(cs.txNotifier).ReapMaxBytesMaxGas(int64(len(txBytes)), -1)
  146. if len(txs) == 0 {
  147. emptyMempoolCh <- struct{}{}
  148. return
  149. }
  150. time.Sleep(10 * time.Millisecond)
  151. }
  152. }()
  153. // Wait until the tx returns
  154. ticker := time.After(time.Second * 5)
  155. select {
  156. case <-checkTxRespCh:
  157. // success
  158. case <-ticker:
  159. t.Errorf("timed out waiting for tx to return")
  160. return
  161. }
  162. // Wait until the tx is removed
  163. ticker = time.After(time.Second * 5)
  164. select {
  165. case <-emptyMempoolCh:
  166. // success
  167. case <-ticker:
  168. t.Errorf("timed out waiting for tx to be removed")
  169. return
  170. }
  171. }
  172. // CounterApplication that maintains a mempool state and resets it upon commit
  173. type CounterApplication struct {
  174. abci.BaseApplication
  175. txCount int
  176. mempoolTxCount int
  177. }
  178. func NewCounterApplication() *CounterApplication {
  179. return &CounterApplication{}
  180. }
  181. func (app *CounterApplication) Info(req abci.RequestInfo) abci.ResponseInfo {
  182. return abci.ResponseInfo{Data: fmt.Sprintf("txs:%v", app.txCount)}
  183. }
  184. func (app *CounterApplication) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
  185. txValue := txAsUint64(req.Tx)
  186. if txValue != uint64(app.txCount) {
  187. return abci.ResponseDeliverTx{
  188. Code: code.CodeTypeBadNonce,
  189. Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
  190. }
  191. app.txCount++
  192. return abci.ResponseDeliverTx{Code: code.CodeTypeOK}
  193. }
  194. func (app *CounterApplication) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
  195. txValue := txAsUint64(req.Tx)
  196. if txValue != uint64(app.mempoolTxCount) {
  197. return abci.ResponseCheckTx{
  198. Code: code.CodeTypeBadNonce,
  199. Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.mempoolTxCount, txValue)}
  200. }
  201. app.mempoolTxCount++
  202. return abci.ResponseCheckTx{Code: code.CodeTypeOK}
  203. }
  204. func txAsUint64(tx []byte) uint64 {
  205. tx8 := make([]byte, 8)
  206. copy(tx8[len(tx8)-len(tx):], tx)
  207. return binary.BigEndian.Uint64(tx8)
  208. }
  209. func (app *CounterApplication) Commit() abci.ResponseCommit {
  210. app.mempoolTxCount = app.txCount
  211. if app.txCount == 0 {
  212. return abci.ResponseCommit{}
  213. }
  214. hash := make([]byte, 8)
  215. binary.BigEndian.PutUint64(hash, uint64(app.txCount))
  216. return abci.ResponseCommit{Data: hash}
  217. }