Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
roy-dydx committed Mar 22, 2024
1 parent 1788384 commit 46c4029
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 66 deletions.
1 change: 1 addition & 0 deletions .github/workflows/protocol-build-and-push-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/protocol-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 25 additions & 66 deletions protocol/x/clob/keeper/order_state.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -135,79 +137,40 @@ 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`.
// If an order is deemed prunable at this `blockHeight`, then it is pruned.
// 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)
Expand All @@ -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
}

Expand Down

0 comments on commit 46c4029

Please sign in to comment.