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.

133 lines
3.5 KiB

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