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

Add cosmwasm #1103

Merged
merged 11 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG RUNNER_IMAGE="alpine:3.16"
FROM golang:${GO_VERSION}-alpine as builder

WORKDIR /opt
RUN apk add --no-cache make git gcc musl-dev openssl-dev linux-headers
RUN apk add --no-cache make git gcc musl-dev openssl-dev linux-headers ca-certificates build-base

COPY go.mod .
COPY go.sum .
Expand All @@ -15,10 +15,13 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/go/pkg/mod \
go mod download

# Copy the remaining files
RUN WASMVM_VERSION=$(cat go.mod | grep github.com/CosmWasm/wasmvm | awk '{print $2}') \
&& wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$(uname -m).a \
-O /lib/libwasmvm_muslc.a

COPY . .

RUN LINK_STATICALLY=true make build
RUN BUILD_TAGS=muslc LINK_STATICALLY=true make build

# Add to a distroless container
FROM ${RUNNER_IMAGE}
Expand Down
19 changes: 17 additions & 2 deletions app/ante_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package app

import (
errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
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"
Expand All @@ -16,8 +19,11 @@ import (
type HandlerOptions struct {
ante.HandlerOptions

IBCKeeper *ibckeeper.Keeper
ConsumerKeeper ccvconsumerkeeper.Keeper
IBCKeeper *ibckeeper.Keeper
ConsumerKeeper ccvconsumerkeeper.Keeper
WasmConfig *wasmtypes.WasmConfig
WasmKeeper *wasmkeeper.Keeper
TXCounterStoreKey storetypes.StoreKey
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
Expand All @@ -30,6 +36,12 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.SignModeHandler == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}
if options.WasmConfig == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
}
if options.TXCounterStoreKey == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "tx counter key is required for ante builder")
}

var sigGasConsumer = options.SigGasConsumer
if sigGasConsumer == nil {
Expand All @@ -38,6 +50,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(),
wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit),
wasmkeeper.NewCountTXDecorator(options.TXCounterStoreKey),
wasmkeeper.NewGasRegisterDecorator(options.WasmKeeper.GetGasRegister()),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
// temporarily disabled so that chain can be tested locally without the provider chain running
consumerante.NewDisabledModulesDecorator("/cosmos.evidence", "/cosmos.slashing"),
Expand Down
221 changes: 126 additions & 95 deletions app/app.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions app/test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

sdkmath "cosmossdk.io/math"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
cometbftdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/abci/types"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -56,6 +57,7 @@ func InitStrideTestApp(initChain bool) *StrideApp {
5,
MakeEncodingConfig(),
simtestutil.EmptyAppOptions{},
[]wasmkeeper.Option{},
)
if initChain {
genesisState := GenesisStateWithValSet(app)
Expand Down
17 changes: 17 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"fmt"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
authz "github.com/cosmos/cosmos-sdk/x/authz"
Expand All @@ -23,6 +24,7 @@ import (
v16 "github.com/Stride-Labs/stride/v18/app/upgrades/v16"
v17 "github.com/Stride-Labs/stride/v18/app/upgrades/v17"
v18 "github.com/Stride-Labs/stride/v18/app/upgrades/v18"
v19 "github.com/Stride-Labs/stride/v18/app/upgrades/v19"
v2 "github.com/Stride-Labs/stride/v18/app/upgrades/v2"
v3 "github.com/Stride-Labs/stride/v18/app/upgrades/v3"
v4 "github.com/Stride-Labs/stride/v18/app/upgrades/v4"
Expand Down Expand Up @@ -247,6 +249,17 @@ func (app *StrideApp) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
),
)

// v19 upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v19.UpgradeName,
v19.CreateUpgradeHandler(
app.mm,
app.configurator,
app.appCodec,
app.WasmKeeper,
),
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Errorf("Failed to read upgrade info from disk: %w", err))
Expand Down Expand Up @@ -296,6 +309,10 @@ func (app *StrideApp) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{staketiatypes.ModuleName},
}
case "v19":
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{wasmtypes.ModuleName},
}
}

if storeUpgrades != nil {
Expand Down
47 changes: 47 additions & 0 deletions app/upgrades/v19/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v19

import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

const (
UpgradeName = "v19"
)

// CreateUpgradeHandler creates an SDK upgrade handler for v19
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
cdc codec.Codec,
wasmKeeper wasmkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting upgrade v19...")

// Run module migrations first to add wasm to the store
ctx.Logger().Info("Running module migrations...")
newVm, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return newVm, err
}

// Update wasm params so that contracts can only be uploaded through governance
// QUESTION: Should we set an admin address for instantiation?
wasmParams := wasmKeeper.GetParams(ctx)
wasmParams.CodeUploadAccess = wasmtypes.AccessConfig{
Permission: wasmtypes.AccessTypeNobody,
ethan-stride marked this conversation as resolved.
Show resolved Hide resolved
Addresses: []string{},
}
wasmParams.InstantiateDefaultPermission = wasmtypes.AccessTypeNobody
if err := wasmKeeper.SetParams(ctx, wasmParams); err != nil {
return newVm, err
}

return newVm, nil
}
}
5 changes: 3 additions & 2 deletions cmd/strided/config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package types

import (
errorsmod "cosmossdk.io/errors"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"

errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

Expand Down Expand Up @@ -45,6 +45,7 @@ func SetBech32Prefixes(config *sdk.Config) {
config.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(Bech32PrefixValAddr, Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(Bech32PrefixConsAddr, Bech32PrefixConsPub)
config.SetAddressVerifier(wasmtypes.VerifyAddressLen())
}

// SetBip44CoinType sets the global coin type to be used in hierarchical deterministic wallets.
Expand Down
52 changes: 23 additions & 29 deletions cmd/strided/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ import (
"path/filepath"
"strings"

"github.com/cosmos/cosmos-sdk/snapshots"
"gopkg.in/yaml.v2"

"github.com/Stride-Labs/stride/v18/utils"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
cometbftdb "github.com/cometbft/cometbft-db"
tmcfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/libs/cli"
tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cometbft/cometbft/libs/log"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

tmcfg "github.com/cometbft/cometbft/config"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -34,6 +28,7 @@ import (
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/snapshots"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -46,8 +41,14 @@ import (
"github.com/cosmos/cosmos-sdk/x/genutil"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"

"github.com/Stride-Labs/stride/v18/app"
"github.com/Stride-Labs/stride/v18/utils"
)

var ChainID string
Expand Down Expand Up @@ -120,21 +121,10 @@ func initTendermintConfig() *tmcfg.Config {
// initAppConfig helps to override default appConfig template and configs.
// return "", nil if no custom configuration is required for the application.
func initAppConfig() (string, interface{}) {
// The following code snippet is just for reference.

// WASMConfig defines configuration for the wasm module.
type WASMConfig struct {
// This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries
QueryGasLimit uint64 `mapstructure:"query_gas_limit"`

// Address defines the gRPC-web server to listen on
LruSize uint64 `mapstructure:"lru_size"`
}

type CustomAppConfig struct {
serverconfig.Config

WASM WASMConfig `mapstructure:"wasm"`
Wasm wasmtypes.WasmConfig `mapstructure:"wasm"`
}

// Optionally allow the chain developer to overwrite the SDK's default
Expand All @@ -161,12 +151,10 @@ func initAppConfig() (string, interface{}) {
// This ensures that upgraded nodes will use iavl fast node.
//srvCfg.IAVLDisableFastNode = false

// TODO [CW]: Confirm these defaults are fine
customAppConfig := CustomAppConfig{
Config: *srvCfg,
WASM: WASMConfig{
LruSize: 1,
QueryGasLimit: 300000,
},
Wasm: wasmtypes.DefaultWasmConfig(),
}

customAppTemplate := serverconfig.DefaultConfigTemplate + `
Expand Down Expand Up @@ -196,7 +184,6 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig) {
tmcli.NewCompletionCmd(rootCmd, true),
debug.Cmd(),
config.Cmd(),
// this line is used by starport scaffolding # stargate/root/commands
)

a := appCreator{encodingConfig}
Expand All @@ -214,7 +201,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig) {

func addModuleInitFlags(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
// this line is used by starport scaffolding # stargate/root/initFlags
wasm.AddModuleInitFlags(startCmd)
}

func queryCommand() *cobra.Command {
Expand Down Expand Up @@ -348,12 +335,16 @@ func (a appCreator) newApp(logger log.Logger, db cometbftdb.DB, traceStore io.Wr
if err != nil {
panic(err)
}

snapshotOptions := snapshottypes.NewSnapshotOptions(
cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval)),
cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent)),
)

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

homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
chainID := cast.ToString(appOpts.Get(flags.FlagChainID))
if chainID == "" {
Expand All @@ -373,6 +364,7 @@ func (a appCreator) newApp(logger log.Logger, db cometbftdb.DB, traceStore io.Wr
a.encCfg,
// this line is used by starport scaffolding # stargate/root/appArgument
appOpts,
wasmOpts,
baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))),
baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))),
Expand Down Expand Up @@ -411,6 +403,7 @@ func (a appCreator) appExport(
uint(1),
a.encCfg,
appOpts,
[]wasmkeeper.Option{},
)

if err := anApp.LoadHeight(height); err != nil {
Expand All @@ -427,6 +420,7 @@ func (a appCreator) appExport(
uint(1),
a.encCfg,
appOpts,
[]wasmkeeper.Option{},
)
}

Expand Down
Loading
Loading