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.

78 lines
2.1 KiB

  1. package state
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/tendermint/tendermint/crypto/ed25519"
  6. dbm "github.com/tendermint/tendermint/libs/db"
  7. "github.com/tendermint/tendermint/libs/log"
  8. )
  9. func TestValidateBlock(t *testing.T) {
  10. state, _ := state(1, 1)
  11. blockExec := NewBlockExecutor(dbm.NewMemDB(), log.TestingLogger(), nil, nil, nil)
  12. // proper block must pass
  13. block := makeBlock(state, 1)
  14. err := blockExec.ValidateBlock(state, block)
  15. require.NoError(t, err)
  16. // wrong chain fails
  17. block = makeBlock(state, 1)
  18. block.ChainID = "not-the-real-one"
  19. err = blockExec.ValidateBlock(state, block)
  20. require.Error(t, err)
  21. // wrong height fails
  22. block = makeBlock(state, 1)
  23. block.Height += 10
  24. err = blockExec.ValidateBlock(state, block)
  25. require.Error(t, err)
  26. // wrong total tx fails
  27. block = makeBlock(state, 1)
  28. block.TotalTxs += 10
  29. err = blockExec.ValidateBlock(state, block)
  30. require.Error(t, err)
  31. // wrong blockid fails
  32. block = makeBlock(state, 1)
  33. block.LastBlockID.PartsHeader.Total += 10
  34. err = blockExec.ValidateBlock(state, block)
  35. require.Error(t, err)
  36. // wrong app hash fails
  37. block = makeBlock(state, 1)
  38. block.AppHash = []byte("wrong app hash")
  39. err = blockExec.ValidateBlock(state, block)
  40. require.Error(t, err)
  41. // wrong consensus hash fails
  42. block = makeBlock(state, 1)
  43. block.ConsensusHash = []byte("wrong consensus hash")
  44. err = blockExec.ValidateBlock(state, block)
  45. require.Error(t, err)
  46. // wrong results hash fails
  47. block = makeBlock(state, 1)
  48. block.LastResultsHash = []byte("wrong results hash")
  49. err = blockExec.ValidateBlock(state, block)
  50. require.Error(t, err)
  51. // wrong validators hash fails
  52. block = makeBlock(state, 1)
  53. block.ValidatorsHash = []byte("wrong validators hash")
  54. err = blockExec.ValidateBlock(state, block)
  55. require.Error(t, err)
  56. // wrong proposer address
  57. block = makeBlock(state, 1)
  58. block.ProposerAddress = ed25519.GenPrivKey().PubKey().Address()
  59. err = blockExec.ValidateBlock(state, block)
  60. require.Error(t, err)
  61. block.ProposerAddress = []byte("wrong size")
  62. err = blockExec.ValidateBlock(state, block)
  63. require.Error(t, err)
  64. }