github.com/DFWallet/tendermint-cosmos@v0.0.2/mempool/cache_test.go (about)

     1  package mempool
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/sha256"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/DFWallet/tendermint-cosmos/abci/example/kvstore"
    11  	abci "github.com/DFWallet/tendermint-cosmos/abci/types"
    12  	"github.com/DFWallet/tendermint-cosmos/proxy"
    13  	"github.com/DFWallet/tendermint-cosmos/types"
    14  )
    15  
    16  func TestCacheRemove(t *testing.T) {
    17  	cache := newMapTxCache(100)
    18  	numTxs := 10
    19  	txs := make([][]byte, numTxs)
    20  	for i := 0; i < numTxs; i++ {
    21  		// probability of collision is 2**-256
    22  		txBytes := make([]byte, 32)
    23  		_, err := rand.Read(txBytes)
    24  		require.NoError(t, err)
    25  		txs[i] = txBytes
    26  		cache.Push(txBytes)
    27  		// make sure its added to both the linked list and the map
    28  		require.Equal(t, i+1, len(cache.cacheMap))
    29  		require.Equal(t, i+1, cache.list.Len())
    30  	}
    31  	for i := 0; i < numTxs; i++ {
    32  		cache.Remove(txs[i])
    33  		// make sure its removed from both the map and the linked list
    34  		require.Equal(t, numTxs-(i+1), len(cache.cacheMap))
    35  		require.Equal(t, numTxs-(i+1), cache.list.Len())
    36  	}
    37  }
    38  
    39  func TestCacheAfterUpdate(t *testing.T) {
    40  	app := kvstore.NewApplication()
    41  	cc := proxy.NewLocalClientCreator(app)
    42  	mempool, cleanup := newMempoolWithApp(cc)
    43  	defer cleanup()
    44  
    45  	// reAddIndices & txsInCache can have elements > numTxsToCreate
    46  	// also assumes max index is 255 for convenience
    47  	// txs in cache also checks order of elements
    48  	tests := []struct {
    49  		numTxsToCreate int
    50  		updateIndices  []int
    51  		reAddIndices   []int
    52  		txsInCache     []int
    53  	}{
    54  		{1, []int{}, []int{1}, []int{1, 0}},    // adding new txs works
    55  		{2, []int{1}, []int{}, []int{1, 0}},    // update doesn't remove tx from cache
    56  		{2, []int{2}, []int{}, []int{2, 1, 0}}, // update adds new tx to cache
    57  		{2, []int{1}, []int{1}, []int{1, 0}},   // re-adding after update doesn't make dupe
    58  	}
    59  	for tcIndex, tc := range tests {
    60  		for i := 0; i < tc.numTxsToCreate; i++ {
    61  			tx := types.Tx{byte(i)}
    62  			err := mempool.CheckTx(tx, nil, TxInfo{})
    63  			require.NoError(t, err)
    64  		}
    65  
    66  		updateTxs := []types.Tx{}
    67  		for _, v := range tc.updateIndices {
    68  			tx := types.Tx{byte(v)}
    69  			updateTxs = append(updateTxs, tx)
    70  		}
    71  		err := mempool.Update(int64(tcIndex), updateTxs, abciResponses(len(updateTxs), abci.CodeTypeOK), nil, nil)
    72  		require.NoError(t, err)
    73  
    74  		for _, v := range tc.reAddIndices {
    75  			tx := types.Tx{byte(v)}
    76  			_ = mempool.CheckTx(tx, nil, TxInfo{})
    77  		}
    78  
    79  		cache := mempool.cache.(*mapTxCache)
    80  		node := cache.list.Front()
    81  		counter := 0
    82  		for node != nil {
    83  			require.NotEqual(t, len(tc.txsInCache), counter,
    84  				"cache larger than expected on testcase %d", tcIndex)
    85  
    86  			nodeVal := node.Value.([sha256.Size]byte)
    87  			expectedBz := sha256.Sum256([]byte{byte(tc.txsInCache[len(tc.txsInCache)-counter-1])})
    88  			// Reference for reading the errors:
    89  			// >>> sha256('\x00').hexdigest()
    90  			// '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d'
    91  			// >>> sha256('\x01').hexdigest()
    92  			// '4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a'
    93  			// >>> sha256('\x02').hexdigest()
    94  			// 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986'
    95  
    96  			require.Equal(t, expectedBz, nodeVal, "Equality failed on index %d, tc %d", counter, tcIndex)
    97  			counter++
    98  			node = node.Next()
    99  		}
   100  		require.Equal(t, len(tc.txsInCache), counter,
   101  			"cache smaller than expected on testcase %d", tcIndex)
   102  		mempool.Flush()
   103  	}
   104  }