Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sontrinh16 committed Nov 21, 2024
1 parent 14c8813 commit c25f6cf
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 34 deletions.
5 changes: 5 additions & 0 deletions core/router/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ type Service interface {
Invoke(ctx context.Context, req transaction.Msg) (res transaction.Msg, err error)
}

type ServiceBuilder interface {
BuildMsgRouter([]byte) Service
BuildQueryRouter() Service
}

type RouterServiceFactory func([]byte) Service
56 changes: 37 additions & 19 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func init() {
ProvideKVService,
ProvideModuleConfigMaps,
ProvideModuleScopedConfigMap,
ProvideRouterService,
),
appconfig.Invoke(SetupAppBuilder),
)
Expand Down Expand Up @@ -199,13 +198,6 @@ func ProvideKVService(
return kvFactory([]byte(kvStoreKey)), stf.NewMemoryStoreService([]byte(fmt.Sprintf("memory:%s", kvStoreKey)))
}

func ProvideRouterService(
key depinject.ModuleKey,
routerFactory router.RouterServiceFactory,
) router.Service {
return routerFactory([]byte(key.Name()))
}

func storeKeyOverride(config *runtimev2.Module, moduleName string) *runtimev2.StoreKeyConfig {
for _, cfg := range config.OverrideStoreKeys {
if cfg.ModuleName == moduleName {
Expand All @@ -224,30 +216,53 @@ func ProvideEnvironment(
headerService header.Service,
eventService event.Service,
branchService branch.Service,
routerService router.Service,
routerBuilder router.ServiceBuilder,
) appmodulev2.Environment {
return appmodulev2.Environment{
Logger: logger,
BranchService: branchService,
EventService: eventService,
GasService: stf.NewGasMeterService(),
HeaderService: headerService,
QueryRouterService: stf.NewQueryRouterService(),
MsgRouterService: routerService,
QueryRouterService: routerBuilder.BuildQueryRouter(),
MsgRouterService: routerBuilder.BuildMsgRouter([]byte(key.Name())),
TransactionService: services.NewContextAwareTransactionService(),
KVStoreService: kvService,
MemStoreService: memKvService,
}
}

type RouterBuilder struct {
msgRouterServiceFactory router.RouterServiceFactory
queryRouter router.Service
}

func NewRouterBuilder(
msgRouterServiceFactory router.RouterServiceFactory,
queryRouter router.Service,
) RouterBuilder {
return RouterBuilder{
msgRouterServiceFactory: msgRouterServiceFactory,
queryRouter: queryRouter,
}
}

func (b RouterBuilder) BuildMsgRouter(actor []byte) router.Service {
return b.msgRouterServiceFactory(actor)
}

func (b RouterBuilder) BuildQueryRouter() router.Service {
return b.queryRouter
}

// DefaultServiceBindings provides default services for the following service interfaces:
// - store.KVStoreServiceFactory
// - header.Service
// - comet.Service
// - event.Service
// - store/v2/root.Builder
// - branch.Service
// - router.ServiceFactory
// - router.ServiceBuilder
//
// They are all required. For most use cases these default services bindings should be sufficient.
// Power users (or tests) may wish to provide their own services bindings, in which case they must
Expand All @@ -260,16 +275,19 @@ func DefaultServiceBindings() depinject.Config {
stf.NewKVStoreService(actor),
)
}
routerServiceFactory router.RouterServiceFactory = stf.NewMsgRouterService
cometService comet.Service = &services.ContextAwareCometInfoService{}
headerService = services.NewGenesisHeaderService(stf.HeaderService{})
eventService = services.NewGenesisEventService(stf.NewEventService())
storeBuilder = root.NewBuilder()
branchService = stf.BranchService{}
routerBuilder router.ServiceBuilder = RouterBuilder{
msgRouterServiceFactory: stf.NewMsgRouterService,
queryRouter: stf.NewQueryRouterService(),
}
cometService comet.Service = &services.ContextAwareCometInfoService{}
headerService = services.NewGenesisHeaderService(stf.HeaderService{})
eventService = services.NewGenesisEventService(stf.NewEventService())
storeBuilder = root.NewBuilder()
branchService = stf.BranchService{}
)
return depinject.Supply(
routerServiceFactory,
kvServiceFactory,
routerBuilder,
headerService,
cometService,
eventService,
Expand Down
1 change: 1 addition & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ replace (
cosmossdk.io/api => ../api
cosmossdk.io/client/v2 => ../client/v2
cosmossdk.io/collections => ../collections
// TODO: remove when new core tag is released
cosmossdk.io/core => ../core
cosmossdk.io/core/testing => ../core/testing
cosmossdk.io/indexer/postgres => ../indexer/postgres
Expand Down
13 changes: 8 additions & 5 deletions tests/integration/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func NewApp(
appConfig depinject.Config,
startupConfig StartupConfig,
branchService corebranch.Service,
routerServiceFactory router.RouterServiceFactory,
routerServiceBuilder router.ServiceBuilder,
extraOutputs ...interface{},
) (*App, error) {
// create the app with depinject
Expand All @@ -161,11 +161,14 @@ func NewApp(
err error
)

// set default branch and msg router service
if routerServiceFactory == nil {
routerServiceFactory = stf.NewMsgRouterService
// set default router service builder if not provided
if routerServiceBuilder == nil {
routerServiceBuilder = runtime.NewRouterBuilder(
stf.NewMsgRouterService, stf.NewQueryRouterService(),
)
}

// set default branch service if not provided
if branchService == nil {
branchService = stf.BranchService{}
}
Expand All @@ -191,7 +194,7 @@ func NewApp(
&eventService{},
storeBuilder,
branchService,
routerServiceFactory,
routerServiceBuilder,
),
depinject.Invoke(
std.RegisterInterfaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ func TestAccountsBaseAccountRetroCompat(t *testing.T) {
anyPk, err := codectypes.NewAnyWithValue(secp256k1.GenPrivKey().PubKey())
require.NoError(t, err)

// we init two accounts to have account num not be zero.
// we init two accounts. Account number should start with 4
// since the first three accounts are fee_collector, bonded_tokens_pool, not_bonded_tokens_pool
// generated by init genesis plus one more genesis account, which make the current account number 4.
_, _, err = f.accountsKeeper.Init(f.ctx, "base", []byte("me"), &basev1.MsgInit{PubKey: anyPk}, nil)
require.NoError(t, err)

Expand All @@ -178,5 +180,6 @@ func TestAccountsBaseAccountRetroCompat(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, info.Info)
require.Equal(t, info.Info.PubKey, anyPk)
require.Equal(t, info.Info.AccountNumber, uint64(1))
// Account number should be 5
require.Equal(t, info.Info.AccountNumber, uint64(5))
}
35 changes: 27 additions & 8 deletions tests/integration/v2/auth/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/x/accounts"
_ "cosmossdk.io/x/accounts" // import as blank for app wiring
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
accountsv1 "cosmossdk.io/x/accounts/v1"
_ "cosmossdk.io/x/bank" // import as blank for app wiring
bankkeeper "cosmossdk.io/x/bank/keeper"
banktypes "cosmossdk.io/x/bank/types"
Expand All @@ -37,8 +38,6 @@ type suite struct {
authKeeper authkeeper.AccountKeeper
accountsKeeper accounts.Keeper
bankKeeper bankkeeper.Keeper

routerService *integration.RouterService
}

func (s suite) mustAddr(address []byte) string {
Expand All @@ -65,13 +64,18 @@ func createTestSuite(t *testing.T) *suite {
var err error
startupCfg := integration.DefaultStartUpConfig(t)

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

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

queryRouterService := integration.NewRouterService()
res.registerQueryRouterService(queryRouterService)

serviceBuilder := runtime.NewRouterBuilder(routerFactory, queryRouterService)

res.app, err = integration.NewApp(
depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Provide(
// inject desired account types:
Expand All @@ -85,7 +89,7 @@ func createTestSuite(t *testing.T) *suite {
ProvideMockRetroCompatAccountNoInfo,
ProvideMockRetroCompatAccountNoImplement,
), depinject.Supply(log.NewNopLogger())),
startupCfg, &integration.BranchService{}, routerFactory,
startupCfg, &integration.BranchService{}, serviceBuilder,
&res.bankKeeper, &res.accountsKeeper, &res.authKeeper)
require.NoError(t, err)

Expand All @@ -94,7 +98,7 @@ func createTestSuite(t *testing.T) *suite {
return &res
}

func (s *suite) registerRouterService(router *integration.RouterService) {
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)
Expand All @@ -108,3 +112,18 @@ func (s *suite) registerRouterService(router *integration.RouterService) {

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

func (s *suite) registerQueryRouterService(router *integration.RouterService) {
// register custom router service
queryHandler := func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error) {
req, ok := msg.(*accountsv1.AccountNumberRequest)
if !ok {
return nil, integration.ErrInvalidMsgType
}
qs := accounts.NewQueryServer(s.accountsKeeper)
resp, err := qs.AccountNumber(ctx, req)
return resp, err
}

router.RegisterHandler(queryHandler, "cosmos.accounts.v1.AccountNumberRequest")
}
1 change: 1 addition & 0 deletions x/auth/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (ak AccountKeeper) InitGenesis(ctx context.Context, data types.GenesisState
}
lastAccNum = &n
}
fmt.Println(accNum)
ak.SetAccount(ctx, acc)
}

Expand Down

0 comments on commit c25f6cf

Please sign in to comment.