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 authored 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.