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.

169 lines
4.5 KiB

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