Skip to content

Commit

Permalink
Merge pull request #548 from bandprotocol/fix-recover-out-of-gas
Browse files Browse the repository at this point in the history
[Tunnel] fix recover out of gas error
  • Loading branch information
RogerKSI authored Jan 10, 2025
2 parents ddde91c + 5cca261 commit ae94e87
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 13 additions & 0 deletions x/tunnel/keeper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"

sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

feedstypes "github.com/bandprotocol/chain/v3/x/feeds/types"
"github.com/bandprotocol/chain/v3/x/tunnel/types"
Expand Down Expand Up @@ -72,3 +73,15 @@ func calculateDeviationBPS(oldPrice, newPrice sdkmath.Int) sdkmath.Int {

return newPrice.Sub(oldPrice).Abs().MulRaw(10000).Quo(oldPrice)
}

// IsOutOfGasError checks if the error object is an out of gas or gas overflow error type
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, ""
}
}
10 changes: 7 additions & 3 deletions x/tunnel/keeper/keeper_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ func (k Keeper) CreatePacket(
func (k Keeper) SendPacket(ctx sdk.Context, packet types.Packet) (err error) {
defer func() {
if r := recover(); r != nil {
ctx.Logger().Error(fmt.Sprintf("Panic recovered: %v", r))
err = types.ErrSendPacketPanic
return
if isErr, _ := IsOutOfGasError(r); isErr {
// We panic with the same error, to replicate the normal tx execution flow.
panic(r)
} else {
ctx.Logger().Error(fmt.Sprintf("Panic recovered: %v", r))
err = types.ErrSendPacketPanic
}
}
}()

Expand Down

0 comments on commit ae94e87

Please sign in to comment.