diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 3b80170d78..803225487c 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 c5af0087fc..79634cf43d 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) }