From 569e3ff6a0d78ae6344752f2e243a1d192ddfbd7 Mon Sep 17 00:00:00 2001 From: testinginprod <98415576+testinginprod@users.noreply.github.com> Date: Tue, 14 Nov 2023 11:07:55 +0100 Subject: [PATCH] feat(core): add Branch.ExecuteWithGasLimit API (#18457) Co-authored-by: unknown unknown --- core/CHANGELOG.md | 1 + core/branch/branch.go | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 3b80170d78aa..803225487c10 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -37,6 +37,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] * [#18379](https://github.com/cosmos/cosmos-sdk/pull/18379) Add branch service. +* [#18457](https://github.com/cosmos/cosmos-sdk/pull/18457) Add branch.ExecuteWithGasLimit. ## [v0.12.0](https://github.com/cosmos/cosmos-sdk/releases/tag/core%2Fv0.11.0) diff --git a/core/branch/branch.go b/core/branch/branch.go index c5af0087fc20..79634cf43d31 100644 --- a/core/branch/branch.go +++ b/core/branch/branch.go @@ -1,7 +1,14 @@ // Package branch contains the core branch service interface. package branch -import "context" +import ( + "context" + "errors" +) + +// ErrGasLimitExceeded is returned when the gas limit is exceeded in a +// Service.ExecuteWithGasLimit call. +var ErrGasLimitExceeded = errors.New("branch: gas limit exceeded") // Service is the branch service interface. It can be used to execute // code paths in an isolated execution context that can be reverted. @@ -16,4 +23,12 @@ type Service interface { // passed to the Execute function, and is what should be used with other // core services in order to ensure the execution remains isolated. Execute(ctx context.Context, f func(ctx context.Context) error) error + // ExecuteWithGasLimit executes the given function `f` in an isolated context, + // with the provided gas limit, this is advanced usage and is used to disallow + // an execution path to consume an indefinite amount of gas. + // If the execution fails or succeeds the gas limit is still applied to the + // parent context, the function returns a gasUsed value which is the amount + // of gas used by the execution path. If the execution path exceeds the gas + // ErrGasLimitExceeded is returned. + ExecuteWithGasLimit(ctx context.Context, gasLimit uint64, f func(ctx context.Context) error) (gasUsed uint64, err error) }