github.com/aakash4dev/cometbft@v0.38.2/mempool/cache_test.go (about)

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