Skip to content

Commit

Permalink
Merge branch 'main' into fix/wasm-dir-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
milapsheth committed Feb 28, 2024
2 parents 4be0354 + 6f90d60 commit 4edfd89
Show file tree
Hide file tree
Showing 18 changed files with 1,529 additions and 1,374 deletions.
88 changes: 74 additions & 14 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import (
"github.com/axelarnetwork/axelar-core/x/vote"
voteKeeper "github.com/axelarnetwork/axelar-core/x/vote/keeper"
voteTypes "github.com/axelarnetwork/axelar-core/x/vote/types"
"github.com/axelarnetwork/utils/funcs"

// Override with generated statik docs
_ "github.com/axelarnetwork/axelar-core/client/docs/statik"
Expand Down Expand Up @@ -214,15 +215,6 @@ func NewAxelarApp(
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)

if wasmDir != "" {
wasmPath, err := filepath.Abs(wasmDir)
if err != nil {
panic(fmt.Sprintf("failed to resolve absolute path for new wasm dir %s: %v", wasmDir, err))
}

wasmDir = wasmPath
}

keepers := NewKeeperCache()
SetKeeper(keepers, InitParamsKeeper(encodingConfig, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey]))

Expand Down Expand Up @@ -265,7 +257,22 @@ func NewAxelarApp(
SetKeeper(keepers, initAxelarIBCKeeper(keepers))

if IsWasmEnabled() {
SetKeeper(keepers, initWasmKeeper(encodingConfig, keys, keepers, bApp, appOpts, wasmOpts, homePath, wasmDir))
if wasmDir == "" {
dbDir := cast.ToString(appOpts.Get("db_dir"))
wasmDir = filepath.Join(homePath, dbDir, "wasm")
}

wasmPath, err := filepath.Abs(wasmDir)
if err != nil {
panic(fmt.Sprintf("failed to resolve absolute path for new wasm dir %s: %v", wasmDir, err))

Check warning on line 267 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L267

Added line #L267 was not covered by tests
}

// Migrate wasm dir from old path to new path
// TODO: Remove this once nodes have migrated
oldWasmDir := filepath.Join(homePath, "wasm")
funcs.MustNoErr(migrateWasmDir(oldWasmDir, wasmDir))

SetKeeper(keepers, initWasmKeeper(encodingConfig, keys, keepers, bApp, appOpts, wasmOpts, wasmPath))
SetKeeper(keepers, initWasmContractKeeper(keepers))

// set the contract keeper for the Ics20WasmHooks
Expand Down Expand Up @@ -334,7 +341,7 @@ func NewAxelarApp(
upgradeKeeper: *getKeeper[upgradekeeper.Keeper](keepers),
}

app.setUpgradeBehaviour(configurator)
app.setUpgradeBehaviour(configurator, keepers)

// initialize stores
app.MountKVStores(keys)
Expand All @@ -348,6 +355,10 @@ func NewAxelarApp(

app.SetAnteHandler(initAnteHandlers(encodingConfig, keys, keepers, appOpts))

// Register wasm snapshot extension for state-sync compatibility
// MUST be done before loading the version
app.registerWasmSnapshotExtension(keepers)

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
Expand Down Expand Up @@ -457,11 +468,60 @@ func initMessageRouter(keepers *KeeperCache) nexusTypes.MessageRouter {
return messageRouter
}

func (app *AxelarApp) setUpgradeBehaviour(configurator module.Configurator) {
func migrateWasmDir(oldWasmDir, newWasmDir string) error {
// If the new wasm dir exists, there's nothing to do
if _, err := os.Stat(newWasmDir); err == nil {
return nil
}

// If the old wasm dir doesn't exist, there's nothing to do
if _, err := os.Stat(oldWasmDir); err != nil && os.IsNotExist(err) {
return nil
}

// Move the wasm dir from old path to new path
if err := os.Rename(oldWasmDir, newWasmDir); err != nil {
return fmt.Errorf("failed to move wasm directory from %s to %s: %v", oldWasmDir, newWasmDir, err)

Check warning on line 484 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L483-L484

Added lines #L483 - L484 were not covered by tests
}

return nil

Check warning on line 487 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L487

Added line #L487 was not covered by tests
}

func (app *AxelarApp) registerWasmSnapshotExtension(keepers *KeeperCache) {
// Register wasm snapshot extension to enable state-sync compatibility for wasm.
// MUST be done before loading the version
// Requires the snapshot store to be created and registered as a BaseAppOption
if IsWasmEnabled() {
if manager := app.SnapshotManager(); manager != nil {
err := manager.RegisterExtensions(
wasmkeeper.NewWasmSnapshotter(app.CommitMultiStore(), getKeeper[wasm.Keeper](keepers)),
)
if err != nil {
panic(fmt.Errorf("failed to register snapshot extension: %s", err))
}
}
}
}

func (app *AxelarApp) setUpgradeBehaviour(configurator module.Configurator, keepers *KeeperCache) {
app.upgradeKeeper.SetUpgradeHandler(
upgradeName(app.Version()),
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, configurator, fromVM)
updatedVM, err := app.mm.RunMigrations(ctx, configurator, fromVM)
if err != nil {
return updatedVM, err
}

// TODO: remove after v35 upgrade
// Override wasm module default params
if upgradeName(app.Version()) == "v0.35" && IsWasmEnabled() {
getKeeper[wasm.Keeper](keepers).SetParams(ctx, wasmtypes.Params{
CodeUploadAccess: wasmtypes.AllowNobody,
InstantiateDefaultPermission: wasmtypes.AccessTypeNobody,
})
}

return updatedVM, err
},
)

Expand Down Expand Up @@ -1045,7 +1105,7 @@ func GetModuleBasics() module.BasicManager {
}

if IsWasmEnabled() {
managers = append(managers, NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{}, authtypes.NewModuleAddress(govtypes.ModuleName)))
managers = append(managers, NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{}))
}

if IsIBCWasmHooksEnabled() {
Expand Down
35 changes: 1 addition & 34 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package app

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

Expand Down Expand Up @@ -49,7 +47,6 @@ import (
ibcclienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
ibchost "github.com/cosmos/ibc-go/v4/modules/core/24-host"
ibckeeper "github.com/cosmos/ibc-go/v4/modules/core/keeper"
"github.com/spf13/cast"
"golang.org/x/mod/semver"

axelarParams "github.com/axelarnetwork/axelar-core/app/params"
Expand All @@ -73,7 +70,6 @@ import (
tssTypes "github.com/axelarnetwork/axelar-core/x/tss/types"
voteKeeper "github.com/axelarnetwork/axelar-core/x/vote/keeper"
voteTypes "github.com/axelarnetwork/axelar-core/x/vote/types"
"github.com/axelarnetwork/utils/funcs"
"github.com/axelarnetwork/utils/maps"
)

Expand Down Expand Up @@ -165,38 +161,9 @@ func InitStakingKeeper(appCodec codec.Codec, keys map[string]*sdk.KVStoreKey, ke
return &stakingK
}

func migrateWasmDir(oldWasmDir, newWasmDir string) error {
// If the new wasm dir exists, there's nothing to do
if _, err := os.Stat(newWasmDir); err == nil {
return nil
}

// If the old wasm dir doesn't exist, there's nothing to do
if _, err := os.Stat(oldWasmDir); err != nil && os.IsNotExist(err) {
return nil
}

// Move the wasm dir from old path to new path
if err := os.Rename(oldWasmDir, newWasmDir); err != nil {
return fmt.Errorf("failed to move wasm directory from %s to %s: %v", oldWasmDir, newWasmDir, err)
}

return nil
}

func initWasmKeeper(encodingConfig axelarParams.EncodingConfig, keys map[string]*sdk.KVStoreKey, keepers *KeeperCache, bApp *bam.BaseApp, appOpts types.AppOptions, wasmOpts []wasm.Option, homePath, wasmDir string) *wasm.Keeper {
func initWasmKeeper(encodingConfig axelarParams.EncodingConfig, keys map[string]*sdk.KVStoreKey, keepers *KeeperCache, bApp *bam.BaseApp, appOpts types.AppOptions, wasmOpts []wasm.Option, wasmDir string) *wasm.Keeper {
wasmConfig := mustReadWasmConfig(appOpts)

if wasmDir == "" {
dbDir := cast.ToString(appOpts.Get("db_dir"))
wasmDir = filepath.Join(homePath, dbDir, "wasm")
}

// Migrate wasm dir from old path to new path
// TODO: Remove this once nodes have migrated
oldWasmDir := filepath.Join(homePath, "wasm")
funcs.MustNoErr(migrateWasmDir(oldWasmDir, wasmDir))

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
wasmOpts = append(wasmOpts, wasmkeeper.WithMessageHandlerDecorator(
Expand Down
8 changes: 3 additions & 5 deletions app/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ func isIBCSendPacketMsg(msg wasmvmtypes.CosmosMsg) bool {

type WasmAppModuleBasicOverride struct {
wasm.AppModuleBasic
uploader sdk.AccAddress
}

func NewWasmAppModuleBasicOverride(wasmModule wasm.AppModuleBasic, uploader sdk.AccAddress) WasmAppModuleBasicOverride {
func NewWasmAppModuleBasicOverride(wasmModule wasm.AppModuleBasic) WasmAppModuleBasicOverride {
return WasmAppModuleBasicOverride{
AppModuleBasic: wasmModule,
uploader: uploader,
}
}

Expand All @@ -134,8 +132,8 @@ func NewWasmAppModuleBasicOverride(wasmModule wasm.AppModuleBasic, uploader sdk.
func (m WasmAppModuleBasicOverride) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(&wasm.GenesisState{
Params: wasmtypes.Params{
CodeUploadAccess: wasmtypes.AccessTypeAnyOfAddresses.With(m.uploader),
InstantiateDefaultPermission: wasmtypes.AccessTypeAnyOfAddresses,
CodeUploadAccess: wasmtypes.AllowNobody,
InstantiateDefaultPermission: wasmtypes.AccessTypeNobody,
},
})
}
9 changes: 3 additions & 6 deletions app/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -242,8 +241,7 @@ func TestMsgTypeBlacklistMessenger_DispatchMsg(t *testing.T) {
}

func TestNewWasmAppModuleBasicOverride(t *testing.T) {
uploader := authtypes.NewModuleAddress("allowed to upload")
wasmModule := app.NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{}, uploader)
wasmModule := app.NewWasmAppModuleBasicOverride(wasm.AppModuleBasic{})
cdc := app.MakeEncodingConfig().Codec

genesis := wasmModule.DefaultGenesis(cdc)
Expand All @@ -252,9 +250,8 @@ func TestNewWasmAppModuleBasicOverride(t *testing.T) {
var state wasm.GenesisState
assert.NoError(t, cdc.UnmarshalJSON(genesis, &state))

assert.Equal(t, state.Params.InstantiateDefaultPermission, wasmtypes.AccessTypeAnyOfAddresses)
assert.True(t, state.Params.CodeUploadAccess.Allowed(uploader))
assert.Len(t, state.Params.CodeUploadAccess.AllAuthorizedAddresses(), 1)
assert.Equal(t, state.Params.InstantiateDefaultPermission, wasmtypes.AccessTypeNobody)
assert.True(t, state.Params.CodeUploadAccess.Equals(wasmtypes.AllowNobody))
}

func TestICSMiddleWare(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/axelard/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {

starterFlags := func(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
startCmd.Flags().String(wasmDirFlag, "", "path to the wasm directory, default option stores it under the home/db_dir")
startCmd.Flags().String(wasmDirFlag, "", "path to the wasm directory, by default set to 'wasm' directory inside the '--db_dir' directory")
}

server.AddCommands(rootCmd, app.DefaultNodeHome, newApp, export(encodingConfig), starterFlags)
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/axelard_start.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4edfd89

Please sign in to comment.