From 46c4029fbfde02a52ae510f9948fac0bc39ed7e4 Mon Sep 17 00:00:00 2001 From: Roy Li Date: Fri, 22 Mar 2024 14:28:10 -0400 Subject: [PATCH] test --- .../protocol-build-and-push-snapshot.yml | 1 + .github/workflows/protocol-build-and-push.yml | 1 + protocol/x/clob/keeper/order_state.go | 91 +++++-------------- 3 files changed, 27 insertions(+), 66 deletions(-) diff --git a/.github/workflows/protocol-build-and-push-snapshot.yml b/.github/workflows/protocol-build-and-push-snapshot.yml index 51ffb241932..9e29124fa0e 100644 --- a/.github/workflows/protocol-build-and-push-snapshot.yml +++ b/.github/workflows/protocol-build-and-push-snapshot.yml @@ -3,6 +3,7 @@ name: Protocol Build & Push Image to AWS ECR on: # yamllint disable-line rule:truthy push: branches: + - roy/pruneorder - main - 'release/[a-z]+/v0.[0-9]+.x' # e.g. release/protocol/v0.1.x - 'release/[a-z]+/v[0-9]+.x' # e.g. release/protocol/v1.x diff --git a/.github/workflows/protocol-build-and-push.yml b/.github/workflows/protocol-build-and-push.yml index b2463692f73..2ced5251daf 100644 --- a/.github/workflows/protocol-build-and-push.yml +++ b/.github/workflows/protocol-build-and-push.yml @@ -3,6 +3,7 @@ name: Protocol Build & Push Image to AWS ECR on: # yamllint disable-line rule:truthy push: branches: + - roy/pruneorder - main - 'release/[a-z]+/v0.[0-9]+.x' # e.g. release/protocol/v0.1.x - 'release/[a-z]+/v[0-9]+.x' # e.g. release/protocol/v1.x diff --git a/protocol/x/clob/keeper/order_state.go b/protocol/x/clob/keeper/order_state.go index 814684e1415..b2b6f1ca375 100644 --- a/protocol/x/clob/keeper/order_state.go +++ b/protocol/x/clob/keeper/order_state.go @@ -1,6 +1,8 @@ package keeper import ( + "bytes" + "cosmossdk.io/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/dydxprotocol/v4-chain/protocol/lib" @@ -135,51 +137,17 @@ func (k Keeper) AddOrdersForPruning(ctx sdk.Context, orderIds []types.OrderId, p []byte(types.BlockHeightToPotentiallyPrunableOrdersPrefix), ) - // Retrieve the `PotentiallyPrunableOrders` bytes from the store. - potentiallyPrunableOrdersBytes := store.Get( - lib.Uint32ToKey(prunableBlockHeight), - ) - - var potentiallyPrunableOrdersSet = make(map[types.OrderId]bool) - var potentiallyPrunableOrders = types.PotentiallyPrunableOrders{} - var potentiallyPrunableOrderIds = make([]types.OrderId, len(orderIds)) - - // Initialize `potentiallyPrunableOrderIds` with the provided `orderIds`. - // Copy to avoid mutating the provided `orderIds`. - copy(potentiallyPrunableOrderIds, orderIds) - - // If the state already contains `potentiallyPrunableOrders` for this `prunableBlockHeight`, add them to the list of - // `potentiallyPrunableOrderIds`. - if potentiallyPrunableOrdersBytes != nil { - k.cdc.MustUnmarshal(potentiallyPrunableOrdersBytes, &potentiallyPrunableOrders) - potentiallyPrunableOrderIds = append(potentiallyPrunableOrders.OrderIds, potentiallyPrunableOrderIds...) - } - - // Iterate over all `potentiallyPrunableOrderIds` and place them in the set in order to dedupe them. - for _, orderId := range potentiallyPrunableOrderIds { - potentiallyPrunableOrdersSet[orderId] = true - } - - // Iterate over the set and build a list of `dedupedOrderIds`. - var dedupedOrderIds = make([]types.OrderId, 0, len(potentiallyPrunableOrdersSet)) - for orderId := range potentiallyPrunableOrdersSet { - dedupedOrderIds = append(dedupedOrderIds, orderId) + for _, orderId := range orderIds { + // Write `prunableOrders` to state for the appropriate block height. + var buf bytes.Buffer + buf.Write(lib.Uint32ToKey(prunableBlockHeight)) + buf.Write([]byte(":")) + buf.Write(orderId.ToStateKey()) + store.Set( + buf.Bytes(), + k.cdc.MustMarshal(&orderId), + ) } - - // Sort the orderIds so that the state write is deterministic. - types.MustSortAndHaveNoDuplicates(dedupedOrderIds) - - // Set the new `dedupedOrderIds` on the `potentiallyPrunableOrders`. - potentiallyPrunableOrders.OrderIds = dedupedOrderIds - - // Marshal `prunableOrders` back to bytes. - potentiallyPrunableOrdersBytes = k.cdc.MustMarshal(&potentiallyPrunableOrders) - - // Write `prunableOrders` to state for the appropriate block height. - store.Set( - lib.Uint32ToKey(prunableBlockHeight), - potentiallyPrunableOrdersBytes, - ) } // PruneOrdersForBlockHeight checks all orders for prunability given the provided `blockHeight`. @@ -187,27 +155,22 @@ func (k Keeper) AddOrdersForPruning(ctx sdk.Context, orderIds []types.OrderId, p // Note: An order is only deemed prunable if the `prunableBlockHeight` on the `OrderFillState` is less than or equal // to the provided `blockHeight` passed this method. Returns a slice of unique `OrderIds` which were pruned from state. func (k Keeper) PruneOrdersForBlockHeight(ctx sdk.Context, blockHeight uint32) (prunedOrderIds []types.OrderId) { - // Retrieve an instance of the stores. - blockHeightToPotentiallyPrunableOrdersStore := prefix.NewStore( - ctx.KVStore(k.storeKey), - []byte(types.BlockHeightToPotentiallyPrunableOrdersPrefix), - ) + var buf bytes.Buffer + buf.Write([]byte(types.BlockHeightToPotentiallyPrunableOrdersPrefix)) + buf.Write(lib.Uint32ToKey(blockHeight)) + buf.Write([]byte(":")) - // Retrieve the raw bytes of the `prunableOrders`. - potentiallyPrunableOrderBytes := blockHeightToPotentiallyPrunableOrdersStore.Get( - lib.Uint32ToKey(blockHeight), + potentiallyPrunableOrdersStore := prefix.NewStore( + ctx.KVStore(k.storeKey), + buf.Bytes(), ) - // If there are no prunable orders for this block, then there is nothing to do. Early return. - if potentiallyPrunableOrderBytes == nil { - return - } - - var potentiallyPrunableOrders types.PotentiallyPrunableOrders - k.cdc.MustUnmarshal(potentiallyPrunableOrderBytes, &potentiallyPrunableOrders) + it := potentiallyPrunableOrdersStore.Iterator(nil, nil) + defer it.Close() - for _, orderId := range potentiallyPrunableOrders.OrderIds { - // Check if the order can be pruned, and prune if so. + for ; it.Valid(); it.Next() { + var orderId types.OrderId + k.cdc.MustUnmarshal(it.Value(), &orderId) exists, _, prunableBlockHeight := k.GetOrderFillAmount(ctx, orderId) if exists && prunableBlockHeight <= blockHeight { k.RemoveOrderFillAmount(ctx, orderId) @@ -221,13 +184,9 @@ func (k Keeper) PruneOrdersForBlockHeight(ctx sdk.Context, blockHeight uint32) ( ) } } + potentiallyPrunableOrdersStore.Delete(it.Key()) } - // Delete the key for prunable orders at this block height. - blockHeightToPotentiallyPrunableOrdersStore.Delete( - lib.Uint32ToKey(blockHeight), - ) - return prunedOrderIds }