Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamer committed Mar 6, 2024
1 parent 3bd879c commit 2671d64
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 44 deletions.
46 changes: 7 additions & 39 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -33,9 +32,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/evmos/ethermint/ethereum/eip712"
srvflags "github.com/evmos/ethermint/server/flags"

irishubante "github.com/irisnet/irishub/v2/ante"
Expand Down Expand Up @@ -92,7 +89,8 @@ func NewIrisApp(
logger,
db,
encodingConfig.TxConfig.TxDecoder(),
baseAppOptions...)
baseAppOptions...,
)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
bApp.SetInterfaceRegistry(interfaceRegistry)
Expand Down Expand Up @@ -197,7 +195,7 @@ func NewIrisApp(
},
)

app.Inject()
app.Init()
app.SetAnteHandler(anteHandler)
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
Expand Down Expand Up @@ -296,35 +294,6 @@ func (app *IrisApp) InterfaceRegistry() types.InterfaceRegistry {
return app.interfaceRegistry
}

// GetKey returns the KVStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
func (app *IrisApp) GetKey(storeKey string) *storetypes.KVStoreKey {
return app.KvStoreKeys()[storeKey]
}

// GetTKey returns the TransientStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
func (app *IrisApp) GetTKey(storeKey string) *storetypes.TransientStoreKey {
return app.TransientStoreKeys()[storeKey]
}

// GetMemKey returns the MemStoreKey for the provided mem key.
//
// NOTE: This is solely used for testing purposes.
func (app *IrisApp) GetMemKey(storeKey string) *storetypes.MemoryStoreKey {
return app.MemoryStoreKeys()[storeKey]
}

// GetSubspace returns a param subspace for a given module name.
//
// NOTE: This is solely to be used for testing purposes.
func (app *IrisApp) GetSubspace(moduleName string) paramstypes.Subspace {
subspace, _ := app.ParamsKeeper.GetSubspace(moduleName)
return subspace
}

// SimulationManager implements the SimulationApp interface
func (app *IrisApp) SimulationManager() *module.SimulationManager {
return app.sm
Expand Down Expand Up @@ -401,11 +370,10 @@ func (app *IrisApp) DefaultGenesis() map[string]json.RawMessage {
return ModuleBasics.DefaultGenesis(app.AppCodec())
}

func (app *IrisApp) Inject() {
eip712.InjectCodec(eip712.Codec{
InterfaceRegistry: app.interfaceRegistry,
Amino: app.legacyAmino,
})
// Init initializes the IrisApp.
//
func (app *IrisApp) Init() {
iristypes.Init(app.legacyAmino, app.interfaceRegistry)
}

// NoOpMempoolOption returns a function that sets up a no-op mempool for the given BaseApp.
Expand Down
7 changes: 6 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ import (
iristypes "github.com/irisnet/irishub/v2/types"
)

// AppKeepers defines a structure used to consolidate all
// the keepers needed to run an iris app.
type AppKeepers struct {

// keys to access the substores
keys map[string]*storetypes.KVStoreKey
tkeys map[string]*storetypes.TransientStoreKey
Expand Down Expand Up @@ -180,6 +181,10 @@ type AppKeepers struct {
IBCNftTransferModule nfttransfer.AppModule
}

// New initializes a new instance of AppKeepers.
//
// It takes in various parameters including appCodec, bApp, legacyAmino, maccPerms, modAccAddrs, blockedAddress, skipUpgradeHeights, homePath, invCheckPeriod, logger, and appOpts.
// It returns an instance of AppKeepers.
func New(
appCodec codec.Codec,
bApp *baseapp.BaseApp,
Expand Down
12 changes: 12 additions & 0 deletions app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,26 @@ func (appKeepers *AppKeepers) genStoreKeys() {

}

// KvStoreKeys returns the map of string to KVStoreKey.
//
// None.
// map[string]*storetypes.KVStoreKey.
func (appKeepers *AppKeepers) KvStoreKeys() map[string]*storetypes.KVStoreKey {
return appKeepers.keys
}

// TransientStoreKeys returns the map of transient store keys.
//
// No parameters.
// Returns a map[string]*storetypes.TransientStoreKey.
func (appKeepers *AppKeepers) TransientStoreKeys() map[string]*storetypes.TransientStoreKey {
return appKeepers.tkeys
}

// MemoryStoreKeys returns the map of type map[string]*storetypes.MemoryStoreKey.
//
// No parameters.
// Returns a map of type map[string]*storetypes.MemoryStoreKey.
func (appKeepers *AppKeepers) MemoryStoreKeys() map[string]*storetypes.MemoryStoreKey {
return appKeepers.memKeys
}
Expand Down
2 changes: 1 addition & 1 deletion app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/irisnet/irishub/v2/app/upgrades"
Expand All @@ -26,7 +27,6 @@ func (app *IrisApp) RegisterUpgradePlans() {
func (app *IrisApp) upgradeTools() upgrades.Tools {
return upgrades.Tools{
AppCodec: app.AppCodec(),
GetKey: app.GetKey,
ModuleManager: app.mm,
ReaderWriter: app,
AppKeepers: app.AppKeepers,
Expand Down
8 changes: 6 additions & 2 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
store "github.com/cosmos/cosmos-sdk/store/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
Expand All @@ -28,14 +27,15 @@ type Upgrade struct {
StoreUpgrades *store.StoreUpgrades
}

// ConsensusParamsReaderWriter defines the interface for reading and writing consensus params
type ConsensusParamsReaderWriter interface {
StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusParams)
GetConsensusParams(ctx sdk.Context) *tmproto.ConsensusParams
}

// Tools contains all the modules necessary for an upgrade
type Tools struct {
AppCodec codec.Codec
GetKey func(moduleName string) *storetypes.KVStoreKey
ModuleManager *module.Manager
ReaderWriter ConsensusParamsReaderWriter
keepers.AppKeepers
Expand All @@ -45,6 +45,10 @@ type upgradeRouter struct {
mu map[string]Upgrade
}

// NewUpgradeRouter creates a new upgrade router.
//
// No parameters.
// Returns a pointer to upgradeRouter.
func NewUpgradeRouter() *upgradeRouter {
return &upgradeRouter{make(map[string]Upgrade)}
}
Expand Down
22 changes: 21 additions & 1 deletion types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (

"github.com/cometbft/cometbft/crypto"

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

"github.com/evmos/ethermint/ethereum/eip712"
etherminttypes "github.com/evmos/ethermint/types"

tokentypes "github.com/irisnet/irismod/modules/token/types"
Expand All @@ -18,16 +21,27 @@ import (
)

const (
// AppName is the name of the app
AppName = "IrisApp"
)

var (
// NativeToken represents the native token
NativeToken tokenv1.Token
// EvmToken represents the EVM token
EvmToken tokenv1.Token
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
)

func init() {
// Init initializes the legacyAmino and interfaceRegistry.
// Parameters: legacyAmino *codec.LegacyAmino, interfaceRegistry types.InterfaceRegistry.
func Init(legacyAmino *codec.LegacyAmino, interfaceRegistry types.InterfaceRegistry) {
initApp()
initEVM(legacyAmino, interfaceRegistry)
}

func initApp() {
// set bech32 prefix
address.ConfigureBech32Prefix()

Expand Down Expand Up @@ -80,6 +94,12 @@ func init() {
NativeToken.Mintable,
owner,
)
}

func initEVM(legacyAmino *codec.LegacyAmino, interfaceRegistry types.InterfaceRegistry) {
etherminttypes.InjectChainIDParser(parseChainID)
eip712.InjectCodec(eip712.Codec{
InterfaceRegistry: interfaceRegistry,
Amino: legacyAmino,
})
}

0 comments on commit 2671d64

Please sign in to comment.