From 8120a86eaa01d34de9ef314a69efa9c9c3a78430 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 25 Jan 2024 12:01:48 -0500 Subject: [PATCH] fix(ve): Ensure that sdk side ve math matches `cometbft` (#19200) --- CHANGELOG.md | 1 + baseapp/abci_test.go | 17 ++++++++++++++++- baseapp/abci_utils.go | 22 ++++++++++------------ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81ebd9dd02..8b7dff6def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,6 +88,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i * (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. * (simulation) [#18196](https://github.com/cosmos/cosmos-sdk/pull/18196) Fix the problem of `validator set is empty after InitGenesis` in simulation test. * (baseapp) [#18551](https://github.com/cosmos/cosmos-sdk/pull/18551) Fix SelectTxForProposal the calculation method of tx bytes size is inconsistent with CometBFT +* (abci): [#19200](https://github.com/cosmos/cosmos-sdk/pull/19200) Ensure that sdk side ve math matches cometbft ### API Breaking Changes diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index efca5b7407..769349f84e 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -2224,11 +2224,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, }, } @@ -2287,6 +2301,7 @@ func TestBaseApp_VoteExtensions(t *testing.T) { ExtensionSignature: extSig, Validator: abci.Validator{ Address: vals[i].Bytes(), + Power: 666, }, }) } diff --git a/baseapp/abci_utils.go b/baseapp/abci_utils.go index 53fb89db5a..1c42411d2e 100644 --- a/baseapp/abci_utils.go +++ b/baseapp/abci_utils.go @@ -14,17 +14,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 @@ -133,13 +126,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 }