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.

123 lines
3.5 KiB

  1. package core
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. dbm "github.com/tendermint/tm-db"
  8. abci "github.com/tendermint/tendermint/abci/types"
  9. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  10. rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
  11. sm "github.com/tendermint/tendermint/state"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. func TestBlockchainInfo(t *testing.T) {
  15. cases := []struct {
  16. min, max int64
  17. height int64
  18. limit int64
  19. resultLength int64
  20. wantErr bool
  21. }{
  22. // min > max
  23. {0, 0, 0, 10, 0, true}, // min set to 1
  24. {0, 1, 0, 10, 0, true}, // max set to height (0)
  25. {0, 0, 1, 10, 1, false}, // max set to height (1)
  26. {2, 0, 1, 10, 0, true}, // max set to height (1)
  27. {2, 1, 5, 10, 0, true},
  28. // negative
  29. {1, 10, 14, 10, 10, false}, // control
  30. {-1, 10, 14, 10, 0, true},
  31. {1, -10, 14, 10, 0, true},
  32. {-9223372036854775808, -9223372036854775788, 100, 20, 0, true},
  33. // check limit and height
  34. {1, 1, 1, 10, 1, false},
  35. {1, 1, 5, 10, 1, false},
  36. {2, 2, 5, 10, 1, false},
  37. {1, 2, 5, 10, 2, false},
  38. {1, 5, 1, 10, 1, false},
  39. {1, 5, 10, 10, 5, false},
  40. {1, 15, 10, 10, 10, false},
  41. {1, 15, 15, 10, 10, false},
  42. {1, 15, 15, 20, 15, false},
  43. {1, 20, 15, 20, 15, false},
  44. {1, 20, 20, 20, 20, false},
  45. }
  46. for i, c := range cases {
  47. caseString := fmt.Sprintf("test %d failed", i)
  48. min, max, err := filterMinMax(c.height, c.min, c.max, c.limit)
  49. if c.wantErr {
  50. require.Error(t, err, caseString)
  51. } else {
  52. require.NoError(t, err, caseString)
  53. require.Equal(t, 1+max-min, c.resultLength, caseString)
  54. }
  55. }
  56. }
  57. func TestBlockResults(t *testing.T) {
  58. results := &sm.ABCIResponses{
  59. DeliverTxs: []*abci.ResponseDeliverTx{
  60. {Code: 0, Data: []byte{0x01}, Log: "ok"},
  61. {Code: 0, Data: []byte{0x02}, Log: "ok"},
  62. {Code: 1, Log: "not ok"},
  63. },
  64. EndBlock: &abci.ResponseEndBlock{},
  65. BeginBlock: &abci.ResponseBeginBlock{},
  66. }
  67. stateDB = dbm.NewMemDB()
  68. sm.SaveABCIResponses(stateDB, 100, results)
  69. blockStore = mockBlockStore{height: 100}
  70. testCases := []struct {
  71. height int64
  72. wantErr bool
  73. wantRes *ctypes.ResultBlockResults
  74. }{
  75. {-1, true, nil},
  76. {0, true, nil},
  77. {101, true, nil},
  78. {100, false, &ctypes.ResultBlockResults{
  79. Height: 100,
  80. TxsResults: results.DeliverTxs,
  81. BeginBlockEvents: results.BeginBlock.Events,
  82. EndBlockEvents: results.EndBlock.Events,
  83. ValidatorUpdates: results.EndBlock.ValidatorUpdates,
  84. ConsensusParamUpdates: results.EndBlock.ConsensusParamUpdates,
  85. }},
  86. }
  87. for _, tc := range testCases {
  88. res, err := BlockResults(&rpctypes.Context{}, &tc.height)
  89. if tc.wantErr {
  90. assert.Error(t, err)
  91. } else {
  92. assert.NoError(t, err)
  93. assert.Equal(t, tc.wantRes, res)
  94. }
  95. }
  96. }
  97. type mockBlockStore struct {
  98. height int64
  99. }
  100. func (store mockBlockStore) Height() int64 { return store.height }
  101. func (mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return nil }
  102. func (mockBlockStore) LoadBlock(height int64) *types.Block { return nil }
  103. func (mockBlockStore) LoadBlockByHash(hash []byte) *types.Block { return nil }
  104. func (mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
  105. func (mockBlockStore) LoadBlockCommit(height int64) *types.Commit { return nil }
  106. func (mockBlockStore) LoadSeenCommit(height int64) *types.Commit { return nil }
  107. func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
  108. }