Skip to content

Commit

Permalink
chore: import cycle resolution with migration records (#5596)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn authored Jun 22, 2023
1 parent df4c956 commit 0fbeed2
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 155 deletions.
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.DistrKeeper,
appKeepers.PoolManagerKeeper,
appKeepers.EpochsKeeper,
appKeepers.SuperfluidKeeper,
appKeepers.GAMMKeeper,
)
appKeepers.PoolIncentivesKeeper = &poolIncentivesKeeper
appKeepers.PoolManagerKeeper.SetPoolIncentivesKeeper(appKeepers.PoolIncentivesKeeper)
Expand Down
6 changes: 3 additions & 3 deletions app/upgrades/v16/concentrated_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/osmosis-labs/osmosis/v16/app/keepers"
clmodel "github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/model"
gammkeeper "github.com/osmosis-labs/osmosis/v16/x/gamm/keeper"
gammtypes "github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
"github.com/osmosis-labs/osmosis/v16/x/poolmanager"
poolmanagertypes "github.com/osmosis-labs/osmosis/v16/x/poolmanager/types"
)
Expand Down Expand Up @@ -70,8 +70,8 @@ func createCanonicalConcentratedLiquidityPoolAndMigrationLink(ctx sdk.Context, c

// Set the migration link in x/gamm.
// This will also migrate the CFMM distribution records to point to the new CL pool.
err = keepers.GAMMKeeper.OverwriteMigrationRecordsAndRedirectDistrRecords(ctx, gammtypes.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammtypes.BalancerToConcentratedPoolLink{
err = keepers.GAMMKeeper.OverwriteMigrationRecordsAndRedirectDistrRecords(ctx, gammmigration.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammmigration.BalancerToConcentratedPoolLink{
{
BalancerPoolId: cfmmPoolId,
ClPoolId: concentratedPool.GetId(),
Expand Down
6 changes: 3 additions & 3 deletions app/upgrades/v16/concentrated_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/osmosis-labs/osmosis/v16/app/apptesting"
v16 "github.com/osmosis-labs/osmosis/v16/app/upgrades/v16"
gammtypes "github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
poolincentivestypes "github.com/osmosis-labs/osmosis/v16/x/pool-incentives/types"
)

Expand Down Expand Up @@ -244,8 +244,8 @@ func (suite *ConcentratedUpgradeTestSuite) TestCreateCanonicalConcentratedLiquid
// Validate migration record.
migrationInfo, err := suite.App.GAMMKeeper.GetAllMigrationInfo(suite.Ctx)
suite.Require().NoError(err)
suite.Require().Equal(migrationInfo, gammtypes.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammtypes.BalancerToConcentratedPoolLink{
suite.Require().Equal(migrationInfo, gammmigration.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammmigration.BalancerToConcentratedPoolLink{
{
BalancerPoolId: balancerId,
ClPoolId: clPoolInState.GetId(),
Expand Down
2 changes: 1 addition & 1 deletion proto/osmosis/gamm/v1beta1/shared.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/osmosis-labs/osmosis/v16/x/gamm/types";
option go_package = "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration";

// MigrationRecords contains all the links between balancer and concentrated
// pools
Expand Down
6 changes: 3 additions & 3 deletions x/concentrated-liquidity/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
clmodel "github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/model"
"github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/types"
"github.com/osmosis-labs/osmosis/v16/x/gamm/pool-models/balancer"
gammtypes "github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
)

type BenchTestSuite struct {
Expand Down Expand Up @@ -102,8 +102,8 @@ func runBenchmark(b *testing.B, testFunc func(b *testing.B, s *BenchTestSuite, p
gammKeeper := s.App.GAMMKeeper

// Create a link between the balancer and cl pool.
record := gammtypes.BalancerToConcentratedPoolLink{BalancerPoolId: gammPoolId, ClPoolId: clPoolId}
err = gammKeeper.ReplaceMigrationRecords(s.Ctx, []gammtypes.BalancerToConcentratedPoolLink{record})
record := gammmigration.BalancerToConcentratedPoolLink{BalancerPoolId: gammPoolId, ClPoolId: clPoolId}
err = gammKeeper.ReplaceMigrationRecords(s.Ctx, []gammmigration.BalancerToConcentratedPoolLink{record})
s.Require().NoError(err)

_, err = gammKeeper.GetLinkedConcentratedPoolID(s.Ctx, gammPoolId)
Expand Down
17 changes: 9 additions & 8 deletions x/concentrated-liquidity/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/types"
"github.com/osmosis-labs/osmosis/v16/x/gamm/pool-models/balancer"
gammtypes "github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
poolincentivestypes "github.com/osmosis-labs/osmosis/v16/x/pool-incentives/types"
)

Expand Down Expand Up @@ -1019,8 +1020,8 @@ func (s *KeeperTestSuite) TestUpdateUptimeAccumulatorsToNow() {
// Create balancer pool and bond its shares
balancerPoolId = s.setupBalancerPoolWithFractionLocked(tc.canonicalBalancerPoolAssets, sdk.OneDec())
s.App.GAMMKeeper.OverwriteMigrationRecordsAndRedirectDistrRecords(s.Ctx,
gammtypes.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammtypes.BalancerToConcentratedPoolLink{
gammmigration.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammmigration.BalancerToConcentratedPoolLink{
{BalancerPoolId: balancerPoolId, ClPoolId: clPool.GetId()},
},
},
Expand Down Expand Up @@ -3589,8 +3590,8 @@ func (s *KeeperTestSuite) TestPrepareBalancerPoolAsFullRange() {
balancerPoolId = 0
} else {
s.App.GAMMKeeper.OverwriteMigrationRecordsAndRedirectDistrRecords(s.Ctx,
gammtypes.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammtypes.BalancerToConcentratedPoolLink{
gammmigration.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammmigration.BalancerToConcentratedPoolLink{
{BalancerPoolId: balancerPoolId, ClPoolId: clPool.GetId()},
},
},
Expand Down Expand Up @@ -3723,8 +3724,8 @@ func (s *KeeperTestSuite) TestPrepareBalancerPoolAsFullRangeWithNonExistentPools
}

s.App.GAMMKeeper.OverwriteMigrationRecordsAndRedirectDistrRecords(s.Ctx,
gammtypes.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammtypes.BalancerToConcentratedPoolLink{
gammmigration.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammmigration.BalancerToConcentratedPoolLink{
{BalancerPoolId: balancerPoolId, ClPoolId: clPool.GetId()},
},
},
Expand Down Expand Up @@ -3891,8 +3892,8 @@ func (s *KeeperTestSuite) TestClaimAndResetFullRangeBalancerPool() {

// Link the balancer and CL pools
s.App.GAMMKeeper.OverwriteMigrationRecordsAndRedirectDistrRecords(s.Ctx,
gammtypes.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammtypes.BalancerToConcentratedPoolLink{
gammmigration.MigrationRecords{
BalancerToConcentratedPoolLinks: []gammmigration.BalancerToConcentratedPoolLink{
{BalancerPoolId: balancerPoolId, ClPoolId: clPoolId},
},
})
Expand Down
7 changes: 4 additions & 3 deletions x/gamm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/osmosis-labs/osmosis/v16/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v16/x/gamm/pool-models/stableswap"
"github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
poolmanagertypes "github.com/osmosis-labs/osmosis/v16/x/poolmanager/types"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -630,7 +631,7 @@ func ParseCoinsNoSort(coinsStr string) (sdk.Coins, error) {
return sdk.NormalizeCoins(decCoins), nil
}

func parseMigrationRecords(cmd *cobra.Command) ([]types.BalancerToConcentratedPoolLink, error) {
func parseMigrationRecords(cmd *cobra.Command) ([]gammmigration.BalancerToConcentratedPoolLink, error) {
assetsStr, err := cmd.Flags().GetString(FlagMigrationRecords)
if err != nil {
return nil, err
Expand All @@ -642,7 +643,7 @@ func parseMigrationRecords(cmd *cobra.Command) ([]types.BalancerToConcentratedPo
return nil, errors.New("migration records should be a list of balancer pool id and concentrated pool id pairs")
}

replaceMigrations := []types.BalancerToConcentratedPoolLink{}
replaceMigrations := []gammmigration.BalancerToConcentratedPoolLink{}
i := 0
for i < len(assets) {
balancerPoolId, err := strconv.Atoi(assets[i])
Expand All @@ -654,7 +655,7 @@ func parseMigrationRecords(cmd *cobra.Command) ([]types.BalancerToConcentratedPo
return nil, err
}

replaceMigrations = append(replaceMigrations, types.BalancerToConcentratedPoolLink{
replaceMigrations = append(replaceMigrations, gammmigration.BalancerToConcentratedPoolLink{
BalancerPoolId: uint64(balancerPoolId),
ClPoolId: uint64(clPoolId),
})
Expand Down
3 changes: 2 additions & 1 deletion x/gamm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
)

// InitGenesis initializes the x/gamm module's state from a provided genesis
Expand Down Expand Up @@ -36,7 +37,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState, unpack
k.setTotalLiquidity(ctx, liquidity)

if genState.MigrationRecords == nil {
k.SetMigrationRecords(ctx, types.MigrationRecords{})
k.SetMigrationRecords(ctx, gammmigration.MigrationRecords{})
} else {
k.SetMigrationRecords(ctx, *genState.MigrationRecords)
}
Expand Down
3 changes: 2 additions & 1 deletion x/gamm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

"github.com/osmosis-labs/osmosis/v16/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
)

var DefaultMigrationRecords = types.MigrationRecords{BalancerToConcentratedPoolLinks: []types.BalancerToConcentratedPoolLink{
var DefaultMigrationRecords = gammmigration.MigrationRecords{BalancerToConcentratedPoolLinks: []gammmigration.BalancerToConcentratedPoolLink{
{BalancerPoolId: 1, ClPoolId: 4},
{BalancerPoolId: 2, ClPoolId: 5},
{BalancerPoolId: 3, ClPoolId: 6},
Expand Down
25 changes: 13 additions & 12 deletions x/gamm/keeper/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/osmosis-labs/osmosis/osmoutils"
cltypes "github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/types"
"github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
poolmanagertypes "github.com/osmosis-labs/osmosis/v16/x/poolmanager/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
Expand Down Expand Up @@ -57,17 +58,17 @@ func (k Keeper) MigrateUnlockedPositionFromBalancerToConcentrated(ctx sdk.Contex

// GetAllMigrationInfo gets all existing links between Balancer Pool and Concentrated Pool,
// wraps and returns them in `MigrationRecords`.
func (k Keeper) GetAllMigrationInfo(ctx sdk.Context) (types.MigrationRecords, error) {
func (k Keeper) GetAllMigrationInfo(ctx sdk.Context) (gammmigration.MigrationRecords, error) {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixMigrationInfoBalancerPool)

iter := prefixStore.Iterator(nil, nil)
defer iter.Close()

balancerToClPoolLinks := []types.BalancerToConcentratedPoolLink{}
balancerToClPoolLinks := []gammmigration.BalancerToConcentratedPoolLink{}
for ; iter.Valid(); iter.Next() {
// balancer Pool Id
balancerToClPoolLink := types.BalancerToConcentratedPoolLink{}
balancerToClPoolLink := gammmigration.BalancerToConcentratedPoolLink{}
balancerToClPoolLink.BalancerPoolId = sdk.BigEndianToUint64(iter.Key())

// concentrated Pool Id
Expand All @@ -76,7 +77,7 @@ func (k Keeper) GetAllMigrationInfo(ctx sdk.Context) (types.MigrationRecords, er
balancerToClPoolLinks = append(balancerToClPoolLinks, balancerToClPoolLink)
}

migrationRecords := types.MigrationRecords{}
migrationRecords := gammmigration.MigrationRecords{}
migrationRecords.BalancerToConcentratedPoolLinks = balancerToClPoolLinks
return migrationRecords, nil
}
Expand Down Expand Up @@ -112,7 +113,7 @@ func (k Keeper) GetLinkedBalancerPoolID(ctx sdk.Context, concentratedPoolId uint
// OverwriteMigrationRecordsAndRedirectDistrRecords sets the balancer to gamm pool migration info to the store and deletes all existing records
// migrationInfo in state is completely overwitten by the given migrationInfo.
// Additionally, the distribution record for the balancer pool is modified to redirect incentives to the new concentrated pool.
func (k Keeper) OverwriteMigrationRecordsAndRedirectDistrRecords(ctx sdk.Context, migrationInfo types.MigrationRecords) error {
func (k Keeper) OverwriteMigrationRecordsAndRedirectDistrRecords(ctx sdk.Context, migrationInfo gammmigration.MigrationRecords) error {
store := ctx.KVStore(k.storeKey)

// delete all existing migration records
Expand All @@ -137,7 +138,7 @@ func (k Keeper) OverwriteMigrationRecordsAndRedirectDistrRecords(ctx sdk.Context
}

// SetMigrationRecords is used in initGenesis, setting the balancer to gamm pool migration info in store.
func (k Keeper) SetMigrationRecords(ctx sdk.Context, migrationInfo types.MigrationRecords) {
func (k Keeper) SetMigrationRecords(ctx sdk.Context, migrationInfo gammmigration.MigrationRecords) {
store := ctx.KVStore(k.storeKey)

for _, balancerToCLPoolLink := range migrationInfo.BalancerToConcentratedPoolLinks {
Expand Down Expand Up @@ -198,7 +199,7 @@ func (k Keeper) redirectDistributionRecord(ctx sdk.Context, cfmmPoolId, clPoolId
// 3) the balancer pool has exactly two tokens
// 4) the denoms of the tokens in the balancer pool match the denoms of the tokens in the gamm pool
// It also reorders records from lowest to highest balancer pool ID if they are not provided in order already.
func (k Keeper) validateRecords(ctx sdk.Context, records []types.BalancerToConcentratedPoolLink) error {
func (k Keeper) validateRecords(ctx sdk.Context, records []gammmigration.BalancerToConcentratedPoolLink) error {
lastBalancerPoolID := uint64(0)
balancerIdFlags := make(map[uint64]bool, len(records))
clIdFlags := make(map[uint64]bool, len(records))
Expand Down Expand Up @@ -284,7 +285,7 @@ func (k Keeper) validateRecords(ctx sdk.Context, records []types.BalancerToConce

// ReplaceMigrationRecords gets the current migration records and replaces it in its entirety with the provided records.
// It is checked for no err when a proposal is made, and executed when a proposal passes.
func (k Keeper) ReplaceMigrationRecords(ctx sdk.Context, records []types.BalancerToConcentratedPoolLink) error {
func (k Keeper) ReplaceMigrationRecords(ctx sdk.Context, records []gammmigration.BalancerToConcentratedPoolLink) error {
err := k.validateRecords(ctx, records)
if err != nil {
return err
Expand All @@ -307,13 +308,13 @@ func (k Keeper) ReplaceMigrationRecords(ctx sdk.Context, records []types.Balance

// UpdateMigrationRecords gets the current migration records and only updates the records that are provided.
// It is checked for no err when a proposal is made, and executed when a proposal passes.
func (k Keeper) UpdateMigrationRecords(ctx sdk.Context, records []types.BalancerToConcentratedPoolLink) error {
func (k Keeper) UpdateMigrationRecords(ctx sdk.Context, records []gammmigration.BalancerToConcentratedPoolLink) error {
err := k.validateRecords(ctx, records)
if err != nil {
return err
}

recordsMap := make(map[uint64]types.BalancerToConcentratedPoolLink, len(records))
recordsMap := make(map[uint64]gammmigration.BalancerToConcentratedPoolLink, len(records))

// Set up a map of the existing records
migrationInfos, err := k.GetAllMigrationInfo(ctx)
Expand All @@ -329,7 +330,7 @@ func (k Keeper) UpdateMigrationRecords(ctx sdk.Context, records []types.Balancer
recordsMap[record.BalancerPoolId] = record
}

newRecords := []types.BalancerToConcentratedPoolLink{}
newRecords := []gammmigration.BalancerToConcentratedPoolLink{}

// Iterate through the map and add all the records to a new list
// if the clPoolId is 0, we remove the entire record
Expand All @@ -346,7 +347,7 @@ func (k Keeper) UpdateMigrationRecords(ctx sdk.Context, records []types.Balancer

// We now have a list of all previous records, as well as records that have been updated.
// We can now remove all previous records and replace them with the new ones.
err = k.OverwriteMigrationRecordsAndRedirectDistrRecords(ctx, types.MigrationRecords{
err = k.OverwriteMigrationRecordsAndRedirectDistrRecords(ctx, gammmigration.MigrationRecords{
BalancerToConcentratedPoolLinks: newRecords,
})
if err != nil {
Expand Down
Loading

0 comments on commit 0fbeed2

Please sign in to comment.