From 73add8faea34ebcaa3fb1d26f006abfd87447382 Mon Sep 17 00:00:00 2001 From: Jay Yu <103467857+jayy04@users.noreply.github.com> Date: Mon, 2 Dec 2024 12:24:23 -0500 Subject: [PATCH] comments --- protocol/mocks/MemClob.go | 8 ++++---- protocol/x/clob/keeper/orders.go | 16 +++++++++++----- protocol/x/clob/keeper/orders_test.go | 22 +++++++++++----------- protocol/x/clob/memclob/memclob.go | 17 ++++++++++------- protocol/x/clob/types/memclob.go | 2 +- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/protocol/mocks/MemClob.go b/protocol/mocks/MemClob.go index 93e0eacb2c..60cb6aca69 100644 --- a/protocol/mocks/MemClob.go +++ b/protocol/mocks/MemClob.go @@ -578,9 +578,9 @@ func (_m *MemClob) RemoveOrderIfFilled(ctx types.Context, orderId clobtypes.Orde _m.Called(ctx, orderId) } -// ReplayOperations provides a mock function with given fields: ctx, localOperations, shortTermOrderTxBytes, existingOffchainUpdates, onlyPlacePostOnly -func (_m *MemClob) ReplayOperations(ctx types.Context, localOperations []clobtypes.InternalOperation, shortTermOrderTxBytes map[clobtypes.OrderHash][]byte, existingOffchainUpdates *clobtypes.OffchainUpdates, onlyPlacePostOnly bool) *clobtypes.OffchainUpdates { - ret := _m.Called(ctx, localOperations, shortTermOrderTxBytes, existingOffchainUpdates, onlyPlacePostOnly) +// ReplayOperations provides a mock function with given fields: ctx, localOperations, shortTermOrderTxBytes, existingOffchainUpdates, postOnlyFilter +func (_m *MemClob) ReplayOperations(ctx types.Context, localOperations []clobtypes.InternalOperation, shortTermOrderTxBytes map[clobtypes.OrderHash][]byte, existingOffchainUpdates *clobtypes.OffchainUpdates, postOnlyFilter bool) *clobtypes.OffchainUpdates { + ret := _m.Called(ctx, localOperations, shortTermOrderTxBytes, existingOffchainUpdates, postOnlyFilter) if len(ret) == 0 { panic("no return value specified for ReplayOperations") @@ -588,7 +588,7 @@ func (_m *MemClob) ReplayOperations(ctx types.Context, localOperations []clobtyp var r0 *clobtypes.OffchainUpdates if rf, ok := ret.Get(0).(func(types.Context, []clobtypes.InternalOperation, map[clobtypes.OrderHash][]byte, *clobtypes.OffchainUpdates, bool) *clobtypes.OffchainUpdates); ok { - r0 = rf(ctx, localOperations, shortTermOrderTxBytes, existingOffchainUpdates, onlyPlacePostOnly) + r0 = rf(ctx, localOperations, shortTermOrderTxBytes, existingOffchainUpdates, postOnlyFilter) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*clobtypes.OffchainUpdates) diff --git a/protocol/x/clob/keeper/orders.go b/protocol/x/clob/keeper/orders.go index 3316ff0948..eba66f9f69 100644 --- a/protocol/x/clob/keeper/orders.go +++ b/protocol/x/clob/keeper/orders.go @@ -487,6 +487,9 @@ func (k Keeper) AddPreexistingStatefulOrder( // PlaceStatefulOrdersFromLastBlock validates and places stateful orders from the last block onto the memclob. // Note that stateful orders could fail to be placed due to various reasons such as collateralization // check failures, self-trade errors, etc. In these cases the `checkState` will not be written to. +// Note that this function also takes in a postOnlyFilter variable and only places post-only orders if +// postOnlyFilter is true and non-post-only orders if postOnlyFilter is false. +// // This function is used in: // 1. `PrepareCheckState` to place newly placed long term orders from the last // block from ProcessProposerMatchesEvents.PlacedStatefulOrderIds. This is step 3 in PrepareCheckState. @@ -496,7 +499,7 @@ func (k Keeper) PlaceStatefulOrdersFromLastBlock( ctx sdk.Context, placedStatefulOrderIds []types.OrderId, existingOffchainUpdates *types.OffchainUpdates, - onlyPlacePostOnly bool, + postOnlyFilter bool, ) ( offchainUpdates *types.OffchainUpdates, ) { @@ -523,8 +526,8 @@ func (k Keeper) PlaceStatefulOrdersFromLastBlock( order := orderPlacement.GetOrder() - // Skip the order if it is a post-only order and we are only placing post-only orders. - if onlyPlacePostOnly != order.IsPostOnlyOrder() { + // Skip post-only orders if postOnlyFilter is false or non-post-only orders if postOnlyFilter is true. + if postOnlyFilter != order.IsPostOnlyOrder() { continue } @@ -582,11 +585,14 @@ func (k Keeper) PlaceStatefulOrdersFromLastBlock( // PlaceConditionalOrdersTriggeredInLastBlock takes in a list of conditional order ids that were triggered // in the last block, verifies they are conditional orders, verifies they are in triggered state, and places // the orders on the memclob. +// +// Note that this function also takes in a postOnlyFilter variable and only places post-only orders if +// postOnlyFilter is true and non-post-only orders if postOnlyFilter is false. func (k Keeper) PlaceConditionalOrdersTriggeredInLastBlock( ctx sdk.Context, conditionalOrderIdsTriggeredInLastBlock []types.OrderId, existingOffchainUpdates *types.OffchainUpdates, - onlyPlacePostOnly bool, + postOnlyFilter bool, ) ( offchainUpdates *types.OffchainUpdates, ) { @@ -620,7 +626,7 @@ func (k Keeper) PlaceConditionalOrdersTriggeredInLastBlock( ctx, conditionalOrderIdsTriggeredInLastBlock, existingOffchainUpdates, - onlyPlacePostOnly, + postOnlyFilter, ) } diff --git a/protocol/x/clob/keeper/orders_test.go b/protocol/x/clob/keeper/orders_test.go index be55fe27d4..09d2025918 100644 --- a/protocol/x/clob/keeper/orders_test.go +++ b/protocol/x/clob/keeper/orders_test.go @@ -2250,13 +2250,13 @@ func TestPlaceStatefulOrdersFromLastBlock(t *testing.T) { func TestPlaceStatefulOrdersFromLastBlock_PostOnly(t *testing.T) { tests := map[string]struct { - orders []types.Order - onlyPlacePostOnly bool + orders []types.Order + postOnlyFilter bool expectedOrderPlacementCalls []types.Order }{ - "places PO stateful orders from last block when onlyPlacePostOnly = true": { - onlyPlacePostOnly: true, + "places PO stateful orders from last block when postOnlyFilter = true": { + postOnlyFilter: true, orders: []types.Order{ constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT5_PO, }, @@ -2264,22 +2264,22 @@ func TestPlaceStatefulOrdersFromLastBlock_PostOnly(t *testing.T) { constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT5_PO, }, }, - "does not places non-PO stateful orders when onlyPlacePostOnly = true": { - onlyPlacePostOnly: true, + "does not places non-PO stateful orders when postOnlyFilter = true": { + postOnlyFilter: true, orders: []types.Order{ constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT5, }, expectedOrderPlacementCalls: []types.Order{}, }, - "does not places PO stateful orders from last block, when onlyPlacePostOnly = false": { - onlyPlacePostOnly: false, + "does not places PO stateful orders from last block, when postOnlyFilter = false": { + postOnlyFilter: false, orders: []types.Order{ constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT5_PO, }, expectedOrderPlacementCalls: []types.Order{}, }, - "places non-PO stateful orders from last block when onlyPlacePostOnly = false": { - onlyPlacePostOnly: false, + "places non-PO stateful orders from last block when postOnlyFilter = false": { + postOnlyFilter: false, orders: []types.Order{ constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT5, }, @@ -2350,7 +2350,7 @@ func TestPlaceStatefulOrdersFromLastBlock_PostOnly(t *testing.T) { for _, order := range tc.orders { orderIds = append(orderIds, order.OrderId) } - ks.ClobKeeper.PlaceStatefulOrdersFromLastBlock(ctx, orderIds, offchainUpdates, tc.onlyPlacePostOnly) + ks.ClobKeeper.PlaceStatefulOrdersFromLastBlock(ctx, orderIds, offchainUpdates, tc.postOnlyFilter) // PlaceStatefulOrdersFromLastBlock utilizes the memclob's PlaceOrder flow, but we // do not want to emit PlaceMessages in offchain events for stateful orders. This assertion diff --git a/protocol/x/clob/memclob/memclob.go b/protocol/x/clob/memclob/memclob.go index 6b38c2f9f8..953d0d758f 100644 --- a/protocol/x/clob/memclob/memclob.go +++ b/protocol/x/clob/memclob/memclob.go @@ -874,12 +874,15 @@ func (m *MemClobPriceTimePriority) matchOrder( // - Pre-existing stateful orders. // - Stateful cancelations. // Note that match operations are no-op. +// +// Note that this function also takes in a postOnlyFilter variable and only places post-only orders if +// postOnlyFilter is true and non-post-only orders if postOnlyFilter is false. func (m *MemClobPriceTimePriority) ReplayOperations( ctx sdk.Context, localOperations []types.InternalOperation, shortTermOrderTxBytes map[types.OrderHash][]byte, existingOffchainUpdates *types.OffchainUpdates, - onlyPlacePostOnly bool, + postOnlyFilter bool, ) *types.OffchainUpdates { lib.AssertCheckTxMode(ctx) @@ -924,8 +927,8 @@ func (m *MemClobPriceTimePriority) ReplayOperations( case *types.InternalOperation_ShortTermOrderPlacement: order := operation.GetShortTermOrderPlacement().Order - // Skip the order if it is a post-only order and we are only replaying post-only orders. - if onlyPlacePostOnly != order.IsPostOnlyOrder() { + // Skip post-only orders if postOnlyFilter is false or non-post-only orders if postOnlyFilter is true. + if postOnlyFilter != order.IsPostOnlyOrder() { continue } @@ -1019,8 +1022,8 @@ func (m *MemClobPriceTimePriority) ReplayOperations( continue } - // Skip the order if it is a post-only order and we are only replaying post-only orders. - if onlyPlacePostOnly != statefulOrderPlacement.Order.IsPostOnlyOrder() { + // Skip post-only orders if postOnlyFilter is false or non-post-only orders if postOnlyFilter is true. + if postOnlyFilter != statefulOrderPlacement.Order.IsPostOnlyOrder() { continue } @@ -1077,8 +1080,8 @@ func (m *MemClobPriceTimePriority) ReplayOperations( continue } - // Skip the order if it is a post-only order and we are only replaying post-only orders. - if onlyPlacePostOnly != statefulOrderPlacement.Order.IsPostOnlyOrder() { + // Skip post-only orders if postOnlyFilter is false or non-post-only orders if postOnlyFilter is true. + if postOnlyFilter != statefulOrderPlacement.Order.IsPostOnlyOrder() { continue } diff --git a/protocol/x/clob/types/memclob.go b/protocol/x/clob/types/memclob.go index ab63fe5fca..2c55eba839 100644 --- a/protocol/x/clob/types/memclob.go +++ b/protocol/x/clob/types/memclob.go @@ -110,7 +110,7 @@ type MemClob interface { localOperations []InternalOperation, shortTermOrderTxBytes map[OrderHash][]byte, existingOffchainUpdates *OffchainUpdates, - onlyPlacePostOnly bool, + postOnlyFilter bool, ) (offchainUpdates *OffchainUpdates) SetMemclobGauges( ctx sdk.Context,