Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify group dependency graph and align 0.52 and main #22978

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(x/group): simplify dependency graph
  • Loading branch information
julienrbrt committed Dec 18, 2024
commit 9d6b531d054c1731c20fc12e65dea806dba48818
13 changes: 10 additions & 3 deletions x/bank/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ package testutil
import (
"context"

bankkeeper "cosmossdk.io/x/bank/keeper"
"cosmossdk.io/x/bank/types"

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

// minimalBankKeeper is a subset of the bankkeeper.Keeper interface that is used
// for the bank testing utilities.
type minimalBankKeeper interface {
MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx context.Context, senderModule, recipientModule string, amt sdk.Coins) error
}

// FundAccount is a utility function that funds an account by minting and
// sending the coins to the address. This should be used for testing purposes
// only!
//
// TODO: Instead of using the mint module account, which has the
// permission of minting, create a "faucet" account. (@fdymylja)
func FundAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, amounts sdk.Coins) error {
func FundAccount(ctx context.Context, bankKeeper minimalBankKeeper, addr sdk.AccAddress, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, types.MintModuleName, amounts); err != nil {
return err
}
Expand All @@ -29,7 +36,7 @@ func FundAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, addr sdk.Acc
//
// TODO: Instead of using the mint module account, which has the
// permission of minting, create a "faucet" account. (@fdymylja)
func FundModuleAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, recipientMod string, amounts sdk.Coins) error {
func FundModuleAccount(ctx context.Context, bankKeeper minimalBankKeeper, recipientMod string, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, types.MintModuleName, amounts); err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion x/distribution/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,5 @@ replace github.com/cosmos/cosmos-sdk => ../../.
// TODO remove post spinning out all modules
replace (
cosmossdk.io/x/bank => ../bank
cosmossdk.io/x/protocolpool => ../protocolpool
cosmossdk.io/x/staking => ../staking
)
6 changes: 0 additions & 6 deletions x/group/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,7 @@ replace github.com/cosmos/cosmos-sdk => ../../
// TODO remove post spinning out all modules
replace (
cosmossdk.io/client/v2 => ../../client/v2
cosmossdk.io/x/accounts => ../accounts
cosmossdk.io/x/bank => ../bank
cosmossdk.io/x/consensus => ../consensus
cosmossdk.io/x/epochs => ../epochs
cosmossdk.io/x/gov => ../gov
cosmossdk.io/x/mint => ../mint
cosmossdk.io/x/protocolpool => ../protocolpool
cosmossdk.io/x/slashing => ../slashing
cosmossdk.io/x/staking => ../staking
)
109 changes: 26 additions & 83 deletions x/group/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,18 @@ package keeper_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/suite"

"cosmossdk.io/core/address"
"cosmossdk.io/core/header"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/math"
bankkeeper "cosmossdk.io/x/bank/keeper"
"cosmossdk.io/x/bank/testutil"
banktypes "cosmossdk.io/x/bank/types"
"cosmossdk.io/x/group"
"cosmossdk.io/x/group/keeper"
grouptestutil "cosmossdk.io/x/group/testutil"
stakingkeeper "cosmossdk.io/x/staking/keeper"

codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"

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

type IntegrationTestSuite struct {
suite.Suite

app *runtime.App
ctx sdk.Context
addrs []sdk.AccAddress
groupKeeper keeper.Keeper
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
interfaceRegistry codectypes.InterfaceRegistry

addressCodec address.Codec
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

func (s *IntegrationTestSuite) SetupTest() {
app, err := simtestutil.Setup(
depinject.Configs(
grouptestutil.AppConfig,
depinject.Supply(log.NewNopLogger()),
),
&s.interfaceRegistry,
&s.bankKeeper,
&s.stakingKeeper,
&s.groupKeeper,
)
s.Require().NoError(err)

ctx := app.BaseApp.NewContext(false)

ctx = ctx.WithHeaderInfo(header.Info{Time: time.Now()})

s.ctx = ctx

s.addrs = simtestutil.AddTestAddrsIncremental(s.bankKeeper, s.stakingKeeper, ctx, 4, math.NewInt(30000000))

s.addressCodec = codecaddress.NewBech32Codec("cosmos")
}

func (s *IntegrationTestSuite) TestEndBlockerPruning() {
ctx := s.ctx
func (s *TestSuite) TestEndBlockerPruning() {
ctx := s.sdkCtx
addr1, err := s.addressCodec.BytesToString(s.addrs[0])
s.Require().NoError(err)
addr2, err := s.addressCodec.BytesToString(s.addrs[1])
Expand Down Expand Up @@ -166,7 +109,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
"proposal pruned after executor result success": {
setupProposal: func(ctx sdk.Context) uint64 {
msgs := []sdk.Msg{msgSend1}
pID, err := submitProposalAndVoteHelper(s, s.app, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
pID, err := submitProposalAndVoteHelper(s, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
s.Require().NoError(err)
_, err = s.groupKeeper.Exec(ctx, &group.MsgExec{Executor: addr3, ProposalId: pID})
s.Require().NoError(err)
Expand All @@ -181,7 +124,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
"proposal with multiple messages pruned when executed with result success": {
setupProposal: func(ctx sdk.Context) uint64 {
msgs := []sdk.Msg{msgSend1, msgSend1}
pID, err := submitProposalAndVoteHelper(s, s.app, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
pID, err := submitProposalAndVoteHelper(s, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
s.Require().NoError(err)
_, err = s.groupKeeper.Exec(ctx, &group.MsgExec{Executor: addr3, ProposalId: pID})
s.Require().NoError(err)
Expand All @@ -196,7 +139,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
"proposal not pruned when not executed and rejected": {
setupProposal: func(ctx sdk.Context) uint64 {
msgs := []sdk.Msg{msgSend1}
pID, err := submitProposalAndVoteHelper(s, s.app, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_NO)
pID, err := submitProposalAndVoteHelper(s, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_NO)
s.Require().NoError(err)
_, err = s.groupKeeper.Exec(ctx, &group.MsgExec{Executor: addr3, ProposalId: pID})
s.Require().NoError(err)
Expand All @@ -211,7 +154,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
},
"open proposal is not pruned which must not fail ": {
setupProposal: func(ctx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, s.app, ctx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
pID, err := submitProposalHelper(s, ctx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
s.Require().NoError(err)
_, err = s.groupKeeper.Exec(ctx, &group.MsgExec{Executor: addr3, ProposalId: pID})
s.Require().NoError(err)
Expand All @@ -225,7 +168,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
},
"proposal not pruned with group policy modified before tally": {
setupProposal: func(ctx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, s.app, ctx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
pID, err := submitProposalHelper(s, ctx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
s.Require().NoError(err)
_, err = s.groupKeeper.UpdateGroupPolicyMetadata(ctx, &group.MsgUpdateGroupPolicyMetadata{
Admin: addr1,
Expand All @@ -245,7 +188,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
"pruned when proposal is executable when failed before": {
setupProposal: func(ctx sdk.Context) uint64 {
msgs := []sdk.Msg{msgSend1}
pID, err := submitProposalAndVoteHelper(s, s.app, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
pID, err := submitProposalAndVoteHelper(s, ctx, msgs, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
s.Require().NoError(err)
_, err = s.groupKeeper.Exec(ctx, &group.MsgExec{Executor: addr3, ProposalId: pID})
s.Require().NoError(err)
Expand All @@ -257,7 +200,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
},
"proposal with status withdrawn is pruned after voting period end": {
setupProposal: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, s.app, sdkCtx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
pID, err := submitProposalHelper(s, sdkCtx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
s.Require().NoError(err)
_, err = s.groupKeeper.WithdrawProposal(ctx, &group.MsgWithdrawProposal{
ProposalId: pID,
Expand All @@ -272,7 +215,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
},
"proposal with status withdrawn is not pruned (before voting period)": {
setupProposal: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, s.app, sdkCtx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
pID, err := submitProposalHelper(s, sdkCtx, []sdk.Msg{msgSend1}, proposers, groupPolicyAddr)
s.Require().NoError(err)
_, err = s.groupKeeper.WithdrawProposal(ctx, &group.MsgWithdrawProposal{
ProposalId: pID,
Expand All @@ -288,7 +231,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
},
"proposal with status aborted is pruned after voting period end (due to updated group policy decision policy)": {
setupProposal: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, s.app, sdkCtx, []sdk.Msg{msgSend2}, proposers, groupPolicyAddr2)
pID, err := submitProposalHelper(s, sdkCtx, []sdk.Msg{msgSend2}, proposers, groupPolicyAddr2)
s.Require().NoError(err)

policy := group.NewThresholdDecisionPolicy("3", time.Second, 0)
Expand All @@ -310,7 +253,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
},
"proposal with status aborted is not pruned before voting period end (due to updated group policy)": {
setupProposal: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, s.app, sdkCtx, []sdk.Msg{msgSend2}, proposers, groupPolicyAddr2)
pID, err := submitProposalHelper(s, sdkCtx, []sdk.Msg{msgSend2}, proposers, groupPolicyAddr2)
s.Require().NoError(err)

policy := group.NewThresholdDecisionPolicy("3", time.Second, 0)
Expand Down Expand Up @@ -369,9 +312,8 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() {
}
}

func (s *IntegrationTestSuite) TestEndBlockerTallying() {
app := s.app
ctx := s.ctx
func (s *TestSuite) TestEndBlockerTallying() {
ctx := s.sdkCtx

addrs := s.addrs
addr0, err := s.addressCodec.BytesToString(addrs[0])
Expand Down Expand Up @@ -435,7 +377,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
}{
"tally updated after voting period end": {
preRun: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, app, sdkCtx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr)
pID, err := submitProposalHelper(s, sdkCtx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr)
s.Require().NoError(err)
return pID
},
Expand All @@ -446,7 +388,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
},
"tally within voting period": {
preRun: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, app, sdkCtx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr)
pID, err := submitProposalHelper(s, sdkCtx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr)
s.Require().NoError(err)

return pID
Expand All @@ -458,7 +400,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
},
"tally within voting period(with votes)": {
preRun: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalAndVoteHelper(s, app, ctx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
pID, err := submitProposalAndVoteHelper(s, ctx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
s.Require().NoError(err)

return pID
Expand All @@ -471,7 +413,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
"tally after voting period (not passing)": {
preRun: func(sdkCtx sdk.Context) uint64 {
// `addrs[1]` has weight 1
pID, err := submitProposalAndVoteHelper(s, app, ctx, []sdk.Msg{msgSend}, []string{addr1}, groupPolicyAddr, group.VOTE_OPTION_YES)
pID, err := submitProposalAndVoteHelper(s, ctx, []sdk.Msg{msgSend}, []string{addr1}, groupPolicyAddr, group.VOTE_OPTION_YES)
s.Require().NoError(err)

return pID
Expand All @@ -488,7 +430,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
},
"tally after voting period(with votes)": {
preRun: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalAndVoteHelper(s, app, ctx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
pID, err := submitProposalAndVoteHelper(s, ctx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
s.Require().NoError(err)

return pID
Expand All @@ -505,7 +447,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
},
"tally of withdrawn proposal": {
preRun: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalHelper(s, app, sdkCtx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr)
pID, err := submitProposalHelper(s, sdkCtx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr)
s.Require().NoError(err)

_, err = s.groupKeeper.WithdrawProposal(ctx, &group.MsgWithdrawProposal{
Expand All @@ -523,7 +465,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
},
"tally of withdrawn proposal (with votes)": {
preRun: func(sdkCtx sdk.Context) uint64 {
pID, err := submitProposalAndVoteHelper(s, app, ctx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
pID, err := submitProposalAndVoteHelper(s, ctx, []sdk.Msg{msgSend}, proposers, groupPolicyAddr, group.VOTE_OPTION_YES)
s.Require().NoError(err)

_, err = s.groupKeeper.WithdrawProposal(ctx, &group.MsgWithdrawProposal{
Expand Down Expand Up @@ -564,7 +506,7 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() {
}
}

func submitProposalHelper(s *IntegrationTestSuite, app *runtime.App, ctx context.Context, msgs []sdk.Msg, proposers []string, groupPolicyAddr sdk.AccAddress) (uint64, error) {
func submitProposalHelper(s *TestSuite, ctx context.Context, msgs []sdk.Msg, proposers []string, groupPolicyAddr sdk.AccAddress) (uint64, error) {
gpAddr, err := s.addressCodec.BytesToString(groupPolicyAddr)
s.Require().NoError(err)
proposalReq := &group.MsgSubmitProposal{
Expand All @@ -585,10 +527,10 @@ func submitProposalHelper(s *IntegrationTestSuite, app *runtime.App, ctx context
}

func submitProposalAndVoteHelper(
s *IntegrationTestSuite, app *runtime.App, ctx context.Context, msgs []sdk.Msg,
s *TestSuite, ctx context.Context, msgs []sdk.Msg,
proposers []string, groupPolicyAddr sdk.AccAddress, voteOption group.VoteOption,
) (uint64, error) {
myProposalID, err := submitProposalHelper(s, app, ctx, msgs, proposers, groupPolicyAddr)
myProposalID, err := submitProposalHelper(s, ctx, msgs, proposers, groupPolicyAddr)
if err != nil {
return 0, err
}
Expand All @@ -600,5 +542,6 @@ func submitProposalAndVoteHelper(
if err != nil {
return 0, err
}

return myProposalID, nil
}
Loading
Loading