Skip to content

Commit

Permalink
Merge pull request #312 from okp4/fix/out-of-gas
Browse files Browse the repository at this point in the history
🐛 Fix out of gas recover on logic
  • Loading branch information
bdeneux authored Mar 17, 2023
2 parents e89fcf2 + 04dcf6e commit e403c17
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions x/logic/keeper/grpc_query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo

panic(r)
}
if sdkCtx.GasMeter().IsOutOfGas() {
response, err = nil, sdkerrors.Wrapf(
types.LimitExceeded, "out of gas: %s (%d/%d)",
types.ModuleName, sdkCtx.GasMeter().GasConsumed(), sdkCtx.GasMeter().Limit())

return
}
}()
sdkCtx.GasMeter().ConsumeGas(sdkCtx.GasMeter().GasConsumed(), types.ModuleName)

Expand Down
15 changes: 15 additions & 0 deletions x/logic/types/gas.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"runtime"
"sync"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,6 +19,20 @@ type safeGasMeter struct {

func (m *safeGasMeter) ConsumeGas(amount uint64, descriptor string) {
m.mutex.Lock()
defer func() {
if r := recover(); r != nil {
if _, ok := r.(sdk.ErrorOutOfGas); ok {
// Since predicate is called into a goroutine, when out of gas is thrown, the main caller
// (grpc: https://github.com/okp4/okp4d/blob/main/x/logic/keeper/grpc_query_ask.go#L25-L36, or querier)
// cannot recover ErrOutOfGas. To avoid the chain panicking, we need to exit without panic.
// Goexit runs all deferred calls before terminating the goroutine. Because Goexit
// is not a panic, any recover calls in those deferred functions will return nil.
// This is a temporary solution before implementing a context cancellation.
runtime.Goexit()
}
panic(r)
}
}()
defer m.mutex.Unlock()

m.gasMeter.ConsumeGas(amount, descriptor)
Expand Down

0 comments on commit e403c17

Please sign in to comment.