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.

150 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, 2, 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. maxEvidenceBytes int64,
  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. MaxBytes: maxEvidenceBytes,
  64. },
  65. Validator: tmproto.ValidatorParams{
  66. PubKeyTypes: pubkeyTypes,
  67. },
  68. }
  69. }
  70. func TestConsensusParamsHash(t *testing.T) {
  71. params := []tmproto.ConsensusParams{
  72. makeParams(4, 2, 10, 3, 1, valEd25519),
  73. makeParams(1, 4, 10, 3, 1, valEd25519),
  74. makeParams(1, 2, 10, 4, 1, valEd25519),
  75. makeParams(2, 5, 10, 7, 1, valEd25519),
  76. makeParams(1, 7, 10, 6, 1, valEd25519),
  77. makeParams(9, 5, 10, 4, 1, valEd25519),
  78. makeParams(7, 8, 10, 9, 1, valEd25519),
  79. makeParams(4, 6, 10, 5, 1, valEd25519),
  80. }
  81. hashes := make([][]byte, len(params))
  82. for i := range params {
  83. hashes[i] = HashConsensusParams(params[i])
  84. }
  85. // make sure there are no duplicates...
  86. // sort, then check in order for matches
  87. sort.Slice(hashes, func(i, j int) bool {
  88. return bytes.Compare(hashes[i], hashes[j]) < 0
  89. })
  90. for i := 0; i < len(hashes)-1; i++ {
  91. assert.NotEqual(t, hashes[i], hashes[i+1])
  92. }
  93. }
  94. func TestConsensusParamsUpdate(t *testing.T) {
  95. testCases := []struct {
  96. params tmproto.ConsensusParams
  97. updates *abci.ConsensusParams
  98. updatedParams tmproto.ConsensusParams
  99. }{
  100. // empty updates
  101. {
  102. makeParams(1, 2, 10, 3, 0, valEd25519),
  103. &abci.ConsensusParams{},
  104. makeParams(1, 2, 10, 3, 0, valEd25519),
  105. },
  106. // fine updates
  107. {
  108. makeParams(1, 2, 10, 3, 0, valEd25519),
  109. &abci.ConsensusParams{
  110. Block: &abci.BlockParams{
  111. MaxBytes: 100,
  112. MaxGas: 200,
  113. },
  114. Evidence: &tmproto.EvidenceParams{
  115. MaxAgeNumBlocks: 300,
  116. MaxAgeDuration: time.Duration(300),
  117. MaxBytes: 50,
  118. },
  119. Validator: &tmproto.ValidatorParams{
  120. PubKeyTypes: valEd25519,
  121. },
  122. },
  123. makeParams(100, 200, 10, 300, 50, valEd25519),
  124. },
  125. }
  126. for _, tc := range testCases {
  127. assert.Equal(t, tc.updatedParams, UpdateConsensusParams(tc.params, tc.updates))
  128. }
  129. }
  130. func TestConsensusParamsUpdate_AppVersion(t *testing.T) {
  131. params := makeParams(1, 2, 10, 3, 0, valEd25519)
  132. assert.EqualValues(t, 0, params.Version.AppVersion)
  133. updated := UpdateConsensusParams(params,
  134. &abci.ConsensusParams{Version: &tmproto.VersionParams{AppVersion: 1}})
  135. assert.EqualValues(t, 1, updated.Version.AppVersion)
  136. }