|
|
@ -102,14 +102,10 @@ func checkTxs(t *testing.T, txmp *TxMempool, numTxs int, peerID uint16) []testTx |
|
|
|
_, err := rng.Read(prefix) |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
// sender := make([]byte, 10)
|
|
|
|
// _, err = rng.Read(sender)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
|
|
|
|
priority := int64(rng.Intn(9999-1000) + 1000) |
|
|
|
|
|
|
|
txs[i] = testTx{ |
|
|
|
tx: []byte(fmt.Sprintf("sender-%d=%X=%d", i, prefix, priority)), |
|
|
|
tx: []byte(fmt.Sprintf("sender-%d-%d=%X=%d", i, peerID, prefix, priority)), |
|
|
|
priority: priority, |
|
|
|
} |
|
|
|
require.NoError(t, txmp.CheckTx(context.Background(), txs[i].tx, nil, txInfo)) |
|
|
@ -176,7 +172,7 @@ func TestTxMempool_Size(t *testing.T) { |
|
|
|
txmp := setup(t, 0) |
|
|
|
txs := checkTxs(t, txmp, 100, 0) |
|
|
|
require.Equal(t, len(txs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
|
|
|
|
rawTxs := make([]types.Tx, len(txs)) |
|
|
|
for i, tx := range txs { |
|
|
@ -193,14 +189,14 @@ func TestTxMempool_Size(t *testing.T) { |
|
|
|
txmp.Unlock() |
|
|
|
|
|
|
|
require.Equal(t, len(rawTxs)/2, txmp.Size()) |
|
|
|
require.Equal(t, int64(2750), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(2850), txmp.SizeBytes()) |
|
|
|
} |
|
|
|
|
|
|
|
func TestTxMempool_Flush(t *testing.T) { |
|
|
|
txmp := setup(t, 0) |
|
|
|
txs := checkTxs(t, txmp, 100, 0) |
|
|
|
require.Equal(t, len(txs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
|
|
|
|
rawTxs := make([]types.Tx, len(txs)) |
|
|
|
for i, tx := range txs { |
|
|
@ -225,7 +221,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) { |
|
|
|
txmp := setup(t, 0) |
|
|
|
tTxs := checkTxs(t, txmp, 100, 0) // all txs request 1 gas unit
|
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
|
|
|
|
txMap := make(map[[mempool.TxKeySize]byte]testTx) |
|
|
|
priorities := make([]int64, len(tTxs)) |
|
|
@ -252,30 +248,30 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) { |
|
|
|
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50) |
|
|
|
ensurePrioritized(reapedTxs) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
require.Len(t, reapedTxs, 50) |
|
|
|
|
|
|
|
// reap by transaction bytes only
|
|
|
|
reapedTxs = txmp.ReapMaxBytesMaxGas(1000, -1) |
|
|
|
ensurePrioritized(reapedTxs) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Len(t, reapedTxs, 17) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
require.GreaterOrEqual(t, len(reapedTxs), 16) |
|
|
|
|
|
|
|
// Reap by both transaction bytes and gas, where the size yields 31 reaped
|
|
|
|
// transactions and the gas limit reaps 26 transactions.
|
|
|
|
// transactions and the gas limit reaps 25 transactions.
|
|
|
|
reapedTxs = txmp.ReapMaxBytesMaxGas(1500, 30) |
|
|
|
ensurePrioritized(reapedTxs) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Len(t, reapedTxs, 26) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
require.Len(t, reapedTxs, 25) |
|
|
|
} |
|
|
|
|
|
|
|
func TestTxMempool_ReapMaxTxs(t *testing.T) { |
|
|
|
txmp := setup(t, 0) |
|
|
|
tTxs := checkTxs(t, txmp, 100, 0) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
|
|
|
|
txMap := make(map[[mempool.TxKeySize]byte]testTx) |
|
|
|
priorities := make([]int64, len(tTxs)) |
|
|
@ -302,21 +298,21 @@ func TestTxMempool_ReapMaxTxs(t *testing.T) { |
|
|
|
reapedTxs := txmp.ReapMaxTxs(-1) |
|
|
|
ensurePrioritized(reapedTxs) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
require.Len(t, reapedTxs, len(tTxs)) |
|
|
|
|
|
|
|
// reap a single transaction
|
|
|
|
reapedTxs = txmp.ReapMaxTxs(1) |
|
|
|
ensurePrioritized(reapedTxs) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
require.Len(t, reapedTxs, 1) |
|
|
|
|
|
|
|
// reap half of the transactions
|
|
|
|
reapedTxs = txmp.ReapMaxTxs(len(tTxs) / 2) |
|
|
|
ensurePrioritized(reapedTxs) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, int64(5490), txmp.SizeBytes()) |
|
|
|
require.Equal(t, int64(5690), txmp.SizeBytes()) |
|
|
|
require.Len(t, reapedTxs, len(tTxs)/2) |
|
|
|
} |
|
|
|
|
|
|
@ -431,3 +427,53 @@ func TestTxMempool_ConcurrentTxs(t *testing.T) { |
|
|
|
require.Zero(t, txmp.Size()) |
|
|
|
require.Zero(t, txmp.SizeBytes()) |
|
|
|
} |
|
|
|
|
|
|
|
func TestTxMempool_ExpiredTxs_NumBlocks(t *testing.T) { |
|
|
|
txmp := setup(t, 500) |
|
|
|
txmp.height = 100 |
|
|
|
txmp.config.TTLNumBlocks = 10 |
|
|
|
|
|
|
|
tTxs := checkTxs(t, txmp, 100, 0) |
|
|
|
require.Equal(t, len(tTxs), txmp.Size()) |
|
|
|
require.Equal(t, 100, txmp.heightIndex.Size()) |
|
|
|
|
|
|
|
// reap 5 txs at the next height -- no txs should expire
|
|
|
|
reapedTxs := txmp.ReapMaxTxs(5) |
|
|
|
responses := make([]*abci.ResponseDeliverTx, len(reapedTxs)) |
|
|
|
for i := 0; i < len(responses); i++ { |
|
|
|
responses[i] = &abci.ResponseDeliverTx{Code: abci.CodeTypeOK} |
|
|
|
} |
|
|
|
|
|
|
|
txmp.Lock() |
|
|
|
require.NoError(t, txmp.Update(txmp.height+1, reapedTxs, responses, nil, nil)) |
|
|
|
txmp.Unlock() |
|
|
|
|
|
|
|
require.Equal(t, 95, txmp.Size()) |
|
|
|
require.Equal(t, 95, txmp.heightIndex.Size()) |
|
|
|
|
|
|
|
// check more txs at height 101
|
|
|
|
_ = checkTxs(t, txmp, 50, 1) |
|
|
|
require.Equal(t, 145, txmp.Size()) |
|
|
|
require.Equal(t, 145, txmp.heightIndex.Size()) |
|
|
|
|
|
|
|
// Reap 5 txs at a height that would expire all the transactions from before
|
|
|
|
// the previous Update (height 100).
|
|
|
|
//
|
|
|
|
// NOTE: When we reap txs below, we do not know if we're picking txs from the
|
|
|
|
// initial CheckTx calls or from the second round of CheckTx calls. Thus, we
|
|
|
|
// cannot guarantee that all 95 txs are remaining that should be expired and
|
|
|
|
// removed. However, we do know that that at most 95 txs can be expired and
|
|
|
|
// removed.
|
|
|
|
reapedTxs = txmp.ReapMaxTxs(5) |
|
|
|
responses = make([]*abci.ResponseDeliverTx, len(reapedTxs)) |
|
|
|
for i := 0; i < len(responses); i++ { |
|
|
|
responses[i] = &abci.ResponseDeliverTx{Code: abci.CodeTypeOK} |
|
|
|
} |
|
|
|
|
|
|
|
txmp.Lock() |
|
|
|
require.NoError(t, txmp.Update(txmp.height+10, reapedTxs, responses, nil, nil)) |
|
|
|
txmp.Unlock() |
|
|
|
|
|
|
|
require.GreaterOrEqual(t, txmp.Size(), 45) |
|
|
|
require.GreaterOrEqual(t, txmp.heightIndex.Size(), 45) |
|
|
|
} |