2017-12-13 10:53:37 -08:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
crypto "github.com/tendermint/go-crypto"
|
2017-12-20 20:53:15 -08:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2017-12-13 10:53:37 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateBlock(t *testing.T) {
|
|
|
|
txs := []Tx{Tx("foo"), Tx("bar")}
|
2017-12-26 17:34:57 -08:00
|
|
|
lastID := makeBlockIDRandom()
|
2017-12-13 10:53:37 -08:00
|
|
|
h := int64(3)
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
|
2017-12-20 20:53:15 -08:00
|
|
|
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
|
2017-12-13 10:53:37 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-12-20 20:53:15 -08:00
|
|
|
block := MakeBlock(h, txs, commit)
|
2017-12-13 10:53:37 -08:00
|
|
|
require.NotNil(t, block)
|
|
|
|
|
|
|
|
// proper block must pass
|
2017-12-20 20:53:15 -08:00
|
|
|
err = block.ValidateBasic()
|
2017-12-13 10:53:37 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-12-20 20:53:15 -08:00
|
|
|
// tamper with NumTxs
|
|
|
|
block = MakeBlock(h, txs, commit)
|
|
|
|
block.NumTxs += 1
|
|
|
|
err = block.ValidateBasic()
|
2017-12-13 10:53:37 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2017-12-20 20:53:15 -08:00
|
|
|
// remove 1/2 the commits
|
|
|
|
block = MakeBlock(h, txs, commit)
|
|
|
|
block.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
|
|
|
|
block.LastCommit.hash = nil // clear hash or change wont be noticed
|
|
|
|
err = block.ValidateBasic()
|
2017-12-13 10:53:37 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2017-12-20 20:53:15 -08:00
|
|
|
// tamper with LastCommitHash
|
|
|
|
block = MakeBlock(h, txs, commit)
|
|
|
|
block.LastCommitHash = []byte("something else")
|
|
|
|
err = block.ValidateBasic()
|
2017-12-13 10:53:37 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2017-12-20 20:53:15 -08:00
|
|
|
// tamper with data
|
|
|
|
block = MakeBlock(h, txs, commit)
|
|
|
|
block.Data.Txs[0] = Tx("something else")
|
|
|
|
block.Data.hash = nil // clear hash or change wont be noticed
|
|
|
|
err = block.ValidateBasic()
|
2017-12-13 10:53:37 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2017-12-20 20:53:15 -08:00
|
|
|
// tamper with DataHash
|
|
|
|
block = MakeBlock(h, txs, commit)
|
|
|
|
block.DataHash = cmn.RandBytes(len(block.DataHash))
|
|
|
|
err = block.ValidateBasic()
|
2017-12-14 01:28:04 -08:00
|
|
|
require.Error(t, err)
|
2017-12-13 10:53:37 -08:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:34:57 -08:00
|
|
|
func makeBlockIDRandom() BlockID {
|
2017-12-13 10:53:37 -08:00
|
|
|
blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
|
|
|
|
return BlockID{blockHash, blockPartsHeader}
|
|
|
|
}
|
2017-12-26 17:34:57 -08:00
|
|
|
|
|
|
|
func makeBlockID(hash string, partSetSize int, partSetHash string) BlockID {
|
|
|
|
return BlockID{
|
|
|
|
Hash: []byte(hash),
|
|
|
|
PartsHeader: PartSetHeader{
|
|
|
|
Total: partSetSize,
|
|
|
|
Hash: []byte(partSetHash),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|