Skip to content

Commit

Permalink
reuse same order ID in vault
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyaoy committed Jun 5, 2024
1 parent 0e67d7a commit d3c5c22
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/protocol-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on: # yamllint disable-line rule:truthy
- main
- 'release/protocol/v[0-9]+.[0-9]+.x' # e.g. release/protocol/v0.1.x
- 'release/protocol/v[0-9]+.x' # e.g. release/protocol/v1.x

- 'chenyao/reuse-order-id-in-vault'
jobs:
build-and-push-dev:
runs-on: ubuntu-latest
Expand Down
7 changes: 2 additions & 5 deletions protocol/x/vault/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,9 @@ func (k Keeper) GetVaultClobOrderClientId(
sideBit := uint32(side - 1)
sideBit <<= 31

blockHeightBit := uint32(ctx.BlockHeight() % 2)
blockHeightBit <<= 30
layerBits := uint32(layer) << 23

layerBits := uint32(layer) << 22

return sideBit | blockHeightBit | layerBits
return sideBit | layerBits
}

// PlaceVaultClobOrder places a vault CLOB order as an order internal to the protocol,
Expand Down
62 changes: 20 additions & 42 deletions protocol/x/vault/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,64 +696,42 @@ func TestGetVaultClobOrderClientId(t *testing.T) {
/* --- Setup --- */
// side.
side clobtypes.Order_Side
// block height.
blockHeight int64
// layer.
layer uint8

/* --- Expectations --- */
// Expected client ID.
expectedClientId uint32
}{
"Buy, Block Height Odd, Layer 1": {
"Buy, Layer 1": {
side: clobtypes.Order_SIDE_BUY, // 0<<31
blockHeight: 1, // 1<<30
layer: 1, // 1<<22
expectedClientId: 0<<31 | 1<<30 | 1<<22,
layer: 1, // 1<<23
expectedClientId: 0<<31 | 1<<23,
},
"Buy, Block Height Even, Layer 1": {
side: clobtypes.Order_SIDE_BUY, // 0<<31
blockHeight: 2, // 0<<30
layer: 1, // 1<<22
expectedClientId: 0<<31 | 0<<30 | 1<<22,
},
"Sell, Block Height Odd, Layer 2": {
"Sell, Layer 2": {
side: clobtypes.Order_SIDE_SELL, // 1<<31
blockHeight: 1, // 1<<30
layer: 2, // 2<<22
expectedClientId: 1<<31 | 1<<30 | 2<<22,
layer: 2, // 2<<23
expectedClientId: 1<<31 | 2<<23,
},
"Sell, Block Height Even, Layer 2": {
side: clobtypes.Order_SIDE_SELL, // 1<<31
blockHeight: 2, // 0<<30
layer: 2, // 2<<22
expectedClientId: 1<<31 | 0<<30 | 2<<22,
},
"Buy, Block Height Even, Layer Max Uint8": {
"Buy, Layer Max Uint8": {
side: clobtypes.Order_SIDE_BUY, // 0<<31
blockHeight: 123456, // 0<<30
layer: math.MaxUint8, // 255<<22
expectedClientId: 0<<31 | 0<<30 | 255<<22,
layer: math.MaxUint8, // 255<<23
expectedClientId: 0<<31 | 255<<23,
},
"Sell, Block Height Odd, Layer 0": {
"Sell, Layer 0": {
side: clobtypes.Order_SIDE_SELL, // 1<<31
blockHeight: 12345654321, // 1<<30
layer: 0, // 0<<22
expectedClientId: 1<<31 | 1<<30 | 0<<22,
layer: 0, // 0<<23
expectedClientId: 1<<31 | 0<<23,
},
"Sell, Block Height Odd (negative), Layer 202": {
side: clobtypes.Order_SIDE_SELL, // 1<<31
// Negative block height shouldn't happen but blockHeight
// is represented as int64.
blockHeight: -678987, // 1<<30
layer: 202, // 202<<22
expectedClientId: 1<<31 | 1<<30 | 202<<22,
"Sell, Layer 202": {
side: clobtypes.Order_SIDE_SELL, // 1<<31
layer: 202, // 202<<23
expectedClientId: 1<<31 | 202<<23,
},
"Buy, Block Height Even (zero), Layer 157": {
"Buy, Layer 157": {
side: clobtypes.Order_SIDE_SELL, // 1<<31
blockHeight: 0, // 0<<30
layer: 157, // 157<<22
expectedClientId: 1<<31 | 0<<30 | 157<<22,
layer: 157, // 157<<23
expectedClientId: 1<<31 | 157<<23,
},
}

Expand All @@ -763,7 +741,7 @@ func TestGetVaultClobOrderClientId(t *testing.T) {
ctx := tApp.InitChain()

clientId := tApp.App.VaultKeeper.GetVaultClobOrderClientId(
ctx.WithBlockHeight(tc.blockHeight),
ctx,
tc.side,
tc.layer,
)
Expand Down

0 comments on commit d3c5c22

Please sign in to comment.