update the VoteSet methods to only validate extensions when enabled

This commit is contained in:
William Banfield
2022-05-16 19:08:34 -04:00
parent 78c0c9c6ce
commit 44f1f045e2
7 changed files with 29 additions and 24 deletions
+4 -3
View File
@@ -1014,18 +1014,19 @@ func (ec *ExtendedCommit) Clone() *ExtendedCommit {
return &ecc
}
// ToStrictVoteSet constructs a VoteSet from the Commit and validator set.
// ToExtendedVoteSet constructs a VoteSet from the Commit and validator set.
// Panics if signatures from the ExtendedCommit can't be added to the voteset.
// Panics if any of the votes have invalid or absent vote extension data.
// Inverse of VoteSet.MakeExtendedCommit().
func (ec *ExtendedCommit) ToStrictVoteSet(chainID string, vals *ValidatorSet) *VoteSet {
voteSet := NewStrictVoteSet(chainID, ec.Height, ec.Round, tmproto.PrecommitType, vals)
func (ec *ExtendedCommit) ToExtendedVoteSet(chainID string, vals *ValidatorSet) *VoteSet {
voteSet := NewExtendedVoteSet(chainID, ec.Height, ec.Round, tmproto.PrecommitType, vals)
ec.addSigsToVoteSet(voteSet)
return voteSet
}
// ToVoteSet constructs a VoteSet from the Commit and validator set.
// Panics if signatures from the ExtendedCommit can't be added to the voteset.
// Panics if any of the votes have extension data.
// Inverse of VoteSet.MakeExtendedCommit().
func (ec *ExtendedCommit) ToVoteSet(chainID string, vals *ValidatorSet) *VoteSet {
voteSet := NewVoteSet(chainID, ec.Height, ec.Round, tmproto.PrecommitType, vals)
+2 -2
View File
@@ -583,7 +583,7 @@ func TestVoteSetToExtendedCommit(t *testing.T) {
valSet, vals := randValidatorPrivValSet(ctx, t, 10, 1)
var voteSet *VoteSet
if testCase.includeExtension {
voteSet = NewStrictVoteSet("test_chain_id", 3, 1, tmproto.PrecommitType, valSet)
voteSet = NewExtendedVoteSet("test_chain_id", 3, 1, tmproto.PrecommitType, valSet)
} else {
voteSet = NewVoteSet("test_chain_id", 3, 1, tmproto.PrecommitType, valSet)
}
@@ -668,7 +668,7 @@ func TestExtendedCommitToVoteSet(t *testing.T) {
chainID := voteSet.ChainID()
var voteSet2 *VoteSet
if testCase.includeExtension {
voteSet2 = extCommit.ToStrictVoteSet(chainID, valSet)
voteSet2 = extCommit.ToExtendedVoteSet(chainID, valSet)
} else {
voteSet2 = extCommit.ToVoteSet(chainID, valSet)
}
+10 -6
View File
@@ -3,6 +3,7 @@ package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
@@ -58,7 +59,7 @@ type VoteSet struct {
round int32
signedMsgType tmproto.SignedMsgType
valSet *ValidatorSet
requireExtensions bool
extensionsEnabled bool
mtx sync.Mutex
votesBitArray *bits.BitArray
@@ -93,13 +94,13 @@ func NewVoteSet(chainID string, height int64, round int32,
}
}
// NewStrictVoteSet constructs a vote set with additional vote verification logic.
// The VoteSet constructed with NewStrictVoteSet verifies the vote extension
// NewExtendedVoteSet constructs a vote set with additional vote verification logic.
// The VoteSet constructed with NewExtendedVoteSet verifies the vote extension
// data for every vote added to the set.
func NewStrictVoteSet(chainID string, height int64, round int32,
func NewExtendedVoteSet(chainID string, height int64, round int32,
signedMsgType tmproto.SignedMsgType, valSet *ValidatorSet) *VoteSet {
vs := NewVoteSet(chainID, height, round, signedMsgType, valSet)
vs.requireExtensions = true
vs.extensionsEnabled = true
return vs
}
@@ -208,7 +209,7 @@ func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) {
}
// Check signature.
if voteSet.requireExtensions || len(vote.ExtensionSignature) > 0 {
if voteSet.extensionsEnabled {
if err := vote.VerifyVoteAndExtension(voteSet.chainID, val.PubKey); err != nil {
return false, fmt.Errorf("failed to verify vote with ChainID %s and PubKey %s: %w", voteSet.chainID, val.PubKey, err)
}
@@ -216,6 +217,9 @@ func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) {
if err := vote.Verify(voteSet.chainID, val.PubKey); err != nil {
return false, fmt.Errorf("failed to verify vote with ChainID %s and PubKey %s: %w", voteSet.chainID, val.PubKey, err)
}
if len(vote.ExtensionSignature) > 0 || len(vote.Extension) > 0 {
return false, errors.New("unexpected vote extension data present in vote")
}
}
// Add vote and get conflicting vote if any.
+3 -3
View File
@@ -534,7 +534,7 @@ func TestVoteSet_VoteExtensionsEnabled(t *testing.T) {
valSet, privValidators := randValidatorPrivValSet(ctx, t, 5, 10)
var voteSet *VoteSet
if tc.requireExtensions {
voteSet = NewStrictVoteSet("test_chain_id", height, round, tmproto.PrecommitType, valSet)
voteSet = NewExtendedVoteSet("test_chain_id", height, round, tmproto.PrecommitType, valSet)
} else {
voteSet = NewVoteSet("test_chain_id", height, round, tmproto.PrecommitType, valSet)
}
@@ -590,7 +590,7 @@ func randVoteSet(
) (*VoteSet, *ValidatorSet, []PrivValidator) {
t.Helper()
valSet, privValidators := randValidatorPrivValSet(ctx, t, numValidators, votingPower)
return NewStrictVoteSet("test_chain_id", height, round, signedMsgType, valSet), valSet, privValidators
return NewExtendedVoteSet("test_chain_id", height, round, signedMsgType, valSet), valSet, privValidators
}
func deterministicVoteSet(
@@ -603,7 +603,7 @@ func deterministicVoteSet(
) (*VoteSet, *ValidatorSet, []PrivValidator) {
t.Helper()
valSet, privValidators := deterministicValidatorSet(ctx, t)
return NewStrictVoteSet("test_chain_id", height, round, signedMsgType, valSet), valSet, privValidators
return NewExtendedVoteSet("test_chain_id", height, round, signedMsgType, valSet), valSet, privValidators
}
func randValidatorPrivValSet(ctx context.Context, t testing.TB, numValidators int, votingPower int64) (*ValidatorSet, []PrivValidator) {