Skip to content

Commit

Permalink
convert wasm to use ignite method which places wasm in its own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dekm committed Feb 19, 2024
1 parent 4dcceb8 commit 472ebb3
Show file tree
Hide file tree
Showing 17 changed files with 104,211 additions and 13,998 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ COPY --from=go-builder /code/bin/paxd /usr/bin/paxd
COPY docker/* /opt/
RUN chmod +x /opt/*.sh

COPY contracts/artifacts /opt/artifacts/

WORKDIR /opt

# Expose necessary ports (adjust these according to your application's needs)
Expand Down
104 changes: 70 additions & 34 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package app

import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"

//tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
Expand All @@ -31,7 +32,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"

"github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -290,11 +290,6 @@ func New(
panic(err)
}

// register interfaces for wasm
//RegisterInterfaces(app.interfaceRegistry)
//wasmtypes.RegisterLegacyAminoCodec(app.legacyAmino)
wasmtypes.RegisterInterfaces(app.interfaceRegistry)

// Below we could construct and set an application specific mempool and
// ABCI 1.0 PrepareProposal and ProcessProposal handlers. These defaults are
// already set in the SDK's BaseApp, this shows an example of how to override
Expand Down Expand Up @@ -331,6 +326,21 @@ func New(

app.registerLegecyModules(appOpts)

// In v0.46, the SDK introduces _postHandlers_. PostHandlers are like
// antehandlers, but are run _after_ the `runMsgs` execution. They are also
// defined as a chain, and have the same signature as antehandlers.
//
// In baseapp, postHandlers are run in the same store branch as `runMsgs`,
// meaning that both `runMsgs` and `postHandler` state will be committed if
// both are successful, and both will be reverted if any of the two fails.
//
// The SDK exposes a default postHandlers chain
//
// Please note that changing any of the anteHandler or postHandler chain is
// likely to be a state-machine breaking change, which needs a coordinated
// upgrade.
app.setPostHandler()

// register streaming services
if err := app.RegisterStreamingServices(appOpts, app.kvStoreKeys()); err != nil {
return nil, err
Expand All @@ -354,6 +364,9 @@ func New(

app.sm.RegisterStoreDecoders()
app.SetInitChainer(app.InitChainer)
// app.SetPreBlocker(app.PreBlocker)
// app.SetBeginBlocker(app.BeginBlocker)
// app.SetEndBlocker(app.EndBlocker)
// A custom InitChainer can be set if extra pre-init-genesis logic is required.
// By default, when using app wiring enabled module, this is not required.
// For instance, the upgrade module will set automatically the module version map in its init genesis thanks to app wiring.
Expand All @@ -365,14 +378,26 @@ func New(
// })

// wasm
txConfig := authtx.NewTxConfig(app.appCodec, authtx.DefaultSignModes)
wasmConfig := wasmtypes.DefaultWasmConfig()
app.setAnteHandler(txConfig, wasmConfig, app.GetKey(wasmtypes.StoreKey))
// txConfig := authtx.NewTxConfig(app.appCodec, authtx.DefaultSignModes)
// wasmConfig := wasmtypes.DefaultWasmConfig()
// app.setAnteHandler(txConfig, wasmConfig, app.GetKey(wasmtypes.StoreKey))

if err := app.Load(loadLatest); err != nil {
return nil, err
}

// if loadLatest {
// if err := app.LoadLatestVersion(); err != nil {
// panic(fmt.Errorf("error loading last version: %w", err))
// }
// ctx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})

// // Initialize pinned codes in wasmvm as they are not persisted there
// if err := app.WasmKeeper.InitializePinnedCodes(ctx); err != nil {
// panic(fmt.Sprintf("failed initialize pinned codes %s", err))
// }
// }

return app, nil
}

Expand Down Expand Up @@ -506,30 +531,41 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
wasmtypes.RegisterInterfaces(registry)
}

func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig, txCounterStoreKey *storetypes.KVStoreKey) {
anteHandler, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
IBCKeeper: app.IBCKeeper,
WasmConfig: &wasmConfig,
WasmKeeper: &app.WasmKeeper,
TXCounterStoreService: runtime.NewKVStoreService(txCounterStoreKey),
CircuitKeeper: &app.CircuitBreakerKeeper,
},
)
if err != nil {
panic(fmt.Errorf("failed to create AnteHandler: %s", err))
}

// Set the AnteHandler for the app
app.SetAnteHandler(anteHandler)
}
// func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig, txCounterStoreKey *storetypes.KVStoreKey) {
// anteHandler, err := NewAnteHandler(
// HandlerOptions{
// HandlerOptions: ante.HandlerOptions{
// AccountKeeper: app.AccountKeeper,
// BankKeeper: app.BankKeeper,
// SignModeHandler: txConfig.SignModeHandler(),
// FeegrantKeeper: app.FeeGrantKeeper,
// SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
// },
// IBCKeeper: app.IBCKeeper,
// WasmConfig: &wasmConfig,
// WasmKeeper: &app.WasmKeeper,
// TXCounterStoreService: runtime.NewKVStoreService(txCounterStoreKey),
// CircuitKeeper: &app.CircuitBreakerKeeper,
// },
// )
// if err != nil {
// panic(fmt.Errorf("failed to create AnteHandler: %s", err))
// }
// fmt.Println("AnteHandler created")
// // Set the AnteHandler for the app
// app.SetAnteHandler(anteHandler)
// }

// func (app *App) setPostHandler() {
// postHandler, err := posthandler.NewPostHandler(
// posthandler.HandlerOptions{},
// )
// if err != nil {
// panic(err)
// }

// app.SetPostHandler(postHandler)
// }

// TxConfig returns WasmApp's TxConfig
func (app *App) TxConfig() client.TxConfig {
Expand Down
126 changes: 61 additions & 65 deletions app/legecyModules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package app

import (
"fmt"
"path/filepath"
"strings"

storetypes "cosmossdk.io/store/types"
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/runtime"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
Expand Down Expand Up @@ -43,9 +37,6 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
solomachine "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
paxwasm "github.com/unigrid-project/pax/app/wasm"
// this line is used by starport scaffolding # ibc/app/import
)

Expand All @@ -61,7 +52,7 @@ func (app *App) registerLegecyModules(appOpts servertypes.AppOptions) {
storetypes.NewKVStoreKey(icacontrollertypes.StoreKey),
storetypes.NewMemoryStoreKey(capabilitytypes.MemStoreKey),
storetypes.NewTransientStoreKey(paramstypes.TStoreKey),
storetypes.NewKVStoreKey(wasmtypes.StoreKey),
//storetypes.NewKVStoreKey(wasmtypes.StoreKey),
); err != nil {
panic(err)
}
Expand All @@ -71,12 +62,12 @@ func (app *App) registerLegecyModules(appOpts servertypes.AppOptions) {
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
app.ParamsKeeper.Subspace(ibcexported.ModuleName).WithKeyTable(keyTable)
app.ParamsKeeper.Subspace(ibctransfertypes.ModuleName).WithKeyTable(ibctransfertypes.ParamKeyTable())
app.ParamsKeeper.Subspace(wasmtypes.ModuleName)
//app.ParamsKeeper.Subspace(wasmtypes.ModuleName)
app.ParamsKeeper.Subspace(icacontrollertypes.SubModuleName).WithKeyTable(icacontrollertypes.ParamKeyTable())
app.ParamsKeeper.Subspace(icahosttypes.SubModuleName).WithKeyTable(icahosttypes.ParamKeyTable())
// add capability keeper and ScopeToModule for ibc module
app.CapabilityKeeper = capabilitykeeper.NewKeeper(
app.AppCodec(),
app.appCodec,
app.GetKey(capabilitytypes.StoreKey),
app.GetMemKey(capabilitytypes.MemStoreKey),
)
Expand All @@ -86,8 +77,8 @@ func (app *App) registerLegecyModules(appOpts servertypes.AppOptions) {
scopedIBCTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
scopedICAControllerKeeper := app.CapabilityKeeper.ScopeToModule(icacontrollertypes.SubModuleName)
scopedICAHostKeeper := app.CapabilityKeeper.ScopeToModule(icahosttypes.SubModuleName)
scopedWasmKeeper := app.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName)

//scopedWasmKeeper := app.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName)
//app.CapabilityKeeper.Seal()
// Create IBC keeper
app.IBCKeeper = ibckeeper.NewKeeper(
app.appCodec,
Expand Down Expand Up @@ -153,6 +144,48 @@ func (app *App) registerLegecyModules(appOpts servertypes.AppOptions) {
)
app.GovKeeper.SetLegacyRouter(govRouter)

// this line is used by starport scaffolding # ibc/app/module

// homePath := cast.ToString(appOpts.Get(flags.FlagHome))
// wasmDir := filepath.Join(homePath, "wasm")
// wasmConfig, err := wasm.ReadWasmConfig(appOpts)
// if err != nil {
// panic(fmt.Sprintf("error while reading wasm config: %s", err))
// }

// var wasmOpts []wasmkeeper.Option
// if cast.ToBool(appOpts.Get("telemetry.enabled")) {
// wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
// }

// availableCapabilities := strings.Join(paxwasm.AllCapabilities(), ",")
// app.WasmKeeper = wasmkeeper.NewKeeper(
// app.appCodec,
// runtime.NewKVStoreService(app.GetKey(wasmtypes.StoreKey)),
// app.AccountKeeper,
// app.BankKeeper,
// app.StakingKeeper,
// distrkeeper.NewQuerier(app.DistrKeeper),
// app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
// app.IBCKeeper.ChannelKeeper,
// app.IBCKeeper.PortKeeper,
// scopedWasmKeeper,
// app.TransferKeeper,
// app.MsgServiceRouter(),
// app.GRPCQueryRouter(),
// wasmDir,
// wasmConfig,
// availableCapabilities,
// authtypes.NewModuleAddress(govtypes.ModuleName).String(),
// wasmOpts...,
// )

app.ScopedIBCKeeper = scopedIBCKeeper
app.ScopedIBCTransferKeeper = scopedIBCTransferKeeper
app.ScopedICAHostKeeper = scopedICAHostKeeper
app.ScopedICAControllerKeeper = scopedICAControllerKeeper
//app.ScopedWasmKeeper = scopedWasmKeeper

// Create IBC modules with ibcfee middleware
transferIBCModule := ibcfee.NewIBCMiddleware(ibctransfer.NewIBCModule(app.TransferKeeper), app.IBCFeeKeeper)

Expand All @@ -165,67 +198,29 @@ func (app *App) registerLegecyModules(appOpts servertypes.AppOptions) {

icaHostIBCModule := ibcfee.NewIBCMiddleware(icahost.NewIBCModule(app.ICAHostKeeper), app.IBCFeeKeeper)

// Create fee enabled wasm ibc Stack
var wasmStack porttypes.IBCModule
wasmStack = wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper, app.IBCFeeKeeper)
wasmStack = ibcfee.NewIBCMiddleware(wasmStack, app.IBCFeeKeeper)
// // Create fee enabled wasm ibc Stack
// var wasmStack porttypes.IBCModule
// wasmStack = wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper, app.IBCFeeKeeper)
// wasmStack = ibcfee.NewIBCMiddleware(wasmStack, app.IBCFeeKeeper)
// Create static IBC router, add transfer route, then set and seal it
wasmStack, err := app.registerWasmModules(appOpts)
if err != nil {
fmt.Printf("error while registering wasm modules: %s", err)
panic(err)
}

ibcRouter := porttypes.NewRouter().
AddRoute(ibctransfertypes.ModuleName, transferIBCModule).
AddRoute(wasmtypes.ModuleName, wasmStack).
AddRoute(icacontrollertypes.SubModuleName, icaControllerIBCModule).
AddRoute(icahosttypes.SubModuleName, icaHostIBCModule)

app.IBCKeeper.SetRouter(ibcRouter)
// this line is used by starport scaffolding # ibc/app/module

homePath := cast.ToString(appOpts.Get(flags.FlagHome))
wasmDir := filepath.Join(homePath, "wasm")
wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
panic(fmt.Sprintf("error while reading wasm config: %s", err))
}

var wasmOpts []wasmkeeper.Option
if cast.ToBool(appOpts.Get("telemetry.enabled")) {
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
}
//add if we use custom commands in wasm
//wasmOpts = append(wasmOpts, wasmkeeper.WithQueryPlugins(okp4wasm.CustomQueryPlugins(&app.LogicKeeper)))

availableCapabilities := strings.Join(paxwasm.AllCapabilities(), ",")
app.WasmKeeper = wasmkeeper.NewKeeper(
app.AppCodec(),
runtime.NewKVStoreService(app.GetKey(wasmtypes.StoreKey)),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
distrkeeper.NewQuerier(app.DistrKeeper),
app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.PortKeeper,
scopedWasmKeeper,
app.TransferKeeper,
app.MsgServiceRouter(),
app.GRPCQueryRouter(),
wasmDir,
wasmConfig,
availableCapabilities,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
wasmOpts...,
)

app.ScopedIBCKeeper = scopedIBCKeeper
app.ScopedIBCTransferKeeper = scopedIBCTransferKeeper
app.ScopedICAHostKeeper = scopedICAHostKeeper
app.ScopedICAControllerKeeper = scopedICAControllerKeeper
app.ScopedWasmKeeper = scopedWasmKeeper

// register modules

fmt.Println("In RegisterModules")
fmt.Println("where RegisterModules")
if err := app.RegisterModules(
wasm.NewAppModule(app.appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
//wasm.NewAppModule(app.appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
ibc.NewAppModule(app.IBCKeeper),
ibctransfer.NewAppModule(app.TransferKeeper),
ibcfee.NewAppModule(app.IBCFeeKeeper),
Expand All @@ -236,11 +231,12 @@ func (app *App) registerLegecyModules(appOpts servertypes.AppOptions) {
); err != nil {
panic(err)
}

}

// AddIBCModuleManager adds the missing IBC modules into the module manager.
func AddLegecyModuleManager(moduleManager module.BasicManager) {
//moduleManager[wasmtypes.ModuleName] = wasm.AppModule{}
moduleManager[wasmtypes.ModuleName] = wasm.AppModule{}
moduleManager[ibcexported.ModuleName] = ibc.AppModule{}
moduleManager[ibctransfertypes.ModuleName] = ibctransfer.AppModule{}
moduleManager[ibcfeetypes.ModuleName] = ibcfee.AppModule{}
Expand Down
Loading

0 comments on commit 472ebb3

Please sign in to comment.