From e14c3335bab8858ee239130c2ddc20ec03a19b7e Mon Sep 17 00:00:00 2001 From: GNaD Date: Mon, 3 Oct 2022 10:26:54 +0700 Subject: [PATCH 1/3] add check version wasm --- x/wasm/module.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/x/wasm/module.go b/x/wasm/module.go index c7ea6339c0..90bc0ee12a 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -3,7 +3,10 @@ package wasm import ( "context" "encoding/json" + "fmt" "math/rand" + "runtime/debug" + "strings" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -24,6 +27,7 @@ import ( "github.com/CosmWasm/wasmd/x/wasm/keeper" "github.com/CosmWasm/wasmd/x/wasm/simulation" "github.com/CosmWasm/wasmd/x/wasm/types" + wasmvm "github.com/CosmWasm/wasmvm" ) var ( @@ -217,6 +221,8 @@ 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 @@ -250,3 +256,48 @@ func ReadWasmConfig(opts servertypes.AppOptions) (types.WasmConfig, error) { } return cfg, nil } + +func getExpectedLibwasmVersion() string { + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + panic("can't read build info") + } + for _, d := range buildInfo.Deps { + if d.Path != "github.com/CosmWasm/wasmvm" { + continue + } + if d.Replace != nil { + return fmt.Sprintf(d.Replace.Version) + + } + return fmt.Sprintf(d.Version) + } + return "" +} + +func checkLibwasmVersion(cmd *cobra.Command, args []string) error { + wasmVersion, err := wasmvm.LibwasmvmVersion() + if err != nil { + return fmt.Errorf("unable to retrieve libwasmversion %w", err) + } + wasmExpectedVersion := getExpectedLibwasmVersion() + if !strings.Contains(wasmExpectedVersion, wasmVersion) { + return fmt.Errorf("libwasmversion mismatch. got: %s; expected: %s", wasmVersion, wasmExpectedVersion) + } + 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 691d8815badb148d0c152924e7a17488ce54f54c Mon Sep 17 00:00:00 2001 From: GNaD Date: Mon, 3 Oct 2022 12:00:12 +0700 Subject: [PATCH 2/3] add check wasmExpectedVersion --- x/wasm/module.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index 90bc0ee12a..68c23da30b 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -268,7 +268,6 @@ func getExpectedLibwasmVersion() string { } if d.Replace != nil { return fmt.Sprintf(d.Replace.Version) - } return fmt.Sprintf(d.Version) } @@ -281,6 +280,9 @@ func checkLibwasmVersion(cmd *cobra.Command, args []string) error { 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) } From 0f727c04a7c1fdc6bf4c4873abbad5d1ee310dc6 Mon Sep 17 00:00:00 2001 From: GNaD Date: Fri, 7 Oct 2022 10:40:16 +0700 Subject: [PATCH 3/3] update on nit --- x/wasm/module.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/wasm/module.go b/x/wasm/module.go index 68c23da30b..7973c8db97 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -267,9 +267,9 @@ func getExpectedLibwasmVersion() string { continue } if d.Replace != nil { - return fmt.Sprintf(d.Replace.Version) + return d.Replace.Version } - return fmt.Sprintf(d.Version) + return d.Version } return "" }