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: improve code #420

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
paths:
- '**/*.go'
- '**/*.mod'
paths-ignore:
- '**/*' # 忽略其他所有文件
jobs:
test-unit:
name: Test Units
Expand Down
3 changes: 1 addition & 2 deletions e2e/random/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type QueryTestSuite struct {

// SetupSuite sets up test suite
func (s *QueryTestSuite) SetupSuite() {
s.SetModifyConfigFn(func(cfg *network.Config) {
s.SetupSuiteWithModifyConfigFn(func(cfg *network.Config) {
var serviceGenState servicetypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[servicetypes.ModuleName], &serviceGenState)

Expand All @@ -43,7 +43,6 @@ func (s *QueryTestSuite) SetupSuite() {
)
cfg.GenesisState[servicetypes.ModuleName] = cfg.Codec.MustMarshalJSON(&serviceGenState)
})
s.TestSuite.SetupSuite()
}

// TestQueryCmd tests all query command in the nft module
Expand Down
3 changes: 1 addition & 2 deletions e2e/random/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type TxTestSuite struct {

// SetupSuite sets up test suite
func (s *TxTestSuite) SetupSuite() {
s.SetModifyConfigFn(func(cfg *network.Config) {
s.SetupSuiteWithModifyConfigFn(func(cfg *network.Config) {
var serviceGenState servicetypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[servicetypes.ModuleName], &serviceGenState)

Expand All @@ -42,7 +42,6 @@ func (s *TxTestSuite) SetupSuite() {
)
cfg.GenesisState[servicetypes.ModuleName] = cfg.Codec.MustMarshalJSON(&serviceGenState)
})
s.TestSuite.SetupSuite()
}

// TestTxCmd tests all tx command in the nft module
Expand Down
3 changes: 1 addition & 2 deletions e2e/service/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type QueryTestSuite struct {

// SetupSuite sets up test suite
func (s *QueryTestSuite) SetupSuite() {
s.SetModifyConfigFn(func(cfg *network.Config) {
s.SetupSuiteWithModifyConfigFn(func(cfg *network.Config) {
var serviceGenesisState servicetypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[servicetypes.ModuleName], &serviceGenesisState)

Expand All @@ -36,7 +36,6 @@ func (s *QueryTestSuite) SetupSuite() {
cfg.GenesisState[servicetypes.ModuleName] = cfg.Codec.MustMarshalJSON(&serviceGenesisState)
cfg.NumValidators = 1
})
s.TestSuite.SetupSuite()
}

// TestQueryCmd tests all query command in the service module
Expand Down
3 changes: 1 addition & 2 deletions e2e/service/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type TxTestSuite struct {

// SetupSuite sets up test suite
func (s *TxTestSuite) SetupSuite() {
s.SetModifyConfigFn(func(cfg *network.Config) {
s.SetupSuiteWithModifyConfigFn(func(cfg *network.Config) {
var serviceGenesisState servicetypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[servicetypes.ModuleName], &serviceGenesisState)

Expand All @@ -36,7 +36,6 @@ func (s *TxTestSuite) SetupSuite() {
cfg.GenesisState[servicetypes.ModuleName] = cfg.Codec.MustMarshalJSON(&serviceGenesisState)
cfg.NumValidators = 1
})
s.TestSuite.SetupSuite()
}

// TestQueryCmd tests all query command in the service module
Expand Down
47 changes: 26 additions & 21 deletions e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,44 @@ type ModifyConfigFn = func(cfg *network.Config)
type TestSuite struct {
suite.Suite
simapp.Network
modifyConfigFn ModifyConfigFn

}

// SetupSuite creates a new network for integration tests
func (s *TestSuite) SetupSuite() {
// SetupSuiteWithModifyConfigFn sets up the end-to-end test suite with the given modifyConfigFn.
//
// Parameters:
// - modifyConfigFn: A function that modifies the config for the test suite.
//
// Return type: None.
func (s *TestSuite) SetupSuiteWithModifyConfigFn(modifyConfigFn ModifyConfigFn) {
s.T().Log("setting up e2e test suite")

depInjectOptions := simapp.DepinjectOptions{
Config: AppConfig,
Providers: []interface{}{
keeper.ProvideMockEVM(),
keeper.ProvideMockICS20(),
},
}
if s.modifyConfigFn == nil {
s.Network = simapp.SetupNetwork(s.T(), depInjectOptions)
return
}

cfg, err := simapp.NewConfig(depInjectOptions)
cfg, err := simapp.NewConfig(s.DepinjectOptions())
s.Require().NoError(err)

s.modifyConfigFn(&cfg)
modifyConfigFn(&cfg)
s.Network = simapp.SetupNetworkWithConfig(s.T(), cfg)
}

// SetupSuite creates a new network for integration tests
func (s *TestSuite) SetupSuite() {
s.T().Log("setting up e2e test suite")
s.Network = simapp.SetupNetwork(s.T(), s.DepinjectOptions())
}

// TearDownSuite tears down the integration test suite
func (s *TestSuite) TearDownSuite() {
s.T().Log("tearing down e2e test suite")
s.Network.Cleanup()
}

// SetModifyConfigFn sets the modify config function
func (s *TestSuite) SetModifyConfigFn(fn ModifyConfigFn) {
s.modifyConfigFn = fn
}
// DepinjectOptions returns the depinject options for the test suite
func (s *TestSuite) DepinjectOptions() simapp.DepinjectOptions {
return simapp.DepinjectOptions{
Config: AppConfig,
Providers: []interface{}{
keeper.ProvideMockEVM(),
keeper.ProvideMockICS20(),
},
}
}
15 changes: 11 additions & 4 deletions modules/coinswap/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {
)
}

// ProvideKeyTable provides module's KVStore keys
func ProvideKeyTable() types.KeyTable {
return types.ParamKeyTable()
}
Expand All @@ -33,7 +34,8 @@ func (am AppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

type CoinswapInputs struct {
// Inputs define the module inputs for the depinject.
type Inputs struct {
depinject.In

Config *modulev1.Module
Expand All @@ -47,14 +49,19 @@ type CoinswapInputs struct {
LegacySubspace types.Subspace `optional:"true"`
}

type CoinswapOutputs struct {
// Outputs define the module outputs for the depinject.
type Outputs struct {
depinject.Out

CoinswapKeeper keeper.Keeper
Module appmodule.AppModule
}

func ProvideModule(in CoinswapInputs) CoinswapOutputs {
// ProvideModule creates and returns the coinswap module with the specified inputs.
//
// It takes Inputs as the parameter, which includes the configuration, codec, key, account keeper, bank keeper, and legacy subspace.
// It returns Outputs containing the coinswap keeper and the app module.
func ProvideModule(in Inputs) Outputs {
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
if in.Config.Authority != "" {
Expand All @@ -71,5 +78,5 @@ func ProvideModule(in CoinswapInputs) CoinswapOutputs {
)
m := NewAppModule(in.Cdc, keeper, in.AccountKeeper, in.BankKeeper, in.LegacySubspace)

return CoinswapOutputs{CoinswapKeeper: keeper, Module: m}
return Outputs{CoinswapKeeper: keeper, Module: m}
}
18 changes: 14 additions & 4 deletions modules/farm/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func init() {
)
}

// ProvideKeyTable returns the KeyTable for the farm module's parameters.
//
// No parameters.
// Returns a types.KeyTable.
func ProvideKeyTable() types.KeyTable {
return types.ParamKeyTable()
}
Expand All @@ -33,7 +37,8 @@ func (am AppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

type FarmInputs struct {
// Inputs define the module inputs for the depinject.
type Inputs struct {
depinject.In

Config *modulev1.Module
Expand All @@ -50,14 +55,19 @@ type FarmInputs struct {
LegacySubspace types.Subspace `optional:"true"`
}

type FarmOutputs struct {
// Outputs define the module outputs for the depinject.
type Outputs struct {
depinject.Out

FarmKeeper keeper.Keeper
Module appmodule.AppModule
}

func ProvideModule(in FarmInputs) FarmOutputs {
// ProvideModule creates and returns the farm module with the specified inputs.
//
// It takes Inputs as the parameter, which includes the configuration, codec, key, account keeper, bank keeper, governance keeper, coinswap keeper, and legacy subspace.
// It returns Outputs containing the farm keeper and the app module.
func ProvideModule(in Inputs) Outputs {
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
if in.Config.Authority != "" {
Expand All @@ -78,5 +88,5 @@ func ProvideModule(in FarmInputs) FarmOutputs {
)
m := NewAppModule(in.Cdc, keeper, in.AccountKeeper, in.BankKeeper, in.LegacySubspace)

return FarmOutputs{FarmKeeper: keeper, Module: m}
return Outputs{FarmKeeper: keeper, Module: m}
}
18 changes: 14 additions & 4 deletions modules/htlc/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func init() {
)
}

// ProvideKeyTable returns the key table for the htlc module.
//
// No parameters.
// Returns a types.KeyTable.
func ProvideKeyTable() types.KeyTable {
return types.ParamKeyTable()
}
Expand All @@ -32,7 +36,8 @@ func (am AppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

type HTLCInputs struct {
// Inputs define the module inputs for the depinject.
type Inputs struct {
depinject.In

Config *modulev1.Module
Expand All @@ -46,14 +51,19 @@ type HTLCInputs struct {
LegacySubspace types.Subspace `optional:"true"`
}

type HTLCOutputs struct {
// Outputs define the module outputs for the depinject.
type Outputs struct {
depinject.Out

HTLCKeeper keeper.Keeper
Module appmodule.AppModule
}

func ProvideModule(in HTLCInputs) HTLCOutputs {
// ProvideModule creates and returns the HTLC module with the specified inputs.
//
// It takes Inputs as the parameter, which includes the configuration, codec, key, account keeper, and bank keeper.
// It returns Outputs containing the HTLC keeper and the app module.
func ProvideModule(in Inputs) Outputs {
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
if in.Config.Authority != "" {
Expand All @@ -69,5 +79,5 @@ func ProvideModule(in HTLCInputs) HTLCOutputs {
)
m := NewAppModule(in.Cdc, keeper, in.AccountKeeper, in.BankKeeper, in.LegacySubspace)

return HTLCOutputs{HTLCKeeper: keeper, Module: m}
return Outputs{HTLCKeeper: keeper, Module: m}
}
17 changes: 13 additions & 4 deletions modules/mt/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func (am AppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

type MTInputs struct {
// Inputs define the module inputs for the depinject.
type Inputs struct {
depinject.In

Config *modulev1.Module
Expand All @@ -37,19 +38,27 @@ type MTInputs struct {
BankKeeper types.BankKeeper
}

type MTOutputs struct {
// Outputs define the module outputs for the depinject.
type Outputs struct {
depinject.Out

MTKeeper keeper.Keeper
Module appmodule.AppModule
}

func ProvideModule(in MTInputs) MTOutputs {
// ProvideModule creates a new MTKeeper and AppModule using the provided inputs and returns the Outputs.
//
// Parameters:
// - in: the Inputs struct containing the necessary dependencies for creating the MTKeeper and AppModule.
//
// Returns:
// - Outputs: the struct containing the MTKeeper and AppModule.
func ProvideModule(in Inputs) Outputs {
keeper := keeper.NewKeeper(
in.Cdc,
in.Key,
)
m := NewAppModule(in.Cdc, keeper, in.AccountKeeper, in.BankKeeper)

return MTOutputs{MTKeeper: keeper, Module: m}
return Outputs{MTKeeper: keeper, Module: m}
}
15 changes: 10 additions & 5 deletions modules/nft/depinject.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package module
package nft

import (
"cosmossdk.io/core/appmodule"
Expand Down Expand Up @@ -26,7 +26,8 @@ func (am AppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

type NFTInputs struct {
// Inputs define the arguments used to instantiate an app module.
type Inputs struct {
depinject.In

Config *modulev1.Module
Expand All @@ -37,14 +38,18 @@ type NFTInputs struct {
BankKeeper types.BankKeeper
}

type NFTOutputs struct {
// Outputs define the read-only arguments return by depinject.
type Outputs struct {
depinject.Out

NFTKeeper keeper.Keeper
Module appmodule.AppModule
}

func ProvideModule(in NFTInputs) NFTOutputs {
// ProvideModule provides a module for the NFT with the given inputs and returns the NFT keeper and module.
//
// Takes Inputs as input parameters and returns Outputs.
func ProvideModule(in Inputs) Outputs {
keeper := keeper.NewKeeper(
in.Cdc,
in.Key,
Expand All @@ -53,5 +58,5 @@ func ProvideModule(in NFTInputs) NFTOutputs {
)
m := NewAppModule(in.Cdc, keeper, in.AccountKeeper, in.BankKeeper)

return NFTOutputs{NFTKeeper: keeper, Module: m}
return Outputs{NFTKeeper: keeper, Module: m}
}
2 changes: 1 addition & 1 deletion modules/nft/module.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package module
package nft

import (
"context"
Expand Down
Loading
Loading