diff --git a/app/app.go b/app/app.go index 7bd10845..1d60abb1 100644 --- a/app/app.go +++ b/app/app.go @@ -26,7 +26,6 @@ import ( "cosmossdk.io/x/feegrant" feegrantkeeper "cosmossdk.io/x/feegrant/keeper" feegrantmodule "cosmossdk.io/x/feegrant/module" - "cosmossdk.io/x/tx/signing" "cosmossdk.io/x/upgrade" upgradekeeper "cosmossdk.io/x/upgrade/keeper" upgradetypes "cosmossdk.io/x/upgrade/types" @@ -42,7 +41,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services" @@ -100,17 +98,11 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/cosmos/gogoproto/proto" "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" @@ -118,14 +110,26 @@ import ( 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" @@ -135,7 +139,8 @@ import ( ) const ( - Name = "seda-chain" + Name = "seda-chain" + DefaultMaxGas = 100000000 ) var ( @@ -165,6 +170,7 @@ var ( capability.AppModuleBasic{}, wasm.AppModuleBasic{}, ibc.AppModuleBasic{}, + ibctm.AppModuleBasic{}, ibcfee.AppModuleBasic{}, params.AppModuleBasic{}, transfer.AppModuleBasic{}, @@ -172,7 +178,6 @@ var ( wasmstorage.AppModuleBasic{}, randomness.AppModuleBasic{}, crisis.AppModuleBasic{}, - // ibctm.AppModuleBasic{}, // solomachine.AppModuleBasic{}, ) @@ -244,6 +249,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 @@ -259,6 +265,7 @@ type App struct { ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper ScopedWasmKeeper capabilitykeeper.ScopedKeeper + // seda modules WasmStorageKeeper wasmstoragekeeper.Keeper RandomnessKeeper randomnesskeeper.Keeper @@ -281,30 +288,22 @@ func NewApp( appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp), ) *App { - // building encodings - interfaceRegistry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{ - ProtoFiles: proto.HybridResolver, - SigningOptions: signing.Options{ - AddressCodec: address.Bech32Codec{ - Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(), - }, - ValidatorAddressCodec: address.Bech32Codec{ - Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(), - }, - }, - }) - if err != nil { - panic(err) - } - appCodec := codec.NewProtoCodec(interfaceRegistry) - legacyAmino := codec.NewLegacyAmino() - txConfig := authtx.NewTxConfig(appCodec, authtx.DefaultSignModes) + /* =================================================== */ + /* ENCODING CONFIG */ + /* =================================================== */ + encodingConfig := GetEncodingConfig() + + interfaceRegistry := encodingConfig.InterfaceRegistry + appCodec := encodingConfig.Marshaler + txConfig := encodingConfig.TxConfig + legacyAmino := encodingConfig.Amino std.RegisterLegacyAminoCodec(legacyAmino) std.RegisterInterfaces(interfaceRegistry) - // TO-DO - // building BaseApp + /* =================================================== */ + /* BASEAPP CONFIG */ + /* =================================================== */ nonceMempool := mempool.NewSenderNonceMempool() mempoolOpt := baseapp.SetMempool(nonceMempool) baseAppOptions = append(baseAppOptions, mempoolOpt) @@ -315,6 +314,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, @@ -341,6 +343,9 @@ func NewApp( memKeys: memKeys, } + /* =================================================== */ + /* KEEPERS */ + /* =================================================== */ app.ParamsKeeper = initParamsKeeper( appCodec, legacyAmino, @@ -483,9 +488,6 @@ func NewApp( authtypes.NewModuleAddress(govtypes.ModuleName).String(), ) - // ... other modules keepers - - // Create IBC Keeper app.IBCKeeper = ibckeeper.NewKeeper( appCodec, keys[ibcexported.StoreKey], @@ -496,7 +498,6 @@ 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 @@ -504,7 +505,6 @@ func NewApp( app.IBCKeeper.PortKeeper, app.AccountKeeper, app.BankKeeper, ) - // Create Transfer Keepers app.TransferKeeper = ibctransferkeeper.NewKeeper( appCodec, keys[ibctransfertypes.StoreKey], @@ -517,8 +517,6 @@ func NewApp( scopedTransferKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), ) - transferModule := transfer.NewAppModule(app.TransferKeeper) - transferIBCModule := transfer.NewIBCModule(app.TransferKeeper) app.ICAHostKeeper = icahostkeeper.NewKeeper( appCodec, @@ -541,8 +539,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( @@ -568,16 +564,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]), @@ -594,7 +586,7 @@ func NewApp( app.GRPCQueryRouter(), wasmDir, wasmConfig, - availableCapabilities, + GetWasmCapabilities(), authtypes.NewModuleAddress(govtypes.ModuleName).String(), wasmOpts..., ) @@ -614,9 +606,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( @@ -634,15 +628,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 @@ -654,7 +684,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. @@ -663,6 +695,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), @@ -685,8 +718,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 @@ -717,6 +750,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, @@ -733,8 +767,7 @@ func NewApp( paramstypes.ModuleName, vestingtypes.ModuleName, consensusparamtypes.ModuleName, - // - capabilitytypes.ModuleName, + // ibc ibctransfertypes.ModuleName, ibcexported.ModuleName, icatypes.ModuleName, @@ -749,6 +782,7 @@ func NewApp( crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName, + capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, @@ -763,8 +797,7 @@ func NewApp( upgradetypes.ModuleName, vestingtypes.ModuleName, consensusparamtypes.ModuleName, - // - capabilitytypes.ModuleName, + // ibc ibctransfertypes.ModuleName, ibcexported.ModuleName, icatypes.ModuleName, @@ -781,6 +814,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, @@ -799,13 +833,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, @@ -820,6 +853,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 { @@ -886,6 +921,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()) @@ -920,6 +964,13 @@ func (app *App) InitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci. if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil { panic(err) } + // override block max gas + if req.ConsensusParams.Block.MaxGas != DefaultMaxGas { + req.ConsensusParams.Block.MaxGas = DefaultMaxGas + } + if err := app.StoreConsensusParams(ctx, *req.ConsensusParams); err != nil { + return nil, fmt.Errorf("failed to store consensus params: %w", err) + } app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap()) return app.mm.InitGenesis(ctx, app.appCodec, genesisState) } @@ -1043,7 +1094,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) // register app's OpenAPI routes. docs.RegisterOpenAPIService(Name, apiSvr.Router) diff --git a/app/encoding.go b/app/encoding.go index 6af75256..b115e08f 100644 --- a/app/encoding.go +++ b/app/encoding.go @@ -1,14 +1,18 @@ package app import ( + "cosmossdk.io/x/tx/signing" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/std" - "github.com/cosmos/cosmos-sdk/x/auth/tx" + sdk "github.com/cosmos/cosmos-sdk/types" + authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" + "github.com/cosmos/gogoproto/proto" ) -var encodingConfig = initEncodingConfig() +var encodingConfig = newEncodingConfig() // EncodingConfig specifies the concrete encoding types to use for a given app. // This is provided for compatibility between protobuf and amino implementations. @@ -24,29 +28,36 @@ func GetEncodingConfig() EncodingConfig { return encodingConfig } -// makeEncodingConfig creates an EncodingConfig for an amino based test configuration. -func makeEncodingConfig() EncodingConfig { +func newEncodingConfig() EncodingConfig { amino := codec.NewLegacyAmino() - interfaceRegistry := types.NewInterfaceRegistry() + + interfaceRegistry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{ + ProtoFiles: proto.HybridResolver, + SigningOptions: signing.Options{ + AddressCodec: address.Bech32Codec{ + Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(), + }, + ValidatorAddressCodec: address.Bech32Codec{ + Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(), + }, + }, + }) + if err != nil { + panic(err) + } + codec := codec.NewProtoCodec(interfaceRegistry) - txCfg := tx.NewTxConfig(codec, tx.DefaultSignModes) + txCfg := authtx.NewTxConfig(codec, authtx.DefaultSignModes) - return EncodingConfig{ + encodingConfig := EncodingConfig{ InterfaceRegistry: interfaceRegistry, Marshaler: codec, TxConfig: txCfg, Amino: amino, } -} - -// initEncodingConfig initializes an EncodingConfig. -func initEncodingConfig() EncodingConfig { - encodingConfig := makeEncodingConfig() std.RegisterLegacyAminoCodec(encodingConfig.Amino) std.RegisterInterfaces(encodingConfig.InterfaceRegistry) - - // ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino) ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry) return encodingConfig diff --git a/app/wasm.go b/app/wasm.go new file mode 100644 index 00000000..4bace1a3 --- /dev/null +++ b/app/wasm.go @@ -0,0 +1,18 @@ +package app + +import "strings" + +// 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 +var wasmCapabilities = []string{ + "iterator", + "staking", + "stargate", + "cosmwasm_1_1", + "cosmwasm_1_2", +} + +func GetWasmCapabilities() string { + return strings.Join(wasmCapabilities, ",") +}