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.

74 lines
1.9 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. crypto "github.com/tendermint/go-crypto"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. )
  8. func TestValidateBlock(t *testing.T) {
  9. txs := []Tx{Tx("foo"), Tx("bar")}
  10. lastID := makeBlockIDRandom()
  11. h := int64(3)
  12. voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  13. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  14. require.NoError(t, err)
  15. block := MakeBlock(h, txs, commit)
  16. require.NotNil(t, block)
  17. // proper block must pass
  18. err = block.ValidateBasic()
  19. require.NoError(t, err)
  20. // tamper with NumTxs
  21. block = MakeBlock(h, txs, commit)
  22. block.NumTxs += 1
  23. err = block.ValidateBasic()
  24. require.Error(t, err)
  25. // remove 1/2 the commits
  26. block = MakeBlock(h, txs, commit)
  27. block.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
  28. block.LastCommit.hash = nil // clear hash or change wont be noticed
  29. err = block.ValidateBasic()
  30. require.Error(t, err)
  31. // tamper with LastCommitHash
  32. block = MakeBlock(h, txs, commit)
  33. block.LastCommitHash = []byte("something else")
  34. err = block.ValidateBasic()
  35. require.Error(t, err)
  36. // tamper with data
  37. block = MakeBlock(h, txs, commit)
  38. block.Data.Txs[0] = Tx("something else")
  39. block.Data.hash = nil // clear hash or change wont be noticed
  40. err = block.ValidateBasic()
  41. require.Error(t, err)
  42. // tamper with DataHash
  43. block = MakeBlock(h, txs, commit)
  44. block.DataHash = cmn.RandBytes(len(block.DataHash))
  45. err = block.ValidateBasic()
  46. require.Error(t, err)
  47. }
  48. func makeBlockIDRandom() BlockID {
  49. blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
  50. return BlockID{blockHash, blockPartsHeader}
  51. }
  52. func makeBlockID(hash string, partSetSize int, partSetHash string) BlockID {
  53. return BlockID{
  54. Hash: []byte(hash),
  55. PartsHeader: PartSetHeader{
  56. Total: partSetSize,
  57. Hash: []byte(partSetHash),
  58. },
  59. }
  60. }