From 8e3f5df1be4adcbdd6b266861c4219b7fda46cb8 Mon Sep 17 00:00:00 2001 From: Julian Compagni Portis Date: Mon, 23 Sep 2024 17:17:30 -0400 Subject: [PATCH 1/6] Remove hardcoded dex pause logic --- Makefile | 2 +- x/dex/keeper/msg_server.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index a2a54ab7c..9c8a00e2d 100644 --- a/Makefile +++ b/Makefile @@ -178,7 +178,7 @@ test: test-unit test-all: check test-race test-cover test-unit: - @VERSION=$(VERSION) go test -mod=readonly -tags='ledger test_ledger_mock' `go list ./... | grep -v dex` + @VERSION=$(VERSION) go test -mod=readonly -tags='ledger test_ledger_mock' ./... test-race: @VERSION=$(VERSION) go test -mod=readonly -race -tags='ledger test_ledger_mock' ./... diff --git a/x/dex/keeper/msg_server.go b/x/dex/keeper/msg_server.go index 2b3bca666..1211cc57b 100644 --- a/x/dex/keeper/msg_server.go +++ b/x/dex/keeper/msg_server.go @@ -259,6 +259,12 @@ func (k MsgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam return &types.MsgUpdateParamsResponse{}, nil } -func (k MsgServer) AssertNotPaused(_ context.Context) error { - return types.ErrDexPaused +func (k MsgServer) AssertNotPaused(goCtx context.Context) error { + ctx := sdk.UnwrapSDKContext(goCtx) + paused := k.GetParams(ctx).Paused + + if paused { + return types.ErrDexPaused + } + return nil } From 0c6e58ad6d27b2b210a74ffeb3f17f61b55c3f13 Mon Sep 17 00:00:00 2001 From: Julian Compagni Portis Date: Mon, 23 Sep 2024 17:41:21 -0400 Subject: [PATCH 2/6] Add dex pause migration --- app/app.go | 4 +- app/upgrades/types.go | 2 + app/upgrades/v5.0.0/constants.go | 20 +++++++++ app/upgrades/v5.0.0/upgrades.go | 54 +++++++++++++++++++++++ app/upgrades/v5.0.0/upgrades_test.go | 64 ++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 app/upgrades/v5.0.0/constants.go create mode 100644 app/upgrades/v5.0.0/upgrades.go create mode 100644 app/upgrades/v5.0.0/upgrades_test.go diff --git a/app/app.go b/app/app.go index 0d105a861..e5f286eab 100644 --- a/app/app.go +++ b/app/app.go @@ -37,6 +37,7 @@ import ( servicemetrics "github.com/skip-mev/slinky/service/metrics" v401 "github.com/neutron-org/neutron/v4/app/upgrades/v4.0.1" + v500 "github.com/neutron-org/neutron/v4/app/upgrades/v5.0.0" "github.com/neutron-org/neutron/v4/x/globalfee" globalfeetypes "github.com/neutron-org/neutron/v4/x/globalfee/types" @@ -221,7 +222,7 @@ const ( ) var ( - Upgrades = []upgrades.Upgrade{v401.Upgrade} + Upgrades = []upgrades.Upgrade{v401.Upgrade, v500.Upgrade} // DefaultNodeHome default home directories for the application daemon DefaultNodeHome string @@ -1396,6 +1397,7 @@ func (app *App) setupUpgradeHandlers() { MarketmapKeeper: app.MarketMapKeeper, FeeMarketKeeper: app.FeeMarkerKeeper, DynamicfeesKeeper: app.DynamicFeesKeeper, + DexKeeper: &app.DexKeeper, GlobalFeeSubspace: app.GetSubspace(globalfee.ModuleName), CcvConsumerSubspace: app.GetSubspace(ccvconsumertypes.ModuleName), }, diff --git a/app/upgrades/types.go b/app/upgrades/types.go index d94491025..dfbce160c 100644 --- a/app/upgrades/types.go +++ b/app/upgrades/types.go @@ -21,6 +21,7 @@ import ( contractmanagerkeeper "github.com/neutron-org/neutron/v4/x/contractmanager/keeper" cronkeeper "github.com/neutron-org/neutron/v4/x/cron/keeper" + dexkeeper "github.com/neutron-org/neutron/v4/x/dex/keeper" feeburnerkeeper "github.com/neutron-org/neutron/v4/x/feeburner/keeper" icqkeeper "github.com/neutron-org/neutron/v4/x/interchainqueries/keeper" tokenfactorykeeper "github.com/neutron-org/neutron/v4/x/tokenfactory/keeper" @@ -64,6 +65,7 @@ type UpgradeKeepers struct { MarketmapKeeper *marketmapkeeper.Keeper FeeMarketKeeper *feemarketkeeper.Keeper DynamicfeesKeeper *dynamicfeeskeeper.Keeper + DexKeeper *dexkeeper.Keeper // subspaces GlobalFeeSubspace paramtypes.Subspace CcvConsumerSubspace paramtypes.Subspace diff --git a/app/upgrades/v5.0.0/constants.go b/app/upgrades/v5.0.0/constants.go new file mode 100644 index 000000000..1eeb344c5 --- /dev/null +++ b/app/upgrades/v5.0.0/constants.go @@ -0,0 +1,20 @@ +package v400 + +import ( + storetypes "cosmossdk.io/store/types" + + "github.com/neutron-org/neutron/v4/app/upgrades" +) + +const ( + // UpgradeName defines the on-chain upgrade name. + UpgradeName = "v5.0.0" +) + +var Upgrade = upgrades.Upgrade{ + UpgradeName: UpgradeName, + CreateUpgradeHandler: CreateUpgradeHandler, + StoreUpgrades: storetypes.StoreUpgrades{ + Added: []string{}, + }, +} diff --git a/app/upgrades/v5.0.0/upgrades.go b/app/upgrades/v5.0.0/upgrades.go new file mode 100644 index 000000000..648145982 --- /dev/null +++ b/app/upgrades/v5.0.0/upgrades.go @@ -0,0 +1,54 @@ +package v400 + +import ( + "context" + "fmt" + + upgradetypes "cosmossdk.io/x/upgrade/types" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/neutron-org/neutron/v4/app/upgrades" + dexkeeper "github.com/neutron-org/neutron/v4/x/dex/keeper" +) + +func CreateUpgradeHandler( + mm *module.Manager, + configurator module.Configurator, + keepers *upgrades.UpgradeKeepers, + _ upgrades.StoreKeys, + _ codec.Codec, +) upgradetypes.UpgradeHandler { + return func(c context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + ctx := sdk.UnwrapSDKContext(c) + + ctx.Logger().Info("Starting module migrations...") + vm, err := mm.RunMigrations(ctx, configurator, vm) + if err != nil { + return vm, err + } + + ctx.Logger().Info("Running dex upgrades...") + err = upgradeDexPause(ctx, *keepers.DexKeeper) + if err != nil { + return nil, err + } + + ctx.Logger().Info(fmt.Sprintf("Migration {%s} applied", UpgradeName)) + return vm, nil + } +} + +func upgradeDexPause(ctx sdk.Context, k dexkeeper.Keeper) error { + // Set the dex to paused + ctx.Logger().Info("Pausing dex...") + + params := k.GetParams(ctx) + params.Paused = true + k.SetParams(ctx, params) + + ctx.Logger().Info("Dex is paused ") + + return nil +} diff --git a/app/upgrades/v5.0.0/upgrades_test.go b/app/upgrades/v5.0.0/upgrades_test.go new file mode 100644 index 000000000..61d774aad --- /dev/null +++ b/app/upgrades/v5.0.0/upgrades_test.go @@ -0,0 +1,64 @@ +package v400_test + +import ( + "testing" + + "cosmossdk.io/math" + upgradetypes "cosmossdk.io/x/upgrade/types" + v500 "github.com/neutron-org/neutron/v4/app/upgrades/v5.0.0" + "github.com/neutron-org/neutron/v4/testutil/common/sample" + math_utils "github.com/neutron-org/neutron/v4/utils/math" + "github.com/stretchr/testify/suite" + + "github.com/neutron-org/neutron/v4/testutil" + dexkeeper "github.com/neutron-org/neutron/v4/x/dex/keeper" + dextypes "github.com/neutron-org/neutron/v4/x/dex/types" +) + +type UpgradeTestSuite struct { + testutil.IBCConnectionTestSuite +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(UpgradeTestSuite)) +} + +func (suite *UpgradeTestSuite) SetupTest() { + suite.IBCConnectionTestSuite.SetupTest() +} + +func (suite *UpgradeTestSuite) TestUpgradeDexPause() { + var ( + app = suite.GetNeutronZoneApp(suite.ChainA) + ctx = suite.ChainA.GetContext() + msgServer = dexkeeper.NewMsgServerImpl(app.DexKeeper) + ) + + params := app.DexKeeper.GetParams(ctx) + + suite.False(params.Paused) + + upgrade := upgradetypes.Plan{ + Name: v500.UpgradeName, + Info: "some text here", + Height: 100, + } + suite.NoError(app.UpgradeKeeper.ApplyUpgrade(ctx, upgrade)) + + params = app.DexKeeper.GetParams(ctx) + + suite.True(params.Paused) + + price := math_utils.OnePrecDec() + _, err := msgServer.PlaceLimitOrder(ctx, &dextypes.MsgPlaceLimitOrder{ + Creator: sample.AccAddress(), + Receiver: sample.AccAddress(), + TokenIn: "TokenA", + TokenOut: "TokenB", + LimitSellPrice: &price, + AmountIn: math.OneInt(), + }) + + suite.ErrorIs(err, dextypes.ErrDexPaused) + +} From 18751824bd9a2d93723524d9c1231a14c713fb7e Mon Sep 17 00:00:00 2001 From: Julian Compagni Portis Date: Mon, 23 Sep 2024 17:44:04 -0400 Subject: [PATCH 3/6] fmt --- app/upgrades/v5.0.0/upgrades.go | 7 +++++-- app/upgrades/v5.0.0/upgrades_test.go | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/upgrades/v5.0.0/upgrades.go b/app/upgrades/v5.0.0/upgrades.go index 648145982..eda525e31 100644 --- a/app/upgrades/v5.0.0/upgrades.go +++ b/app/upgrades/v5.0.0/upgrades.go @@ -46,9 +46,12 @@ func upgradeDexPause(ctx sdk.Context, k dexkeeper.Keeper) error { params := k.GetParams(ctx) params.Paused = true - k.SetParams(ctx, params) - ctx.Logger().Info("Dex is paused ") + if err := k.SetParams(ctx, params); err != nil { + return err + } + + ctx.Logger().Info("Dex is paused") return nil } diff --git a/app/upgrades/v5.0.0/upgrades_test.go b/app/upgrades/v5.0.0/upgrades_test.go index 61d774aad..c9e1da782 100644 --- a/app/upgrades/v5.0.0/upgrades_test.go +++ b/app/upgrades/v5.0.0/upgrades_test.go @@ -5,10 +5,11 @@ import ( "cosmossdk.io/math" upgradetypes "cosmossdk.io/x/upgrade/types" + "github.com/stretchr/testify/suite" + v500 "github.com/neutron-org/neutron/v4/app/upgrades/v5.0.0" "github.com/neutron-org/neutron/v4/testutil/common/sample" math_utils "github.com/neutron-org/neutron/v4/utils/math" - "github.com/stretchr/testify/suite" "github.com/neutron-org/neutron/v4/testutil" dexkeeper "github.com/neutron-org/neutron/v4/x/dex/keeper" @@ -60,5 +61,4 @@ func (suite *UpgradeTestSuite) TestUpgradeDexPause() { }) suite.ErrorIs(err, dextypes.ErrDexPaused) - } From a51c29a36daddcd8991a2028a605ccef674dadc0 Mon Sep 17 00:00:00 2001 From: Julian Compagni Portis Date: Mon, 23 Sep 2024 17:54:12 -0400 Subject: [PATCH 4/6] Only pause dex for mainnet --- app/upgrades/v5.0.0/upgrades.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/upgrades/v5.0.0/upgrades.go b/app/upgrades/v5.0.0/upgrades.go index eda525e31..debee5718 100644 --- a/app/upgrades/v5.0.0/upgrades.go +++ b/app/upgrades/v5.0.0/upgrades.go @@ -30,7 +30,11 @@ func CreateUpgradeHandler( } ctx.Logger().Info("Running dex upgrades...") - err = upgradeDexPause(ctx, *keepers.DexKeeper) + // Only pause dex for mainnet + if ctx.ChainID() == "neutron-1" { + err = upgradeDexPause(ctx, *keepers.DexKeeper) + } + if err != nil { return nil, err } From 8e40bbe41d48951ae52fa5d375854950b5bec63b Mon Sep 17 00:00:00 2001 From: Julian Compagni Portis Date: Mon, 23 Sep 2024 17:54:59 -0400 Subject: [PATCH 5/6] typo --- app/upgrades/v5.0.0/upgrades.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/upgrades/v5.0.0/upgrades.go b/app/upgrades/v5.0.0/upgrades.go index debee5718..9c5e46a57 100644 --- a/app/upgrades/v5.0.0/upgrades.go +++ b/app/upgrades/v5.0.0/upgrades.go @@ -33,10 +33,9 @@ func CreateUpgradeHandler( // Only pause dex for mainnet if ctx.ChainID() == "neutron-1" { err = upgradeDexPause(ctx, *keepers.DexKeeper) - } - - if err != nil { - return nil, err + if err != nil { + return nil, err + } } ctx.Logger().Info(fmt.Sprintf("Migration {%s} applied", UpgradeName)) From da18547bd0ba80485ce2ca111ee55eaf580304f3 Mon Sep 17 00:00:00 2001 From: Julian Compagni Portis Date: Mon, 23 Sep 2024 18:28:07 -0400 Subject: [PATCH 6/6] fix tests --- app/upgrades/v5.0.0/upgrades_test.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/app/upgrades/v5.0.0/upgrades_test.go b/app/upgrades/v5.0.0/upgrades_test.go index c9e1da782..0a5d07207 100644 --- a/app/upgrades/v5.0.0/upgrades_test.go +++ b/app/upgrades/v5.0.0/upgrades_test.go @@ -9,7 +9,6 @@ import ( v500 "github.com/neutron-org/neutron/v4/app/upgrades/v5.0.0" "github.com/neutron-org/neutron/v4/testutil/common/sample" - math_utils "github.com/neutron-org/neutron/v4/utils/math" "github.com/neutron-org/neutron/v4/testutil" dexkeeper "github.com/neutron-org/neutron/v4/x/dex/keeper" @@ -31,7 +30,7 @@ func (suite *UpgradeTestSuite) SetupTest() { func (suite *UpgradeTestSuite) TestUpgradeDexPause() { var ( app = suite.GetNeutronZoneApp(suite.ChainA) - ctx = suite.ChainA.GetContext() + ctx = suite.ChainA.GetContext().WithChainID("neutron-1") msgServer = dexkeeper.NewMsgServerImpl(app.DexKeeper) ) @@ -50,14 +49,16 @@ func (suite *UpgradeTestSuite) TestUpgradeDexPause() { suite.True(params.Paused) - price := math_utils.OnePrecDec() - _, err := msgServer.PlaceLimitOrder(ctx, &dextypes.MsgPlaceLimitOrder{ - Creator: sample.AccAddress(), - Receiver: sample.AccAddress(), - TokenIn: "TokenA", - TokenOut: "TokenB", - LimitSellPrice: &price, - AmountIn: math.OneInt(), + _, err := msgServer.Deposit(ctx, &dextypes.MsgDeposit{ + Creator: sample.AccAddress(), + Receiver: sample.AccAddress(), + TokenA: "TokenA", + TokenB: "TokenB", + TickIndexesAToB: []int64{1}, + Fees: []uint64{1}, + AmountsA: []math.Int{math.OneInt()}, + AmountsB: []math.Int{math.ZeroInt()}, + Options: []*dextypes.DepositOptions{{}}, }) suite.ErrorIs(err, dextypes.ErrDexPaused)