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.

124 lines
4.4 KiB

  1. package state
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/crypto/ed25519"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. "github.com/tendermint/tendermint/libs/log"
  9. "github.com/tendermint/tendermint/types"
  10. )
  11. // TODO(#2589):
  12. // - generalize this past the first height
  13. // - add txs and build up full State properly
  14. // - test block.Time (see #2587 - there are no conditions on time for the first height)
  15. func TestValidateBlockHeader(t *testing.T) {
  16. var height int64 = 1 // TODO(#2589): generalize
  17. state, stateDB := state(1, int(height))
  18. blockExec := NewBlockExecutor(stateDB, log.TestingLogger(), nil, nil, nil)
  19. // A good block passes.
  20. block := makeBlock(state, height)
  21. err := blockExec.ValidateBlock(state, block)
  22. require.NoError(t, err)
  23. wrongHash := tmhash.Sum([]byte("this hash is wrong"))
  24. // Manipulation of any header field causes failure.
  25. testCases := []struct {
  26. name string
  27. malleateBlock func(block *types.Block)
  28. }{
  29. {"ChainID wrong", func(block *types.Block) { block.ChainID = "not-the-real-one" }},
  30. {"Height wrong", func(block *types.Block) { block.Height += 10 }},
  31. {"Time wrong", func(block *types.Block) { block.Time = block.Time.Add(-time.Second * 3600 * 24) }},
  32. {"NumTxs wrong", func(block *types.Block) { block.NumTxs += 10 }},
  33. {"TotalTxs wrong", func(block *types.Block) { block.TotalTxs += 10 }},
  34. {"LastBlockID wrong", func(block *types.Block) { block.LastBlockID.PartsHeader.Total += 10 }},
  35. {"LastCommitHash wrong", func(block *types.Block) { block.LastCommitHash = wrongHash }},
  36. {"DataHash wrong", func(block *types.Block) { block.DataHash = wrongHash }},
  37. {"ValidatorsHash wrong", func(block *types.Block) { block.ValidatorsHash = wrongHash }},
  38. {"NextValidatorsHash wrong", func(block *types.Block) { block.NextValidatorsHash = wrongHash }},
  39. {"ConsensusHash wrong", func(block *types.Block) { block.ConsensusHash = wrongHash }},
  40. {"AppHash wrong", func(block *types.Block) { block.AppHash = wrongHash }},
  41. {"LastResultsHash wrong", func(block *types.Block) { block.LastResultsHash = wrongHash }},
  42. {"EvidenceHash wrong", func(block *types.Block) { block.EvidenceHash = wrongHash }},
  43. {"Proposer wrong", func(block *types.Block) { block.ProposerAddress = ed25519.GenPrivKey().PubKey().Address() }},
  44. {"Proposer invalid", func(block *types.Block) { block.ProposerAddress = []byte("wrong size") }},
  45. }
  46. for _, tc := range testCases {
  47. block := makeBlock(state, height)
  48. tc.malleateBlock(block)
  49. err := blockExec.ValidateBlock(state, block)
  50. require.Error(t, err, tc.name)
  51. }
  52. }
  53. /*
  54. TODO(#2589):
  55. - test Block.Data.Hash() == Block.DataHash
  56. - test len(Block.Data.Txs) == Block.NumTxs
  57. */
  58. func TestValidateBlockData(t *testing.T) {
  59. }
  60. /*
  61. TODO(#2589):
  62. - test len(block.LastCommit.Precommits) == state.LastValidators.Size()
  63. - test state.LastValidators.VerifyCommit
  64. */
  65. func TestValidateBlockCommit(t *testing.T) {
  66. }
  67. /*
  68. TODO(#2589):
  69. - test good/bad evidence in block
  70. */
  71. func TestValidateBlockEvidence(t *testing.T) {
  72. var height int64 = 1 // TODO(#2589): generalize
  73. state, stateDB := state(1, int(height))
  74. blockExec := NewBlockExecutor(stateDB, log.TestingLogger(), nil, nil, nil)
  75. // make some evidence
  76. addr, _ := state.Validators.GetByIndex(0)
  77. goodEvidence := types.NewMockGoodEvidence(height, 0, addr)
  78. // A block with a couple pieces of evidence passes.
  79. block := makeBlock(state, height)
  80. block.Evidence.Evidence = []types.Evidence{goodEvidence, goodEvidence}
  81. block.EvidenceHash = block.Evidence.Hash()
  82. err := blockExec.ValidateBlock(state, block)
  83. require.NoError(t, err)
  84. // A block with too much evidence fails.
  85. maxBlockSize := state.ConsensusParams.BlockSize.MaxBytes
  86. maxEvidenceBytes := types.MaxEvidenceBytesPerBlock(maxBlockSize)
  87. maxEvidence := maxEvidenceBytes / types.MaxEvidenceBytes
  88. require.True(t, maxEvidence > 2)
  89. for i := int64(0); i < maxEvidence; i++ {
  90. block.Evidence.Evidence = append(block.Evidence.Evidence, goodEvidence)
  91. }
  92. block.EvidenceHash = block.Evidence.Hash()
  93. err = blockExec.ValidateBlock(state, block)
  94. require.Error(t, err)
  95. _, ok := err.(*types.ErrEvidenceOverflow)
  96. require.True(t, ok)
  97. }
  98. /*
  99. TODO(#2589):
  100. - test unmarshalling BlockParts that are too big into a Block that
  101. (note this logic happens in the consensus, not in the validation here).
  102. - test making blocks from the types.MaxXXX functions works/fails as expected
  103. */
  104. func TestValidateBlockSize(t *testing.T) {
  105. }