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.

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