Skip to content

Commit

Permalink
fix: run validateBasic for vote before AddVote (#770)
Browse files Browse the repository at this point in the history
* fix: run validateBasic before adding vote

* chore: add test for panic
  • Loading branch information
jaeseung-bae committed Mar 26, 2024
1 parent e1be3c4 commit b954189
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 7 additions & 2 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"

vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"

"github.com/Finschia/ostracon/crypto"
"github.com/Finschia/ostracon/crypto/ed25519"
"github.com/Finschia/ostracon/crypto/merkle"
Expand All @@ -23,7 +25,6 @@ import (
tmsync "github.com/Finschia/ostracon/libs/sync"
ocproto "github.com/Finschia/ostracon/proto/ostracon/types"
"github.com/Finschia/ostracon/version"
vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
)

const (
Expand Down Expand Up @@ -824,7 +825,11 @@ func CommitToVoteSet(chainID string, commit *Commit, vals *ValidatorSet) *VoteSe
if commitSig.Absent() {
continue // OK, some precommits can be missing.
}
added, err := voteSet.AddVote(commit.GetVote(int32(idx)))
vote := commit.GetVote(int32(idx))
if err := vote.ValidateBasic(); err != nil {
panic(fmt.Errorf("failed to validate vote reconstructed from LastCommit: %w", err))
}
added, err := voteSet.AddVote(vote)
if !added || err != nil {
panic(fmt.Sprintf("Failed to reconstruct LastCommit: %v", err))
}
Expand Down
18 changes: 17 additions & 1 deletion types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"

vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"

"github.com/Finschia/ostracon/crypto"
"github.com/Finschia/ostracon/crypto/merkle"
"github.com/Finschia/ostracon/crypto/tmhash"
Expand All @@ -27,7 +29,6 @@ import (
tmrand "github.com/Finschia/ostracon/libs/rand"
tmtime "github.com/Finschia/ostracon/types/time"
"github.com/Finschia/ostracon/version"
vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
)

var TestConsensusVersion = tmversion.Consensus{
Expand Down Expand Up @@ -717,6 +718,21 @@ func TestCommitToVoteSet(t *testing.T) {
}
}

func TestCommitToVoteSetShouldPanicWhenInvalidVote(t *testing.T) {
voteSet, valSet, _ := randVoteSet(1, 1, tmproto.PrecommitType, 10, 1)
chainID := voteSet.ChainID()
commitWithInvalidVote := &Commit{
Height: 1,
Signatures: []CommitSig{{
BlockIDFlag: BlockIDFlagCommit,
}},
}

assert.Panics(t, func() {
CommitToVoteSet(chainID, commitWithInvalidVote, valSet)
})
}

func TestCommitToVoteSetWithVotesForNilBlock(t *testing.T) {
blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))

Expand Down

0 comments on commit b954189

Please sign in to comment.