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 1 commit
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
80 changes: 40 additions & 40 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ func (k Keeper) executeEntryFunction(
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 @@ func (k Keeper) executeEntryFunction(
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 @@ func (k Keeper) executeScript(
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 @@ func (k Keeper) executeScript(
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 @@ func (k Keeper) executeViewFunction(
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 @@ -596,15 +584,16 @@ func (k Keeper) executeViewFunction(
sdkCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sdkCtx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

gasBalance := gasForRuntime
viewRes, err := vm.ExecuteViewFunction(
&gasBalance,
types.NewVMStore(ctx, k.VMStore),
api,
env,
payload,
)
viewRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ViewOutput, error) {
return vm.ExecuteViewFunction(
&gasBalance,
types.NewVMStore(ctx, k.VMStore),
api,
env,
payload,
)
})
if err != nil {
return vmtypes.ViewOutput{}, 0, err
}
Expand All @@ -615,3 +604,14 @@ func (k Keeper) executeViewFunction(

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