Skip to content

Commit

Permalink
wip: deprecate in-memory UCO
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Jul 2, 2024
1 parent e154776 commit a1a45c8
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 219 deletions.
30 changes: 15 additions & 15 deletions protocol/x/clob/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,26 @@ func EndBlocker(
}

// Prune expired untriggered conditional orders from the in-memory UntriggeredConditionalOrders struct.
keeper.PruneUntriggeredConditionalOrders(
expiredStatefulOrderIds,
processProposerMatchesEvents.PlacedStatefulCancellationOrderIds,
)
// keeper.PruneUntriggeredConditionalOrders(
// expiredStatefulOrderIds,
// processProposerMatchesEvents.PlacedStatefulCancellationOrderIds,
// )

// Update the memstore with expired order ids.
// These expired stateful order ids will be purged from the memclob in `Commit`.
processProposerMatchesEvents.ExpiredStatefulOrderIds = expiredStatefulOrderIds

// Before triggering conditional orders, add newly-placed conditional orders to the clob keeper's
// in-memory UntriggeredConditionalOrders data structure to allow conditional orders to
// trigger in the same block they are placed. Skip triggering orders which have been cancelled
// or expired.
// TODO(CLOB-773) Support conditional order replacements. Ensure replacements are de-duplicated.
keeper.AddUntriggeredConditionalOrders(
ctx,
processProposerMatchesEvents.PlacedConditionalOrderIds,
lib.UniqueSliceToSet(processProposerMatchesEvents.GetPlacedStatefulCancellationOrderIds()),
lib.UniqueSliceToSet(expiredStatefulOrderIds),
)
// // Before triggering conditional orders, add newly-placed conditional orders to the clob keeper's
// // in-memory UntriggeredConditionalOrders data structure to allow conditional orders to
// // trigger in the same block they are placed. Skip triggering orders which have been cancelled
// // or expired.
// // TODO(CLOB-773) Support conditional order replacements. Ensure replacements are de-duplicated.
// keeper.AddUntriggeredConditionalOrders(
// ctx,
// processProposerMatchesEvents.PlacedConditionalOrderIds,
// lib.UniqueSliceToSet(processProposerMatchesEvents.GetPlacedStatefulCancellationOrderIds()),
// lib.UniqueSliceToSet(expiredStatefulOrderIds),
// )

// Poll out all triggered conditional orders from `UntriggeredConditionalOrders` and update state.
triggeredConditionalOrderIds := keeper.MaybeTriggerConditionalOrders(ctx)
Expand Down
108 changes: 54 additions & 54 deletions protocol/x/clob/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,16 @@ func TestEndBlocker_Success(t *testing.T) {
)
}

ks.ClobKeeper.UntriggeredConditionalOrders = map[types.ClobPairId]*keeper.UntriggeredConditionalOrders{
constants.ClobPair_Btc.GetClobPairId(): {
OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{orderToPrune1, orderToPrune2, orderToPrune4},
OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{},
},
constants.ClobPair_Eth.GetClobPairId(): {
OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{},
OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{orderToPrune3},
},
}
// ks.ClobKeeper.UntriggeredConditionalOrders = map[types.ClobPairId]*keeper.UntriggeredConditionalOrders{
// constants.ClobPair_Btc.GetClobPairId(): {
// OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{orderToPrune1, orderToPrune2, orderToPrune4},
// OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{},
// },
// constants.ClobPair_Eth.GetClobPairId(): {
// OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{},
// OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{orderToPrune3},
// },
// }

ks.ClobKeeper.MustSetProcessProposerMatchesEvents(
ctx,
Expand Down Expand Up @@ -366,43 +366,43 @@ func TestEndBlocker_Success(t *testing.T) {
})
require.NoError(t, err)

ks.ClobKeeper.UntriggeredConditionalOrders = map[types.ClobPairId]*keeper.UntriggeredConditionalOrders{
constants.ClobPair_Btc.GetClobPairId(): {
// 10 oracle price subticks triggers 3 orders here.
OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{
constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15_TakeProfit10,
constants.ConditionalOrder_Alice_Num0_Id1_Clob0_Buy15_Price10_GTBT15_TakeProfit5,
constants.ConditionalOrder_Alice_Num0_Id2_Clob0_Buy20_Price10_GTBT15_TakeProfit10,
constants.ConditionalOrder_Alice_Num0_Id3_Clob0_Sell25_Price10_GTBT15_StopLoss10,
},
// 10 oracle price subticks triggers no orders here.
OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{
constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price20_GTBT15_StopLoss20,
constants.ConditionalOrder_Alice_Num0_Id1_Clob0_Buy15_Price25_GTBT15_StopLoss25,
constants.ConditionalOrder_Alice_Num0_Id2_Clob0_Sell20_Price20_GTBT15_TakeProfit20,
constants.ConditionalOrder_Alice_Num0_Id3_Clob0_Buy25_Price25_GTBT15_StopLoss25,
},
},
constants.ClobPair_Eth.GetClobPairId(): {
// 35 oracle price subticks triggers no orders here.
OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{
constants.ConditionalOrder_Alice_Num0_Id0_Clob1_Buy5_Price10_GTBT15_TakeProfit30,
},
// 35 oracle price subticks triggers one order here.
OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{
constants.ConditionalOrder_Alice_Num0_Id3_Clob1_Buy25_Price10_GTBT15_StopLoss20,
},
},
}

for _, untrigCondOrders := range ks.ClobKeeper.UntriggeredConditionalOrders {
for _, conditionalOrder := range untrigCondOrders.OrdersToTriggerWhenOraclePriceGTETriggerPrice {
ks.ClobKeeper.SetLongTermOrderPlacement(ctx, conditionalOrder, blockHeight)
}
for _, conditionalOrder := range untrigCondOrders.OrdersToTriggerWhenOraclePriceLTETriggerPrice {
ks.ClobKeeper.SetLongTermOrderPlacement(ctx, conditionalOrder, blockHeight)
}
}
// ks.ClobKeeper.UntriggeredConditionalOrders = map[types.ClobPairId]*keeper.UntriggeredConditionalOrders{
// constants.ClobPair_Btc.GetClobPairId(): {
// // 10 oracle price subticks triggers 3 orders here.
// OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{
// constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15_TakeProfit10,
// constants.ConditionalOrder_Alice_Num0_Id1_Clob0_Buy15_Price10_GTBT15_TakeProfit5,
// constants.ConditionalOrder_Alice_Num0_Id2_Clob0_Buy20_Price10_GTBT15_TakeProfit10,
// constants.ConditionalOrder_Alice_Num0_Id3_Clob0_Sell25_Price10_GTBT15_StopLoss10,
// },
// // 10 oracle price subticks triggers no orders here.
// OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{
// constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price20_GTBT15_StopLoss20,
// constants.ConditionalOrder_Alice_Num0_Id1_Clob0_Buy15_Price25_GTBT15_StopLoss25,
// constants.ConditionalOrder_Alice_Num0_Id2_Clob0_Sell20_Price20_GTBT15_TakeProfit20,
// constants.ConditionalOrder_Alice_Num0_Id3_Clob0_Buy25_Price25_GTBT15_StopLoss25,
// },
// },
// constants.ClobPair_Eth.GetClobPairId(): {
// // 35 oracle price subticks triggers no orders here.
// OrdersToTriggerWhenOraclePriceLTETriggerPrice: []types.Order{
// constants.ConditionalOrder_Alice_Num0_Id0_Clob1_Buy5_Price10_GTBT15_TakeProfit30,
// },
// // 35 oracle price subticks triggers one order here.
// OrdersToTriggerWhenOraclePriceGTETriggerPrice: []types.Order{
// constants.ConditionalOrder_Alice_Num0_Id3_Clob1_Buy25_Price10_GTBT15_StopLoss20,
// },
// },
// }

// for _, untrigCondOrders := range ks.ClobKeeper.UntriggeredConditionalOrders {
// for _, conditionalOrder := range untrigCondOrders.OrdersToTriggerWhenOraclePriceGTETriggerPrice {
// ks.ClobKeeper.SetLongTermOrderPlacement(ctx, conditionalOrder, blockHeight)
// }
// for _, conditionalOrder := range untrigCondOrders.OrdersToTriggerWhenOraclePriceLTETriggerPrice {
// ks.ClobKeeper.SetLongTermOrderPlacement(ctx, conditionalOrder, blockHeight)
// }
// }

ks.ClobKeeper.MustSetProcessProposerMatchesEvents(
ctx,
Expand Down Expand Up @@ -815,13 +815,13 @@ func TestEndBlocker_Success(t *testing.T) {
require.False(t, exists)
}

if tc.expectedUntriggeredConditionalOrders != nil {
require.Equal(
t,
tc.expectedUntriggeredConditionalOrders,
ks.ClobKeeper.UntriggeredConditionalOrders,
)
}
// if tc.expectedUntriggeredConditionalOrders != nil {
// require.Equal(
// t,
// tc.expectedUntriggeredConditionalOrders,
// ks.ClobKeeper.UntriggeredConditionalOrders,
// )
// }

// Assert that the necessary off-chain indexer events have been added.
mockIndexerEventManager.AssertExpectations(t)
Expand Down
13 changes: 6 additions & 7 deletions protocol/x/clob/keeper/clob_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/testutil/nullify"
perptest "github.com/dydxprotocol/v4-chain/protocol/testutil/perpetuals"
pricestest "github.com/dydxprotocol/v4-chain/protocol/testutil/prices"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/memclob"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"github.com/dydxprotocol/v4-chain/protocol/x/perpetuals"
Expand Down Expand Up @@ -684,9 +683,9 @@ func TestUpdateClobPair_FinalSettlement(t *testing.T) {
}
}

ks.ClobKeeper.UntriggeredConditionalOrders = map[types.ClobPairId]*keeper.UntriggeredConditionalOrders{
0: {}, // leaving blank, orders here don't matter in particular since we clear the whole key
}
// ks.ClobKeeper.UntriggeredConditionalOrders = map[types.ClobPairId]*keeper.UntriggeredConditionalOrders{
// 0: {}, // leaving blank, orders here don't matter in particular since we clear the whole key
// }

clobPair.Status = types.ClobPair_STATUS_FINAL_SETTLEMENT
err = ks.ClobKeeper.UpdateClobPair(ks.Ctx, clobPair)
Expand All @@ -712,9 +711,9 @@ func TestUpdateClobPair_FinalSettlement(t *testing.T) {
ppme.RemovedStatefulOrderIds,
)

// Verify UntriggeredConditionalOrders is cleared.
_, found := ks.ClobKeeper.UntriggeredConditionalOrders[0]
require.False(t, found)
// // Verify UntriggeredConditionalOrders is cleared.
// _, found := ks.ClobKeeper.UntriggeredConditionalOrders[0]
// require.False(t, found)
}

func TestUpdateClobPair(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/final_settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func (k Keeper) mustTransitionToFinalSettlement(ctx sdk.Context, clobPairId type
// Forcefully cancel all stateful orders from state for this clob pair.
k.mustCancelStatefulOrdersForFinalSettlement(ctx, clobPairId)

// Delete untriggered conditional orders for this clob pair from memory.
delete(k.UntriggeredConditionalOrders, clobPairId)
// // Delete untriggered conditional orders for this clob pair from memory.
// delete(k.UntriggeredConditionalOrders, clobPairId)
}

// mustCancelStatefulOrdersForFinalSettlement forcefully cancels all stateful orders
Expand Down
50 changes: 24 additions & 26 deletions protocol/x/clob/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ type (
transientStoreKey storetypes.StoreKey
authorities map[string]struct{}

MemClob types.MemClob
UntriggeredConditionalOrders map[types.ClobPairId]*UntriggeredConditionalOrders
PerpetualIdToClobPairId map[uint32][]types.ClobPairId
MemClob types.MemClob
PerpetualIdToClobPairId map[uint32][]types.ClobPairId

subaccountsKeeper types.SubaccountsKeeper
assetsKeeper types.AssetsKeeper
Expand Down Expand Up @@ -92,28 +91,27 @@ func NewKeeper(
daemonLiquidationInfo *liquidationtypes.DaemonLiquidationInfo,
) *Keeper {
keeper := &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
transientStoreKey: liquidationsStoreKey,
authorities: lib.UniqueSliceToSet(authorities),
MemClob: memClob,
UntriggeredConditionalOrders: make(map[types.ClobPairId]*UntriggeredConditionalOrders),
PerpetualIdToClobPairId: make(map[uint32][]types.ClobPairId),
subaccountsKeeper: subaccountsKeeper,
assetsKeeper: assetsKeeper,
blockTimeKeeper: blockTimeKeeper,
bankKeeper: bankKeeper,
feeTiersKeeper: feeTiersKeeper,
perpetualsKeeper: perpetualsKeeper,
pricesKeeper: pricesKeeper,
statsKeeper: statsKeeper,
rewardsKeeper: rewardsKeeper,
indexerEventManager: indexerEventManager,
streamingManager: grpcStreamingManager,
memStoreInitialized: &atomic.Bool{}, // False by default.
initialized: &atomic.Bool{}, // False by default.
txDecoder: txDecoder,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
transientStoreKey: liquidationsStoreKey,
authorities: lib.UniqueSliceToSet(authorities),
MemClob: memClob,
PerpetualIdToClobPairId: make(map[uint32][]types.ClobPairId),
subaccountsKeeper: subaccountsKeeper,
assetsKeeper: assetsKeeper,
blockTimeKeeper: blockTimeKeeper,
bankKeeper: bankKeeper,
feeTiersKeeper: feeTiersKeeper,
perpetualsKeeper: perpetualsKeeper,
pricesKeeper: pricesKeeper,
statsKeeper: statsKeeper,
rewardsKeeper: rewardsKeeper,
indexerEventManager: indexerEventManager,
streamingManager: grpcStreamingManager,
memStoreInitialized: &atomic.Bool{}, // False by default.
initialized: &atomic.Bool{}, // False by default.
txDecoder: txDecoder,
mevTelemetryConfig: MevTelemetryConfig{
Enabled: clobFlags.MevTelemetryEnabled,
Hosts: clobFlags.MevTelemetryHosts,
Expand Down Expand Up @@ -190,7 +188,7 @@ func (k Keeper) Initialize(ctx sdk.Context) {
k.HydrateClobPairAndPerpetualMapping(checkCtx)
// Initialize the untriggered conditional orders data structure with untriggered
// conditional orders in state.
k.HydrateUntriggeredConditionalOrders(checkCtx)
// k.HydrateUntriggeredConditionalOrders(checkCtx)
}

// InitMemStore initializes the memstore of the `clob` keeper.
Expand Down
53 changes: 22 additions & 31 deletions protocol/x/clob/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,37 +1243,28 @@ func (k Keeper) InitStatefulOrders(
}
}

// HydrateUntriggeredConditionalOrders inserts all untriggered conditional orders in state into the
// `UntriggeredConditionalOrders` data structure. Note that all untriggered conditional orders will
// be ordered by time priority. This function should only be called on application startup.
func (k Keeper) HydrateUntriggeredConditionalOrders(
ctx sdk.Context,
) {
defer telemetry.ModuleMeasureSince(
types.ModuleName,
time.Now(),
metrics.ConditionalOrderUntriggered,
metrics.Hydrate,
metrics.Latency,
)

// Get all untriggered conditional orders in state, ordered by time priority ascending order,
// and add them to the `UntriggeredConditionalOrders` data structure.
untriggeredConditionalOrders := k.GetAllUntriggeredConditionalOrders(ctx)
k.AddUntriggeredConditionalOrders(
ctx,
lib.MapSlice(
untriggeredConditionalOrders,
func(o types.Order) types.OrderId {
return o.OrderId
},
),
// Note both of these arguments are empty slices since the untriggered conditional orders
// shouldn't be expired or canceled.
map[types.OrderId]struct{}{},
map[types.OrderId]struct{}{},
)
}
// // HydrateUntriggeredConditionalOrders inserts all untriggered conditional orders in state into the
// // `UntriggeredConditionalOrders` data structure. Note that all untriggered conditional orders will
// // be ordered by time priority. This function should only be called on application startup.
// func (k Keeper) HydrateUntriggeredConditionalOrders(
// ctx sdk.Context,
// ) {
// defer telemetry.ModuleMeasureSince(
// types.ModuleName,
// time.Now(),
// metrics.ConditionalOrderUntriggered,
// metrics.Hydrate,
// metrics.Latency,
// )

// // Get all untriggered conditional orders in state, ordered by time priority ascending order,
// // and add them to the `UntriggeredConditionalOrders` data structure.
// untriggeredConditionalOrders := k.GetAllUntriggeredConditionalOrders(ctx)
// untriggeredConditonals := GetInMemUntriggeredConditionalOrders(
// ctx,
// untriggeredConditionalOrders,
// )
// }

// sendOffchainMessagesWithTxHash sends all the `Message` in the offchainUpdates passed in along with
// an additional header for the transaction hash passed in.
Expand Down
6 changes: 3 additions & 3 deletions protocol/x/clob/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2171,10 +2171,10 @@ func TestHydrateUntriggeredConditionalOrdersInMemClob(t *testing.T) {
untriggeredConditionalOrders.AddUntriggeredConditionalOrder(order)
}

// Run the test and verify expectations.
ks.ClobKeeper.HydrateUntriggeredConditionalOrders(ks.Ctx)
// // Run the test and verify expectations.
// ks.ClobKeeper.HydrateUntriggeredConditionalOrders(ks.Ctx)

require.Equal(t, expectedUntriggeredConditionalOrders, ks.ClobKeeper.UntriggeredConditionalOrders)
// require.Equal(t, expectedUntriggeredConditionalOrders, ks.ClobKeeper.UntriggeredConditionalOrders)
})
}
}
Expand Down
Loading

0 comments on commit a1a45c8

Please sign in to comment.