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

Better errors on out of gas #7181

Merged
merged 5 commits into from
Jan 16, 2024
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### State Breaking

* [#7181](https://github.com/osmosis-labs/osmosis/pull/7181) Improve errors for out of gas

## v22.0.0

### Fee Market Parameter Updates
Expand Down Expand Up @@ -119,6 +125,7 @@ Epoch and CPU time optimizations
* [#7093](https://github.com/osmosis-labs/osmosis/pull/7093),[#7100](https://github.com/osmosis-labs/osmosis/pull/7100),[#7172](https://github.com/osmosis-labs/osmosis/pull/7172),[#7174](https://github.com/osmosis-labs/osmosis/pull/7174),[#7186](https://github.com/osmosis-labs/osmosis/pull/7186), [#7192](https://github.com/osmosis-labs/osmosis/pull/7192) Lower CPU overheads of the Osmosis epoch.
* [#7106](https://github.com/osmosis-labs/osmosis/pull/7106) Halve the time of log2 calculation (speeds up TWAP code)


## v21.1.2

* [#7170](https://github.com/osmosis-labs/osmosis/pull/7170) Update mempool-eip1559 params to cause less base fee spikes on mainnet.
Expand Down
20 changes: 17 additions & 3 deletions x/gamm/keeper/pool_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"fmt"

"github.com/osmosis-labs/osmosis/osmoutils"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -142,7 +144,11 @@ func (k Keeper) JoinPoolNoSwap(
if r := recover(); r != nil {
tokenIn = sdk.Coins{}
sharesOut = osmomath.Int{}
err = fmt.Errorf("function JoinPoolNoSwap failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function JoinPoolNoSwap failed due to lack of gas: %v", d)
} else {
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
Expand Down Expand Up @@ -234,7 +240,11 @@ func (k Keeper) JoinSwapExactAmountIn(
defer func() {
if r := recover(); r != nil {
sharesOut = osmomath.Int{}
err = fmt.Errorf("function JoinSwapExactAmountIn failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function JoinSwapExactAmountIn failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function JoinSwapExactAmountIn failed due to internal reason: %v", r)
}
}
}()

Expand Down Expand Up @@ -278,7 +288,11 @@ func (k Keeper) JoinSwapShareAmountOut(
defer func() {
if r := recover(); r != nil {
tokenInAmount = osmomath.Int{}
err = fmt.Errorf("function JoinSwapShareAmountOut failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function JoinSwapShareAmountOut failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function JoinSwapShareAmountOut failed due to internal reason: %v", r)
}
}
}()

Expand Down
14 changes: 12 additions & 2 deletions x/gamm/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"

"github.com/osmosis-labs/osmosis/osmoutils"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -40,7 +42,11 @@ func (k Keeper) SwapExactAmountIn(
defer func() {
if r := recover(); r != nil {
tokenOutAmount = osmomath.Int{}
err = fmt.Errorf("function swapExactAmountIn failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function swapExactAmountIn failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function swapExactAmountIn failed due to internal reason: %v", r)
}
}
}()

Expand Down Expand Up @@ -94,7 +100,11 @@ func (k Keeper) SwapExactAmountOut(
defer func() {
if r := recover(); r != nil {
tokenInAmount = osmomath.Int{}
err = fmt.Errorf("function swapExactAmountOut failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function swapExactAmountOut failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function swapExactAmountOut failed due to internal reason: %v", r)
}
}
}()

Expand Down
14 changes: 14 additions & 0 deletions x/gamm/keeper/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ func (s *KeeperTestSuite) TestActiveBalancerPoolSwap() {
}
}

func (s *KeeperTestSuite) TestOutOfGasError() {
s.SetupTest()
poolId := s.PrepareBalancerPool()

pool, err := s.App.GAMMKeeper.GetPool(s.Ctx, poolId)
s.Require().NoError(err)
foocoin := sdk.NewCoin("foo", osmomath.NewInt(10))
spreadFactor := pool.GetSpreadFactor(s.Ctx)
ctx := s.Ctx.WithGasMeter(sdk.NewGasMeter(10))
_, err = s.App.GAMMKeeper.SwapExactAmountIn(ctx, s.TestAccs[0], pool, foocoin, "bar", osmomath.ZeroInt(), spreadFactor)
s.Require().Error(err)
s.Require().Contains(err.Error(), "lack of gas")
}

// UNFORKINGNOTE: This test really wasn't testing anything important
// With the unfork, we can no longer utilize mocks when calling SetPools, since
// the interface needs to be registered with codec, and the mocks aren't wired to do that.
Expand Down
18 changes: 15 additions & 3 deletions x/poolmanager/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ func (k Keeper) MultihopEstimateOutGivenExactAmountIn(
defer func() {
if r := recover(); r != nil {
tokenOutAmount = osmomath.Int{}
err = fmt.Errorf("function MultihopEstimateOutGivenExactAmountIn failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function MultihopEstimateOutGivenExactAmountIn failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function MultihopEstimateOutGivenExactAmountIn failed due to internal reason: %v", r)
}
}
}()

Expand Down Expand Up @@ -303,7 +307,11 @@ func (k Keeper) RouteExactAmountOut(ctx sdk.Context,
defer func() {
if r := recover(); r != nil {
tokenInAmount = osmomath.Int{}
err = fmt.Errorf("function RouteExactAmountOut failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function RouteExactAmountOut failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function RouteExactAmountOut failed due to internal reason: %v", r)
}
}
}()

Expand Down Expand Up @@ -495,7 +503,11 @@ func (k Keeper) MultihopEstimateInGivenExactAmountOut(
defer func() {
if r := recover(); r != nil {
insExpected = []osmomath.Int{}
err = fmt.Errorf("function MultihopEstimateInGivenExactAmountOut failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function MultihopEstimateInGivenExactAmountOut failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function MultihopEstimateInGivenExactAmountOut failed due to internal reason: %v", r)
}
}
}()

Expand Down
8 changes: 7 additions & 1 deletion x/protorev/keeper/posthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"fmt"

"github.com/osmosis-labs/osmosis/osmoutils"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/osmomath"
Expand Down Expand Up @@ -113,7 +115,11 @@ func (k Keeper) ProtoRevTrade(ctx sdk.Context, swappedPools []SwapToBackrun) (er
// recover from panic
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Protorev failed due to internal reason: %v", r)
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("protorev failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("protorev failed due to internal reason: %v", r)
}
}
}()

Expand Down