Skip to content

Commit

Permalink
refactor: simplify group dependency graph and align 0.52 and main (#2…
Browse files Browse the repository at this point in the history
…2978)

(cherry picked from commit 4d6c991)

# Conflicts:
#	tests/integration/v2/gov/common_test.go
#	tests/integration/v2/gov/keeper/fixture_test.go
#	x/distribution/go.mod
#	x/group/go.mod
#	x/group/go.sum
#	x/mint/go.mod
#	x/params/go.mod
  • Loading branch information
julienrbrt authored and mergify[bot] committed Dec 18, 2024
1 parent 98acf3a commit 8e67584
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 169 deletions.
206 changes: 206 additions & 0 deletions tests/integration/v2/gov/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package gov

import (
"bytes"
"context"
"log"
"sort"
"testing"

"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"cosmossdk.io/core/router"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
sdklog "cosmossdk.io/log"
"cosmossdk.io/math"
"cosmossdk.io/runtime/v2"
_ "cosmossdk.io/x/accounts"
_ "cosmossdk.io/x/bank"
bankkeeper "cosmossdk.io/x/bank/keeper"
banktypes "cosmossdk.io/x/bank/types"
_ "cosmossdk.io/x/consensus"
_ "cosmossdk.io/x/gov"
"cosmossdk.io/x/gov/keeper"
"cosmossdk.io/x/gov/types"
v1 "cosmossdk.io/x/gov/types/v1"
"cosmossdk.io/x/gov/types/v1beta1"
_ "cosmossdk.io/x/mint"
_ "cosmossdk.io/x/protocolpool"
_ "cosmossdk.io/x/staking"
stakingkeeper "cosmossdk.io/x/staking/keeper"
stakingtypes "cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
sdk "github.com/cosmos/cosmos-sdk/types"
_ "github.com/cosmos/cosmos-sdk/x/auth"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

var (
valTokens = sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
TestProposal = v1beta1.NewTextProposal("Test", "description")
TestDescription = stakingtypes.NewDescription("T", "E", "S", "T", "Z", &stakingtypes.Metadata{})
TestCommissionRates = stakingtypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec())
)

// mkTestLegacyContent creates a MsgExecLegacyContent for testing purposes.
func mkTestLegacyContent(t *testing.T) *v1.MsgExecLegacyContent {
t.Helper()
msgContent, err := v1.NewLegacyContent(TestProposal, authtypes.NewModuleAddress(types.ModuleName).String())
assert.NilError(t, err)

return msgContent
}

var pubkeys = []cryptotypes.PubKey{
ed25519.GenPrivKey().PubKey(),
ed25519.GenPrivKey().PubKey(),
ed25519.GenPrivKey().PubKey(),
}

// SortAddresses - Sorts Addresses
func SortAddresses(addrs []sdk.AccAddress) {
byteAddrs := make([][]byte, len(addrs))

for i, addr := range addrs {
byteAddrs[i] = addr.Bytes()
}

SortByteArrays(byteAddrs)

for i, byteAddr := range byteAddrs {
addrs[i] = byteAddr
}
}

// implement `Interface` in sort package.
type sortByteArrays [][]byte

func (b sortByteArrays) Len() int {
return len(b)
}

func (b sortByteArrays) Less(i, j int) bool {
// bytes package already implements Comparable for []byte.
switch bytes.Compare(b[i], b[j]) {
case -1:
return true
case 0, 1:
return false
default:
log.Panic("not fail-able with `bytes.Comparable` bounded [-1, 1].")
return false
}
}

func (b sortByteArrays) Swap(i, j int) {
b[j], b[i] = b[i], b[j]
}

// SortByteArrays - sorts the provided byte array
func SortByteArrays(src [][]byte) [][]byte {
sorted := sortByteArrays(src)
sort.Sort(sorted)
return sorted
}

type suite struct {
cdc codec.Codec
app *integration.App

ctx context.Context

AuthKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
GovKeeper *keeper.Keeper
StakingKeeper *stakingkeeper.Keeper

txConfigOptions tx.ConfigOptions
}

func createTestSuite(t *testing.T, genesisBehavior int) suite {
t.Helper()
res := suite{}

moduleConfigs := []configurator.ModuleOption{
configurator.AccountsModule(),
configurator.AuthModule(),
configurator.StakingModule(),
configurator.TxModule(),
configurator.BankModule(),
configurator.GovModule(),
configurator.MintModule(),
configurator.ConsensusModule(),
configurator.ProtocolPoolModule(),
}

startupCfg := integration.DefaultStartUpConfig(t)

msgRouterService := integration.NewRouterService()
res.registerMsgRouterService(msgRouterService)

var routerFactory runtime.RouterServiceFactory = func(_ []byte) router.Service {
return msgRouterService
}

queryRouterService := integration.NewRouterService()
res.registerQueryRouterService(queryRouterService)
serviceBuilder := runtime.NewRouterBuilder(routerFactory, queryRouterService)

startupCfg.BranchService = &integration.BranchService{}
startupCfg.RouterServiceBuilder = serviceBuilder
startupCfg.HeaderService = &integration.HeaderService{}
startupCfg.GasService = &integration.GasService{}
startupCfg.GenesisBehavior = genesisBehavior

app, err := integration.NewApp(
depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Supply(sdklog.NewNopLogger())),
startupCfg,
&res.AuthKeeper, &res.BankKeeper, &res.GovKeeper, &res.StakingKeeper, &res.cdc, &res.txConfigOptions,
)
require.NoError(t, err)

res.ctx = app.StateLatestContext(t)
res.app = app
return res
}

func (s *suite) registerMsgRouterService(router *integration.RouterService) {
// register custom router service
bankSendHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*banktypes.MsgSend)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := bankkeeper.NewMsgServerImpl(s.BankKeeper)
resp, err := msgServer.Send(ctx, msg)
return resp, err
}

router.RegisterHandler(bankSendHandler, "/cosmos.bank.v1beta1.MsgSend")

// register custom router service

govSubmitProposalHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*v1.MsgExecLegacyContent)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := keeper.NewMsgServerImpl(s.GovKeeper)
resp, err := msgServer.ExecLegacyContent(ctx, msg)
return resp, err
}

router.RegisterHandler(govSubmitProposalHandler, "/cosmos.gov.v1.MsgExecLegacyContent")
}

func (f *suite) registerQueryRouterService(router *integration.RouterService) {
}
108 changes: 108 additions & 0 deletions tests/integration/v2/gov/keeper/fixture_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package keeper

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"cosmossdk.io/core/router"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
_ "cosmossdk.io/x/accounts"
_ "cosmossdk.io/x/bank"
bankkeeper "cosmossdk.io/x/bank/keeper"
_ "cosmossdk.io/x/consensus"
_ "cosmossdk.io/x/gov"
govkeeper "cosmossdk.io/x/gov/keeper"
v1 "cosmossdk.io/x/gov/types/v1"
"cosmossdk.io/x/gov/types/v1beta1"
_ "cosmossdk.io/x/protocolpool"
_ "cosmossdk.io/x/staking"
stakingkeeper "cosmossdk.io/x/staking/keeper"

"github.com/cosmos/cosmos-sdk/tests/integration/v2"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
_ "github.com/cosmos/cosmos-sdk/x/auth"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
)

type fixture struct {
ctx context.Context
app *integration.App

queryServer v1.QueryServer
legacyQueryServer v1beta1.QueryServer

authKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
govKeeper *govkeeper.Keeper
}

func initFixture(t *testing.T) *fixture {
t.Helper()
res := fixture{}

moduleConfigs := []configurator.ModuleOption{
configurator.AccountsModule(),
configurator.AuthModule(),
configurator.StakingModule(),
configurator.BankModule(),
configurator.TxModule(),
configurator.GovModule(),
configurator.ConsensusModule(),
configurator.ProtocolPoolModule(),
}

startupCfg := integration.DefaultStartUpConfig(t)

msgRouterService := integration.NewRouterService()
res.registerMsgRouterService(msgRouterService)

var routerFactory runtime.RouterServiceFactory = func(_ []byte) router.Service {
return msgRouterService
}

queryRouterService := integration.NewRouterService()
res.registerQueryRouterService(queryRouterService)
serviceBuilder := runtime.NewRouterBuilder(routerFactory, queryRouterService)

startupCfg.BranchService = &integration.BranchService{}
startupCfg.RouterServiceBuilder = serviceBuilder
startupCfg.HeaderService = &integration.HeaderService{}

app, err := integration.NewApp(
depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Supply(log.NewNopLogger())),
startupCfg,
&res.authKeeper, &res.bankKeeper, &res.govKeeper, &res.stakingKeeper)
require.NoError(t, err)

res.app = app
res.ctx = app.StateLatestContext(t)

res.queryServer = govkeeper.NewQueryServer(res.govKeeper)
res.legacyQueryServer = govkeeper.NewLegacyQueryServer(res.govKeeper)
return &res
}

func (f *fixture) registerMsgRouterService(router *integration.RouterService) {
// register custom router service

govSubmitProposalHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*v1.MsgExecLegacyContent)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := govkeeper.NewMsgServerImpl(f.govKeeper)
resp, err := msgServer.ExecLegacyContent(ctx, msg)
return resp, err
}

router.RegisterHandler(govSubmitProposalHandler, "/cosmos.gov.v1.MsgExecLegacyContent")
}

func (f *fixture) registerQueryRouterService(router *integration.RouterService) {
}
1 change: 0 additions & 1 deletion x/accounts/defaults/lockup/lockup.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ func (bva *BaseLockup) checkUnbondingEntriesMature(ctx context.Context) error {
if !errorsmod.IsOf(err, stakingtypes.ErrNoUnbondingDelegation) {
return true, err
}

}

found := false
Expand Down
1 change: 0 additions & 1 deletion x/accounts/defaults/lockup/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func newMockContext(t *testing.T) (context.Context, store.KVStoreService) {
default:
return nil, errors.New("unrecognized request type")
}

},
)
}
Expand Down
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
9 changes: 9 additions & 0 deletions x/distribution/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,12 @@ require (
)

replace github.com/cosmos/cosmos-sdk => ../../.
<<<<<<< HEAD
=======

// TODO remove post spinning out all modules
replace (
cosmossdk.io/x/bank => ../bank
cosmossdk.io/x/staking => ../staking
)
>>>>>>> 4d6c991fb (refactor: simplify group dependency graph and align 0.52 and main (#22978))
Loading

0 comments on commit 8e67584

Please sign in to comment.