Skip to content

Commit

Permalink
Add keeper level panic catches (#3312)
Browse files Browse the repository at this point in the history
* Add keeper level panic catches

* Add changelog

* Add panic catch for swap

(cherry picked from commit f88577d)
  • Loading branch information
ValarDragon authored and mergify[bot] committed Nov 9, 2022
1 parent 309a8c5 commit 8bd01d8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#2914](https://github.com/osmosis-labs/osmosis/pull/2914) Remove out of gas panics from node logs
* [#2937](https://github.com/osmosis-labs/osmosis/pull/2937) End block ordering - staking after gov and module sorting.
* [#2923](https://github.com/osmosis-labs/osmosis/pull/2923) TWAP calculation now errors if it uses records that have errored previously.
* [#3312](https://github.com/osmosis-labs/osmosis/pull/3312) Add better panic catches within GAMM txs

### Misc Improvements

Expand Down
28 changes: 26 additions & 2 deletions x/gamm/keeper/pool_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ func (k Keeper) JoinPoolNoSwap(
shareOutAmount sdk.Int,
tokenInMaxs sdk.Coins,
) (tokenIn sdk.Coins, sharesOut sdk.Int, err error) {
// defer to catch panics, in case something internal overflows.
defer func() {
if r := recover(); r != nil {
tokenIn = sdk.Coins{}
sharesOut = sdk.Int{}
err = fmt.Errorf("function JoinPoolNoSwap failed due to internal reason: %v", r)
}
}()
// all pools handled within this method are pointer references, `JoinPool` directly updates the pools
pool, err := k.GetPoolAndPoke(ctx, poolId)
if err != nil {
Expand Down Expand Up @@ -281,13 +289,21 @@ func (k Keeper) JoinSwapExactAmountIn(
poolId uint64,
tokensIn sdk.Coins,
shareOutMinAmount sdk.Int,
) (sdk.Int, error) {
) (sharesOut sdk.Int, err error) {
// defer to catch panics, in case something internal overflows.
defer func() {
if r := recover(); r != nil {
sharesOut = sdk.Int{}
err = fmt.Errorf("function JoinSwapExactAmountIn failed due to internal reason: %v", r)
}
}()

pool, err := k.getPoolForSwap(ctx, poolId)
if err != nil {
return sdk.Int{}, err
}

sharesOut, err := pool.JoinPool(ctx, tokensIn, pool.GetSwapFee(ctx))
sharesOut, err = pool.JoinPool(ctx, tokensIn, pool.GetSwapFee(ctx))
switch {
case err != nil:
return sdk.ZeroInt(), err
Expand Down Expand Up @@ -318,6 +334,14 @@ func (k Keeper) JoinSwapShareAmountOut(
shareOutAmount sdk.Int,
tokenInMaxAmount sdk.Int,
) (tokenInAmount sdk.Int, err error) {
// defer to catch panics, in case something internal overflows.
defer func() {
if r := recover(); r != nil {
tokenInAmount = sdk.Int{}
err = fmt.Errorf("function JoinSwapShareAmountOut failed due to internal reason: %v", r)
}
}()

pool, err := k.getPoolForSwap(ctx, poolId)
if err != nil {
return sdk.Int{}, err
Expand Down
15 changes: 15 additions & 0 deletions x/gamm/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -50,6 +51,13 @@ func (k Keeper) swapExactAmountIn(
}
tokensIn := sdk.Coins{tokenIn}

defer func() {
if r := recover(); r != nil {
tokenOutAmount = sdk.Int{}
err = fmt.Errorf("function swapExactAmountIn failed due to internal reason: %v", r)
}
}()

// Executes the swap in the pool and stores the output. Updates pool assets but
// does not actually transfer any tokens to or from the pool.
tokenOutCoin, err := pool.SwapOutAmtGivenIn(ctx, tokensIn, tokenOutDenom, swapFee)
Expand Down Expand Up @@ -109,6 +117,13 @@ func (k Keeper) swapExactAmountOut(
return sdk.Int{}, errors.New("cannot trade same denomination in and out")
}

defer func() {
if r := recover(); r != nil {
tokenInAmount = sdk.Int{}
err = fmt.Errorf("function swapExactAmountOut failed due to internal reason: %v", r)
}
}()

poolOutBal := pool.GetTotalPoolLiquidity(ctx).AmountOf(tokenOut.Denom)
if tokenOut.Amount.GTE(poolOutBal) {
return sdk.Int{}, sdkerrors.Wrapf(types.ErrTooManyTokensOut,
Expand Down

0 comments on commit 8bd01d8

Please sign in to comment.