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
4.5 KiB

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