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