From 2c8be9a7e9e31fee5efaa80b9362e3185d3f8eef Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 14 Apr 2023 17:24:31 +0200 Subject: [PATCH 1/7] Don't run checkLibwasmVersion automatically on start --- x/wasm/module.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index cc83cf831f..e00985263d 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -227,8 +227,6 @@ func AddModuleInitFlags(startCmd *cobra.Command) { startCmd.Flags().Uint32(flagWasmMemoryCacheSize, defaults.MemoryCacheSize, "Sets the size in MiB (NOT bytes) of an in-memory cache for Wasm modules. Set to 0 to disable.") startCmd.Flags().Uint64(flagWasmQueryGasLimit, defaults.SmartQueryGasLimit, "Set the max gas that can be spent on executing a query with a Wasm contract") startCmd.Flags().String(flagWasmSimulationGasLimit, "", "Set the max gas that can be spent when executing a simulation TX") - - startCmd.PreRunE = chainPreRuns(checkLibwasmVersion, startCmd.PreRunE) } // ReadWasmConfig reads the wasm specifig configuration @@ -280,7 +278,7 @@ func getExpectedLibwasmVersion() string { return "" } -func checkLibwasmVersion(cmd *cobra.Command, args []string) error { +func CheckLibwasmVersion(cmd *cobra.Command, args []string) error { wasmVersion, err := wasmvm.LibwasmvmVersion() if err != nil { return fmt.Errorf("unable to retrieve libwasmversion %w", err) From 97a08e9c4f37aca7c4f8cad5325fe41b21eefef3 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 14 Apr 2023 21:49:49 +0200 Subject: [PATCH 2/7] Remove unused preRunFn, chainPreRuns --- x/wasm/module.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index e00985263d..a569c8652a 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -292,18 +292,3 @@ func CheckLibwasmVersion(cmd *cobra.Command, args []string) error { } return nil } - -type preRunFn func(cmd *cobra.Command, args []string) error - -func chainPreRuns(pfns ...preRunFn) preRunFn { - return func(cmd *cobra.Command, args []string) error { - for _, pfn := range pfns { - if pfn != nil { - if err := pfn(cmd, args); err != nil { - return err - } - } - } - return nil - } -} From 27d2e3ce67c6117f751252a8dafe38ed1afabc79 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 17 Apr 2023 11:59:30 +0200 Subject: [PATCH 3/7] Add documentation to CheckLibwasmVersion --- x/wasm/module.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/x/wasm/module.go b/x/wasm/module.go index a569c8652a..2fe0741c2e 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -278,6 +278,17 @@ func getExpectedLibwasmVersion() string { return "" } +// CheckLibwasmVersion ensures that the libwasmvm version loaded at runtime matches the version +// of the github.com/CosmWasm/wasmvm dependency in go.mod. This us useful when dealing with +// shared libraries that are copied or moved from their default location, e.g. when building the node +// on one machine and deploying it to other machines. +// +// Usually the libwasmvm version (the Rust project) and wasmvm version (the Go project) match. However, +// there are situations in which this is not the case. This can be during development or if one of the +// two is patched. In such cases it is advised to not execute the check. +// +// An alternative method to obtain the libwasmvm version loaded at runtime is executing +// `wasmd query wasm libwasmvm-version`. func CheckLibwasmVersion(cmd *cobra.Command, args []string) error { wasmVersion, err := wasmvm.LibwasmvmVersion() if err != nil { From 161cf5208728c1d33f11564aaff25f90a4e5e843 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 20 Apr 2023 09:47:49 +0200 Subject: [PATCH 4/7] Add skip wasmvm version flag --- x/wasm/module.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index 2fe0741c2e..aafab43ef6 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -37,9 +37,10 @@ var ( // Module init related flags const ( - flagWasmMemoryCacheSize = "wasm.memory_cache_size" - flagWasmQueryGasLimit = "wasm.query_gas_limit" - flagWasmSimulationGasLimit = "wasm.simulation_gas_limit" + flagWasmMemoryCacheSize = "wasm.memory_cache_size" + flagWasmQueryGasLimit = "wasm.query_gas_limit" + flagWasmSimulationGasLimit = "wasm.simulation_gas_limit" + flagWasmSkipWasmVMVersionCheck = "wasm.skip_wasmvm_version_check" ) // AppModuleBasic defines the basic application module used by the wasm module. @@ -227,6 +228,9 @@ func AddModuleInitFlags(startCmd *cobra.Command) { startCmd.Flags().Uint32(flagWasmMemoryCacheSize, defaults.MemoryCacheSize, "Sets the size in MiB (NOT bytes) of an in-memory cache for Wasm modules. Set to 0 to disable.") startCmd.Flags().Uint64(flagWasmQueryGasLimit, defaults.SmartQueryGasLimit, "Set the max gas that can be spent on executing a query with a Wasm contract") startCmd.Flags().String(flagWasmSimulationGasLimit, "", "Set the max gas that can be spent when executing a simulation TX") + startCmd.Flags().Bool(flagWasmSkipWasmVMVersionCheck, false, "Skip check that ensures that libwasmvm version (the Rust project) and wasmvm version (the Go project) match") + + startCmd.PreRunE = chainPreRuns(CheckLibwasmVersion, startCmd.PreRunE) } // ReadWasmConfig reads the wasm specifig configuration @@ -290,6 +294,14 @@ func getExpectedLibwasmVersion() string { // An alternative method to obtain the libwasmvm version loaded at runtime is executing // `wasmd query wasm libwasmvm-version`. func CheckLibwasmVersion(cmd *cobra.Command, args []string) error { + skip, err := cmd.Flags().GetBool(flagWasmSkipWasmVMVersionCheck) + if err != nil { + return fmt.Errorf("unable to read skip flag value: %w", err) + } + if skip { + cmd.Println("libwasmvm version check skipped") + return nil + } wasmVersion, err := wasmvm.LibwasmvmVersion() if err != nil { return fmt.Errorf("unable to retrieve libwasmversion %w", err) @@ -303,3 +315,18 @@ func CheckLibwasmVersion(cmd *cobra.Command, args []string) error { } return nil } + +type preRunFn func(cmd *cobra.Command, args []string) error + +func chainPreRuns(pfns ...preRunFn) preRunFn { + return func(cmd *cobra.Command, args []string) error { + for _, pfn := range pfns { + if pfn != nil { + if err := pfn(cmd, args); err != nil { + return err + } + } + } + return nil + } +} From d54d1969d1a3082fb03e601c71e89eed957660f5 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 20 Apr 2023 13:03:40 +0200 Subject: [PATCH 5/7] Linter false positive --- x/wasm/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index aafab43ef6..c4e20353e6 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -40,7 +40,7 @@ const ( flagWasmMemoryCacheSize = "wasm.memory_cache_size" flagWasmQueryGasLimit = "wasm.query_gas_limit" flagWasmSimulationGasLimit = "wasm.simulation_gas_limit" - flagWasmSkipWasmVMVersionCheck = "wasm.skip_wasmvm_version_check" + flagWasmSkipWasmVMVersionCheck = "wasm.skip_wasmvm_version_check" //nolint:gosec ) // AppModuleBasic defines the basic application module used by the wasm module. From 61008dd32add8b5c5b25003707c048b7886b128b Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 20 Apr 2023 15:57:06 +0200 Subject: [PATCH 6/7] Review feedback --- x/wasm/module.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index c4e20353e6..d9679c1f74 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -230,7 +230,18 @@ func AddModuleInitFlags(startCmd *cobra.Command) { startCmd.Flags().String(flagWasmSimulationGasLimit, "", "Set the max gas that can be spent when executing a simulation TX") startCmd.Flags().Bool(flagWasmSkipWasmVMVersionCheck, false, "Skip check that ensures that libwasmvm version (the Rust project) and wasmvm version (the Go project) match") - startCmd.PreRunE = chainPreRuns(CheckLibwasmVersion, startCmd.PreRunE) + preCheck := func(cmd *cobra.Command, _ []string) error { + skip, err := cmd.Flags().GetBool(flagWasmSkipWasmVMVersionCheck) + if err != nil { + return fmt.Errorf("unable to read skip flag value: %w", err) + } + if skip { + cmd.Println("libwasmvm version check skipped") + return nil + } + return CheckLibwasmVersion() + } + startCmd.PreRunE = chainPreRuns(preCheck, startCmd.PreRunE) } // ReadWasmConfig reads the wasm specifig configuration @@ -293,15 +304,7 @@ func getExpectedLibwasmVersion() string { // // An alternative method to obtain the libwasmvm version loaded at runtime is executing // `wasmd query wasm libwasmvm-version`. -func CheckLibwasmVersion(cmd *cobra.Command, args []string) error { - skip, err := cmd.Flags().GetBool(flagWasmSkipWasmVMVersionCheck) - if err != nil { - return fmt.Errorf("unable to read skip flag value: %w", err) - } - if skip { - cmd.Println("libwasmvm version check skipped") - return nil - } +func CheckLibwasmVersion() error { wasmVersion, err := wasmvm.LibwasmvmVersion() if err != nil { return fmt.Errorf("unable to retrieve libwasmversion %w", err) From 78a3ce6754db94bf6bedec24920aaf481afcd5ec Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 20 Apr 2023 16:02:23 +0200 Subject: [PATCH 7/7] Pass expected version --- x/wasm/module.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index d9679c1f74..98f088b95f 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -239,7 +239,7 @@ func AddModuleInitFlags(startCmd *cobra.Command) { cmd.Println("libwasmvm version check skipped") return nil } - return CheckLibwasmVersion() + return CheckLibwasmVersion(getExpectedLibwasmVersion()) } startCmd.PreRunE = chainPreRuns(preCheck, startCmd.PreRunE) } @@ -304,15 +304,14 @@ func getExpectedLibwasmVersion() string { // // An alternative method to obtain the libwasmvm version loaded at runtime is executing // `wasmd query wasm libwasmvm-version`. -func CheckLibwasmVersion() error { +func CheckLibwasmVersion(wasmExpectedVersion string) error { + if wasmExpectedVersion == "" { + return fmt.Errorf("wasmvm module not exist") + } wasmVersion, err := wasmvm.LibwasmvmVersion() if err != nil { return fmt.Errorf("unable to retrieve libwasmversion %w", err) } - wasmExpectedVersion := getExpectedLibwasmVersion() - if wasmExpectedVersion == "" { - return fmt.Errorf("wasmvm module not exist") - } if !strings.Contains(wasmExpectedVersion, wasmVersion) { return fmt.Errorf("libwasmversion mismatch. got: %s; expected: %s", wasmVersion, wasmExpectedVersion) }