Skip to content

Commit

Permalink
[CL]: Export Position_lock_id mappings to GenesisState (#4912)
Browse files Browse the repository at this point in the history
* added wrapper

* improved test

* roman comments

* nits

* romans feedback

* added changelog
  • Loading branch information
stackman27 authored Apr 16, 2023
1 parent 97c9be1 commit 4f6f95f
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 90 deletions.
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 ];

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

0 comments on commit 4f6f95f

Please sign in to comment.