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.

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