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.

86 lines
2.4 KiB

  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. crypto "github.com/tendermint/go-crypto"
  7. )
  8. func TestValidateBlock(t *testing.T) {
  9. txs := []Tx{Tx("foo"), Tx("bar")}
  10. lastID := makeBlockID()
  11. valHash := []byte("val")
  12. appHash := []byte("app")
  13. consensusHash := []byte("consensus-params")
  14. h := int64(3)
  15. voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit,
  16. 10, 1)
  17. commit, err := makeCommit(lastID, h-1, 1, voteSet, vals)
  18. require.NoError(t, err)
  19. block, _ := MakeBlock(h, "hello", txs, 10, commit,
  20. lastID, valHash, appHash, consensusHash, 2)
  21. require.NotNil(t, block)
  22. // proper block must pass
  23. err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, appHash, consensusHash)
  24. require.NoError(t, err)
  25. // wrong chain fails
  26. err = block.ValidateBasic("other", h-1, 10, lastID, block.Time, appHash, consensusHash)
  27. require.Error(t, err)
  28. // wrong height fails
  29. err = block.ValidateBasic("hello", h+4, 10, lastID, block.Time, appHash, consensusHash)
  30. require.Error(t, err)
  31. // wrong total tx fails
  32. err = block.ValidateBasic("hello", h-1, 15, lastID, block.Time, appHash, consensusHash)
  33. require.Error(t, err)
  34. // wrong blockid fails
  35. err = block.ValidateBasic("hello", h-1, 10, makeBlockID(), block.Time, appHash, consensusHash)
  36. require.Error(t, err)
  37. // wrong app hash fails
  38. err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, []byte("bad-hash"), consensusHash)
  39. require.Error(t, err)
  40. // wrong consensus hash fails
  41. err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, appHash, []byte("wrong-params"))
  42. require.Error(t, err)
  43. }
  44. func makeBlockID() BlockID {
  45. blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
  46. return BlockID{blockHash, blockPartsHeader}
  47. }
  48. func makeCommit(blockID BlockID, height int64, round int,
  49. voteSet *VoteSet,
  50. validators []*PrivValidatorFS) (*Commit, error) {
  51. voteProto := &Vote{
  52. ValidatorAddress: nil,
  53. ValidatorIndex: -1,
  54. Height: height,
  55. Round: round,
  56. Type: VoteTypePrecommit,
  57. BlockID: blockID,
  58. Timestamp: time.Now().UTC(),
  59. }
  60. // all sign
  61. for i := 0; i < len(validators); i++ {
  62. vote := withValidator(voteProto, validators[i].GetAddress(), i)
  63. _, err := signAddVote(validators[i], vote, voteSet)
  64. if err != nil {
  65. return nil, err
  66. }
  67. }
  68. return voteSet.MakeCommit(), nil
  69. }