From 46c33fe215883aa77db86796b465ef0e90b340cf Mon Sep 17 00:00:00 2001 From: Teddy Ding Date: Thu, 3 Oct 2024 14:18:31 -0400 Subject: [PATCH] comments --- protocol/mocks/MemClob.go | 17 +++++++++++++++-- protocol/x/clob/keeper/clob_pair.go | 22 +++++++++++----------- protocol/x/clob/keeper/keeper.go | 2 +- protocol/x/clob/memclob/memclob.go | 5 +++-- protocol/x/clob/types/memclob.go | 2 +- protocol/x/listing/keeper/listing_test.go | 8 -------- 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/protocol/mocks/MemClob.go b/protocol/mocks/MemClob.go index abccc20590..9db224e05b 100644 --- a/protocol/mocks/MemClob.go +++ b/protocol/mocks/MemClob.go @@ -443,8 +443,21 @@ func (_m *MemClob) InsertZeroFillDeleveragingIntoOperationsQueue(subaccountId su } // MaybeCreateOrderbook provides a mock function with given fields: clobPair -func (_m *MemClob) MaybeCreateOrderbook(clobPair clobtypes.ClobPair) { - _m.Called(clobPair) +func (_m *MemClob) MaybeCreateOrderbook(clobPair clobtypes.ClobPair) bool { + ret := _m.Called(clobPair) + + if len(ret) == 0 { + panic("no return value specified for MaybeCreateOrderbook") + } + + var r0 bool + if rf, ok := ret.Get(0).(func(clobtypes.ClobPair) bool); ok { + r0 = rf(clobPair) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 } // PlaceOrder provides a mock function with given fields: ctx, order diff --git a/protocol/x/clob/keeper/clob_pair.go b/protocol/x/clob/keeper/clob_pair.go index 98f03f89b3..65f2e5fee4 100644 --- a/protocol/x/clob/keeper/clob_pair.go +++ b/protocol/x/clob/keeper/clob_pair.go @@ -15,6 +15,7 @@ import ( indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events" "github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager" "github.com/dydxprotocol/v4-chain/protocol/lib" + dydxlog "github.com/dydxprotocol/v4-chain/protocol/lib/log" "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" ) @@ -238,18 +239,18 @@ func (k Keeper) validateClobPair(ctx sdk.Context, clobPair *types.ClobPair) erro return nil } -// maybeCreateOrderbook creates a new orderbook in the memclob. -func (k Keeper) maybeCreateOrderbook(ctx sdk.Context, clobPair types.ClobPair) { - // Create the corresponding orderbook in the memclob. - k.MemClob.MaybeCreateOrderbook(clobPair) -} - -func (k Keeper) ApplySideEffectsForCNewlobPair( +func (k Keeper) ApplySideEffectsForNewClobPair( ctx sdk.Context, clobPair types.ClobPair, ) { - // TODO throw error if orderbook is not created. - k.MemClob.MaybeCreateOrderbook(clobPair) + if created := k.MemClob.MaybeCreateOrderbook(clobPair); !created { + dydxlog.ErrorLog( + ctx, + "ApplySideEffectsForNewClobPair: Orderbook already exists for CLOB pair", + "clob_pair", clobPair, + ) + return + } k.SetClobPairIdForPerpetual(clobPair) } @@ -285,8 +286,7 @@ func (k Keeper) InitMemClobOrderbooks(ctx sdk.Context) { clobPairs := k.GetAllClobPairs(ctx) for _, clobPair := range clobPairs { // Create the corresponding orderbook in the memclob. - k.maybeCreateOrderbook( - ctx, + k.MemClob.MaybeCreateOrderbook( clobPair, ) } diff --git a/protocol/x/clob/keeper/keeper.go b/protocol/x/clob/keeper/keeper.go index e181351531..633a0725b9 100644 --- a/protocol/x/clob/keeper/keeper.go +++ b/protocol/x/clob/keeper/keeper.go @@ -233,7 +233,7 @@ func (k Keeper) ProcessStagedFinalizeBlockEvents(ctx sdk.Context) { switch event := stagedEvent.Event.(type) { case *types.ClobStagedFinalizeBlockEvent_CreateClobPair: - k.ApplySideEffectsForCNewlobPair(ctx, *event.CreateClobPair) + k.ApplySideEffectsForNewClobPair(ctx, *event.CreateClobPair) default: dydxlog.ErrorLog( ctx, diff --git a/protocol/x/clob/memclob/memclob.go b/protocol/x/clob/memclob/memclob.go index d0de165e08..1229e8d3ec 100644 --- a/protocol/x/clob/memclob/memclob.go +++ b/protocol/x/clob/memclob/memclob.go @@ -158,11 +158,12 @@ func (m *MemClobPriceTimePriority) CancelOrder( // MaybeCreateOrderbook is used for updating memclob internal data structures to mark an orderbook as created. func (m *MemClobPriceTimePriority) MaybeCreateOrderbook( clobPair types.ClobPair, -) { +) (created bool) { if _, exists := m.orderbooks[clobPair.GetClobPairId()]; exists { - return + return false } m.CreateOrderbook(clobPair) + return true } // CreateOrderbook is used for updating memclob internal data structures to mark an orderbook as created. diff --git a/protocol/x/clob/types/memclob.go b/protocol/x/clob/types/memclob.go index 17cbb37601..86191376f7 100644 --- a/protocol/x/clob/types/memclob.go +++ b/protocol/x/clob/types/memclob.go @@ -23,7 +23,7 @@ type MemClob interface { ) MaybeCreateOrderbook( clobPair ClobPair, - ) + ) bool GetOperationsToReplay( ctx sdk.Context, ) ( diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index e8ef279145..656fb14aad 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -204,17 +204,14 @@ func TestCreateClobPair(t *testing.T) { tests := map[string]struct { ticker string isDeliverTx bool - isGenesis bool }{ "deliverTx - true": { ticker: "TEST-USD", isDeliverTx: true, - isGenesis: false, }, "deliverTx - false": { ticker: "TEST-USD", isDeliverTx: false, - isGenesis: false, }, } @@ -227,11 +224,6 @@ func TestCreateClobPair(t *testing.T) { &mocks.BankKeeper{}, mockIndexerEventManager, ) - // if tc.isGenesis { - // ctx = ctx.WithBlockHeight(0) - // } else { - // ctx = ctx.WithBlockHeight(1) - // } mockIndexerEventManager.On( "AddTxnEvent", mock.Anything,