Skip to content

Commit

Permalink
test: fix staking (cosmos#19623)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Mar 4, 2024
1 parent 7628592 commit ab34a95
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
5 changes: 3 additions & 2 deletions baseapp/abci_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 3 additions & 26 deletions baseapp/abci_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
62 changes: 57 additions & 5 deletions tests/integration/staking/keeper/vote_extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"bytes"
"sort"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
Expand All @@ -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"
Expand All @@ -21,17 +24,22 @@ 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)

// 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())
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand All @@ -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]
}
7 changes: 4 additions & 3 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab34a95

Please sign in to comment.