Skip to content

Commit b954189

Browse files
authored
fix: run validateBasic for vote before AddVote (#770)
* fix: run validateBasic before adding vote * chore: add test for panic
1 parent e1be3c4 commit b954189

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

types/block.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1414
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
1515

16+
vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
17+
1618
"github.com/Finschia/ostracon/crypto"
1719
"github.com/Finschia/ostracon/crypto/ed25519"
1820
"github.com/Finschia/ostracon/crypto/merkle"
@@ -23,7 +25,6 @@ import (
2325
tmsync "github.com/Finschia/ostracon/libs/sync"
2426
ocproto "github.com/Finschia/ostracon/proto/ostracon/types"
2527
"github.com/Finschia/ostracon/version"
26-
vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
2728
)
2829

2930
const (
@@ -824,7 +825,11 @@ func CommitToVoteSet(chainID string, commit *Commit, vals *ValidatorSet) *VoteSe
824825
if commitSig.Absent() {
825826
continue // OK, some precommits can be missing.
826827
}
827-
added, err := voteSet.AddVote(commit.GetVote(int32(idx)))
828+
vote := commit.GetVote(int32(idx))
829+
if err := vote.ValidateBasic(); err != nil {
830+
panic(fmt.Errorf("failed to validate vote reconstructed from LastCommit: %w", err))
831+
}
832+
added, err := voteSet.AddVote(vote)
828833
if !added || err != nil {
829834
panic(fmt.Sprintf("Failed to reconstruct LastCommit: %v", err))
830835
}

types/block_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
2020
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
2121

22+
vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
23+
2224
"github.com/Finschia/ostracon/crypto"
2325
"github.com/Finschia/ostracon/crypto/merkle"
2426
"github.com/Finschia/ostracon/crypto/tmhash"
@@ -27,7 +29,6 @@ import (
2729
tmrand "github.com/Finschia/ostracon/libs/rand"
2830
tmtime "github.com/Finschia/ostracon/types/time"
2931
"github.com/Finschia/ostracon/version"
30-
vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
3132
)
3233

3334
var TestConsensusVersion = tmversion.Consensus{
@@ -717,6 +718,21 @@ func TestCommitToVoteSet(t *testing.T) {
717718
}
718719
}
719720

721+
func TestCommitToVoteSetShouldPanicWhenInvalidVote(t *testing.T) {
722+
voteSet, valSet, _ := randVoteSet(1, 1, tmproto.PrecommitType, 10, 1)
723+
chainID := voteSet.ChainID()
724+
commitWithInvalidVote := &Commit{
725+
Height: 1,
726+
Signatures: []CommitSig{{
727+
BlockIDFlag: BlockIDFlagCommit,
728+
}},
729+
}
730+
731+
assert.Panics(t, func() {
732+
CommitToVoteSet(chainID, commitWithInvalidVote, valSet)
733+
})
734+
}
735+
720736
func TestCommitToVoteSetWithVotesForNilBlock(t *testing.T) {
721737
blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
722738

0 commit comments

Comments
 (0)