Skip to content

Commit 8120a86

Browse files
author
Devon Bear
authored
fix(ve): Ensure that sdk side ve math matches cometbft (#19200)
1 parent fe32bcc commit 8120a86

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
8888
* (simulation) [#17911](https://github.com/cosmos/cosmos-sdk/pull/17911) Fix all problems with executing command `make test-sim-custom-genesis-fast` for simulation test.
8989
* (simulation) [#18196](https://github.com/cosmos/cosmos-sdk/pull/18196) Fix the problem of `validator set is empty after InitGenesis` in simulation test.
9090
* (baseapp) [#18551](https://github.com/cosmos/cosmos-sdk/pull/18551) Fix SelectTxForProposal the calculation method of tx bytes size is inconsistent with CometBFT
91+
* (abci): [#19200](https://github.com/cosmos/cosmos-sdk/pull/19200) Ensure that sdk side ve math matches cometbft
9192

9293
### API Breaking Changes
9394

baseapp/abci_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2224,11 +2224,25 @@ func TestBaseApp_VoteExtensions(t *testing.T) {
22242224
}
22252225
require.Equal(t, 10, successful)
22262226

2227+
extVotes := []abci.ExtendedVoteInfo{}
2228+
for _, val := range vals {
2229+
extVotes = append(extVotes, abci.ExtendedVoteInfo{
2230+
VoteExtension: allVEs[0],
2231+
BlockIdFlag: cmtproto.BlockIDFlagCommit,
2232+
ExtensionSignature: []byte{},
2233+
Validator: abci.Validator{
2234+
Address: val.Bytes(),
2235+
Power: 666,
2236+
},
2237+
},
2238+
)
2239+
}
2240+
22272241
prepPropReq := &abci.RequestPrepareProposal{
22282242
Height: 1,
22292243
LocalLastCommit: abci.ExtendedCommitInfo{
22302244
Round: 0,
2231-
Votes: []abci.ExtendedVoteInfo{},
2245+
Votes: extVotes,
22322246
},
22332247
}
22342248

@@ -2287,6 +2301,7 @@ func TestBaseApp_VoteExtensions(t *testing.T) {
22872301
ExtensionSignature: extSig,
22882302
Validator: abci.Validator{
22892303
Address: vals[i].Bytes(),
2304+
Power: 666,
22902305
},
22912306
})
22922307
}

baseapp/abci_utils.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,10 @@ import (
1414
protoio "github.com/cosmos/gogoproto/io"
1515
"github.com/cosmos/gogoproto/proto"
1616

17-
"cosmossdk.io/math"
18-
1917
sdk "github.com/cosmos/cosmos-sdk/types"
2018
"github.com/cosmos/cosmos-sdk/types/mempool"
2119
)
2220

23-
// VoteExtensionThreshold defines the total voting power % that must be
24-
// submitted in order for all vote extensions to be considered valid for a
25-
// given height.
26-
var VoteExtensionThreshold = math.LegacyNewDecWithPrec(667, 3)
27-
2821
type (
2922
// ValidatorStore defines the interface contract require for verifying vote
3023
// extension signatures. Typically, this will be implemented by the x/staking
@@ -133,13 +126,18 @@ func ValidateVoteExtensions(
133126
sumVP += vote.Validator.Power
134127
}
135128

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

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

0 commit comments

Comments
 (0)