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

Add check version wasm #1029

Merged
merged 4 commits into from
Oct 7, 2022
Merged
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
53 changes: 53 additions & 0 deletions x/wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice chaining

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!!!

}

// ReadWasmConfig reads the wasm specifig configuration
Expand Down Expand Up @@ -250,3 +256,50 @@ 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent 🦅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!!!

return d.Replace.Version
}
return 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 wasmExpectedVersion == "" {
return fmt.Errorf("wasmvm module not exist")
}
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
}
}