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