Skip to content

Commit dd3b272

Browse files
authored
Merge pull request #982 from neutron-org/chore/bpa_test
Chore: bpa test
2 parents df57cf8 + bc5f39a commit dd3b272

File tree

9 files changed

+135
-21
lines changed

9 files changed

+135
-21
lines changed

app/app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
v700 "github.com/neutron-org/neutron/v8/app/upgrades/v7.0.0"
1414
v800 "github.com/neutron-org/neutron/v8/app/upgrades/v8.0.0"
1515
v800_rc0 "github.com/neutron-org/neutron/v8/app/upgrades/v8.0.0-rc0"
16+
v810 "github.com/neutron-org/neutron/v8/app/upgrades/v8.1.0"
1617
dynamicfeestypes "github.com/neutron-org/neutron/v8/x/dynamicfees/types"
1718
stateverifier "github.com/neutron-org/neutron/v8/x/state-verifier"
1819
svkeeper "github.com/neutron-org/neutron/v8/x/state-verifier/keeper"
@@ -239,6 +240,7 @@ var (
239240
v700.Upgrade,
240241
v800_rc0.Upgrade,
241242
v800.Upgrade,
243+
v810.Upgrade,
242244
}
243245

244246
// DefaultNodeHome default home directories for the application daemon

app/upgrades/v8.1.0/constants.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package v810
2+
3+
import (
4+
storetypes "cosmossdk.io/store/types"
5+
6+
"github.com/neutron-org/neutron/v8/app/upgrades"
7+
)
8+
9+
const (
10+
// UpgradeName defines the on-chain upgrade name.
11+
UpgradeName = "v8.1.0"
12+
)
13+
14+
var Upgrade = upgrades.Upgrade{
15+
UpgradeName: UpgradeName,
16+
CreateUpgradeHandler: CreateUpgradeHandler,
17+
StoreUpgrades: storetypes.StoreUpgrades{},
18+
}

app/upgrades/v8.1.0/upgrades.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package v810
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
upgradetypes "cosmossdk.io/x/upgrade/types"
8+
"github.com/cosmos/cosmos-sdk/codec"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
"github.com/cosmos/cosmos-sdk/types/module"
11+
12+
"github.com/neutron-org/neutron/v8/app/upgrades"
13+
)
14+
15+
func CreateUpgradeHandler(
16+
mm *module.Manager,
17+
configurator module.Configurator,
18+
keepers *upgrades.UpgradeKeepers,
19+
_ upgrades.StoreKeys,
20+
cdc codec.Codec,
21+
) upgradetypes.UpgradeHandler {
22+
return func(c context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
23+
ctx := sdk.UnwrapSDKContext(c)
24+
25+
ctx.Logger().Info("Starting module migrations...")
26+
27+
vm, err := mm.RunMigrations(ctx, configurator, vm)
28+
if err != nil {
29+
return vm, err
30+
}
31+
32+
ctx.Logger().Info(fmt.Sprintf("Migration {%s} applied", UpgradeName))
33+
return vm, nil
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package v810_test
2+
3+
import (
4+
"testing"
5+
6+
upgradetypes "cosmossdk.io/x/upgrade/types"
7+
"github.com/stretchr/testify/require"
8+
"github.com/stretchr/testify/suite"
9+
10+
v810 "github.com/neutron-org/neutron/v8/app/upgrades/v8.1.0"
11+
"github.com/neutron-org/neutron/v8/testutil"
12+
)
13+
14+
type UpgradeTestSuite struct {
15+
testutil.IBCConnectionTestSuite
16+
}
17+
18+
func TestKeeperTestSuite(t *testing.T) {
19+
suite.Run(t, new(UpgradeTestSuite))
20+
}
21+
22+
func (suite *UpgradeTestSuite) SetupTest() {
23+
suite.IBCConnectionTestSuite.SetupTest()
24+
}
25+
26+
func (suite *UpgradeTestSuite) TestUpgrade() {
27+
app := suite.GetNeutronZoneApp(suite.ChainA)
28+
ctx := suite.ChainA.GetContext().WithChainID("neutron-1")
29+
t := suite.T()
30+
31+
upgrade := upgradetypes.Plan{
32+
Name: v810.UpgradeName,
33+
Info: "some text here",
34+
Height: 100,
35+
}
36+
require.NoError(t, app.UpgradeKeeper.ApplyUpgrade(ctx, upgrade))
37+
}

x/dex/keeper/deposit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ func (k Keeper) ExecuteDeposit(
121121
if k.IsPoolBehindEnemyLines(ctx, pairID, tickIndex, fee, depositAmount0, depositAmount1) {
122122
err = sdkerrors.Wrapf(types.ErrDepositBehindEnemyLines,
123123
"deposit failed at tick %d fee %d", tickIndex, fee)
124-
if option.FailTxOnBel {
124+
// Always fail entire tx when SwapOnDeposit is enabled or FailTxOnBel is enabled
125+
if option.FailTxOnBel || option.SwapOnDeposit {
125126
return nil, nil, math_utils.ZeroPrecDec(), math_utils.ZeroPrecDec(), nil, nil, nil, err
126127
}
127128
failedDeposits = append(failedDeposits, &types.FailedDeposit{DepositIdx: uint64(i), Error: err.Error()}) //nolint:gosec

x/dex/keeper/integration_placelimitorder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ func (s *DexTestSuite) TestPlaceLimitOrderWithPrice0To1() {
345345
s.aliceWithdrawsLimitSell(trancheKey0)
346346

347347
// THEN alice gets out ~100 TOKENB and bob gets ~10 TOKENA
348-
s.assertAliceBalancesInt(sdkmath.ZeroInt(), sdkmath.NewInt(99_999_977))
349-
s.assertBobBalancesInt(sdkmath.NewInt(10000000), sdkmath.NewInt(22))
348+
s.assertAliceBalancesInt(sdkmath.ZeroInt(), sdkmath.NewInt(99_999_999))
349+
s.assertBobBalancesInt(sdkmath.NewInt(9999002), sdkmath.NewInt(0))
350350
}
351351

352352
func (s *DexTestSuite) TestPlaceLimitOrderWithPrice1To0() {

x/dex/types/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
ErrTickOutsideRange = sdkerrors.Register(
3838
ModuleName,
3939
1117,
40-
"abs(tick) + fee must be < 559,680",
40+
"abs(tick) + fee must be < 529,715",
4141
)
4242
ErrInvalidPoolDenom = sdkerrors.Register(
4343
ModuleName,
@@ -202,7 +202,7 @@ var (
202202
ErrPriceOutsideRange = sdkerrors.Register(
203203
ModuleName,
204204
1160,
205-
"Invalid price; 0.00000000000000000000000050 < PRICE > 2020125331305056766451886.728",
205+
"Invalid price; 0.000000000000000000000000495 < PRICE > 2020125331305056766452345.127500016657360222036663651",
206206
)
207207
ErrInvalidPriceAndTick = sdkerrors.Register(
208208
ModuleName,

x/dex/types/price.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
const (
14-
// NOTE: 559_680 is the highest possible tick at which price can be calculated with a < 1% error
14+
// NOTE: 529_715 is the highest possible tick at which price can be calculated with a < 1% error and all prices are unique for negative ticks
1515
// when using 27 digit decimal precision (via prec_dec).
1616
// The error rate for very negative ticks approaches zero, so there is no concern there
17-
MaxTickExp uint64 = 559_680
18-
MinPrice string = "0.000000000000000000000000495"
19-
MaxPrice string = "2020125331305056766452345.127500016657360222036663651"
17+
MaxTickExp uint64 = 529_715
18+
MinPrice string = "0.000000000000000000000009906"
19+
MaxPrice string = "100943872917137109121294.116592697013542139739189686"
2020
)
2121

2222
//go:embed precomputed_prices.gob
@@ -72,19 +72,25 @@ func BinarySearchPriceToTick(price math_utils.PrecDec) uint64 {
7272
right := MaxTickExp
7373

7474
// Binary search to find the closest precomputed value
75+
BinarySearch:
7576
for left < right {
7677
switch mid := (left + right) / 2; {
7778
case PrecomputedPrices[mid].Equal(price):
7879
return mid
80+
case right-left == 1:
81+
// Use bottom logic
82+
break BinarySearch
7983
case PrecomputedPrices[mid].LT(price):
8084
left = mid + 1
8185
default:
8286
right = mid - 1
8387

8488
}
8589
}
86-
87-
// If exact match is not found, return the upper bound
90+
// Round to the nearest tick or exact match
91+
if PrecomputedPrices[left].Sub(price).Abs().LT(PrecomputedPrices[right].Sub(price).Abs()) {
92+
return left
93+
}
8894
return right
8995
}
9096

x/dex/types/price_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package types_test
22

33
import (
4-
"errors"
54
"testing"
65

76
"github.com/stretchr/testify/require"
87

8+
math_utils "github.com/neutron-org/neutron/v8/utils/math"
99
"github.com/neutron-org/neutron/v8/x/dex/types"
1010
)
1111

1212
func TestCalcTickIndexFromPrice(t *testing.T) {
1313
for _, tc := range []struct {
1414
desc string
1515
tick int64
16+
err bool
1617
}{
1718
{
1819
desc: "0",
@@ -31,16 +32,16 @@ func TestCalcTickIndexFromPrice(t *testing.T) {
3132
tick: 100000,
3233
},
3334
{
34-
desc: "-100000",
35-
tick: -100000,
35+
desc: "-100051",
36+
tick: -100051,
3637
},
3738
{
38-
desc: "-100000",
39-
tick: -100000,
39+
desc: "-200100",
40+
tick: -2000100,
4041
},
4142
{
42-
desc: "-100000",
43-
tick: -100000,
43+
desc: "400000",
44+
tick: 400000,
4445
},
4546
{
4647
desc: "MaxTickExp",
@@ -53,23 +54,37 @@ func TestCalcTickIndexFromPrice(t *testing.T) {
5354
{
5455
desc: "GT MaxTickExp",
5556
tick: int64(types.MaxTickExp) + 1,
57+
err: true,
5658
},
5759
{
5860
desc: "LT MinTickExp",
5961
tick: -1*int64(types.MaxTickExp) - 1,
62+
err: true,
6063
},
6164
} {
6265
t.Run(tc.desc, func(t *testing.T) {
6366
price, err1 := types.CalcPrice(tc.tick)
6467
val, err2 := types.CalcTickIndexFromPrice(price)
65-
if errors.Is(err1, types.ErrTickOutsideRange) {
66-
require.ErrorIs(t, err2, types.ErrPriceOutsideRange)
68+
if err1 != nil {
69+
require.Error(t, err1)
70+
require.Error(t, err2)
6771
} else {
68-
// Only expected error is ErrTickOutsideRange.
6972
// If we are not outside the tick range we should TestCalcTickIndexFromPrice to never throw
73+
require.NoError(t, err1)
7074
require.NoError(t, err2)
7175
require.Equal(t, tc.tick, val)
7276
}
7377
})
7478
}
7579
}
80+
func TestPriceDups(t *testing.T) {
81+
prevPrice := math_utils.ZeroPrecDec()
82+
for i := 0; i >= int(types.MaxTickExp)*-1; i-- {
83+
price, err := types.CalcPrice(int64(i))
84+
require.NoError(t, err)
85+
if price.Equal(prevPrice) {
86+
t.Fatalf("Price (%v) %s is equal to previous price %s", i, price, prevPrice)
87+
}
88+
prevPrice = price
89+
}
90+
}

0 commit comments

Comments
 (0)