github.com/number571/tendermint@v0.34.11-gost/internal/mempool/v0/cache_test.go (about) 1 package v0 2 3 import ( 4 "context" 5 "crypto/sha256" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/number571/tendermint/abci/example/kvstore" 11 abci "github.com/number571/tendermint/abci/types" 12 "github.com/number571/tendermint/internal/mempool" 13 "github.com/number571/tendermint/proxy" 14 "github.com/number571/tendermint/types" 15 ) 16 17 func TestCacheAfterUpdate(t *testing.T) { 18 app := kvstore.NewApplication() 19 cc := proxy.NewLocalClientCreator(app) 20 mp, cleanup := newMempoolWithApp(cc) 21 defer cleanup() 22 23 // reAddIndices & txsInCache can have elements > numTxsToCreate 24 // also assumes max index is 255 for convenience 25 // txs in cache also checks order of elements 26 tests := []struct { 27 numTxsToCreate int 28 updateIndices []int 29 reAddIndices []int 30 txsInCache []int 31 }{ 32 {1, []int{}, []int{1}, []int{1, 0}}, // adding new txs works 33 {2, []int{1}, []int{}, []int{1, 0}}, // update doesn't remove tx from cache 34 {2, []int{2}, []int{}, []int{2, 1, 0}}, // update adds new tx to cache 35 {2, []int{1}, []int{1}, []int{1, 0}}, // re-adding after update doesn't make dupe 36 } 37 for tcIndex, tc := range tests { 38 for i := 0; i < tc.numTxsToCreate; i++ { 39 tx := types.Tx{byte(i)} 40 err := mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{}) 41 require.NoError(t, err) 42 } 43 44 updateTxs := []types.Tx{} 45 for _, v := range tc.updateIndices { 46 tx := types.Tx{byte(v)} 47 updateTxs = append(updateTxs, tx) 48 } 49 err := mp.Update(int64(tcIndex), updateTxs, abciResponses(len(updateTxs), abci.CodeTypeOK), nil, nil) 50 require.NoError(t, err) 51 52 for _, v := range tc.reAddIndices { 53 tx := types.Tx{byte(v)} 54 _ = mp.CheckTx(context.Background(), tx, nil, mempool.TxInfo{}) 55 } 56 57 cache := mp.cache.(*mempool.LRUTxCache) 58 node := cache.GetList().Front() 59 counter := 0 60 for node != nil { 61 require.NotEqual(t, len(tc.txsInCache), counter, 62 "cache larger than expected on testcase %d", tcIndex) 63 64 nodeVal := node.Value.([sha256.Size]byte) 65 expectedBz := sha256.Sum256([]byte{byte(tc.txsInCache[len(tc.txsInCache)-counter-1])}) 66 // Reference for reading the errors: 67 // >>> sha256('\x00').hexdigest() 68 // '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d' 69 // >>> sha256('\x01').hexdigest() 70 // '4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a' 71 // >>> sha256('\x02').hexdigest() 72 // 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986' 73 74 require.Equal(t, expectedBz, nodeVal, "Equality failed on index %d, tc %d", counter, tcIndex) 75 counter++ 76 node = node.Next() 77 } 78 require.Equal(t, len(tc.txsInCache), counter, 79 "cache smaller than expected on testcase %d", tcIndex) 80 mp.Flush() 81 } 82 }