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

design: Why does consumeGas panic & not return an error? #1771

Open
Reecepbcups opened this issue Jan 8, 2024 · 0 comments
Open

design: Why does consumeGas panic & not return an error? #1771

Reecepbcups opened this issue Jan 8, 2024 · 0 comments

Comments

@Reecepbcups
Copy link

func (k Keeper) consumeRuntimeGas(ctx sdk.Context, gas uint64) panics in function calls such as Sudo calls even though an error for this function is thrown

For developers, this is unexpected behavior, especially when using a childCtx with a gas limit, expecting an error to be thrown.

It then required work around like the following from Juno with a recover()

// Execute contract, recover from panic
func ExecuteContract(k wasmtypes.ContractOpsKeeper, childCtx sdk.Context, contractAddr sdk.AccAddress, msgBz []byte, err *error) {
	// Recover from panic, return error
	defer func() {
		if recoveryError := recover(); recoveryError != nil {
			// Determine error associated with panic
			if isOutofGas, msg := IsOutOfGasError(recoveryError); isOutofGas {
				*err = ErrOutOfGas.Wrapf("%s", msg)
			} else {
				*err = ErrContractExecutionPanic.Wrapf("%s", recoveryError)
			}
		}
	}()

	// Execute contract with sudo
	_, *err = k.Sudo(childCtx, contractAddr, msgBz)
}

// Check if error is out of gas error
func IsOutOfGasError(err any) (bool, string) {
	switch e := err.(type) {
	case storetypes.ErrorOutOfGas:
		return true, e.Descriptor
	case storetypes.ErrorGasOverflow:
		return true, e.Descriptor
	default:
		return false, ""
	}
}

This just seems wrong given Sudo already returns an error, so persist a Gas error up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant