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.

131 lines
3.4 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "sort"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. )
  9. var (
  10. valEd25519 = []string{ABCIPubKeyTypeEd25519}
  11. valSecp256k1 = []string{ABCIPubKeyTypeSecp256k1}
  12. )
  13. func TestConsensusParamsValidation(t *testing.T) {
  14. testCases := []struct {
  15. params ConsensusParams
  16. valid bool
  17. }{
  18. // test block params
  19. 0: {makeParams(1, 0, 10, 1, valEd25519), true},
  20. 1: {makeParams(0, 0, 10, 1, valEd25519), false},
  21. 2: {makeParams(47*1024*1024, 0, 10, 1, valEd25519), true},
  22. 3: {makeParams(10, 0, 10, 1, valEd25519), true},
  23. 4: {makeParams(100*1024*1024, 0, 10, 1, valEd25519), true},
  24. 5: {makeParams(101*1024*1024, 0, 10, 1, valEd25519), false},
  25. 6: {makeParams(1024*1024*1024, 0, 10, 1, valEd25519), false},
  26. 7: {makeParams(1024*1024*1024, 0, 10, -1, valEd25519), false},
  27. 8: {makeParams(1, 0, -10, 1, valEd25519), false},
  28. // test evidence params
  29. 9: {makeParams(1, 0, 10, 0, valEd25519), false},
  30. 10: {makeParams(1, 0, 10, -1, valEd25519), false},
  31. // test no pubkey type provided
  32. 11: {makeParams(1, 0, 10, 1, []string{}), false},
  33. // test invalid pubkey type provided
  34. 12: {makeParams(1, 0, 10, 1, []string{"potatoes make good pubkeys"}), false},
  35. }
  36. for i, tc := range testCases {
  37. if tc.valid {
  38. assert.NoErrorf(t, tc.params.Validate(), "expected no error for valid params (#%d)", i)
  39. } else {
  40. assert.Errorf(t, tc.params.Validate(), "expected error for non valid params (#%d)", i)
  41. }
  42. }
  43. }
  44. func makeParams(
  45. blockBytes, blockGas int64,
  46. blockTimeIotaMs int64,
  47. evidenceAge int64,
  48. pubkeyTypes []string,
  49. ) ConsensusParams {
  50. return ConsensusParams{
  51. Block: BlockParams{
  52. MaxBytes: blockBytes,
  53. MaxGas: blockGas,
  54. TimeIotaMs: blockTimeIotaMs,
  55. },
  56. Evidence: EvidenceParams{
  57. MaxAge: evidenceAge,
  58. },
  59. Validator: ValidatorParams{
  60. PubKeyTypes: pubkeyTypes,
  61. },
  62. }
  63. }
  64. func TestConsensusParamsHash(t *testing.T) {
  65. params := []ConsensusParams{
  66. makeParams(4, 2, 10, 3, valEd25519),
  67. makeParams(1, 4, 10, 3, valEd25519),
  68. makeParams(1, 2, 10, 4, valEd25519),
  69. makeParams(2, 5, 10, 7, valEd25519),
  70. makeParams(1, 7, 10, 6, valEd25519),
  71. makeParams(9, 5, 10, 4, valEd25519),
  72. makeParams(7, 8, 10, 9, valEd25519),
  73. makeParams(4, 6, 10, 5, valEd25519),
  74. }
  75. hashes := make([][]byte, len(params))
  76. for i := range params {
  77. hashes[i] = params[i].Hash()
  78. }
  79. // make sure there are no duplicates...
  80. // sort, then check in order for matches
  81. sort.Slice(hashes, func(i, j int) bool {
  82. return bytes.Compare(hashes[i], hashes[j]) < 0
  83. })
  84. for i := 0; i < len(hashes)-1; i++ {
  85. assert.NotEqual(t, hashes[i], hashes[i+1])
  86. }
  87. }
  88. func TestConsensusParamsUpdate(t *testing.T) {
  89. testCases := []struct {
  90. params ConsensusParams
  91. updates *abci.ConsensusParams
  92. updatedParams ConsensusParams
  93. }{
  94. // empty updates
  95. {
  96. makeParams(1, 2, 10, 3, valEd25519),
  97. &abci.ConsensusParams{},
  98. makeParams(1, 2, 10, 3, valEd25519),
  99. },
  100. // fine updates
  101. {
  102. makeParams(1, 2, 10, 3, valEd25519),
  103. &abci.ConsensusParams{
  104. Block: &abci.BlockParams{
  105. MaxBytes: 100,
  106. MaxGas: 200,
  107. TimeIotaMs: 300,
  108. },
  109. Evidence: &abci.EvidenceParams{
  110. MaxAge: 400,
  111. },
  112. Validator: &abci.ValidatorParams{
  113. PubKeyTypes: valSecp256k1,
  114. },
  115. },
  116. makeParams(100, 200, 300, 400, valSecp256k1),
  117. },
  118. }
  119. for _, tc := range testCases {
  120. assert.Equal(t, tc.updatedParams, tc.params.Update(tc.updates))
  121. }
  122. }