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.3 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. 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, 2, 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. maxEvidenceBytes int64,
  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. MaxBytes: maxEvidenceBytes,
  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. MaxBytes: 50,
  119. },
  120. Validator: &tmproto.ValidatorParams{
  121. PubKeyTypes: valSecp256k1,
  122. },
  123. },
  124. makeParams(100, 200, 10, 300, 50, valSecp256k1),
  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. }