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

fix: release vm right after execution finished #274

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 3 additions & 6 deletions x/move/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ func (k Keeper) Initialize(
_allowedPublishers[i] = addr
}

// TODO - remove this after loader v2 is installed
vm := k.acquireVM(ctx)
defer k.releaseVM()

// The default upgrade policy is compatible when it's not set.
vmStore := types.NewVMStore(ctx, k.VMStore)
execRes, err := vm.Initialize(vmStore, api, env, vmtypes.NewModuleBundle(modules...), _allowedPublishers)
execRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ExecutionResult, error) {
return vm.Initialize(vmStore, api, env, vmtypes.NewModuleBundle(modules...), _allowedPublishers)
})
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
85 changes: 43 additions & 42 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@
args [][]byte,
isJSON bool,
) error {
// TODO - remove this after loader v2 is installed
vm := k.acquireVM(ctx)
defer k.releaseVM()

payload, err := types.BuildExecuteEntryFunctionPayload(
moduleAddr,
moduleName,
Expand All @@ -194,24 +190,24 @@
sdkCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sdkCtx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

if isSimulation(ctx) {
gasForRuntime = k.config.ContractSimulationGasLimit
}

// delegate gas metering to move vm
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())

// run vm
gasBalance := gasForRuntime
execRes, err := vm.ExecuteEntryFunction(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
execRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ExecutionResult, error) {
return vm.ExecuteEntryFunction(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
})

// consume gas first and check error
gasUsed := gasForRuntime - gasBalance
Expand Down Expand Up @@ -284,10 +280,6 @@
args [][]byte,
isJSON bool,
) error {
// TODO - remove this after loader v2 is installed
vm := k.acquireVM(ctx)
defer k.releaseVM()

// prepare payload
payload, err := types.BuildExecuteScriptPayload(
byteCodes,
Expand All @@ -313,24 +305,24 @@
sdkCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sdkCtx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

if isSimulation(ctx) {
gasForRuntime = k.config.ContractSimulationGasLimit
}

// delegate gas metering to move vm
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())

// run vm
gasBalance := gasForRuntime
execRes, err := vm.ExecuteScript(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
execRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ExecutionResult, error) {
return vm.ExecuteScript(
&gasBalance,
types.NewVMStore(sdkCtx, k.VMStore),
NewApi(k, sdkCtx),
types.NewEnv(sdkCtx, ac, ec),
senders,
payload,
)
})

// consume gas first and check error
gasUsed := gasForRuntime - gasBalance
Expand Down Expand Up @@ -565,10 +557,6 @@
args [][]byte,
isJSON bool,
) (vmtypes.ViewOutput, uint64, error) {
// TODO - remove this after loader v2 is installed
vm := k.acquireVM(ctx)
defer k.releaseVM()

payload, err := types.BuildExecuteViewFunctionPayload(
moduleAddr,
moduleName,
Expand Down Expand Up @@ -598,20 +586,33 @@
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

gasBalance := gasForRuntime
viewRes, err := vm.ExecuteViewFunction(
&gasBalance,
types.NewVMStore(ctx, k.VMStore),
api,
env,
payload,
)
if err != nil {
return vmtypes.ViewOutput{}, 0, err
}
viewRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ViewOutput, error) {
return vm.ExecuteViewFunction(
&gasBalance,
types.NewVMStore(ctx, k.VMStore),
api,
env,
payload,
)
})

// consume gas first and check error
gasUsed := gasForRuntime - gasBalance
gasMeter.ConsumeGas(gasUsed, "view; move runtime")
if err != nil {
return vmtypes.ViewOutput{}, gasUsed, err
}

Check warning on line 604 in x/move/keeper/handler.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/handler.go#L603-L604

Added lines #L603 - L604 were not covered by tests

return viewRes, gasUsed, nil
}

// execVM runs vm in separate function statement to release right after execution
// to avoid deadlock even if the function panics
//
// TODO - remove this after loader v2 is installed
func execVM[T any](ctx context.Context, k Keeper, f func(types.VMEngine) (T, error)) (T, error) {
vm := k.acquireVM(ctx)
defer k.releaseVM()

return f(vm)
}
Loading