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

Refine IBC Integration & app.go Refactor #138

Merged
merged 3 commits into from
Jan 4, 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
146 changes: 102 additions & 44 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,35 @@ import (
"github.com/cosmos/ibc-go/modules/capability"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ica "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
icacontrollerkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/keeper"
icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types"
icahost "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host"
icahostkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/keeper"
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"

// ibc
ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee"
ibcfeekeeper "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/keeper"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcclient "github.com/cosmos/ibc-go/v8/modules/core/02-client"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcporttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"

// ica
ica "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
icacontroller "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller"
icacontrollerkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/keeper"
icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types"
icahost "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host"
icahostkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/keeper"
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"

appparams "github.com/sedaprotocol/seda-chain/app/params"
"github.com/sedaprotocol/seda-chain/docs"
randomness "github.com/sedaprotocol/seda-chain/x/randomness"
"github.com/sedaprotocol/seda-chain/x/randomness/keeper"
randomnesskeeper "github.com/sedaprotocol/seda-chain/x/randomness/keeper"
randomnesstypes "github.com/sedaprotocol/seda-chain/x/randomness/types"
customstaking "github.com/sedaprotocol/seda-chain/x/staking"
Expand Down Expand Up @@ -165,14 +172,14 @@ var (
capability.AppModuleBasic{},
wasm.AppModuleBasic{},
ibc.AppModuleBasic{},
ibctm.AppModuleBasic{},
ibcfee.AppModuleBasic{},
params.AppModuleBasic{},
transfer.AppModuleBasic{},
ica.AppModuleBasic{},
wasmstorage.AppModuleBasic{},
randomness.AppModuleBasic{},
crisis.AppModuleBasic{},
// ibctm.AppModuleBasic{},
// solomachine.AppModuleBasic{},
)

Expand Down Expand Up @@ -244,6 +251,7 @@ type App struct {
ConsensusParamsKeeper consensusparamkeeper.Keeper
CircuitKeeper circuitkeeper.Keeper

// ibc
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
ICAHostKeeper icahostkeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
Expand All @@ -259,6 +267,7 @@ type App struct {
ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper

// seda modules
WasmStorageKeeper wasmstoragekeeper.Keeper
RandomnessKeeper randomnesskeeper.Keeper

Expand All @@ -281,6 +290,9 @@ func NewApp(
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *App {
/* =================================================== */
/* ENCODING CONFIG */
/* =================================================== */
// building encodings
interfaceRegistry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
Expand All @@ -303,8 +315,9 @@ func NewApp(
std.RegisterLegacyAminoCodec(legacyAmino)
std.RegisterInterfaces(interfaceRegistry)

// TO-DO
// building BaseApp
/* =================================================== */
/* BASEAPP CONFIG */
/* =================================================== */
nonceMempool := mempool.NewSenderNonceMempool()
mempoolOpt := baseapp.SetMempool(nonceMempool)
baseAppOptions = append(baseAppOptions, mempoolOpt)
Expand All @@ -315,6 +328,9 @@ func NewApp(
bApp.SetInterfaceRegistry(interfaceRegistry)
bApp.SetTxEncoder(txConfig.TxEncoder())

/* =================================================== */
/* STORE KEYS */
/* =================================================== */
keys := storetypes.NewKVStoreKeys(
authtypes.StoreKey, authz.ModuleName, banktypes.StoreKey, stakingtypes.StoreKey,
crisistypes.StoreKey, minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
Expand All @@ -341,6 +357,9 @@ func NewApp(
memKeys: memKeys,
}

/* =================================================== */
/* KEEPERS */
/* =================================================== */
app.ParamsKeeper = initParamsKeeper(
appCodec,
legacyAmino,
Expand Down Expand Up @@ -483,9 +502,6 @@ func NewApp(
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

// ... other modules keepers

// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
keys[ibcexported.StoreKey],
Expand All @@ -496,15 +512,13 @@ func NewApp(
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec, keys[ibcfeetypes.StoreKey],
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.PortKeeper, app.AccountKeeper, app.BankKeeper,
)

// Create Transfer Keepers
app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
keys[ibctransfertypes.StoreKey],
Expand All @@ -517,8 +531,6 @@ func NewApp(
scopedTransferKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
transferModule := transfer.NewAppModule(app.TransferKeeper)
transferIBCModule := transfer.NewIBCModule(app.TransferKeeper)

app.ICAHostKeeper = icahostkeeper.NewKeeper(
appCodec,
Expand All @@ -541,8 +553,6 @@ func NewApp(
scopedICAControllerKeeper, app.MsgServiceRouter(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
icaModule := ica.NewAppModule(&icaControllerKeeper, &app.ICAHostKeeper)
icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper)

// Create evidence Keeper for to register the IBC light client misbehavior evidence route
evidenceKeeper := evidencekeeper.NewKeeper(
Expand All @@ -568,16 +578,12 @@ func NewApp(
panic(fmt.Sprintf("error while reading wasm config: %s", err))
}

randomnessQueryPlugin := keeper.NewQuerierImpl(app.RandomnessKeeper)
randomnessQueryPlugin := randomnesskeeper.NewQuerierImpl(app.RandomnessKeeper)
queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: keeper.SeedQueryPlugin(randomnessQueryPlugin),
Custom: randomnesskeeper.SeedQueryPlugin(randomnessQueryPlugin),
})
wasmOpts := []wasmkeeper.Option{queryPluginOpt}

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
// See https://github.com/CosmWasm/cosmwasm/blob/main/docs/CAPABILITIES-BUILT-IN.md
availableCapabilities := "iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2"
app.WasmKeeper = wasmkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[wasmtypes.StoreKey]),
Expand All @@ -594,7 +600,7 @@ func NewApp(
app.GRPCQueryRouter(),
wasmDir,
wasmConfig,
availableCapabilities,
GetWasmCapabilities(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
wasmOpts...,
)
Expand All @@ -614,9 +620,11 @@ func NewApp(
)

govRouter := govv1beta1.NewRouter()
// register the proposal types
govRouter.
AddRoute(govtypes.RouterKey, govv1beta1.ProposalHandler).
AddRoute(paramsproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper))
AddRoute(paramsproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper))

app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
Expand All @@ -634,15 +642,51 @@ func NewApp(
)
wasmStorageModule := wasmstorage.NewAppModule(appCodec, app.WasmStorageKeeper, app.AccountKeeper, app.BankKeeper)

/**** IBC Routing ****/
/* =================================================== */
/* TRANSFER STACK */
/* =================================================== */
var transferStack ibcporttypes.IBCModule
transferStack = transfer.NewIBCModule(app.TransferKeeper)
transferStack = ibcfee.NewIBCMiddleware(transferStack, app.IBCFeeKeeper)
// TO-DO consider adding ibc hooks

/* =================================================== */
/* ICA STACK */
/* =================================================== */
// icaAuthModuleKeeper.SendTx -> icaController.SendPacket -> fee.SendPacket -> channel.SendPacket
var icaControllerStack ibcporttypes.IBCModule
var noAuthzModule ibcporttypes.IBCModule
icaControllerStack = icacontroller.NewIBCMiddleware(noAuthzModule, app.ICAControllerKeeper)
icaControllerStack = ibcfee.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper)

// RecvPacket, message that originates from core IBC and goes down to app, the flow is:
// channel.RecvPacket -> fee.OnRecvPacket -> icaHost.OnRecvPacket
var icaHostStack ibcporttypes.IBCModule
icaHostStack = icahost.NewIBCModule(app.ICAHostKeeper)
icaHostStack = ibcfee.NewIBCMiddleware(icaHostStack, app.IBCFeeKeeper)

/* =================================================== */
/* WASM STACK */
/* =================================================== */
var wasmStack ibcporttypes.IBCModule
wasmStack = wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper, app.IBCFeeKeeper)
wasmStack = ibcfee.NewIBCMiddleware(wasmStack, app.IBCFeeKeeper)

/* =================================================== */
/* IBC ROUTING */
/* =================================================== */

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
ibcRouter.AddRoute(icahosttypes.SubModuleName, icaHostIBCModule).
AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
ibcRouter := ibcporttypes.NewRouter().
AddRoute(icahosttypes.SubModuleName, icaHostStack).
AddRoute(wasmtypes.ModuleName, wasmStack).
AddRoute(icacontrollertypes.SubModuleName, icaControllerStack).
AddRoute(ibctransfertypes.ModuleName, transferStack)
app.IBCKeeper.SetRouter(ibcRouter)

/**** Module Hooks ****/
/* =================================================== */
/* MODULE HOOKS */
/* =================================================== */

// register hooks after all modules have been initialized

Expand All @@ -654,7 +698,9 @@ func NewApp(
),
)

/**** Module Options ****/
/* =================================================== */
/* MODULE OPTIONS */
/* =================================================== */

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
Expand All @@ -663,6 +709,7 @@ func NewApp(
// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.

// TO-DO double check the modules
app.mm = module.NewManager(
genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app, txConfig),
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, nil),
Expand All @@ -685,8 +732,8 @@ func NewApp(
ibc.NewAppModule(app.IBCKeeper),
ibcfee.NewAppModule(app.IBCFeeKeeper),
params.NewAppModule(app.ParamsKeeper),
transferModule,
icaModule,
transfer.NewAppModule(app.TransferKeeper),
ica.NewAppModule(&icaControllerKeeper, &app.ICAHostKeeper),
wasmStorageModule,
randomnessModule,
crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, nil), // always be last to make sure that it checks for all invariants and not only part of them
Expand Down Expand Up @@ -717,6 +764,7 @@ func NewApp(
app.mm.SetOrderBeginBlockers(
// upgrades should be run first
upgradetypes.ModuleName,
capabilitytypes.ModuleName, // capability module's beginblocker must come before any modules using capabilities (e.g. IBC)
minttypes.ModuleName,
distrtypes.ModuleName,
slashingtypes.ModuleName,
Expand All @@ -733,8 +781,7 @@ func NewApp(
paramstypes.ModuleName,
vestingtypes.ModuleName,
consensusparamtypes.ModuleName,
//
capabilitytypes.ModuleName,
// ibc
ibctransfertypes.ModuleName,
ibcexported.ModuleName,
icatypes.ModuleName,
Expand All @@ -749,6 +796,7 @@ func NewApp(
crisistypes.ModuleName,
govtypes.ModuleName,
stakingtypes.ModuleName,
capabilitytypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
distrtypes.ModuleName,
Expand All @@ -763,8 +811,7 @@ func NewApp(
upgradetypes.ModuleName,
vestingtypes.ModuleName,
consensusparamtypes.ModuleName,
//
capabilitytypes.ModuleName,
// ibc
ibctransfertypes.ModuleName,
ibcexported.ModuleName,
icatypes.ModuleName,
Expand All @@ -781,6 +828,7 @@ func NewApp(
// so that other modules that want to create or claim capabilities afterwards in InitChain
// can do so safely.
genesisModuleOrder := []string{
capabilitytypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
distrtypes.ModuleName,
Expand All @@ -799,13 +847,12 @@ func NewApp(
vestingtypes.ModuleName,
consensusparamtypes.ModuleName,
circuittypes.ModuleName,
// ibc modules
capabilitytypes.ModuleName,
// ibc
ibctransfertypes.ModuleName,
ibcexported.ModuleName,
icatypes.ModuleName,
ibcfeetypes.ModuleName,
wasmtypes.ModuleName,
wasmtypes.ModuleName, // wasm after ibc transfer
// custom modules
wasmstoragetypes.ModuleName,
randomnesstypes.ModuleName,
Expand All @@ -820,6 +867,8 @@ func NewApp(
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.mm.RegisterServices(app.configurator)

// TO-DO register upgrade handlers

autocliv1.RegisterQueryServer(app.GRPCQueryRouter(), runtimeservices.NewAutoCLIQueryService(app.mm.Modules))
reflectionSvc, err := runtimeservices.NewReflectionService()
if err != nil {
Expand Down Expand Up @@ -886,6 +935,15 @@ func NewApp(
),
)

if manager := app.SnapshotManager(); manager != nil {
err = manager.RegisterExtensions(
wasmkeeper.NewWasmSnapshotter(app.CommitMultiStore(), &app.WasmKeeper),
)
if err != nil {
panic("failed to register snapshot extension: " + err.Error())
}
}

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
Expand Down Expand Up @@ -1043,7 +1101,7 @@ func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig
nodeservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

// Register grpc-gateway routes for all modules.
app.BasicModuleManager().RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this causing a specific issue?


// register app's OpenAPI routes.
docs.RegisterOpenAPIService(Name, apiSvr.Router)
Expand Down
Loading
Loading