Skip to content

Commit

Permalink
refactor(tests/integration): Port distribution integration tests to s…
Browse files Browse the repository at this point in the history
…erver v2 (#22667)
  • Loading branch information
sontrinh16 authored Dec 3, 2024
1 parent 78c8057 commit 6a52694
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 404 deletions.
36 changes: 0 additions & 36 deletions tests/integration/distribution/keeper/common_test.go

This file was deleted.

31 changes: 0 additions & 31 deletions tests/integration/distribution/module_test.go

This file was deleted.

15 changes: 14 additions & 1 deletion tests/integration/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
corebranch "cosmossdk.io/core/branch"
"cosmossdk.io/core/comet"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/header"
"cosmossdk.io/core/server"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
Expand Down Expand Up @@ -96,6 +97,8 @@ type StartupConfig struct {
// RouterServiceBuilder defines the custom builder
// for msg router and query router service to be used in the app.
RouterServiceBuilder runtime.RouterServiceBuilder
// HeaderService defines the custom header service to be used in the app.
HeaderService header.Service
}

func DefaultStartUpConfig(t *testing.T) StartupConfig {
Expand Down Expand Up @@ -125,6 +128,7 @@ func DefaultStartUpConfig(t *testing.T) StartupConfig {
RouterServiceBuilder: runtime.NewRouterBuilder(
stf.NewMsgRouterService, stf.NewQueryRouterService(),
),
HeaderService: services.NewGenesisHeaderService(stf.HeaderService{}),
}
}

Expand Down Expand Up @@ -182,13 +186,13 @@ func NewApp(
"minimum-gas-prices": "0stake",
},
},
services.NewGenesisHeaderService(stf.HeaderService{}),
cometService,
kvFactory,
&eventService{},
storeBuilder,
startupConfig.BranchService,
startupConfig.RouterServiceBuilder,
startupConfig.HeaderService,
),
depinject.Invoke(
std.RegisterInterfaces,
Expand Down Expand Up @@ -313,6 +317,10 @@ type App struct {
txConfig client.TxConfig
}

func (a App) LastBlockHeight() uint64 {
return a.lastHeight
}

// Deliver delivers a block with the given transactions and returns the resulting state.
func (a *App) Deliver(
t *testing.T, ctx context.Context, txs []stateMachineTx,
Expand All @@ -327,6 +335,11 @@ func (a *App) Deliver(
resp, state, err := a.DeliverBlock(ctx, req)
require.NoError(t, err)
a.lastHeight++
// update block heigh if integeration context is present
iCtx, ok := ctx.Value(contextKey).(*integrationContext)
if ok {
iCtx.header.Height = int64(a.lastHeight)
}
return resp, state
}

Expand Down
3 changes: 3 additions & 0 deletions tests/integration/v2/auth/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/runtime/v2/services"
"cosmossdk.io/server/v2/stf"
"cosmossdk.io/x/accounts"
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
accountsv1 "cosmossdk.io/x/accounts/v1"
Expand Down Expand Up @@ -79,6 +81,7 @@ func createTestSuite(t *testing.T) *suite {

startupCfg.BranchService = &integration.BranchService{}
startupCfg.RouterServiceBuilder = serviceBuilder
startupCfg.HeaderService = services.NewGenesisHeaderService(stf.HeaderService{})

res.app, err = integration.NewApp(
depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Provide(
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/v2/distribution/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package distribution

import (
"testing"

"gotest.tools/v3/assert"

"cosmossdk.io/math"
"cosmossdk.io/x/distribution/types"
stakingtestutil "cosmossdk.io/x/staking/testutil"
stakingtypes "cosmossdk.io/x/staking/types"

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

func setupValidatorWithCommission(t *testing.T, f *fixture, valAddr sdk.ValAddress, initialStake int64) {
t.Helper()
initTokens := f.stakingKeeper.TokensFromConsensusPower(f.ctx, int64(1000))
assert.NilError(t, f.bankKeeper.MintCoins(f.ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))
assert.NilError(t, f.stakingKeeper.Params.Set(f.ctx, stakingtypes.DefaultParams()))

funds := f.stakingKeeper.TokensFromConsensusPower(f.ctx, int64(1000))
assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.ctx, types.ModuleName, sdk.AccAddress(valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))))
f.authKeeper.SetAccount(f.ctx, f.authKeeper.NewAccountWithAddress(f.ctx, sdk.AccAddress(valAddr)))

tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0))
tstaking.CreateValidator(valAddr, valConsPk0, math.NewInt(initialStake), true)
}
160 changes: 160 additions & 0 deletions tests/integration/v2/distribution/fixture_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package distribution

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"cosmossdk.io/core/comet"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/router"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
_ "cosmossdk.io/x/accounts" // import as blank for app wiring
_ "cosmossdk.io/x/bank" // import as blank for app wiring
bankkeeper "cosmossdk.io/x/bank/keeper"
banktypes "cosmossdk.io/x/bank/types"
_ "cosmossdk.io/x/consensus" // import as blank for app wiring
_ "cosmossdk.io/x/distribution" // import as blank for app wiring
distrkeeper "cosmossdk.io/x/distribution/keeper"
_ "cosmossdk.io/x/mint" // import as blank for app wiring
_ "cosmossdk.io/x/protocolpool" // import as blank for app wiring
poolkeeper "cosmossdk.io/x/protocolpool/keeper"
_ "cosmossdk.io/x/staking" // import as blank for app wiring
stakingkeeper "cosmossdk.io/x/staking/keeper"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/tests/integration/v2"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
_ "github.com/cosmos/cosmos-sdk/x/auth" // import as blank for app wiring
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import as blank for app wiring``
_ "github.com/cosmos/cosmos-sdk/x/auth/vesting" // import as blank for app wiring
_ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring
)

var (
emptyDelAddr sdk.AccAddress
emptyValAddr sdk.ValAddress
)

var (
PKS = simtestutil.CreateTestPubKeys(3)

valConsPk0 = PKS[0]
)

type fixture struct {
app *integration.App

ctx context.Context
cdc codec.Codec

queryClient distrkeeper.Querier

authKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
distrKeeper distrkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
poolKeeper poolkeeper.Keeper

addr sdk.AccAddress
valAddr sdk.ValAddress
}

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

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

var err error
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{}

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

addr := sdk.AccAddress(PKS[0].Address())
valAddr := sdk.ValAddress(addr)
valConsAddr := sdk.ConsAddress(valConsPk0.Address())

ctx := res.app.StateLatestContext(t)
res.addr = addr
res.valAddr = valAddr

// set proposer and vote infos
res.ctx = context.WithValue(ctx, corecontext.CometInfoKey, comet.Info{
LastCommit: comet.CommitInfo{
Votes: []comet.VoteInfo{
{
Validator: comet.Validator{
Address: valAddr,
Power: 100,
},
BlockIDFlag: comet.BlockIDFlagCommit,
},
},
},
ProposerAddress: valConsAddr,
})

res.queryClient = distrkeeper.NewQuerier(res.distrKeeper)

return &res
}

func (s *fixture) 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")
}

func (s *fixture) registerQueryRouterService(router *integration.RouterService) {
// register custom router service

}
Loading

0 comments on commit 6a52694

Please sign in to comment.