Skip to content

Commit

Permalink
feat: Add wasm module for rollapp-wasm (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
trinitys7 authored Mar 12, 2024
1 parent 079f479 commit 9829d4a
Show file tree
Hide file tree
Showing 26 changed files with 826 additions and 376 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ install:
@echo "--> Ensure dependencies have not been modified"
@go mod verify
@echo "--> installing rollappd"
@go install $(BUILD_FLAGS) -v -mod=readonly ./cmd/rollappd
@go install $(BUILD_FLAGS) -v -mod=readonly ./rollappd


.PHONY: build
build: ## Compiles the rollapd binary
go build -o build/rollappd $(BUILD_FLAGS) ./cmd/rollappd
go build -o build/rollappd $(BUILD_FLAGS) ./rollappd


.PHONY: clean
Expand Down
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It uses Cosmos-SDK's [simapp](https://github.com/cosmos/cosmos-sdk/tree/main/sim
- minimal app setup
- wired IBC for [ICS 20 Fungible Token Transfers](https://github.com/cosmos/ibc/tree/main/spec/app/ics-020-fungible-token-transfer)
- Uses `dymint` for block sequencing and replacing `tendermint`
- Uses modules from `dymension-RDK` to sync with `dymint` and provide RollApp custom logic
- Uses modules from `dymension-RDK` to sync with `dymint` and provide RollApp custom logic

## Overview

Expand All @@ -36,7 +36,7 @@ make install
export the following variables:

```shell
export ROLLAPP_CHAIN_ID="demo-dymension-rollapp"
export ROLLAPP_CHAIN_ID="rollappwasm_1234-1"
export KEY_NAME_ROLLAPP="rol-user"
export DENOM="urax"
export MONIKER="$ROLLAPP_CHAIN_ID-sequencer"
Expand All @@ -48,6 +48,14 @@ And initialize the rollapp:
sh scripts/init.sh
```

### Download cw20-ics20 smartcontract

Download cw20-ics20 smartcontract with a specific version:

```shell
sh scripts/download_release.sh v1.0.0
```

### Run rollapp

```shell
Expand All @@ -74,7 +82,7 @@ SEQUENCER_ADDR=`dymd keys show sequencer --address --keyring-backend test --keyr
fund the sequencer account

```shell
dymd tx bank send local-user $SEQUENCER_ADDR 10000000000000000000000udym --keyring-backend test --broadcast-mode block
dymd tx bank send local-user $SEQUENCER_ADDR 10000000000000000000000adym --keyring-backend test --broadcast-mode block --fees 20000000000000adym -y
```

### Register rollapp on settlement
Expand All @@ -96,6 +104,7 @@ set:

```shell
settlement_layer = "dymension"
gas_prices = "0.025adym"
```

### Run rollapp locally
Expand All @@ -110,7 +119,7 @@ rollappd start

```shell
git clone https://github.com/dymensionxyz/go-relayer.git --branch v0.2.0-v2.3.1-relayer
cd relayer && make install
cd go-relayer && make install
```

### Establish IBC channel
Expand All @@ -129,6 +138,18 @@ After successful run, the new established channels will be shown
rly start hub-rollapp
```

### Deploy the installed contract

```shell
sh scripts/wasm/deploy_contract.sh
```

### Make the ibc transfer

```shell
sh scripts/wasm/ibc_transfer.sh
```

## Developers guide

TODO
15 changes: 13 additions & 2 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package app

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"

ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
type HandlerOptions struct {
ante.HandlerOptions

IBCKeeper *ibckeeper.Keeper
IBCKeeper *ibckeeper.Keeper
WasmConfig *wasmtypes.WasmConfig
TxCounterStoreKey storetypes.StoreKey
}

func GetAnteDecorators(options HandlerOptions) []sdk.AnteDecorator {
Expand All @@ -24,7 +30,8 @@ func GetAnteDecorators(options HandlerOptions) []sdk.AnteDecorator {

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first

wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit), // after setup context to enforce limits early
wasmkeeper.NewCountTXDecorator(options.TxCounterStoreKey),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),

ante.NewValidateBasicDecorator(),
Expand Down Expand Up @@ -62,5 +69,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

if options.WasmConfig == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
}

return sdk.ChainAnteDecorators(GetAnteDecorators(options)...), nil
}
95 changes: 85 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package app

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
abci "github.com/tendermint/tendermint/abci/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -90,7 +93,12 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper"
ibctestingtypes "github.com/cosmos/ibc-go/v6/testing/types"

rollappparams "github.com/dymensionxyz/rollapp/app/params"
wasm "github.com/CosmWasm/wasmd/x/wasm"
wasmclient "github.com/CosmWasm/wasmd/x/wasm/client"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

rollappparams "github.com/dymensionxyz/rollapp-wasm/app/params"

// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
Expand Down Expand Up @@ -120,13 +128,13 @@ var (
ibchost.StoreKey, upgradetypes.StoreKey,
epochstypes.StoreKey,
ibctransfertypes.StoreKey, capabilitytypes.StoreKey,
wasmtypes.StoreKey,
}
)

func getGovProposalHandlers() []govclient.ProposalHandler {
var govProposalHandlers []govclient.ProposalHandler

govProposalHandlers = append(govProposalHandlers,
govProposalHandlers := append(
wasmclient.ProposalHandlers,
paramsclient.ProposalHandler,
distrclient.ProposalHandler,
upgradeclient.LegacyProposalHandler,
Expand Down Expand Up @@ -161,6 +169,7 @@ var (
ibc.AppModuleBasic{},
ibctransfer.AppModuleBasic{},
vesting.AppModuleBasic{},
wasm.AppModuleBasic{},
)

// module account permissions
Expand All @@ -172,6 +181,7 @@ var (
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
wasmtypes.ModuleName: {authtypes.Burner},
}
)

Expand Down Expand Up @@ -220,10 +230,12 @@ type App struct {
ParamsKeeper paramskeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
TransferKeeper ibctransferkeeper.Keeper
WasmKeeper wasmkeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper

// mm is the module manager
mm *module.Manager
Expand All @@ -245,7 +257,9 @@ func NewRollapp(
homePath string,
invCheckPeriod uint,
encodingConfig rollappparams.EncodingConfig,
enabledProposals []wasm.ProposalType,
appOpts servertypes.AppOptions,
wasmOpts []wasmkeeper.Option,
baseAppOptions ...func(*baseapp.BaseApp),
) *App {

Expand Down Expand Up @@ -301,6 +315,7 @@ func NewRollapp(
// their scoped modules in `NewApp` with `ScopeToModule`
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibchost.ModuleName)
scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
scopedWasmKeeper := app.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName)
app.CapabilityKeeper.Seal()

// add keepers
Expand Down Expand Up @@ -410,9 +425,46 @@ func NewRollapp(
)
transferIBCModule := ibctransfer.NewIBCModule(app.TransferKeeper)

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

// The gov proposal types can be individually enabled
if len(enabledProposals) != 0 {
govRouter.AddRoute(wasm.RouterKey, wasm.NewWasmProposalHandler(app.WasmKeeper, enabledProposals))
}

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
availableCapabilities := strings.Join(AllCapabilities(), ",")
app.WasmKeeper = wasmkeeper.NewKeeper(
appCodec,
keys[wasmtypes.StoreKey],
app.GetSubspace(wasm.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
app.DistrKeeper,
app.IBCKeeper.ChannelKeeper, // may be replaced with middleware such as ics29 feerefunder
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
scopedWasmKeeper,
app.TransferKeeper,
app.MsgServiceRouter(),
app.GRPCQueryRouter(),
wasmDir,
wasmConfig,
availableCapabilities,
wasmOpts...,
)

wasmStack := wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ChannelKeeper)

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

/**** Module Options ****/
Expand All @@ -436,6 +488,7 @@ func NewRollapp(
sequencers.NewAppModule(appCodec, app.SequencersKeeper, app.AccountKeeper, app.BankKeeper),
epochs.NewAppModule(appCodec, app.EpochsKeeper),
params.NewAppModule(app.ParamsKeeper),
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
ibc.NewAppModule(app.IBCKeeper),
ibctransfer.NewAppModule(app.TransferKeeper),
upgrade.NewAppModule(app.UpgradeKeeper),
Expand Down Expand Up @@ -464,6 +517,7 @@ func NewRollapp(
genutiltypes.ModuleName,
epochstypes.ModuleName,
paramstypes.ModuleName,
wasm.ModuleName,
}
app.mm.SetOrderBeginBlockers(beginBlockersList...)

Expand All @@ -483,6 +537,7 @@ func NewRollapp(
upgradetypes.ModuleName,
ibchost.ModuleName,
ibctransfertypes.ModuleName,
wasm.ModuleName,
}
app.mm.SetOrderEndBlockers(endBlockersList...)

Expand All @@ -508,6 +563,7 @@ func NewRollapp(
epochstypes.ModuleName,
upgradetypes.ModuleName,
ibctransfertypes.ModuleName,
wasm.ModuleName,
}
app.mm.SetOrderInitGenesis(initGenesisList...)

Expand Down Expand Up @@ -542,7 +598,16 @@ func NewRollapp(
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.setAnteHandler(encodingConfig.TxConfig)
app.setAnteHandler(encodingConfig.TxConfig, wasmConfig, keys[wasmtypes.StoreKey])

if manager := app.SnapshotManager(); manager != nil {
err := manager.RegisterExtensions(
wasmkeeper.NewWasmSnapshotter(app.CommitMultiStore(), &app.WasmKeeper),
)
if err != nil {
panic(fmt.Errorf("failed to register snapshot extension: %s", err))
}
}
// 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.
Expand All @@ -562,15 +627,22 @@ func NewRollapp(
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}

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))
}
}

app.ScopedIBCKeeper = scopedIBCKeeper
app.ScopedTransferKeeper = scopedTransferKeeper

app.ScopedWasmKeeper = scopedWasmKeeper
return app
}

func (app *App) setAnteHandler(txConfig client.TxConfig) {
func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig, txCounterStoreKey *storetypes.KVStoreKey) {
anteHandler, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
Expand All @@ -579,7 +651,9 @@ func (app *App) setAnteHandler(txConfig client.TxConfig) {
SignModeHandler: txConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
IBCKeeper: app.IBCKeeper,
IBCKeeper: app.IBCKeeper,
WasmConfig: &wasmConfig,
TxCounterStoreKey: app.keys[wasmtypes.StoreKey],
},
)
if err != nil {
Expand Down Expand Up @@ -622,7 +696,7 @@ func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.Res

//Passing the dymint sequencers to the sequencer module from RequestInitChain
if len(req.Validators) == 0 {
panic("Dymint have no sequencers defined on InitChain")
panic(fmt.Sprint("Dymint have no sequencers defined on InitChain, req:", req))
}
app.SequencersKeeper.SetDymintSequencers(ctx, req.Validators)

Expand Down Expand Up @@ -818,5 +892,6 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)

paramsKeeper.Subspace(wasmtypes.ModuleName)
return paramsKeeper
}
2 changes: 1 addition & 1 deletion app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app
import (
"github.com/cosmos/cosmos-sdk/std"

"github.com/dymensionxyz/rollapp/app/params"
"github.com/dymensionxyz/rollapp-wasm/app/params"
)

// MakeEncodingConfig creates an EncodingConfig for testing
Expand Down
Loading

0 comments on commit 9829d4a

Please sign in to comment.