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(tests/integration): Port distribution integration tests to server v2 #22667

Merged
merged 39 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
432fbbf
add auth app test
sontrinh16 Nov 7, 2024
a69a301
add run msg
sontrinh16 Nov 8, 2024
2942559
add auth integration tests
sontrinh16 Nov 19, 2024
940ce7f
minor
sontrinh16 Nov 19, 2024
8409748
Merge branch 'main' into son/port_auth_integration_tests_to_server_v2
sontrinh16 Nov 19, 2024
bd13abb
add router service factory
sontrinh16 Nov 20, 2024
f105e67
fix conflict
sontrinh16 Nov 20, 2024
ce64f48
more resolve conflict
sontrinh16 Nov 20, 2024
4941f18
lint
sontrinh16 Nov 20, 2024
cdcfede
fix tests
sontrinh16 Nov 20, 2024
71affa1
go mod tidy
sontrinh16 Nov 21, 2024
aecd1f1
lint more
sontrinh16 Nov 21, 2024
b9b1a2c
lint
sontrinh16 Nov 21, 2024
2b94d76
lint
sontrinh16 Nov 21, 2024
78767ec
Merge branch 'main' into son/port_auth_integration_tests_to_server_v2
sontrinh16 Nov 21, 2024
400e3e1
clean up and fix tests
sontrinh16 Nov 21, 2024
14c8813
Merge branch 'son/port_auth_integration_tests_to_server_v2' of https:…
sontrinh16 Nov 21, 2024
c25f6cf
fix tests
sontrinh16 Nov 21, 2024
037d482
clean up
sontrinh16 Nov 21, 2024
709ea17
Merge branch 'main' into son/port_auth_integration_tests_to_server_v2
sontrinh16 Nov 21, 2024
422cca9
move router builder to runtime
sontrinh16 Nov 22, 2024
8c8f0e4
Merge branch 'son/port_auth_integration_tests_to_server_v2' of https:…
sontrinh16 Nov 22, 2024
2c1f3a4
godoc
sontrinh16 Nov 22, 2024
eb6f21b
Merge branch 'main' into son/port_auth_integration_tests_to_server_v2
sontrinh16 Nov 22, 2024
f2b6fff
godoc
sontrinh16 Nov 22, 2024
2bfe906
Merge branch 'son/port_auth_integration_tests_to_server_v2' of https:…
sontrinh16 Nov 22, 2024
48efcfe
Merge branch 'main' into son/port_auth_integration_tests_to_server_v2
sontrinh16 Nov 25, 2024
e8a7ff4
port distribution integration tests
sontrinh16 Nov 26, 2024
a1a0e4d
add more test
sontrinh16 Nov 26, 2024
bb177c4
resolve conflict
sontrinh16 Nov 27, 2024
a4ed8a9
minor
sontrinh16 Nov 27, 2024
801a62e
Merge branch 'main' into son/port_distr_integration_tests_to_server_v2
sontrinh16 Nov 28, 2024
2e2d8fc
fix tests
sontrinh16 Nov 28, 2024
2c6f8d3
Merge branch 'son/port_distr_integration_tests_to_server_v2' of https…
sontrinh16 Nov 28, 2024
9cac4a3
minor
sontrinh16 Nov 28, 2024
0aa0e78
Merge branch 'main' into son/port_distr_integration_tests_to_server_v2
sontrinh16 Dec 2, 2024
a6df1d1
remove v1 tests
sontrinh16 Dec 3, 2024
88981ec
Merge branch 'son/port_distr_integration_tests_to_server_v2' of https…
sontrinh16 Dec 3, 2024
1eaacc5
Merge branch 'main' into son/port_distr_integration_tests_to_server_v2
sontrinh16 Dec 3, 2024
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
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
Loading