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.

88 lines
2.2 KiB

7 years ago
7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "sort"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func newConsensusParams(blockSize, partSize int) ConsensusParams {
  9. return ConsensusParams{
  10. BlockSize: BlockSize{MaxBytes: blockSize},
  11. BlockGossip: BlockGossip{BlockPartSizeBytes: partSize},
  12. }
  13. }
  14. func TestConsensusParamsValidation(t *testing.T) {
  15. testCases := []struct {
  16. params ConsensusParams
  17. valid bool
  18. }{
  19. {newConsensusParams(1, 1), true},
  20. {newConsensusParams(1, 0), false},
  21. {newConsensusParams(0, 1), false},
  22. {newConsensusParams(0, 0), false},
  23. {newConsensusParams(0, 10), false},
  24. {newConsensusParams(10, -1), false},
  25. {newConsensusParams(47*1024*1024, 400), true},
  26. {newConsensusParams(10, 400), true},
  27. {newConsensusParams(100*1024*1024, 400), true},
  28. {newConsensusParams(101*1024*1024, 400), false},
  29. {newConsensusParams(1024*1024*1024, 400), false},
  30. }
  31. for _, testCase := range testCases {
  32. if testCase.valid {
  33. assert.NoError(t, testCase.params.Validate(), "expected no error for valid params")
  34. } else {
  35. assert.Error(t, testCase.params.Validate(), "expected error for non valid params")
  36. }
  37. }
  38. }
  39. func makeParams(blockBytes, blockTx, blockGas, txBytes,
  40. txGas, partSize int) ConsensusParams {
  41. return ConsensusParams{
  42. BlockSize: BlockSize{
  43. MaxBytes: blockBytes,
  44. MaxTxs: blockTx,
  45. MaxGas: int64(blockGas),
  46. },
  47. TxSize: TxSize{
  48. MaxBytes: txBytes,
  49. MaxGas: int64(txGas),
  50. },
  51. BlockGossip: BlockGossip{
  52. BlockPartSizeBytes: partSize,
  53. },
  54. }
  55. }
  56. func TestConsensusParamsHash(t *testing.T) {
  57. params := []ConsensusParams{
  58. makeParams(1, 2, 3, 4, 5, 6),
  59. makeParams(7, 2, 3, 4, 5, 6),
  60. makeParams(1, 7, 3, 4, 5, 6),
  61. makeParams(1, 2, 7, 4, 5, 6),
  62. makeParams(1, 2, 3, 7, 5, 6),
  63. makeParams(1, 2, 3, 4, 7, 6),
  64. makeParams(1, 2, 3, 4, 5, 7),
  65. makeParams(6, 5, 4, 3, 2, 1),
  66. }
  67. hashes := make([][]byte, len(params))
  68. for i := range params {
  69. hashes[i] = params[i].Hash()
  70. }
  71. // make sure there are no duplicates...
  72. // sort, then check in order for matches
  73. sort.Slice(hashes, func(i, j int) bool {
  74. return bytes.Compare(hashes[i], hashes[j]) < 0
  75. })
  76. for i := 0; i < len(hashes)-1; i++ {
  77. assert.NotEqual(t, hashes[i], hashes[i+1])
  78. }
  79. }