You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.7 KiB

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