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.

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