github.com/okex/exchain@v1.8.0/libs/tendermint/mempool/cache_test.go (about) 1 package mempool 2 3 import ( 4 "crypto/rand" 5 "testing" 6 7 "github.com/okex/exchain/libs/tendermint/abci/example/kvstore" 8 abci "github.com/okex/exchain/libs/tendermint/abci/types" 9 "github.com/okex/exchain/libs/tendermint/proxy" 10 "github.com/okex/exchain/libs/tendermint/types" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestCacheRemove(t *testing.T) { 15 cache := newMapTxCache(100) 16 numTxs := 10 17 txs := make([][]byte, numTxs) 18 for i := 0; i < numTxs; i++ { 19 // probability of collision is 2**-256 20 txBytes := make([]byte, 32) 21 rand.Read(txBytes) // nolint: gosec 22 txs[i] = txBytes 23 cache.Push(txBytes) 24 require.False(t, cache.PushKey(txKey(txBytes))) 25 // make sure its added to both the linked list and the map 26 //require.Equal(t, i+1, len(cache.cacheMap)) 27 //require.Equal(t, i+1, cache.list.Len()) 28 } 29 for i := 0; i < numTxs; i++ { 30 cache.Remove(txs[i]) 31 require.True(t, cache.PushKey(txKey(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 }