Skip to content

Commit

Permalink
Fix typo of the function ValidateBasic (#504)
Browse files Browse the repository at this point in the history
* fix typo of the function ValidateBasic

* Add test case invalid voters hash
  • Loading branch information
Mdaiki0730 authored Nov 24, 2022
1 parent e6f57a1 commit 6c0f491
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (h Header) ValidateBasic() error {
// Basic validation of hashes related to application data.
// Will validate fully against state in state#ValidateBlock.
if err := ValidateHash(h.VotersHash); err != nil {
return fmt.Errorf("wrong ValidatorsHash: %v", err)
return fmt.Errorf("wrong VotersHash: %v", err)
}
// TODO When we add `Header.ValidatorsHash` in a future commit, we have to add a similar check here.
if err := ValidateHash(h.NextValidatorsHash); err != nil {
Expand Down
85 changes: 85 additions & 0 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,91 @@ func TestHeaderHash(t *testing.T) {
}
}

func TestHeaderValidateBasic(t *testing.T) {
invalidHashLength := tmhash.Size - 1

testCases := []struct {
testName string
malleateHeader func(*Header)
expErr bool
}{
{"Make Header", func(header *Header) {}, false},
{"Incorrect block protocol version", func(header *Header) {
header.Version.Block = uint64(1)
}, true},
{"Too long chainID", func(header *Header) {
header.ChainID = "long chainID" + strings.Repeat("-", MaxChainIDLen)
}, true},
{"Negative Height", func(header *Header) {
header.Height = -1
}, true},
{"Zero Height", func(header *Header) {
header.Height = 0
}, true},
{"Invalid Last Block ID", func(header *Header) {
header.LastBlockID = BlockID{
Hash: make([]byte, invalidHashLength),
PartSetHeader: PartSetHeader{
Total: 6,
Hash: make([]byte, invalidHashLength),
},
}
}, true},
{"Invalid Last Commit Hash", func(header *Header) {
header.LastCommitHash = []byte(strings.Repeat("h", invalidHashLength))
}, true},
{"Invalid Data Hash", func(header *Header) {
header.DataHash = []byte(strings.Repeat("h", invalidHashLength))
}, true},
{"Invalid Evidence Hash", func(header *Header) {
header.EvidenceHash = []byte(strings.Repeat("h", invalidHashLength))
}, true},
{"Invalid Proposer Address length", func(header *Header) {
header.ProposerAddress = make([]byte, crypto.AddressSize-1)
}, true},
{"Invalid Voters Hash", func(header *Header) {
header.VotersHash = []byte(strings.Repeat("h", invalidHashLength))
}, true},
{"Invalid Next Validators Hash", func(header *Header) {
header.NextValidatorsHash = []byte(strings.Repeat("h", invalidHashLength))
}, true},
{"Invalid Consensus Hash", func(header *Header) {
header.ConsensusHash = []byte(strings.Repeat("h", invalidHashLength))
}, true},
{"Invalid Results Hash", func(header *Header) {
header.LastResultsHash = []byte(strings.Repeat("h", invalidHashLength))
}, true},
}
for i, tc := range testCases {
tc := tc
i := i
t.Run(tc.testName, func(t *testing.T) {
header := &Header{
Version: tmversion.Consensus{Block: version.BlockProtocol, App: 2},
ChainID: "chainId",
Height: 3,
Time: time.Date(2019, 10, 13, 16, 14, 44, 0, time.UTC),
LastBlockID: makeBlockID(make([]byte, tmhash.Size), 6, make([]byte, tmhash.Size)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
VotersHash: tmhash.Sum([]byte("voters_hash")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
AppHash: tmhash.Sum([]byte("app_hash")),
LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: crypto.AddressHash([]byte("proposer_address")),
Round: 1,
Proof: tmhash.Sum([]byte("proof")),
}
tc.malleateHeader(header)
err := header.ValidateBasic()
assert.Equal(t, tc.expErr, err != nil, "#%d: %v", i, err)
})
}
}

func TestMaxHeaderBytes(t *testing.T) {
// Construct a UTF-8 string of MaxChainIDLen length using the supplementary
// characters.
Expand Down

0 comments on commit 6c0f491

Please sign in to comment.