Skip to content

Commit 96ae094

Browse files
authored
Merge pull request #2070 from CosmWasm/co/backport-fix
Fix backport to main
2 parents 3336848 + 0b78484 commit 96ae094

File tree

7 files changed

+53
-13
lines changed

7 files changed

+53
-13
lines changed

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ RUN apk add git
1515
WORKDIR /code
1616
COPY . /code/
1717
# See https://github.com/CosmWasm/wasmvm/releases
18-
ADD https://github.com/CosmWasm/wasmvm/releases/download/v2.2.0/libwasmvm_muslc.aarch64.a /lib/libwasmvm_muslc.aarch64.a
19-
ADD https://github.com/CosmWasm/wasmvm/releases/download/v2.2.0/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.x86_64.a
20-
RUN sha256sum /lib/libwasmvm_muslc.aarch64.a | grep f7e3c97481d7d9ce9469c5e14c76e1c45d17e7735d18bdec04782ab2645b382c
21-
RUN sha256sum /lib/libwasmvm_muslc.x86_64.a | grep 7ee83ec96bfb962465000b12d0abc758528e36a4fafb49cdef15708059be0d2b
18+
ADD https://github.com/CosmWasm/wasmvm/releases/download/v2.2.1/libwasmvm_muslc.aarch64.a /lib/libwasmvm_muslc.aarch64.a
19+
ADD https://github.com/CosmWasm/wasmvm/releases/download/v2.2.1/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.x86_64.a
20+
RUN sha256sum /lib/libwasmvm_muslc.aarch64.a | grep ba6cb5db6b14a265c8556326c045880908db9b1d2ffb5d4aa9f09ac09b24cecc
21+
RUN sha256sum /lib/libwasmvm_muslc.x86_64.a | grep b3bd755efac0ff39c01b59b8110f961c48aa3eb93588071d7a628270cc1f2326
2222

2323
# force it to use static lib (from above) not standard libgo_cosmwasm.so file
2424
RUN LEDGER_ENABLED=false BUILD_TAGS=muslc LINK_STATICALLY=true make build

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/CosmWasm/wasmd
33
go 1.23.1
44

55
require (
6-
github.com/CosmWasm/wasmvm/v2 v2.2.0
6+
github.com/CosmWasm/wasmvm/v2 v2.2.1
77
github.com/cosmos/cosmos-proto v1.0.0-beta.5
88
github.com/cosmos/cosmos-sdk v0.50.10
99
github.com/cosmos/gogogateway v1.2.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
223223
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
224224
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
225225
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
226-
github.com/CosmWasm/wasmvm/v2 v2.2.0 h1:PWHqwzltJYRyPPiBZiwBJ3Rkkbr6NESIz03u2TRGQfc=
227-
github.com/CosmWasm/wasmvm/v2 v2.2.0/go.mod h1:bMhLQL4Yp9CzJi9A83aR7VO9wockOsSlZbT4ztOl6bg=
226+
github.com/CosmWasm/wasmvm/v2 v2.2.1 h1:cmOnM+TDfUl2VRugeo1eJBw4U/Lw0WLviuQHKSo9DVQ=
227+
github.com/CosmWasm/wasmvm/v2 v2.2.1/go.mod h1:bMhLQL4Yp9CzJi9A83aR7VO9wockOsSlZbT4ztOl6bg=
228228
github.com/DataDog/datadog-go v3.2.0+incompatible h1:qSG2N4FghB1He/r2mFrWKCaL7dXCilEuNEeAn20fdD4=
229229
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
230230
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=

tests/integration/module_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ var (
112112
oldContract = mustLoad("./testdata/escrow_0.7.wasm")
113113
)
114114

115+
func TestStoreCodeSimulation(t *testing.T) {
116+
data := setupTest(t)
117+
data.ctx = data.ctx.WithExecMode(sdk.ExecModeSimulate)
118+
119+
msg := &types.MsgStoreCode{
120+
Sender: addr1,
121+
WASMByteCode: testContract,
122+
}
123+
124+
h := data.msgServiceRouter.Handler(msg)
125+
126+
_, err := h(data.ctx, msg)
127+
require.NoError(t, err)
128+
}
129+
115130
func TestHandleCreate(t *testing.T) {
116131
cases := map[string]struct {
117132
msg sdk.Msg

x/wasm/keeper/keeper.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,29 @@ func (k Keeper) create(ctx context.Context, creator sdk.AccAddress, wasmCode []b
178178
}
179179

180180
gasLeft := k.runtimeGasForContract(sdkCtx)
181-
checksum, gasUsed, err := k.wasmVM.StoreCode(wasmCode, gasLeft)
181+
var gasUsed uint64
182+
isSimulation := sdkCtx.ExecMode() == sdk.ExecModeSimulate
183+
if isSimulation {
184+
// only simulate storing the code, no files are written
185+
checksum, gasUsed, err = k.wasmVM.SimulateStoreCode(wasmCode, gasLeft)
186+
} else {
187+
checksum, gasUsed, err = k.wasmVM.StoreCode(wasmCode, gasLeft)
188+
}
182189
k.consumeRuntimeGas(sdkCtx, gasUsed)
183190
if err != nil {
184191
return 0, checksum, errorsmod.Wrap(types.ErrCreateFailed, err.Error())
185192
}
186-
report, err := k.wasmVM.AnalyzeCode(checksum)
187-
if err != nil {
188-
return 0, checksum, errorsmod.Wrap(types.ErrCreateFailed, err.Error())
193+
// simulation gets default value for capabilities
194+
var requiredCapabilities string
195+
if !isSimulation {
196+
report, err := k.wasmVM.AnalyzeCode(checksum)
197+
if err != nil {
198+
return 0, checksum, errorsmod.Wrap(types.ErrCreateFailed, err.Error())
199+
}
200+
requiredCapabilities = report.RequiredCapabilities
189201
}
190202
codeID = k.mustAutoIncrementID(sdkCtx, types.KeySequenceCodeID)
191-
k.Logger(sdkCtx).Debug("storing new contract", "capabilities", report.RequiredCapabilities, "code_id", codeID)
203+
k.Logger(sdkCtx).Debug("storing new contract", "capabilities", requiredCapabilities, "code_id", codeID)
192204
codeInfo := types.NewCodeInfo(checksum, creator, *instantiateAccess)
193205
k.mustStoreCodeInfo(sdkCtx, codeID, codeInfo)
194206

@@ -197,7 +209,7 @@ func (k Keeper) create(ctx context.Context, creator sdk.AccAddress, wasmCode []b
197209
sdk.NewAttribute(types.AttributeKeyChecksum, hex.EncodeToString(checksum)),
198210
sdk.NewAttribute(types.AttributeKeyCodeID, strconv.FormatUint(codeID, 10)), // last element to be compatible with scripts
199211
)
200-
for _, f := range strings.Split(report.RequiredCapabilities, ",") {
212+
for _, f := range strings.Split(requiredCapabilities, ",") {
201213
evt.AppendAttributes(sdk.NewAttribute(types.AttributeKeyRequiredCapability, strings.TrimSpace(f)))
202214
}
203215
sdkCtx.EventManager().EmitEvent(evt)

x/wasm/keeper/wasmtesting/mock_engine.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var _ types.WasmEngine = &MockWasmEngine{}
2424
type MockWasmEngine struct {
2525
StoreCodeFn func(codeID wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error)
2626
StoreCodeUncheckedFn func(codeID wasmvm.WasmCode) (wasmvm.Checksum, error)
27+
SimulateStoreCodeFn func(codeID wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error)
2728
AnalyzeCodeFn func(codeID wasmvm.Checksum) (*wasmvmtypes.AnalysisReport, error)
2829
InstantiateFn func(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.ContractResult, uint64, error)
2930
ExecuteFn func(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, executeMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.ContractResult, uint64, error)
@@ -118,6 +119,13 @@ func (m *MockWasmEngine) StoreCodeUnchecked(codeID wasmvm.WasmCode) (wasmvm.Chec
118119
return m.StoreCodeUncheckedFn(codeID)
119120
}
120121

122+
func (m *MockWasmEngine) SimulateStoreCode(codeID wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error) {
123+
if m.SimulateStoreCodeFn == nil {
124+
panic("not supposed to be called!")
125+
}
126+
return m.SimulateStoreCodeFn(codeID, gasLimit)
127+
}
128+
121129
func (m *MockWasmEngine) AnalyzeCode(codeID wasmvm.Checksum) (*wasmvmtypes.AnalysisReport, error) {
122130
if m.AnalyzeCodeFn == nil {
123131
panic("not supposed to be called!")

x/wasm/types/wasmer_engine.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type WasmEngine interface {
3030
// Use this for adding code that was checked before, particularly in the case of state sync.
3131
StoreCodeUnchecked(code wasmvm.WasmCode) (wasmvm.Checksum, error)
3232

33+
// SimulateStoreCode works like StoreCode, but does not actually store the code.
34+
// Instead, it just does all the validation and compilation steps without storing the result on disk.
35+
// Returns both the checksum, as well as the gas cost of compilation (in CosmWasm Gas) or an error.
36+
SimulateStoreCode(code wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error)
37+
3338
// AnalyzeCode will statically analyze the code.
3439
// Currently just reports if it exposes all IBC entry points.
3540
AnalyzeCode(checksum wasmvm.Checksum) (*wasmvmtypes.AnalysisReport, error)

0 commit comments

Comments
 (0)