Skip to content

Commit 4368bb9

Browse files
committed
Fix
1 parent 3336848 commit 4368bb9

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

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)