Skip to content

Commit

Permalink
fix(ve): Ensure that sdk side ve math matches cometbft (backport #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jan 25, 2024
1 parent 861a7d7 commit 0abf94a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* [#19106](https://github.com/cosmos/cosmos-sdk/pull/19106) Allow empty public keys when setting signatures. Public keys aren't needed for every transaction.
* (abci): [#19200](https://github.com/cosmos/cosmos-sdk/pull/19200) Ensure that sdk side ve math matches cometbft
* [#19106](https://github.com/cosmos/cosmos-sdk/pull/19106) Allow empty public keys when setting signatures. Public keys aren't needed for every transaction.

## [v0.50.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.3) - 2023-01-15

Expand Down
17 changes: 16 additions & 1 deletion baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2197,11 +2197,25 @@ func TestBaseApp_VoteExtensions(t *testing.T) {
}
require.Equal(t, 10, successful)

extVotes := []abci.ExtendedVoteInfo{}
for _, val := range vals {
extVotes = append(extVotes, abci.ExtendedVoteInfo{
VoteExtension: allVEs[0],
BlockIdFlag: cmtproto.BlockIDFlagCommit,
ExtensionSignature: []byte{},
Validator: abci.Validator{
Address: val.Bytes(),
Power: 666,
},
},
)
}

prepPropReq := &abci.RequestPrepareProposal{
Height: 1,
LocalLastCommit: abci.ExtendedCommitInfo{
Round: 0,
Votes: []abci.ExtendedVoteInfo{},
Votes: extVotes,
},
}

Expand Down Expand Up @@ -2260,6 +2274,7 @@ func TestBaseApp_VoteExtensions(t *testing.T) {
ExtensionSignature: extSig,
Validator: abci.Validator{
Address: vals[i].Bytes(),
Power: 666,
},
})
}
Expand Down
22 changes: 10 additions & 12 deletions baseapp/abci_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ import (
protoio "github.com/cosmos/gogoproto/io"
"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
)

// VoteExtensionThreshold defines the total voting power % that must be
// submitted in order for all vote extensions to be considered valid for a
// given height.
var VoteExtensionThreshold = math.LegacyNewDecWithPrec(667, 3)

type (
// ValidatorStore defines the interface contract require for verifying vote
// extension signatures. Typically, this will be implemented by the x/staking
Expand Down Expand Up @@ -132,13 +125,18 @@ func ValidateVoteExtensions(
sumVP += vote.Validator.Power
}

if totalVP > 0 {
percentSubmitted := math.LegacyNewDecFromInt(math.NewInt(sumVP)).Quo(math.LegacyNewDecFromInt(math.NewInt(totalVP)))
if percentSubmitted.LT(VoteExtensionThreshold) {
return fmt.Errorf("insufficient cumulative voting power received to verify vote extensions; got: %s, expected: >=%s", percentSubmitted, VoteExtensionThreshold)
}
// This check is probably unnecessary, but better safe than sorry.
if totalVP <= 0 {
return fmt.Errorf("total voting power must be positive, got: %d", totalVP)
}

// If the sum of the voting power has not reached (2/3 + 1) we need to error.
if requiredVP := ((totalVP * 2) / 3) + 1; sumVP < requiredVP {
return fmt.Errorf(
"insufficient cumulative voting power received to verify vote extensions; got: %d, expected: >=%d",
sumVP, requiredVP,
)
}
return nil
}

Expand Down

0 comments on commit 0abf94a

Please sign in to comment.