|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + storetypes "cosmossdk.io/store/types" |
| 8 | + "github.com/CosmWasm/wasmd/x/wasm" |
| 9 | + wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" |
| 10 | + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" |
| 11 | + "github.com/cosmos/cosmos-sdk/client" |
| 12 | + "github.com/cosmos/cosmos-sdk/runtime" |
| 13 | + servertypes "github.com/cosmos/cosmos-sdk/server/types" |
| 14 | + "github.com/cosmos/cosmos-sdk/types/msgservice" |
| 15 | + "github.com/cosmos/cosmos-sdk/x/auth/ante" |
| 16 | + "github.com/cosmos/cosmos-sdk/x/auth/posthandler" |
| 17 | + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" |
| 18 | + distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" |
| 19 | + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" |
| 20 | + "github.com/cosmos/gogoproto/proto" |
| 21 | + ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee" |
| 22 | + porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types" |
| 23 | +) |
| 24 | + |
| 25 | +// AllCapabilities returns all capabilities available with the current wasmvm |
| 26 | +// See https://github.com/CosmWasm/cosmwasm/blob/main/docs/CAPABILITIES-BUILT-IN.md |
| 27 | +// This functionality is going to be moved upstream: https://github.com/CosmWasm/wasmvm/issues/425 |
| 28 | +func AllCapabilities() []string { |
| 29 | + return []string{ |
| 30 | + "iterator", |
| 31 | + "staking", |
| 32 | + "stargate", |
| 33 | + "cosmwasm_1_1", |
| 34 | + "cosmwasm_1_2", |
| 35 | + "cosmwasm_1_3", |
| 36 | + "cosmwasm_1_4", |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// registerWasmModules register CosmWasm keepers and non dependency inject modules. |
| 41 | +func (app *App) registerWasmModules( |
| 42 | + appOpts servertypes.AppOptions, |
| 43 | + wasmOpts ...wasmkeeper.Option, |
| 44 | +) (porttypes.IBCModule, error) { |
| 45 | + // set up non depinject support modules store keys |
| 46 | + if err := app.RegisterStores( |
| 47 | + storetypes.NewKVStoreKey(wasmtypes.StoreKey), |
| 48 | + ); err != nil { |
| 49 | + panic(err) |
| 50 | + } |
| 51 | + |
| 52 | + scopedWasmKeeper := app.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName) |
| 53 | + |
| 54 | + wasmConfig, err := wasm.ReadWasmConfig(appOpts) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("error while reading wasm config: %s", err) |
| 57 | + } |
| 58 | + |
| 59 | + availableCapabilities := strings.Join(AllCapabilities(), ",") |
| 60 | + app.WasmKeeper = wasmkeeper.NewKeeper( |
| 61 | + app.AppCodec(), |
| 62 | + runtime.NewKVStoreService(app.GetKey(wasmtypes.StoreKey)), |
| 63 | + app.AccountKeeper, |
| 64 | + app.BankKeeper, |
| 65 | + app.StakingKeeper, |
| 66 | + distrkeeper.NewQuerier(app.DistrKeeper), |
| 67 | + app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware |
| 68 | + app.IBCKeeper.ChannelKeeper, |
| 69 | + app.IBCKeeper.PortKeeper, |
| 70 | + scopedWasmKeeper, |
| 71 | + app.TransferKeeper, |
| 72 | + app.MsgServiceRouter(), |
| 73 | + app.GRPCQueryRouter(), |
| 74 | + DefaultNodeHome, |
| 75 | + wasmConfig, |
| 76 | + availableCapabilities, |
| 77 | + authtypes.NewModuleAddress(govtypes.ModuleName).String(), |
| 78 | + wasmOpts..., |
| 79 | + ) |
| 80 | + |
| 81 | + // register IBC modules |
| 82 | + if err := app.RegisterModules( |
| 83 | + wasm.NewAppModule( |
| 84 | + app.AppCodec(), |
| 85 | + &app.WasmKeeper, |
| 86 | + app.StakingKeeper, |
| 87 | + app.AccountKeeper, |
| 88 | + app.BankKeeper, |
| 89 | + app.MsgServiceRouter(), |
| 90 | + app.GetSubspace(wasmtypes.ModuleName), |
| 91 | + )); err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + if err := app.setAnteHandler(app.txConfig, wasmConfig, app.GetKey(wasmtypes.StoreKey)); err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + if manager := app.SnapshotManager(); manager != nil { |
| 100 | + err := manager.RegisterExtensions( |
| 101 | + wasmkeeper.NewWasmSnapshotter(app.CommitMultiStore(), &app.WasmKeeper), |
| 102 | + ) |
| 103 | + if err != nil { |
| 104 | + return nil, fmt.Errorf("failed to register snapshot extension: %s", err) |
| 105 | + } |
| 106 | + } |
| 107 | + app.ScopedWasmKeeper = scopedWasmKeeper |
| 108 | + |
| 109 | + if err := app.setPostHandler(); err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + // At startup, after all modules have been registered, check that all proto |
| 114 | + // annotations are correct. |
| 115 | + protoFiles, err := proto.MergedRegistry() |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + err = msgservice.ValidateProtoAnnotations(protoFiles) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + // Create fee enabled wasm ibc Stack |
| 125 | + var wasmStack porttypes.IBCModule |
| 126 | + wasmStack = wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper, app.IBCFeeKeeper) |
| 127 | + wasmStack = ibcfee.NewIBCMiddleware(wasmStack, app.IBCFeeKeeper) |
| 128 | + |
| 129 | + return wasmStack, nil |
| 130 | +} |
| 131 | + |
| 132 | +func (app *App) setPostHandler() error { |
| 133 | + postHandler, err := posthandler.NewPostHandler( |
| 134 | + posthandler.HandlerOptions{}, |
| 135 | + ) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + app.SetPostHandler(postHandler) |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig, txCounterStoreKey *storetypes.KVStoreKey) error { |
| 144 | + anteHandler, err := NewAnteHandler( |
| 145 | + HandlerOptions{ |
| 146 | + HandlerOptions: ante.HandlerOptions{ |
| 147 | + AccountKeeper: app.AccountKeeper, |
| 148 | + BankKeeper: app.BankKeeper, |
| 149 | + SignModeHandler: txConfig.SignModeHandler(), |
| 150 | + FeegrantKeeper: app.FeeGrantKeeper, |
| 151 | + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, |
| 152 | + }, |
| 153 | + IBCKeeper: app.IBCKeeper, |
| 154 | + WasmConfig: &wasmConfig, |
| 155 | + WasmKeeper: &app.WasmKeeper, |
| 156 | + TXCounterStoreService: runtime.NewKVStoreService(txCounterStoreKey), |
| 157 | + CircuitKeeper: &app.CircuitBreakerKeeper, |
| 158 | + }, |
| 159 | + ) |
| 160 | + if err != nil { |
| 161 | + return fmt.Errorf("failed to create AnteHandler: %s", err) |
| 162 | + } |
| 163 | + |
| 164 | + // Set the AnteHandler for the app |
| 165 | + app.SetAnteHandler(anteHandler) |
| 166 | + return nil |
| 167 | +} |
0 commit comments