Skip to content

Commit

Permalink
chore: backport wasm params change, wasm dir, wasm state sync fix, an…
Browse files Browse the repository at this point in the history
…d ibc query (#2119)

* fix!: override wasm default param after initialization (#2105)

* feat(cli): add `--wasm-dir` flag to start command (#2111)

* fix(app): register wasm snapshot extension for state-sync (#2114)

* fix(app): register wasm snapshot extension for state-sync

* feature gate extension registration

* move to a separate function

* rename method

* fix(wasm): migrate wasm cache dir to the new path (#2115)

* add --wasm-dir flag to start command

* use /Users/joaosousa env var

* docs

* programatically determine the default value for the wasm dir

* addendum

* addendum

* fix

* edit flag description

Co-authored-by: Milap Sheth <[email protected]>

* fix

* simplification

* fix(wasm): migrate wasm cache dir to the new path

* convert to abs path

* assume path is already expanded and move check earlier

* dep update

* go mod tidy

* use wasm path

---------

Co-authored-by: João Sousa <[email protected]>
Co-authored-by: jcs47 <[email protected]>

* feat(axelarnet): add queries to retrieve cosmos chains, ibc path (#2116)

* feat(axelarnet): add queries to retrieve cosmos chains, ibc path

* register cli cmds and add tests

* docs

* dep update

* remove chains query

* lint

* revert config change

---------

Co-authored-by: haiyizxx <[email protected]>
Co-authored-by: jcs47 <[email protected]>
Co-authored-by: João Sousa <[email protected]>
  • Loading branch information
4 people authored Mar 1, 2024
1 parent d11b7f2 commit e4311c7
Show file tree
Hide file tree
Showing 29 changed files with 2,388 additions and 121 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ proto-format:
@echo "Formatting Protobuf files"
@$(DOCKER) run --rm -v $(CURDIR):/workspace \
--workdir /workspace tendermintdev/docker-build-proto \
find ./ -not -path "./third_party/*" -name "*.proto" -exec clang-format -i {} \;
$( find ./ -not -path "./third_party/*" -name "*.proto" -exec clang-format -i {} \; )

proto-lint:
@echo "Linting Protobuf files"
Expand Down
80 changes: 75 additions & 5 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 @@ -202,6 +203,7 @@ func NewAxelarApp(
loadLatest bool,
skipUpgradeHeights map[int64]bool,
homePath string,
wasmDir string,
invCheckPeriod uint,
encodingConfig axelarParams.EncodingConfig,
appOpts servertypes.AppOptions,
Expand Down Expand Up @@ -255,7 +257,22 @@ func NewAxelarApp(
SetKeeper(keepers, initAxelarIBCKeeper(keepers))

if IsWasmEnabled() {
SetKeeper(keepers, initWasmKeeper(encodingConfig, keys, keepers, bApp, appOpts, wasmOpts, homePath))
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))
}

// 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, wasmPath))

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 @@ -324,7 +341,7 @@ func NewAxelarApp(
upgradeKeeper: *getKeeper[upgradekeeper.Keeper](keepers),
}

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

// initialize stores
app.MountKVStores(keys)
Expand All @@ -338,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 @@ -447,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)
}

return nil
}

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 @@ -1035,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
1 change: 1 addition & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestNewAxelarApp(t *testing.T) {
true,
nil,
"",
"",
0,
app.MakeEncodingConfig(),
simapp.EmptyAppOptions{},
Expand Down
4 changes: 1 addition & 3 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

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

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

func initWasmKeeper(encodingConfig axelarParams.EncodingConfig, keys map[string]*sdk.KVStoreKey, keepers *KeeperCache, bApp *bam.BaseApp, appOpts types.AppOptions, wasmOpts []wasm.Option, homePath string) *wasm.Keeper {
wasmDir := filepath.Join(homePath, "wasm")
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)

// The last arguments can contain custom message handlers, and custom query handlers,
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
Loading

0 comments on commit e4311c7

Please sign in to comment.