From ab34a95270512d85ee714d7f24c6f90206ac02ae Mon Sep 17 00:00:00 2001 From: Marko Date: Mon, 4 Mar 2024 10:52:17 +0100 Subject: [PATCH] test: fix staking (#19623) --- baseapp/abci_utils.go | 5 +- baseapp/abci_utils_test.go | 29 +-------- .../staking/keeper/vote_extensions_test.go | 62 +++++++++++++++++-- types/context.go | 7 ++- 4 files changed, 67 insertions(+), 36 deletions(-) diff --git a/baseapp/abci_utils.go b/baseapp/abci_utils.go index 450f4c9b235c..e4ae40f71273 100644 --- a/baseapp/abci_utils.go +++ b/baseapp/abci_utils.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/gogoproto/proto" "cosmossdk.io/core/comet" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/mempool" ) @@ -46,8 +47,8 @@ func ValidateVoteExtensions( ) error { // Get values from context cp := ctx.ConsensusParams() - currentHeight := ctx.BlockHeight() - chainID := ctx.BlockHeader().ChainID + currentHeight := ctx.HeaderInfo().Height + chainID := ctx.HeaderInfo().ChainID commitInfo := ctx.CometInfo().LastCommit // Check that both extCommit + commit are ordered in accordance with vp/address. diff --git a/baseapp/abci_utils_test.go b/baseapp/abci_utils_test.go index 7157d3ac9cc3..f2b636fc39d3 100644 --- a/baseapp/abci_utils_test.go +++ b/baseapp/abci_utils_test.go @@ -17,10 +17,11 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "cosmossdk.io/core/comet" + "cosmossdk.io/core/header" "cosmossdk.io/log" authtx "cosmossdk.io/x/auth/tx" - "cosmossdk.io/core/comet" "github.com/cosmos/cosmos-sdk/baseapp" baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil" "github.com/cosmos/cosmos-sdk/baseapp/testutil/mock" @@ -66,13 +67,6 @@ func (t testValidator) toValidator(power int64) abci.Validator { } } -func (t testValidator) toSDKValidator(power int64) comet.Validator { - return comet.Validator{ - Address: t.consAddr.Bytes(), - Power: power, - } -} - type ABCIUtilsTestSuite struct { suite.Suite @@ -139,7 +133,7 @@ func (s *ABCIUtilsTestSuite) TestValidateVoteExtensionsHappyPath() { extSig2, err := s.vals[2].privKey.Sign(bz) s.Require().NoError(err) - s.ctx = s.ctx.WithBlockHeight(3) // enable vote-extensions + s.ctx = s.ctx.WithBlockHeight(3).WithHeaderInfo(header.Info{Height: 3, ChainID: chainID}) // enable vote-extensions llc := abci.ExtendedCommitInfo{ Round: 0, @@ -778,23 +772,6 @@ func extendedCommitToLastCommit(ec abci.ExtendedCommitInfo) (abci.ExtendedCommit } } -type voteInfos []comet.VoteInfo - -func (v voteInfos) Len() int { - return len(v) -} - -func (v voteInfos) Less(i, j int) bool { - if v[i].Validator.Power == v[j].Validator.Power { - return bytes.Compare(v[i].Validator.Address, v[j].Validator.Address) == -1 - } - return v[i].Validator.Power > v[j].Validator.Power -} - -func (v voteInfos) Swap(i, j int) { - v[i], v[j] = v[j], v[i] -} - type extendedVoteInfos []abci.ExtendedVoteInfo func (v extendedVoteInfos) Len() int { diff --git a/tests/integration/staking/keeper/vote_extensions_test.go b/tests/integration/staking/keeper/vote_extensions_test.go index 8e4c1fb0bc2d..506b5b991018 100644 --- a/tests/integration/staking/keeper/vote_extensions_test.go +++ b/tests/integration/staking/keeper/vote_extensions_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "bytes" + "sort" "testing" abci "github.com/cometbft/cometbft/abci/types" @@ -10,6 +11,8 @@ import ( "github.com/cosmos/gogoproto/proto" "gotest.tools/v3/assert" + "cosmossdk.io/core/comet" + "cosmossdk.io/core/header" "cosmossdk.io/math" "cosmossdk.io/x/staking/testutil" stakingtypes "cosmossdk.io/x/staking/types" @@ -21,6 +24,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +const chainID = "chain-id-123" + +// TestValidateVoteExtensions is a unit test function that tests the validation of vote extensions. +// It sets up the necessary fixtures and validators, generates vote extensions for each validator, +// and validates the vote extensions using the baseapp.ValidateVoteExtensions function. func TestValidateVoteExtensions(t *testing.T) { t.Parallel() f := initFixture(t) @@ -28,10 +36,10 @@ func TestValidateVoteExtensions(t *testing.T) { // enable vote extensions cp := simtestutil.DefaultConsensusParams cp.Abci = &cmtproto.ABCIParams{VoteExtensionsEnableHeight: 1} - f.sdkCtx = f.sdkCtx.WithConsensusParams(*cp).WithBlockHeight(2) + f.sdkCtx = f.sdkCtx.WithConsensusParams(*cp).WithHeaderInfo(header.Info{Height: 2, ChainID: chainID}) // setup the validators - numVals := 3 + numVals := 1 privKeys := []cryptotypes.PrivKey{} for i := 0; i < numVals; i++ { privKeys = append(privKeys, ed25519.GenPrivKey()) @@ -61,9 +69,9 @@ func TestValidateVoteExtensions(t *testing.T) { voteExt := []byte("something" + v.OperatorAddress) cve := cmtproto.CanonicalVoteExtension{ Extension: voteExt, - Height: f.sdkCtx.BlockHeight() - 1, // the vote extension was signed in the previous height + Height: f.sdkCtx.HeaderInfo().Height - 1, // the vote extension was signed in the previous height Round: 0, - ChainId: "chain-id-123", + ChainId: chainID, } extSignBytes, err := mashalVoteExt(&cve) @@ -86,7 +94,10 @@ func TestValidateVoteExtensions(t *testing.T) { votes = append(votes, ve) } - err := baseapp.ValidateVoteExtensions(f.sdkCtx, f.stakingKeeper, f.sdkCtx.BlockHeight(), "chain-id-123", abci.ExtendedCommitInfo{Round: 0, Votes: votes}) + eci, ci := extendedCommitToLastCommit(abci.ExtendedCommitInfo{Round: 0, Votes: votes}) + f.sdkCtx = f.sdkCtx.WithCometInfo(ci) + + err := baseapp.ValidateVoteExtensions(f.sdkCtx, f.stakingKeeper, eci) assert.NilError(t, err) } @@ -98,3 +109,44 @@ func mashalVoteExt(msg proto.Message) ([]byte, error) { return buf.Bytes(), nil } + +func extendedCommitToLastCommit(ec abci.ExtendedCommitInfo) (abci.ExtendedCommitInfo, comet.Info) { + // sort the extended commit info + sort.Sort(extendedVoteInfos(ec.Votes)) + + // convert the extended commit info to last commit info + lastCommit := comet.CommitInfo{ + Round: ec.Round, + Votes: make([]comet.VoteInfo, len(ec.Votes)), + } + + for i, vote := range ec.Votes { + lastCommit.Votes[i] = comet.VoteInfo{ + Validator: comet.Validator{ + Address: vote.Validator.Address, + Power: vote.Validator.Power, + }, + } + } + + return ec, comet.Info{ + LastCommit: lastCommit, + } +} + +type extendedVoteInfos []abci.ExtendedVoteInfo + +func (v extendedVoteInfos) Len() int { + return len(v) +} + +func (v extendedVoteInfos) Less(i, j int) bool { + if v[i].Validator.Power == v[j].Validator.Power { + return bytes.Compare(v[i].Validator.Address, v[j].Validator.Address) == -1 + } + return v[i].Validator.Power > v[j].Validator.Power +} + +func (v extendedVoteInfos) Swap(i, j int) { + v[i], v[j] = v[j], v[i] +} diff --git a/types/context.go b/types/context.go index 1eb9d41e4068..2c63445f8709 100644 --- a/types/context.go +++ b/types/context.go @@ -433,16 +433,17 @@ func ToSDKCommitInfo(commit abci.CommitInfo) comet.CommitInfo { func ToSDKExtendedCommitInfo(commit abci.ExtendedCommitInfo) comet.CommitInfo { ci := comet.CommitInfo{ Round: commit.Round, + Votes: make([]comet.VoteInfo, len(commit.Votes)), } - for _, v := range commit.Votes { - ci.Votes = append(ci.Votes, comet.VoteInfo{ + for i, v := range commit.Votes { + ci.Votes[i] = comet.VoteInfo{ Validator: comet.Validator{ Address: v.Validator.Address, Power: v.Validator.Power, }, BlockIDFlag: comet.BlockIDFlag(v.BlockIdFlag), - }) + } } return ci