Skip to content

Commit

Permalink
[OTE-378] Remove offchain updates from ProcessSingleMatch (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding authored Jun 3, 2024
1 parent d7f0143 commit 4c3e158
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 78 deletions.
21 changes: 6 additions & 15 deletions protocol/mocks/ClobKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 6 additions & 15 deletions protocol/mocks/MemClobKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions protocol/testutil/memclob/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ func (f *FakeMemClobKeeper) ProcessSingleMatch(
success bool,
takerUpdateResult satypes.UpdateResult,
makerUpdateResult satypes.UpdateResult,
offchainUpdates *types.OffchainUpdates,
err error,
) {
makerOrder := matchWithOrders.MakerOrder
Expand Down Expand Up @@ -364,7 +363,7 @@ func (f *FakeMemClobKeeper) ProcessSingleMatch(
)
}

return true, satypes.Success, satypes.Success, types.NewOffchainUpdates(), nil
return true, satypes.Success, satypes.Success, nil
}

subaccountMatchedOrders := make(map[satypes.SubaccountId][]types.PendingOpenOrder)
Expand Down Expand Up @@ -411,7 +410,7 @@ func (f *FakeMemClobKeeper) ProcessSingleMatch(
}
}

return success, takerUpdateResult, makerUpdateResult, types.NewOffchainUpdates(), nil
return success, takerUpdateResult, makerUpdateResult, nil
}

func (f *FakeMemClobKeeper) GetStatePosition(
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/process_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func (k Keeper) PersistMatchOrdersToState(
}
makerOrders = append(makerOrders, makerOrder)

_, _, _, _, err = k.ProcessSingleMatch(ctx, &matchWithOrders)
_, _, _, err = k.ProcessSingleMatch(ctx, &matchWithOrders)
if err != nil {
return err
}
Expand Down Expand Up @@ -614,7 +614,7 @@ func (k Keeper) PersistMatchLiquidationToState(

// Write the position updates and state fill amounts for this match.
// Note stateless validation on the constructed `matchWithOrders` is performed within this function.
_, _, _, _, err = k.ProcessSingleMatch(
_, _, _, err = k.ProcessSingleMatch(
ctx,
&matchWithOrders,
)
Expand Down
5 changes: 0 additions & 5 deletions protocol/x/clob/keeper/process_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,6 @@ func setupProcessProposerOperationsTestCase(
tc.rawOperations,
)
} else {
mockIndexerEventManager.On("Enabled").Return(false).Maybe()
mockIndexerEventManager.On("AddTxnEvent",
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -2584,10 +2583,6 @@ func setupNewMockEventManager(
matches []*MatchWithOrdersForTesting,
rawOperations []types.OperationRaw,
) {
if len(matches) > 0 {
mockIndexerEventManager.On("Enabled").Return(true)
}

// Add an expectation to the mock for each expected message.
var matchOrderCallMap = make(map[types.OrderId]*mock.Call)
for _, match := range matches {
Expand Down
48 changes: 13 additions & 35 deletions protocol/x/clob/keeper/process_single_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/indexer/off_chain_updates"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
Expand Down Expand Up @@ -46,7 +45,6 @@ func (k Keeper) ProcessSingleMatch(
success bool,
takerUpdateResult satypes.UpdateResult,
makerUpdateResult satypes.UpdateResult,
offchainUpdates *types.OffchainUpdates,
err error,
) {
if matchWithOrders.TakerOrder.IsLiquidation() {
Expand Down Expand Up @@ -74,14 +72,13 @@ func (k Keeper) ProcessSingleMatch(

// Perform stateless validation on the match.
if err := matchWithOrders.Validate(); err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, errorsmod.Wrapf(
return false, takerUpdateResult, makerUpdateResult, errorsmod.Wrapf(
err,
"ProcessSingleMatch: Invalid MatchWithOrders: %+v",
matchWithOrders,
)
}

offchainUpdates = types.NewOffchainUpdates()
makerMatchableOrder := matchWithOrders.MakerOrder
takerMatchableOrder := matchWithOrders.TakerOrder
fillAmount := matchWithOrders.FillAmount
Expand All @@ -90,15 +87,14 @@ func (k Keeper) ProcessSingleMatch(
clobPairId := makerMatchableOrder.GetClobPairId()
clobPair, found := k.GetClobPair(ctx, clobPairId)
if !found {
return false, takerUpdateResult, makerUpdateResult, nil, types.ErrInvalidClob
return false, takerUpdateResult, makerUpdateResult, types.ErrInvalidClob
}

// Verify that the `fillAmount` is divisible by the `StepBaseQuantums` of the `clobPair`.
if fillAmount.ToUint64()%clobPair.StepBaseQuantums != 0 {
return false,
takerUpdateResult,
makerUpdateResult,
nil,
types.ErrFillAmountNotDivisibleByStepSize
}

Expand All @@ -108,7 +104,7 @@ func (k Keeper) ProcessSingleMatch(
// Calculate the number of quote quantums for the match based on the maker order subticks.
bigFillQuoteQuantums, err := getFillQuoteQuantums(clobPair, makerSubticks, fillAmount)
if err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, err
return false, takerUpdateResult, makerUpdateResult, err
}

if bigFillQuoteQuantums.Sign() == 0 {
Expand All @@ -131,7 +127,7 @@ func (k Keeper) ProcessSingleMatch(
// Retrieve the associated perpetual id for the `ClobPair`.
perpetualId, err := clobPair.GetPerpetualId()
if err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, err
return false, takerUpdateResult, makerUpdateResult, err
}

// Calculate taker and maker fee ppms.
Expand All @@ -157,7 +153,7 @@ func (k Keeper) ProcessSingleMatch(
)

if err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, err
return false, takerUpdateResult, makerUpdateResult, err
}
}

Expand Down Expand Up @@ -190,7 +186,7 @@ func (k Keeper) ProcessSingleMatch(
)

if err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, err
return false, takerUpdateResult, makerUpdateResult, err
}
}

Expand All @@ -212,7 +208,7 @@ func (k Keeper) ProcessSingleMatch(
)

if err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, err
return false, takerUpdateResult, makerUpdateResult, err
}

// Update both subaccounts in the matched order atomically.
Expand All @@ -227,7 +223,7 @@ func (k Keeper) ProcessSingleMatch(
)

if err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, err
return false, takerUpdateResult, makerUpdateResult, err
}

// Update subaccount total quantums liquidated and total insurance fund lost for liquidation orders.
Expand All @@ -238,7 +234,7 @@ func (k Keeper) ProcessSingleMatch(
fillAmount.ToBigInt(),
)
if err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, err
return false, takerUpdateResult, makerUpdateResult, err
}

k.UpdateSubaccountLiquidationInfo(
Expand Down Expand Up @@ -275,24 +271,22 @@ func (k Keeper) ProcessSingleMatch(
// Liquidation orders can only be placed when a subaccount is liquidatable
// and cannot be replayed, therefore we don't need to track their filled amount in state.
if !matchWithOrders.TakerOrder.IsLiquidation() {
takerOffchainUpdates := k.setOrderFillAmountsAndPruning(
k.setOrderFillAmountsAndPruning(
ctx,
matchWithOrders.TakerOrder.MustGetOrder(),
newTakerTotalFillAmount,
curTakerPruneableBlockHeight,
)
offchainUpdates.Append(takerOffchainUpdates)
}

makerOffchainUpdates := k.setOrderFillAmountsAndPruning(
k.setOrderFillAmountsAndPruning(
ctx,
matchWithOrders.MakerOrder.MustGetOrder(),
newMakerTotalFillAmount,
curMakerPruneableBlockHeight,
)
offchainUpdates.Append(makerOffchainUpdates)

return true, takerUpdateResult, makerUpdateResult, offchainUpdates, nil
return true, takerUpdateResult, makerUpdateResult, nil
}

// persistMatchedOrders persists a matched order to the subaccount state,
Expand Down Expand Up @@ -501,10 +495,9 @@ func (k Keeper) setOrderFillAmountsAndPruning(
order types.Order,
newTotalFillAmount satypes.BaseQuantums,
curPruneableBlockHeight uint32,
) *types.OffchainUpdates {
) {
// Note that stateful orders are never pruned by `BlockHeight`, so we set the value to `math.MaxUint32` here.
pruneableBlockHeight := uint32(math.MaxUint32)
offchainUpdates := types.NewOffchainUpdates()

if !order.IsStatefulOrder() {
// Compute the block at which this state fill amount can be pruned. This is the greater of
Expand Down Expand Up @@ -540,21 +533,6 @@ func (k Keeper) setOrderFillAmountsAndPruning(
newTotalFillAmount,
pruneableBlockHeight,
)

if k.GetIndexerEventManager().Enabled() {
if _, exists := k.MemClob.GetOrder(order.OrderId); exists {
// Generate an off-chain update message updating the total filled amount of order.
if message, success := off_chain_updates.CreateOrderUpdateMessage(
ctx,
order.OrderId,
newTotalFillAmount,
); success {
offchainUpdates.AddUpdateMessage(order.OrderId, message)
}
}
}

return offchainUpdates
}

// getUpdatedOrderFillAmount accepts an order's current total fill amount, total base quantums, and a new fill amount,
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/memclob/memclob.go
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ func (m *MemClobPriceTimePriority) mustPerformTakerOrderMatching(
FillAmount: matchedAmount,
}

success, takerUpdateResult, makerUpdateResult, _, err := m.clobKeeper.ProcessSingleMatch(ctx, &matchWithOrders)
success, takerUpdateResult, makerUpdateResult, err := m.clobKeeper.ProcessSingleMatch(ctx, &matchWithOrders)
if err != nil && !errors.Is(err, satypes.ErrFailedToUpdateSubaccounts) {
if errors.Is(err, types.ErrLiquidationExceedsSubaccountMaxInsuranceLost) {
// Subaccount has reached max insurance lost block limit. Stop matching.
Expand Down
1 change: 0 additions & 1 deletion protocol/x/clob/types/clob_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ type ClobKeeper interface {
success bool,
takerUpdateResult satypes.UpdateResult,
makerUpdateResult satypes.UpdateResult,
offchainUpdates *OffchainUpdates,
err error,
)
SetLongTermOrderPlacement(
Expand Down
1 change: 0 additions & 1 deletion protocol/x/clob/types/mem_clob_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type MemClobKeeper interface {
success bool,
takerUpdateResult satypes.UpdateResult,
makerUpdateResult satypes.UpdateResult,
offchainUpdates *OffchainUpdates,
err error,
)
CanDeleverageSubaccount(
Expand Down

0 comments on commit 4c3e158

Please sign in to comment.