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

chore: backport wasm params change, wasm dir, wasm state sync fix, and ibc query #2119

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@
"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 @@
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 @@
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))

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

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

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

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

Check warning on line 500 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L496-L500

Added lines #L496 - L500 were not covered by tests
}
}
}
}

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

Check warning on line 512 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L510-L512

Added lines #L510 - L512 were not covered by tests
}

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

Check warning on line 521 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L517-L521

Added lines #L517 - L521 were not covered by tests
}

return updatedVM, err

Check warning on line 524 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L524

Added line #L524 was not covered by tests
},
)

Expand Down Expand Up @@ -1035,7 +1105,7 @@
}

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
Loading