Skip to content

Commit c09cafd

Browse files
committed
fix: 🎨 move to bottom function invalid orderId -> count relationship
1 parent c740a00 commit c09cafd

7 files changed

Lines changed: 145 additions & 88 deletions

File tree

modules/apps/100-atomic-swap/keeper/atomic_order.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,17 @@ func GetBidIDFromBytes(bz []byte) uint64 {
123123
}
124124

125125
func (k Keeper) MoveOrderToBottom(ctx sdk.Context, orderId string) error {
126-
// Step 1: Retrieve the item based on the given ID.
126+
// Step 1: Retrieve the order based on the given ID.
127127
order, found := k.GetAtomicOrder(ctx, orderId)
128128
if !found {
129129
return types.ErrNotFoundOrder
130130
}
131-
// Step 2: Remove the item from its current position.
131+
// Step 2: Remove the original order from its position.
132132
k.RemoveOrder(ctx, orderId)
133-
134-
// Step 3: Get the current count (which will be the new position for the order).
135-
newCount := k.GetAtomicOrderCount(ctx)
136-
137-
// Step 4: Set the order at its new position.
138-
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
139-
bz := k.cdc.MustMarshal(&order)
140-
store.Set(GetOrderIDBytes(newCount), bz)
141-
142-
// Step 5: Update the orderId -> count relationship.
143-
k.SetAtomicOrderCountToOrderID(ctx, orderId, newCount)
144-
145-
// Step 6: Increment the total order count.
146-
k.SetAtomicOrderCount(ctx, newCount+1)
147-
133+
// Step 3: Append the order to the end of the list.
134+
// (Since AppendAtomicOrder manages the order count and index mapping,
135+
// we can reuse this function to simplify our logic.)
136+
k.AppendAtomicOrder(ctx, order)
148137
return nil
149138
}
150139

modules/apps/100-atomic-swap/keeper/atomic_order_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func (suite *KeeperTestSuite) TestMoveOrderToBottom() {
5959
CancelTimestamp: int64(i),
6060
})
6161
}
62-
6362
// Move the third order to the bottom
6463
err := k.MoveOrderToBottom(ctx, orderIDs[2])
6564
suite.Require().NoError(err)
@@ -73,6 +72,15 @@ func (suite *KeeperTestSuite) TestMoveOrderToBottom() {
7372
movedOrderCount := k.GetAtomicOrderCountByOrderId(ctx, orderIDs[2])
7473
lastOrderCount := k.GetAtomicOrderCount(ctx) - 1
7574
suite.Require().Equal(movedOrderCount, lastOrderCount)
75+
76+
// Verify that all other orders have maintained their original sequence
77+
for i := 0; i < len(orderIDs)-1; i++ { // Exclude the last order (since it was moved)
78+
if i < 2 { // For orders before the moved order
79+
suite.Require().Equal(ordersAfterMove[i].Id, orderIDs[i])
80+
} else { // For orders after the moved order
81+
suite.Require().Equal(ordersAfterMove[i].Id, orderIDs[i+1])
82+
}
83+
}
7684
}
7785

7886
func (suite *KeeperTestSuite) TestTrimExcessOrders() {

modules/apps/100-atomic-swap/keeper/msg_server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ func (k Keeper) OnReceivedMake(ctx sdk.Context, packet channeltypes.Packet, orde
154154
Maker: msg,
155155
}
156156

157-
k.SetAtomicOrder(ctx, order)
158-
157+
k.AppendAtomicOrder(ctx, order)
159158
ctx.EventManager().EmitTypedEvents(msg)
160159
ctx.EventManager().EmitEvent(
161160
sdk.NewEvent(

modules/apps/101-interchain-swap/keeper/interchain_liquidity_pool.go

Lines changed: 96 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@ import (
1010
"github.com/sideprotocol/ibcswap/v6/modules/apps/101-interchain-swap/types"
1111
)
1212

13-
// // SetInterchainLiquidityPool set a specific interchainLiquidityPool in the store from its index
14-
// func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
15-
// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
16-
// b := k.cdc.MustMarshal(&interchainLiquidityPool)
17-
// store.Set(types.InterchainLiquidityPoolKey(
18-
// interchainLiquidityPool.Id,
19-
// ), b)
20-
// }
21-
2213
// SetInterchainLiquidityPool set a specific interchainLiquidityPool in the store from its index
2314
func (k Keeper) SetInitialPoolAssets(ctx sdk.Context, poolId string, tokens sdk.Coins) {
2415
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(poolId))
@@ -71,24 +62,24 @@ func (k Keeper) GetInitialPoolAssets(ctx sdk.Context, poolId string) sdk.Coins {
7162
return tokens
7263
}
7364

74-
// GetInterchainLiquidityPool returns a interchainLiquidityPool from its index
75-
func (k Keeper) GetInterchainLiquidityPool(
76-
ctx sdk.Context,
77-
poolId string,
65+
// // GetInterchainLiquidityPool returns a interchainLiquidityPool from its index
66+
// func (k Keeper) GetInterchainLiquidityPool(
67+
// ctx sdk.Context,
68+
// poolId string,
7869

79-
) (val types.InterchainLiquidityPool, found bool) {
80-
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
70+
// ) (val types.InterchainLiquidityPool, found bool) {
8171

82-
b := store.Get(types.InterchainLiquidityPoolKey(
83-
poolId,
84-
))
85-
if b == nil {
86-
return val, false
87-
}
72+
// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
73+
// b := store.Get(types.InterchainLiquidityPoolKey(
74+
// poolId,
75+
// ))
76+
// if b == nil {
77+
// return val, false
78+
// }
8879

89-
k.cdc.MustUnmarshal(b, &val)
90-
return val, true
91-
}
80+
// k.cdc.MustUnmarshal(b, &val)
81+
// return val, true
82+
// }
9283

9384
// RemoveInterchainLiquidityPool removes a interchainLiquidityPool from the store
9485
func (k Keeper) RemoveInterchainLiquidityPool(
@@ -102,50 +93,32 @@ func (k Keeper) RemoveInterchainLiquidityPool(
10293
))
10394
}
10495

105-
// // GetAllInterchainLiquidityPool returns all interchainLiquidityPool
106-
// func (k Keeper) GetAllInterchainLiquidityPool(ctx sdk.Context) (list []types.InterchainLiquidityPool) {
96+
// func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
10797
// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
108-
// iterator := sdk.KVStorePrefixIterator(store, []byte{})
109-
110-
// defer iterator.Close()
111-
112-
// for ; iterator.Valid(); iterator.Next() {
113-
// var val types.InterchainLiquidityPool
114-
// k.cdc.MustUnmarshal(iterator.Value(), &val)
115-
// list = append(list, val)
116-
// }
117-
// return
118-
// }
119-
120-
// CurrentPoolCountKey stores the current number of pools.
121-
var CurrentPoolCountKey = []byte("CurrentPoolCount")
122-
123-
func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
124-
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
12598

126-
// Get current pool count
127-
poolCount := k.GetPoolCount(ctx)
99+
// // Get current pool count
100+
// poolCount := k.GetPoolCount(ctx)
128101

129-
// Increment the count
130-
poolCount++
102+
// // Increment the count
103+
// poolCount++
131104

132-
// Set the new count
133-
k.SetPoolCount(ctx, poolCount)
105+
// // Set the new count
106+
// k.SetPoolCount(ctx, poolCount)
134107

135-
// Marshal the pool and set in store
136-
b := k.cdc.MustMarshal(&interchainLiquidityPool)
137-
store.Set(GetInterchainLiquidityPoolKey(poolCount), b)
108+
// // Marshal the pool and set in store
109+
// b := k.cdc.MustMarshal(&interchainLiquidityPool)
110+
// store.Set(GetInterchainLiquidityPoolKey(poolCount), b)
138111

139-
// Check if we exceed max pools
140-
if poolCount > types.MaxPoolCount {
141-
// Remove the oldest pool
142-
store.Delete(GetInterchainLiquidityPoolKey(poolCount - types.MaxPoolCount))
143-
}
144-
}
112+
// // Check if we exceed max pools
113+
// if poolCount > types.MaxPoolCount {
114+
// // Remove the oldest pool
115+
// store.Delete(GetInterchainLiquidityPoolKey(poolCount - types.MaxPoolCount))
116+
// }
117+
// }
145118

146119
func (k Keeper) GetPoolCount(ctx sdk.Context) uint64 {
147120
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
148-
b := store.Get(CurrentPoolCountKey)
121+
b := store.Get(types.CurrentPoolCountKey)
149122
if b == nil {
150123
return 0
151124
}
@@ -156,7 +129,7 @@ func (k Keeper) SetPoolCount(ctx sdk.Context, count uint64) {
156129
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
157130
b := make([]byte, 8)
158131
binary.BigEndian.PutUint64(b, count)
159-
store.Set(CurrentPoolCountKey, b)
132+
store.Set(types.CurrentPoolCountKey, b)
160133
}
161134

162135
func GetInterchainLiquidityPoolKey(count uint64) []byte {
@@ -180,3 +153,66 @@ func (k Keeper) GetAllInterchainLiquidityPool(ctx sdk.Context) (list []types.Int
180153
}
181154
return
182155
}
156+
157+
// Sets the mapping between poolId and its count index
158+
func (k Keeper) SetPoolIdToCountMapping(ctx sdk.Context, poolId string, count uint64) {
159+
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PoolIdToCountKeyPrefix)
160+
b := make([]byte, 8)
161+
binary.BigEndian.PutUint64(b, count)
162+
store.Set([]byte(poolId), b)
163+
}
164+
165+
// Gets the count index of the poolId
166+
func (k Keeper) GetCountByPoolId(ctx sdk.Context, poolId string) (count uint64, found bool) {
167+
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PoolIdToCountKeyPrefix)
168+
b := store.Get([]byte(poolId))
169+
if b == nil {
170+
return 0, false
171+
}
172+
return binary.BigEndian.Uint64(b), true
173+
}
174+
175+
// Modified SetInterchainLiquidityPool
176+
func (k Keeper) SetInterchainLiquidityPool(ctx sdk.Context, interchainLiquidityPool types.InterchainLiquidityPool) {
177+
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
178+
179+
// Get current pool count
180+
poolCount := k.GetPoolCount(ctx)
181+
182+
// Increment the count
183+
poolCount++
184+
185+
// Set the new count
186+
k.SetPoolCount(ctx, poolCount)
187+
188+
// Set the poolId to count mapping
189+
k.SetPoolIdToCountMapping(ctx, interchainLiquidityPool.Id, poolCount)
190+
191+
// Marshal the pool and set in store
192+
b := k.cdc.MustMarshal(&interchainLiquidityPool)
193+
store.Set(GetInterchainLiquidityPoolKey(poolCount), b)
194+
195+
// Check if we exceed max pools
196+
if poolCount > types.MaxPoolCount {
197+
// Remove the oldest pool
198+
store.Delete(GetInterchainLiquidityPoolKey(poolCount - types.MaxPoolCount))
199+
}
200+
}
201+
202+
// Modified GetInterchainLiquidityPool
203+
func (k Keeper) GetInterchainLiquidityPool(ctx sdk.Context, poolId string) (val types.InterchainLiquidityPool, found bool) {
204+
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
205+
206+
count, found := k.GetCountByPoolId(ctx, poolId)
207+
if !found {
208+
return val, false
209+
}
210+
211+
b := store.Get(GetInterchainLiquidityPoolKey(count))
212+
if b == nil {
213+
return val, false
214+
}
215+
216+
k.cdc.MustUnmarshal(b, &val)
217+
return val, true
218+
}

modules/apps/101-interchain-swap/keeper/interchain_liquidity_pool_test.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,38 @@ func (suite *KeeperTestSuite) TestSetAndGetInterchainLiquidityPool() {
1010
ctx := suite.chainA.GetContext()
1111
k := suite.chainA.GetSimApp().InterchainSwapKeeper
1212

13-
pool := types.InterchainLiquidityPool{
14-
Id: "pool1",
13+
// Set a few pools
14+
poolCount := 5 // Let's say you want to set 5 pools for this test. Adjust as needed.
15+
createdPools := make([]types.InterchainLiquidityPool, poolCount)
16+
17+
for i := 1; i <= poolCount; i++ {
18+
pool := types.InterchainLiquidityPool{
19+
Id: fmt.Sprintf("pool%d", i),
20+
}
21+
createdPools[i-1] = pool
22+
k.SetInterchainLiquidityPool(ctx, pool)
1523
}
1624

17-
k.SetInterchainLiquidityPool(ctx, pool)
25+
// Get and verify each set pool
26+
for _, createdPool := range createdPools {
27+
retrievedPool, found := k.GetInterchainLiquidityPool(ctx, createdPool.Id)
28+
suite.Require().True(found, "Expected to find the set pool.")
29+
suite.Require().Equal(createdPool, retrievedPool, "The set pool did not match the retrieved pool.")
30+
}
1831

19-
retrievedPool, found := k.GetInterchainLiquidityPool(ctx, pool.Id)
20-
suite.Require().True(found, "Expected to find the set pool.")
21-
suite.Require().Equal(pool, retrievedPool, "The set pool did not match the retrieved pool.")
32+
// Now, let's test exceeding the max pool count (if applicable)
33+
// You'd need to set pools greater than the `types.MaxPoolCount`, and then ensure that the oldest pools are no longer retrievable.
34+
if poolCount < types.MaxPoolCount {
35+
for i := poolCount + 1; i <= types.MaxPoolCount+1; i++ { // +1 to exceed max count by one
36+
pool := types.InterchainLiquidityPool{
37+
Id: fmt.Sprintf("pool%d", i),
38+
}
39+
k.SetInterchainLiquidityPool(ctx, pool)
40+
}
41+
// Now, the oldest pool (pool1) should not be found
42+
_, found := k.GetInterchainLiquidityPool(ctx, "pool1")
43+
suite.Require().False(found, "Expected not to find the oldest pool.")
44+
}
2245
}
2346

2447
func (suite *KeeperTestSuite) TestGetAllInterchainLiquidityPools() {

modules/apps/101-interchain-swap/keeper/msg_server_cancel_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (k msgServer) CancelPool(ctx context.Context, msg *types.MsgCancelPoolReque
7272

7373
sdkCtx.EventManager().EmitEvent(
7474
sdk.NewEvent(
75-
types.EventTypeMakePool,
75+
types.EventTypeCancelPool,
7676
sdk.Attribute{
7777
Key: types.AttributeKeyPoolId,
7878
Value: msg.PoolId,

modules/apps/101-interchain-swap/types/keys.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const (
3030

3131
var (
3232
// PortKey defines the key to store the port ID in store
33-
PortKey = []byte{0x01}
33+
PortKey = []byte{0x01}
34+
PoolIdToCountKeyPrefix = []byte{0x02}
35+
CurrentPoolCountKey = []byte{0x03}
3436
)
3537

3638
func KeyPrefix(p string) []byte {

0 commit comments

Comments
 (0)