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

[CL]: Export Position_lock_id mappings to GenesisState #4912

Merged
merged 7 commits into from
Apr 16, 2023
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#4891](https://github.com/osmosis-labs/osmosis/pull/4891) Enable CORS by default on localosmosis
* [#4892](https://github.com/osmosis-labs/osmosis/pull/4847) Update Golang to 1.20
* [#4893](https://github.com/osmosis-labs/osmosis/pull/4893) Update alpine docker base image to `alpine:3.17`

* [#4912](https://github.com/osmosis-labs/osmosis/pull/4912) Export Position_lock_id mappings to GenesisState

### API breaks

* [#4336](https://github.com/osmosis-labs/osmosis/pull/4336) Move epochs module into its own go.mod
Expand Down
7 changes: 6 additions & 1 deletion proto/osmosis/concentrated-liquidity/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ message PoolData {
[ (gogoproto.nullable) = false ];
}

message PositionData {
Position position = 1;
uint64 lock_id = 2 [ (gogoproto.moretags) = "yaml:\"lock_id\"" ];
}

// GenesisState defines the concentrated liquidity module's genesis state.
message GenesisState {
// params are all the parameters of the module
Params params = 1 [ (gogoproto.nullable) = false ];
// pool data containining serialized pool struct and ticks.
repeated PoolData pool_data = 2 [ (gogoproto.nullable) = false ];

repeated Position positions = 3 [ (gogoproto.nullable) = false ];
repeated PositionData position_data = 3 [ (gogoproto.nullable) = false ];
Comment on lines +51 to +63
Copy link
Member

@czarcas7ic czarcas7ic Apr 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, it seems like the only reason we need to do this is because lock_id is the only externally linked data in all of the KVStore pairs. Maybe it makes sense for us to include underlying lock ID in the position struct after all...

Thoughts @p0mvn ? Just feels odd to make a new proto that links these two when it could just exist in the position struct itself. Not a huge deal tho

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove it from the second index in the first place? Was it the fact that some position IDs don't have a lock id associated with them? If that's the case, I think the current approach is better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it from the denom because of the complexity it introduced when attempting to create intermediary accounts with the base denom rather than the full denom (it would try and gather all locks with the base denom rather the full one). Also, removing it reduced much of the custom lock logic that was added previously

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will proceed with current approach and we can change anything that comes up in the future


uint64 next_position_id = 4
[ (gogoproto.moretags) = "yaml:\"next_position_id\"" ];
Expand Down
29 changes: 23 additions & 6 deletions x/concentrated-liquidity/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState genesis.GenesisState) {
}

// set positions for pool
for _, position := range genState.Positions {
if _, ok := seenPoolIds[position.PoolId]; !ok {
panic(fmt.Sprintf("found position with pool id (%d) but there is no pool with such id that exists", position.PoolId))
for _, positionWrapper := range genState.PositionData {
if _, ok := seenPoolIds[positionWrapper.Position.PoolId]; !ok {
panic(fmt.Sprintf("found position with pool id (%d) but there is no pool with such id that exists", positionWrapper.Position.PoolId))
}

// We hardcode the underlying lock id to 0, because genesisState should already hold the positionId to lockId connections
err := k.SetPosition(ctx, position.PoolId, sdk.MustAccAddressFromBech32(position.Address), position.LowerTick, position.UpperTick, position.JoinTime, position.Liquidity, position.PositionId, 0)
err := k.SetPosition(ctx, positionWrapper.Position.PoolId, sdk.MustAccAddressFromBech32(positionWrapper.Position.Address), positionWrapper.Position.LowerTick, positionWrapper.Position.UpperTick, positionWrapper.Position.JoinTime, positionWrapper.Position.Liquidity, positionWrapper.Position.PositionId, positionWrapper.LockId)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -152,10 +151,28 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *genesis.GenesisState {
panic(err)
}

positionData := make([]genesis.PositionData, 0, len(positions))
for _, position := range positions {
getPosition, err := k.GetPosition(ctx, position.PositionId)
if err != nil {
panic(err)
}

lockId, err := k.GetPositionIdToLock(ctx, position.PositionId)
if err != nil {
panic(err)
}

positionData = append(positionData, genesis.PositionData{
LockId: lockId,
Position: &getPosition,
})
}

return &genesis.GenesisState{
Params: k.GetParams(ctx),
PoolData: poolData,
Positions: positions,
PositionData: positionData,
NextPositionId: k.GetNextPositionId(ctx),
}
}
95 changes: 76 additions & 19 deletions x/concentrated-liquidity/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type singlePoolGenesisEntry struct {
pool model.Pool
tick []genesis.FullTick
positions []model.Position
positionData []genesis.PositionData
feeAccumValues genesis.AccumObject
incentiveAccumulators []genesis.AccumObject
incentiveRecords []types.IncentiveRecord
Expand Down Expand Up @@ -62,14 +62,14 @@ var (
}
)

func positionWithPoolId(position model.Position, poolId uint64) model.Position {
func positionWithPoolId(position model.Position, poolId uint64) *model.Position {
position.PoolId = poolId
return position
return &position
}

func withPositionId(position model.Position, positionId uint64) model.Position {
func withPositionId(position model.Position, positionId uint64) *model.Position {
position.PositionId = positionId
return position
return &position
}

func incentiveAccumsWithPoolId(poolId uint64) []genesis.AccumObject {
Expand Down Expand Up @@ -137,9 +137,8 @@ func setupGenesis(baseGenesis genesis.GenesisState, poolGenesisEntries []singleP
IncentivesAccumulators: poolGenesisEntry.incentiveAccumulators,
IncentiveRecords: poolGenesisEntry.incentiveRecords,
})
baseGenesis.Positions = append(baseGenesis.Positions, poolGenesisEntry.positions...)
baseGenesis.NextPositionId = uint64(len(poolGenesisEntry.positions))

baseGenesis.PositionData = append(baseGenesis.PositionData, poolGenesisEntry.positionData...)
baseGenesis.NextPositionId = uint64(len(poolGenesisEntry.positionData))
}
return baseGenesis
}
Expand All @@ -164,7 +163,7 @@ func (s *KeeperTestSuite) TestInitGenesis() {
genesis genesis.GenesisState
expectedPools []model.Pool
expectedTicksPerPoolId map[uint64][]genesis.FullTick
expectedPositions []model.Position
expectedPositionData []genesis.PositionData
expectedfeeAccumValues []genesis.AccumObject
expectedIncentiveRecords []types.IncentiveRecord
}{
Expand All @@ -177,7 +176,12 @@ func (s *KeeperTestSuite) TestInitGenesis() {
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), -10),
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), 10),
},
positions: []model.Position{testPositionModel},
positionData: []genesis.PositionData{
{
LockId: 1,
Position: &testPositionModel,
},
},
feeAccumValues: genesis.AccumObject{
Name: types.KeyFeePoolAccumulator(1),
AccumContent: &accum.AccumulatorContent{
Expand Down Expand Up @@ -221,7 +225,12 @@ func (s *KeeperTestSuite) TestInitGenesis() {
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), 10),
},
},
expectedPositions: []model.Position{testPositionModel},
expectedPositionData: []genesis.PositionData{
{
LockId: 1,
Position: &testPositionModel,
},
},
expectedfeeAccumValues: []genesis.AccumObject{
{
Name: types.KeyFeePoolAccumulator(1),
Expand Down Expand Up @@ -264,7 +273,12 @@ func (s *KeeperTestSuite) TestInitGenesis() {
tick: []genesis.FullTick{
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), -1234),
},
positions: []model.Position{testPositionModel},
positionData: []genesis.PositionData{
{
LockId: 1,
Position: &testPositionModel,
},
},
feeAccumValues: genesis.AccumObject{
Name: types.KeyFeePoolAccumulator(1),
AccumContent: &accum.AccumulatorContent{
Expand Down Expand Up @@ -293,7 +307,13 @@ func (s *KeeperTestSuite) TestInitGenesis() {
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), 0),
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), 999),
},
positions: []model.Position{withPositionId(positionWithPoolId(testPositionModel, 2), DefaultPositionId+1)},
positionData: []genesis.PositionData{
{
LockId: 2,
Position: withPositionId(*positionWithPoolId(testPositionModel, 2), DefaultPositionId+1),
},
},

feeAccumValues: genesis.AccumObject{
Name: types.KeyFeePoolAccumulator(2),
AccumContent: &accum.AccumulatorContent{
Expand Down Expand Up @@ -370,7 +390,16 @@ func (s *KeeperTestSuite) TestInitGenesis() {
MinUptime: testUptimeOne,
},
},
expectedPositions: []model.Position{testPositionModel, withPositionId(positionWithPoolId(testPositionModel, 2), DefaultPositionId+1)},
expectedPositionData: []genesis.PositionData{
{
LockId: 1,
Position: &testPositionModel,
},
{
LockId: 2,
Position: withPositionId(*positionWithPoolId(testPositionModel, 2), DefaultPositionId+1),
},
},
},
}

Expand Down Expand Up @@ -438,9 +467,22 @@ func (s *KeeperTestSuite) TestInitGenesis() {
// get all positions.
positions, err := clKeeper.GetAllPositions(ctx)
s.Require().NoError(err)
var positionData []genesis.PositionData
for _, position := range positions {
getPosition, err := clKeeper.GetPosition(ctx, position.PositionId)
s.Require().NoError(err)

lockId, err := clKeeper.GetPositionIdToLock(ctx, position.PositionId)
s.Require().NoError(err)

positionData = append(positionData, genesis.PositionData{
LockId: lockId,
Position: &getPosition,
})
}

// Validate positions
s.Require().Equal(tc.expectedPositions, positions)
s.Require().Equal(tc.expectedPositionData, positionData)

// Validate accum objects
s.Require().Equal(len(feeAccums), len(tc.expectedfeeAccumValues))
Expand Down Expand Up @@ -498,7 +540,12 @@ func (s *KeeperTestSuite) TestExportGenesis() {
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), -10),
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), 10),
},
positions: []model.Position{testPositionModel},
positionData: []genesis.PositionData{
{
LockId: 1,
Position: &testPositionModel,
},
},
feeAccumValues: genesis.AccumObject{
Name: types.KeyFeePoolAccumulator(poolOne.Id),
AccumContent: &accum.AccumulatorContent{
Expand Down Expand Up @@ -542,7 +589,12 @@ func (s *KeeperTestSuite) TestExportGenesis() {
tick: []genesis.FullTick{
withTickIndex(withPoolId(defaultFullTick, poolOne.Id), -1234),
},
positions: []model.Position{testPositionModel},
positionData: []genesis.PositionData{
{
LockId: 1,
Position: &testPositionModel,
},
},
feeAccumValues: genesis.AccumObject{
Name: types.KeyFeePoolAccumulator(poolOne.Id),
AccumContent: &accum.AccumulatorContent{
Expand Down Expand Up @@ -592,7 +644,12 @@ func (s *KeeperTestSuite) TestExportGenesis() {
MinUptime: testUptimeOne,
},
},
positions: []model.Position{withPositionId(positionWithPoolId(testPositionModel, 2), DefaultPositionId+1)},
positionData: []genesis.PositionData{
{
LockId: 2,
Position: withPositionId(*positionWithPoolId(testPositionModel, 2), DefaultPositionId+1),
},
},
},
}),
},
Expand Down Expand Up @@ -648,7 +705,7 @@ func (s *KeeperTestSuite) TestExportGenesis() {
}

// Validate positions.
s.Require().Equal(tc.genesis.Positions, actualExported.Positions)
s.Require().Equal(tc.genesis.PositionData, actualExported.PositionData)

// Validate next position id.
s.Require().Equal(tc.genesis.NextPositionId, actualExported.NextPositionId)
Expand Down
Loading