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.

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