@ -170,22 +170,42 @@ func TestMempoolFilters(t *testing.T) {
{ 10 , PreCheckAminoMaxBytes ( 22 ) , PostCheckMaxGas ( 0 ) , 0 } ,
{ 10 , PreCheckAminoMaxBytes ( 22 ) , PostCheckMaxGas ( 0 ) , 0 } ,
}
}
for tcIndex , tt := range tests {
for tcIndex , tt := range tests {
mempool . Update ( 1 , emptyTxArr , tt . preFilter , tt . postFilter )
mempool . Update ( 1 , emptyTxArr , abciResponses ( len ( emptyTxArr ) , abci . CodeTypeOK ) , tt . preFilter , tt . postFilter )
checkTxs ( t , mempool , tt . numTxsToCreate , UnknownPeerID )
checkTxs ( t , mempool , tt . numTxsToCreate , UnknownPeerID )
require . Equal ( t , tt . expectedNumTxs , mempool . Size ( ) , "mempool had the incorrect size, on test case %d" , tcIndex )
require . Equal ( t , tt . expectedNumTxs , mempool . Size ( ) , "mempool had the incorrect size, on test case %d" , tcIndex )
mempool . Flush ( )
mempool . Flush ( )
}
}
}
}
func TestMempoolUpdateAddsTxsToCache ( t * testing . T ) {
func TestMempoolUpdate ( t * testing . T ) {
app := kvstore . NewKVStoreApplication ( )
app := kvstore . NewKVStoreApplication ( )
cc := proxy . NewLocalClientCreator ( app )
cc := proxy . NewLocalClientCreator ( app )
mempool , cleanup := newMempoolWithApp ( cc )
mempool , cleanup := newMempoolWithApp ( cc )
defer cleanup ( )
defer cleanup ( )
mempool . Update ( 1 , [ ] types . Tx { [ ] byte { 0x01 } } , nil , nil )
err := mempool . CheckTx ( [ ] byte { 0x01 } , nil )
if assert . Error ( t , err ) {
assert . Equal ( t , ErrTxInCache , err )
// 1. Adds valid txs to the cache
{
mempool . Update ( 1 , [ ] types . Tx { [ ] byte { 0x01 } } , abciResponses ( 1 , abci . CodeTypeOK ) , nil , nil )
err := mempool . CheckTx ( [ ] byte { 0x01 } , nil )
if assert . Error ( t , err ) {
assert . Equal ( t , ErrTxInCache , err )
}
}
// 2. Removes valid txs from the mempool
{
err := mempool . CheckTx ( [ ] byte { 0x02 } , nil )
require . NoError ( t , err )
mempool . Update ( 1 , [ ] types . Tx { [ ] byte { 0x02 } } , abciResponses ( 1 , abci . CodeTypeOK ) , nil , nil )
assert . Zero ( t , mempool . Size ( ) )
}
// 3. Removes invalid transactions from the cache, but leaves them in the mempool (if present)
{
err := mempool . CheckTx ( [ ] byte { 0x03 } , nil )
require . NoError ( t , err )
mempool . Update ( 1 , [ ] types . Tx { [ ] byte { 0x03 } } , abciResponses ( 1 , 1 ) , nil , nil )
assert . Equal ( t , 1 , mempool . Size ( ) )
}
}
}
}
@ -210,7 +230,7 @@ func TestTxsAvailable(t *testing.T) {
// it should fire once now for the new height
// it should fire once now for the new height
// since there are still txs left
// since there are still txs left
committedTxs , txs := txs [ : 50 ] , txs [ 50 : ]
committedTxs , txs := txs [ : 50 ] , txs [ 50 : ]
if err := mempool . Update ( 1 , committedTxs , nil , nil ) ; err != nil {
if err := mempool . Update ( 1 , committedTxs , abciResponses ( len ( committedTxs ) , abci . CodeTypeOK ) , nil , nil ) ; err != nil {
t . Error ( err )
t . Error ( err )
}
}
ensureFire ( t , mempool . TxsAvailable ( ) , timeoutMS )
ensureFire ( t , mempool . TxsAvailable ( ) , timeoutMS )
@ -222,7 +242,7 @@ func TestTxsAvailable(t *testing.T) {
// now call update with all the txs. it should not fire as there are no txs left
// now call update with all the txs. it should not fire as there are no txs left
committedTxs = append ( txs , moreTxs ... )
committedTxs = append ( txs , moreTxs ... )
if err := mempool . Update ( 2 , committedTxs , nil , nil ) ; err != nil {
if err := mempool . Update ( 2 , committedTxs , abciResponses ( len ( committedTxs ) , abci . CodeTypeOK ) , nil , nil ) ; err != nil {
t . Error ( err )
t . Error ( err )
}
}
ensureNoFire ( t , mempool . TxsAvailable ( ) , timeoutMS )
ensureNoFire ( t , mempool . TxsAvailable ( ) , timeoutMS )
@ -281,7 +301,7 @@ func TestSerialReap(t *testing.T) {
binary . BigEndian . PutUint64 ( txBytes , uint64 ( i ) )
binary . BigEndian . PutUint64 ( txBytes , uint64 ( i ) )
txs = append ( txs , txBytes )
txs = append ( txs , txBytes )
}
}
if err := mempool . Update ( 0 , txs , nil , nil ) ; err != nil {
if err := mempool . Update ( 0 , txs , abciResponses ( len ( txs ) , abci . CodeTypeOK ) , nil , nil ) ; err != nil {
t . Error ( err )
t . Error ( err )
}
}
}
}
@ -462,7 +482,7 @@ func TestMempoolTxsBytes(t *testing.T) {
assert . EqualValues ( t , 1 , mempool . TxsBytes ( ) )
assert . EqualValues ( t , 1 , mempool . TxsBytes ( ) )
// 3. zero again after tx is removed by Update
// 3. zero again after tx is removed by Update
mempool . Update ( 1 , [ ] types . Tx { [ ] byte { 0x01 } } , nil , nil )
mempool . Update ( 1 , [ ] types . Tx { [ ] byte { 0x01 } } , abciResponses ( 1 , abci . CodeTypeOK ) , nil , nil )
assert . EqualValues ( t , 0 , mempool . TxsBytes ( ) )
assert . EqualValues ( t , 0 , mempool . TxsBytes ( ) )
// 4. zero after Flush
// 4. zero after Flush
@ -507,7 +527,7 @@ func TestMempoolTxsBytes(t *testing.T) {
require . NotEmpty ( t , res2 . Data )
require . NotEmpty ( t , res2 . Data )
// Pretend like we committed nothing so txBytes gets rechecked and removed.
// Pretend like we committed nothing so txBytes gets rechecked and removed.
mempool . Update ( 1 , [ ] types . Tx { } , nil , nil )
mempool . Update ( 1 , [ ] types . Tx { } , abciResponses ( 0 , abci . CodeTypeOK ) , nil , nil )
assert . EqualValues ( t , 0 , mempool . TxsBytes ( ) )
assert . EqualValues ( t , 0 , mempool . TxsBytes ( ) )
}
}
@ -570,3 +590,11 @@ func checksumFile(p string, t *testing.T) string {
require . Nil ( t , err , "expecting successful read of %q" , p )
require . Nil ( t , err , "expecting successful read of %q" , p )
return checksumIt ( data )
return checksumIt ( data )
}
}
func abciResponses ( n int , code uint32 ) [ ] * abci . ResponseDeliverTx {
responses := make ( [ ] * abci . ResponseDeliverTx , 0 , n )
for i := 0 ; i < n ; i ++ {
responses = append ( responses , & abci . ResponseDeliverTx { Code : code } )
}
return responses
}