github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/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/line/ostracon/abci/example/kvstore"
    11  	abci "github.com/line/ostracon/abci/types"
    12  	"github.com/line/ostracon/proxy"
    13  	"github.com/line/ostracon/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.CheckTxSync(tx, 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(newTestBlock(int64(tcIndex), updateTxs),
    72  			abciResponses(len(updateTxs), abci.CodeTypeOK), nil, nil)
    73  		require.NoError(t, err)
    74  
    75  		for _, v := range tc.reAddIndices {
    76  			tx := types.Tx{byte(v)}
    77  			_, _ = mempool.CheckTxSync(tx, TxInfo{})
    78  		}
    79  
    80  		cache := mempool.cache.(*mapTxCache)
    81  		node := cache.list.Front()
    82  		counter := 0
    83  		for node != nil {
    84  			require.NotEqual(t, len(tc.txsInCache), counter,
    85  				"cache larger than expected on testcase %d", tcIndex)
    86  
    87  			nodeVal := node.Value.([sha256.Size]byte)
    88  			expectedBz := sha256.Sum256([]byte{byte(tc.txsInCache[len(tc.txsInCache)-counter-1])})
    89  			// Reference for reading the errors:
    90  			// >>> sha256('\x00').hexdigest()
    91  			// '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d'
    92  			// >>> sha256('\x01').hexdigest()
    93  			// '4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a'
    94  			// >>> sha256('\x02').hexdigest()
    95  			// 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986'
    96  
    97  			require.Equal(t, expectedBz, nodeVal, "Equality failed on index %d, tc %d", counter, tcIndex)
    98  			counter++
    99  			node = node.Next()
   100  		}
   101  		require.Equal(t, len(tc.txsInCache), counter,
   102  			"cache smaller than expected on testcase %d", tcIndex)
   103  		mempool.Flush()
   104  	}
   105  }