From a6f719a4023dfcb9cb9536c913ffb322dd1f2517 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 13 Dec 2017 19:53:37 +0100 Subject: [PATCH] Add tests for block validation --- types/block_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 types/block_test.go diff --git a/types/block_test.go b/types/block_test.go new file mode 100644 index 000000000..f69de33f4 --- /dev/null +++ b/types/block_test.go @@ -0,0 +1,80 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/require" + crypto "github.com/tendermint/go-crypto" +) + +func TestValidateBlock(t *testing.T) { + txs := []Tx{Tx("foo"), Tx("bar")} + lastID := makeBlockID() + valHash := []byte("val") + appHash := []byte("app") + h := int64(3) + + voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit, + 10, 1) + commit, err := makeCommit(lastID, h-1, 1, voteSet, vals) + require.NoError(t, err) + + block, _ := MakeBlock(h, "hello", txs, 10, commit, + lastID, valHash, appHash, 2) + require.NotNil(t, block) + + // proper block must pass + err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, appHash) + require.NoError(t, err) + + // wrong chain fails + err = block.ValidateBasic("other", h-1, 10, lastID, block.Time, appHash) + require.Error(t, err) + + // wrong height fails + err = block.ValidateBasic("hello", h+4, 10, lastID, block.Time, appHash) + require.Error(t, err) + + // wrong total tx fails + err = block.ValidateBasic("hello", h-1, 15, lastID, block.Time, appHash) + require.Error(t, err) + + // wrong blockid fails + err = block.ValidateBasic("hello", h-1, 10, makeBlockID(), block.Time, appHash) + require.Error(t, err) + + // wrong app hash fails + err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, []byte("bad-hash")) + require.Error(t, err) + +} + +func makeBlockID() BlockID { + blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)} + return BlockID{blockHash, blockPartsHeader} +} + +func makeCommit(blockID BlockID, height int64, round int, + voteSet *VoteSet, + validators []*PrivValidatorFS) (*Commit, error) { + + voteProto := &Vote{ + ValidatorAddress: nil, + ValidatorIndex: -1, + Height: height, + Round: round, + Type: VoteTypePrecommit, + BlockID: blockID, + } + + // all sign + for i := 0; i < len(validators); i++ { + vote := withValidator(voteProto, validators[i].GetAddress(), i) + _, err := signAddVote(validators[i], vote, voteSet) + if err != nil { + return nil, err + } + } + + return voteSet.MakeCommit(), nil +}