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

Add keeper level panic catches #3312

Merged
merged 3 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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