2015-12-01 20:12:01 -08:00
|
|
|
package mempool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
|
|
|
|
2017-05-01 21:25:10 -04:00
|
|
|
"github.com/tendermint/abci/example/counter"
|
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2016-10-10 17:05:50 -04:00
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2015-12-01 20:12:01 -08:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2017-05-02 11:53:32 +04:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
2015-12-01 20:12:01 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSerialReap(t *testing.T) {
|
2017-05-04 22:33:08 -04:00
|
|
|
config := cfg.ResetTestRoot("mempool_test")
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-02-14 15:03:55 -08:00
|
|
|
app := counter.NewCounterApplication(true)
|
2016-01-06 17:14:20 -08:00
|
|
|
app.SetOption("serial", "on")
|
2016-10-10 17:05:50 -04:00
|
|
|
cc := proxy.NewLocalClientCreator(app)
|
2017-01-12 15:53:32 -05:00
|
|
|
appConnMem, _ := cc.NewABCIClient()
|
2017-05-16 19:06:35 +02:00
|
|
|
appConnMem.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "mempool"))
|
|
|
|
if _, err := appConnMem.Start(); err != nil {
|
|
|
|
t.Fatalf("Error starting ABCI client: %v", err.Error())
|
|
|
|
}
|
2017-01-12 15:53:32 -05:00
|
|
|
appConnCon, _ := cc.NewABCIClient()
|
2017-05-16 19:06:35 +02:00
|
|
|
appConnCon.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "consensus"))
|
|
|
|
if _, err := appConnCon.Start(); err != nil {
|
|
|
|
t.Fatalf("Error starting ABCI client: %v", err.Error())
|
|
|
|
}
|
2017-05-04 22:33:08 -04:00
|
|
|
mempool := NewMempool(config.Mempool, appConnMem)
|
2017-05-02 11:53:32 +04:00
|
|
|
mempool.SetLogger(log.TestingLogger())
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-01-12 15:55:03 -05:00
|
|
|
deliverTxsRange := func(start, end int) {
|
|
|
|
// Deliver some txs.
|
2015-12-01 20:12:01 -08:00
|
|
|
for i := start; i < end; i++ {
|
|
|
|
|
|
|
|
// This will succeed
|
2016-01-25 14:34:08 -08:00
|
|
|
txBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(txBytes, uint64(i))
|
2016-02-08 00:48:58 -08:00
|
|
|
err := mempool.CheckTx(txBytes, nil)
|
2015-12-01 20:12:01 -08:00
|
|
|
if err != nil {
|
2016-01-06 17:14:20 -08:00
|
|
|
t.Fatal("Error after CheckTx: %v", err)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This will fail because not serial (incrementing)
|
|
|
|
// However, error should still be nil.
|
|
|
|
// It just won't show up on Reap().
|
2016-02-08 00:48:58 -08:00
|
|
|
err = mempool.CheckTx(txBytes, nil)
|
2015-12-01 20:12:01 -08:00
|
|
|
if err != nil {
|
2016-01-06 17:14:20 -08:00
|
|
|
t.Fatal("Error after CheckTx: %v", err)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reapCheck := func(exp int) {
|
2016-04-26 22:17:13 -04:00
|
|
|
txs := mempool.Reap(-1)
|
2015-12-01 20:12:01 -08:00
|
|
|
if len(txs) != exp {
|
|
|
|
t.Fatalf("Expected to reap %v txs but got %v", exp, len(txs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRange := func(start, end int) {
|
|
|
|
txs := make([]types.Tx, 0)
|
|
|
|
for i := start; i < end; i++ {
|
2016-01-25 14:34:08 -08:00
|
|
|
txBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(txBytes, uint64(i))
|
2015-12-01 20:12:01 -08:00
|
|
|
txs = append(txs, txBytes)
|
|
|
|
}
|
2016-02-14 17:00:33 -08:00
|
|
|
mempool.Update(0, txs)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
commitRange := func(start, end int) {
|
2017-01-12 15:55:03 -05:00
|
|
|
// Deliver some txs.
|
2015-12-01 20:12:01 -08:00
|
|
|
for i := start; i < end; i++ {
|
2016-01-25 14:34:08 -08:00
|
|
|
txBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(txBytes, uint64(i))
|
2017-01-12 15:55:03 -05:00
|
|
|
res := appConnCon.DeliverTxSync(txBytes)
|
2016-03-20 17:10:59 -07:00
|
|
|
if !res.IsOK() {
|
2016-01-25 14:34:08 -08:00
|
|
|
t.Errorf("Error committing tx. Code:%v result:%X log:%v",
|
2016-03-20 17:10:59 -07:00
|
|
|
res.Code, res.Data, res.Log)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 17:05:50 -04:00
|
|
|
res := appConnCon.CommitSync()
|
2016-03-23 02:49:30 -07:00
|
|
|
if len(res.Data) != 8 {
|
|
|
|
t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
2017-01-12 15:55:03 -05:00
|
|
|
// Deliver some txs.
|
|
|
|
deliverTxsRange(0, 100)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
|
|
|
// Reap the txs.
|
|
|
|
reapCheck(100)
|
|
|
|
|
|
|
|
// Reap again. We should get the same amount
|
|
|
|
reapCheck(100)
|
|
|
|
|
2017-01-12 15:55:03 -05:00
|
|
|
// Deliver 0 to 999, we should reap 900 new txs
|
2015-12-01 20:12:01 -08:00
|
|
|
// because 100 were already counted.
|
2017-01-12 15:55:03 -05:00
|
|
|
deliverTxsRange(0, 1000)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
|
|
|
// Reap the txs.
|
|
|
|
reapCheck(1000)
|
|
|
|
|
|
|
|
// Reap again. We should get the same amount
|
|
|
|
reapCheck(1000)
|
|
|
|
|
2016-01-06 17:14:20 -08:00
|
|
|
// Commit from the conensus AppConn
|
2015-12-01 20:12:01 -08:00
|
|
|
commitRange(0, 500)
|
|
|
|
updateRange(0, 500)
|
|
|
|
|
|
|
|
// We should have 500 left.
|
|
|
|
reapCheck(500)
|
|
|
|
|
2017-01-12 15:55:03 -05:00
|
|
|
// Deliver 100 invalid txs and 100 valid txs
|
|
|
|
deliverTxsRange(900, 1100)
|
2016-01-06 17:14:20 -08:00
|
|
|
|
|
|
|
// We should have 600 now.
|
|
|
|
reapCheck(600)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|