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.

123 lines
3.2 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "sort"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. )
  9. var (
  10. valEd25519 = []string{ABCIPubKeyTypeEd25519}
  11. valSecp256k1 = []string{ABCIPubKeyTypeSecp256k1}
  12. )
  13. func TestConsensusParamsValidation(t *testing.T) {
  14. testCases := []struct {
  15. params ConsensusParams
  16. valid bool
  17. }{
  18. // test block size
  19. 0: {makeParams(1, 0, 1, valEd25519), true},
  20. 1: {makeParams(0, 0, 1, valEd25519), false},
  21. 2: {makeParams(47*1024*1024, 0, 1, valEd25519), true},
  22. 3: {makeParams(10, 0, 1, valEd25519), true},
  23. 4: {makeParams(100*1024*1024, 0, 1, valEd25519), true},
  24. 5: {makeParams(101*1024*1024, 0, 1, valEd25519), false},
  25. 6: {makeParams(1024*1024*1024, 0, 1, valEd25519), false},
  26. 7: {makeParams(1024*1024*1024, 0, -1, valEd25519), false},
  27. // test evidence age
  28. 8: {makeParams(1, 0, 0, valEd25519), false},
  29. 9: {makeParams(1, 0, -1, valEd25519), false},
  30. // test no pubkey type provided
  31. 10: {makeParams(1, 0, 1, []string{}), false},
  32. // test invalid pubkey type provided
  33. 11: {makeParams(1, 0, 1, []string{"potatoes make good pubkeys"}), false},
  34. }
  35. for i, tc := range testCases {
  36. if tc.valid {
  37. assert.NoErrorf(t, tc.params.Validate(), "expected no error for valid params (#%d)", i)
  38. } else {
  39. assert.Errorf(t, tc.params.Validate(), "expected error for non valid params (#%d)", i)
  40. }
  41. }
  42. }
  43. func makeParams(blockBytes, blockGas, evidenceAge int64, pubkeyTypes []string) ConsensusParams {
  44. return ConsensusParams{
  45. BlockSize: BlockSizeParams{
  46. MaxBytes: blockBytes,
  47. MaxGas: blockGas,
  48. },
  49. Evidence: EvidenceParams{
  50. MaxAge: evidenceAge,
  51. },
  52. Validator: ValidatorParams{
  53. PubKeyTypes: pubkeyTypes,
  54. },
  55. }
  56. }
  57. func TestConsensusParamsHash(t *testing.T) {
  58. params := []ConsensusParams{
  59. makeParams(4, 2, 3, valEd25519),
  60. makeParams(1, 4, 3, valEd25519),
  61. makeParams(1, 2, 4, valEd25519),
  62. makeParams(2, 5, 7, valEd25519),
  63. makeParams(1, 7, 6, valEd25519),
  64. makeParams(9, 5, 4, valEd25519),
  65. makeParams(7, 8, 9, valEd25519),
  66. makeParams(4, 6, 5, valEd25519),
  67. }
  68. hashes := make([][]byte, len(params))
  69. for i := range params {
  70. hashes[i] = params[i].Hash()
  71. }
  72. // make sure there are no duplicates...
  73. // sort, then check in order for matches
  74. sort.Slice(hashes, func(i, j int) bool {
  75. return bytes.Compare(hashes[i], hashes[j]) < 0
  76. })
  77. for i := 0; i < len(hashes)-1; i++ {
  78. assert.NotEqual(t, hashes[i], hashes[i+1])
  79. }
  80. }
  81. func TestConsensusParamsUpdate(t *testing.T) {
  82. testCases := []struct {
  83. params ConsensusParams
  84. updates *abci.ConsensusParams
  85. updatedParams ConsensusParams
  86. }{
  87. // empty updates
  88. {
  89. makeParams(1, 2, 3, valEd25519),
  90. &abci.ConsensusParams{},
  91. makeParams(1, 2, 3, valEd25519),
  92. },
  93. // fine updates
  94. {
  95. makeParams(1, 2, 3, valEd25519),
  96. &abci.ConsensusParams{
  97. BlockSize: &abci.BlockSizeParams{
  98. MaxBytes: 100,
  99. MaxGas: 200,
  100. },
  101. Evidence: &abci.EvidenceParams{
  102. MaxAge: 300,
  103. },
  104. Validator: &abci.ValidatorParams{
  105. PubKeyTypes: valSecp256k1,
  106. },
  107. },
  108. makeParams(100, 200, 300, valSecp256k1),
  109. },
  110. }
  111. for _, tc := range testCases {
  112. assert.Equal(t, tc.updatedParams, tc.params.Update(tc.updates))
  113. }
  114. }