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.

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