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 state mappings to GenesisState #4910

Closed
wants to merge 2 commits into from
Closed
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
38 changes: 37 additions & 1 deletion proto/osmosis/concentrated-liquidity/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ message PoolData {
[ (gogoproto.nullable) = false ];
}

message PositionIdToPosition {
uint64 position_id = 1 [ (gogoproto.moretags) = "yaml:\"position_id\"" ];
Position position = 2 [ (gogoproto.nullable) = false ];
}

message AddressToPositionId {
string address = 1 [ (gogoproto.moretags) = "yaml:\"address\"" ];
uint64 pool_id = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
uint64 position_id = 3 [ (gogoproto.moretags) = "yaml:\"position_id\"" ];
Position position = 4 [ (gogoproto.nullable) = false ];
}

// one poolId can hav many position ids
message PoolIdToPositionIds {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
repeated uint64 position_ids = 2
[ (gogoproto.moretags) = "yaml:\"position_id\"" ];
}

message PositionIdToLockId {
uint64 position_id = 1 [ (gogoproto.moretags) = "yaml:\"position_id\"" ];
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
Expand All @@ -57,7 +81,19 @@ message GenesisState {

repeated Position positions = 3 [ (gogoproto.nullable) = false ];

uint64 next_position_id = 4
repeated PositionIdToPosition position_id_to_position = 4
[ (gogoproto.nullable) = false ];

repeated AddressToPositionId address_to_position_id = 5
[ (gogoproto.nullable) = false ];

repeated PoolIdToPositionIds pool_id_to_position_ids = 6
[ (gogoproto.nullable) = false ];

repeated PositionIdToLockId position_id_to_lock_id = 7
[ (gogoproto.nullable) = false ];

uint64 next_position_id = 8
[ (gogoproto.moretags) = "yaml:\"next_position_id\"" ];
}

Expand Down
4 changes: 4 additions & 0 deletions x/concentrated-liquidity/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func (k Keeper) GetAllPositions(ctx sdk.Context) ([]model.Position, error) {
return k.getAllPositions(ctx)
}

func (k Keeper) GetAllPositionIdsForPoolId(ctx sdk.Context, poolId uint64) ([]uint64, error) {
return k.getAllPositionIdsForPoolId(ctx, poolId)
}

func (k Keeper) UpdatePoolForSwap(ctx sdk.Context, pool types.ConcentratedPoolExtension, sender sdk.AccAddress, tokenIn sdk.Coin, tokenOut sdk.Coin, newCurrentTick sdk.Int, newLiquidity sdk.Dec, newSqrtPrice sdk.Dec) error {
return k.updatePoolForSwap(ctx, pool, sender, tokenIn, tokenOut, newCurrentTick, newLiquidity, newSqrtPrice)
}
Expand Down
33 changes: 29 additions & 4 deletions x/concentrated-liquidity/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *genesis.GenesisState {
}

poolData := make([]genesis.PoolData, 0, len(pools))
var poolIdToPositionIds []genesis.PoolIdToPositionIds

for _, poolI := range pools {
poolI := poolI
Expand Down Expand Up @@ -145,17 +146,41 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *genesis.GenesisState {
IncentivesAccumulators: incentivesAccumObject,
IncentiveRecords: incentiveRecordsForPool,
})

positionids, err := k.getAllPositionIdsForPoolId(ctx, poolId)
if err != nil {
panic(err)
}
poolIdToPositionIds = append(poolIdToPositionIds, genesis.PoolIdToPositionIds{
PoolId: poolId,
PositionIds: positionids,
})
}

positions, err := k.getAllPositions(ctx)
if err != nil {
panic(err)
}

var positionIdToPosition []genesis.PositionIdToPosition
for _, position := range positions {
getPosition, err := k.GetPosition(ctx, position.PositionId)
if err != nil {
panic(err)
}

positionIdToPosition = append(positionIdToPosition, genesis.PositionIdToPosition{
PositionId: getPosition.GetPositionId(),
Position: getPosition,
})
}

return &genesis.GenesisState{
Params: k.GetParams(ctx),
PoolData: poolData,
Positions: positions,
NextPositionId: k.GetNextPositionId(ctx),
Params: k.GetParams(ctx),
PoolData: poolData,
Positions: positions,
PositionIdToPosition: positionIdToPosition,
PoolIdToPositionIds: poolIdToPositionIds,
NextPositionId: k.GetNextPositionId(ctx),
}
}
167 changes: 159 additions & 8 deletions x/concentrated-liquidity/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type singlePoolGenesisEntry struct {
feeAccumValues genesis.AccumObject
incentiveAccumulators []genesis.AccumObject
incentiveRecords []types.IncentiveRecord
positionIdToPosition []genesis.PositionIdToPosition
poolIdToPositionIds []genesis.PoolIdToPositionIds
}

var (
Expand Down Expand Up @@ -60,6 +62,28 @@ var (
UpperTick: 100,
JoinTime: defaultBlockTime,
}

testPositionIdToPosition = genesis.PositionIdToPosition{
PositionId: 1,
Position: testPositionModel,
}

testAddressToPositionId = genesis.AddressToPositionId{
Address: testAddressOne.String(),
PoolId: defaultPoolId,
PositionId: 1,
Position: testPositionModel,
}

testPoolIdToPositionId = genesis.PoolIdToPositionIds{
PoolId: defaultPoolId,
PositionIds: []uint64{1},
}

testPositionIdToLockId = genesis.PositionIdToLockId{
PositionId: 1,
LockId: 1,
}
)

func positionWithPoolId(position model.Position, poolId uint64) model.Position {
Expand Down Expand Up @@ -138,6 +162,8 @@ func setupGenesis(baseGenesis genesis.GenesisState, poolGenesisEntries []singleP
IncentiveRecords: poolGenesisEntry.incentiveRecords,
})
baseGenesis.Positions = append(baseGenesis.Positions, poolGenesisEntry.positions...)
baseGenesis.PositionIdToPosition = append(baseGenesis.PositionIdToPosition, poolGenesisEntry.positionIdToPosition...)
baseGenesis.PoolIdToPositionIds = append(baseGenesis.PoolIdToPositionIds, poolGenesisEntry.poolIdToPositionIds...)
baseGenesis.NextPositionId = uint64(len(poolGenesisEntry.positions))

}
Expand All @@ -160,13 +186,15 @@ func (s *KeeperTestSuite) TestInitGenesis() {
defaultTime2 := time.Unix(300, 100)

testCase := []struct {
name string
genesis genesis.GenesisState
expectedPools []model.Pool
expectedTicksPerPoolId map[uint64][]genesis.FullTick
expectedPositions []model.Position
expectedfeeAccumValues []genesis.AccumObject
expectedIncentiveRecords []types.IncentiveRecord
name string
genesis genesis.GenesisState
expectedPools []model.Pool
expectedTicksPerPoolId map[uint64][]genesis.FullTick
expectedPositions []model.Position
expectedfeeAccumValues []genesis.AccumObject
expectedIncentiveRecords []types.IncentiveRecord
expectedPositionIdToPosition []genesis.PositionIdToPosition
expectedPoolIdToPositionIds []genesis.PoolIdToPositionIds
}{
{
name: "one pool, one position, two ticks, one accumulator, two incentive records",
Expand Down Expand Up @@ -210,6 +238,18 @@ func (s *KeeperTestSuite) TestInitGenesis() {
MinUptime: testUptimeOne,
},
},
positionIdToPosition: []genesis.PositionIdToPosition{
{
PositionId: 1,
Position: testPositionModel,
},
},
poolIdToPositionIds: []genesis.PoolIdToPositionIds{
{
PoolId: defaultPoolId,
PositionIds: []uint64{1},
},
},
},
}),
expectedPools: []model.Pool{
Expand Down Expand Up @@ -255,6 +295,18 @@ func (s *KeeperTestSuite) TestInitGenesis() {
MinUptime: testUptimeOne,
},
},
expectedPositionIdToPosition: []genesis.PositionIdToPosition{
{
PositionId: uint64(1),
Position: testPositionModel,
},
},
expectedPoolIdToPositionIds: []genesis.PoolIdToPositionIds{
{
PoolId: defaultPoolId,
PositionIds: []uint64{1},
},
},
},
{
name: "two pools, two positions, one tick pool one, two ticks pool two, two accumulators, one incentive records each",
Expand Down Expand Up @@ -286,6 +338,18 @@ func (s *KeeperTestSuite) TestInitGenesis() {
MinUptime: testUptimeOne,
},
},
positionIdToPosition: []genesis.PositionIdToPosition{
{
PositionId: uint64(1),
Position: testPositionModel,
},
},
poolIdToPositionIds: []genesis.PoolIdToPositionIds{
{
PoolId: defaultPoolId,
PositionIds: []uint64{1},
},
},
},
{
pool: *poolTwo,
Expand Down Expand Up @@ -315,6 +379,22 @@ func (s *KeeperTestSuite) TestInitGenesis() {
MinUptime: testUptimeOne,
},
},
positionIdToPosition: []genesis.PositionIdToPosition{
{
PositionId: uint64(1),
Position: testPositionModel,
},
},
poolIdToPositionIds: []genesis.PoolIdToPositionIds{
{
PoolId: defaultPoolId,
PositionIds: []uint64{1},
},
{
PoolId: uint64(2),
PositionIds: []uint64{2},
},
},
},
}),
expectedPools: []model.Pool{
Expand Down Expand Up @@ -371,6 +451,22 @@ func (s *KeeperTestSuite) TestInitGenesis() {
},
},
expectedPositions: []model.Position{testPositionModel, withPositionId(positionWithPoolId(testPositionModel, 2), DefaultPositionId+1)},
expectedPositionIdToPosition: []genesis.PositionIdToPosition{
{
PositionId: uint64(1),
Position: testPositionModel,
},
},
expectedPoolIdToPositionIds: []genesis.PoolIdToPositionIds{
{
PoolId: defaultPoolId,
PositionIds: []uint64{1},
},
{
PoolId: uint64(2),
PositionIds: []uint64{2},
},
},
},
}

Expand Down Expand Up @@ -463,6 +559,24 @@ func (s *KeeperTestSuite) TestInitGenesis() {
s.Require().Equal(incentiveRecord.IncentiveRecordBody.RemainingAmount.String(), tc.expectedIncentiveRecords[i].IncentiveRecordBody.RemainingAmount.String())
s.Require().True(incentiveRecord.IncentiveRecordBody.StartTime.Equal(tc.expectedIncentiveRecords[i].IncentiveRecordBody.StartTime))
}

// validate PositionIdToPosition
for _, expPosition := range tc.expectedPositionIdToPosition {
getPosition, err := clKeeper.GetPosition(ctx, baseCase.positionId)
s.Require().NoError(err)

s.Require().Equal(expPosition.PositionId, baseCase.positionId)
s.Require().Equal(expPosition.Position, getPosition)
}

// validate PositionIdToPosition
for i, clPool := range clPoolsAfterInitialization {
poolIds, err := clKeeper.GetAllPositionIdsForPoolId(ctx, clPool.GetId())
s.Require().NoError(err)

s.Require().Equal(tc.expectedPoolIdToPositionIds[i].PositionIds, poolIds)
}

// Validate next position id.
s.Require().Equal(tc.genesis.NextPositionId, clKeeper.GetNextPositionId(ctx))
})
Expand Down Expand Up @@ -531,6 +645,18 @@ func (s *KeeperTestSuite) TestExportGenesis() {
MinUptime: testUptimeOne,
},
},
positionIdToPosition: []genesis.PositionIdToPosition{
{
PositionId: uint64(1),
Position: testPositionModel,
},
},
poolIdToPositionIds: []genesis.PoolIdToPositionIds{
{
PoolId: uint64(1),
PositionIds: []uint64{1},
},
},
},
}),
},
Expand Down Expand Up @@ -593,6 +719,26 @@ func (s *KeeperTestSuite) TestExportGenesis() {
},
},
positions: []model.Position{withPositionId(positionWithPoolId(testPositionModel, 2), DefaultPositionId+1)},
positionIdToPosition: []genesis.PositionIdToPosition{
{
PositionId: uint64(1),
Position: testPositionModel,
},
{
PositionId: uint64(2),
Position: withPositionId(positionWithPoolId(testPositionModel, 2), defaultPoolId+1),
},
},
poolIdToPositionIds: []genesis.PoolIdToPositionIds{
{
PoolId: uint64(1),
PositionIds: []uint64{1},
},
{
PoolId: uint64(2),
PositionIds: []uint64{2},
},
},
},
}),
},
Expand Down Expand Up @@ -644,14 +790,19 @@ func (s *KeeperTestSuite) TestExportGenesis() {
s.Require().Equal(incentiveRecord.IncentiveRecordBody.RemainingAmount.String(), expectedPoolData.IncentiveRecords[i].IncentiveRecordBody.RemainingAmount.String())
s.Require().True(incentiveRecord.IncentiveRecordBody.StartTime.Equal(expectedPoolData.IncentiveRecords[i].IncentiveRecordBody.StartTime))
}

}

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

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

// Validate PositionIdToPosition
s.Require().Equal(tc.genesis.PositionIdToPosition, actualExported.PositionIdToPosition)

// Validate PoolIdToPositionIds
s.Require().Equal(tc.genesis.PoolIdToPositionIds, actualExported.PoolIdToPositionIds)
})
}
}
Expand Down
9 changes: 9 additions & 0 deletions x/concentrated-liquidity/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func (k Keeper) getAllPositions(ctx sdk.Context) ([]model.Position, error) {
ctx.KVStore(k.storeKey), types.PositionIdPrefix, ParsePositionFromBz)
}

// getAllPositionIdsForPoolId gets all the position for a specific poolId.
func (k Keeper) getAllPositionIdsForPoolId(ctx sdk.Context, poolId uint64) ([]uint64, error) {
parse := func(bz []byte) (uint64, error) {
return sdk.BigEndianToUint64(bz), nil
}

return osmoutils.GatherValuesFromStorePrefix(ctx.KVStore(k.storeKey), types.KeyPoolPosition(poolId), parse)
}

// ParseLiquidityFromBz parses and returns a position's liquidity from a byte array.
// Returns an error if the byte array is empty.
// Returns an error if fails to parse.
Expand Down
Loading