Skip to content

Commit

Permalink
basic tests with only batch cancel orders submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfung-dydx committed Mar 13, 2024
1 parent 2b4dbb4 commit e73f7c2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
41 changes: 41 additions & 0 deletions protocol/x/clob/e2e/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,47 @@ var (
constants.ConditionalOrder_Alice_Num1_Id0_Clob0_Sell5_Price10_GTB15,
testapp.DefaultGenesis(),
))

BatchCancel_Alice_Num0_Clob0_1_2_3_GTB5 = *clobtypes.NewMsgBatchCancel(
constants.Alice_Num0,
[]clobtypes.OrderBatch{
{
ClobPairId: 0,
ClientIds: []uint32{1, 2, 3},
},
},
5,
)
BatchCancel_Alice_Num0_Clob0_1_2_3_GTB27 = *clobtypes.NewMsgBatchCancel(
constants.Alice_Num0,
[]clobtypes.OrderBatch{
{
ClobPairId: 0,
ClientIds: []uint32{1, 2, 3},
},
},
27,
)
BatchCancel_Alice_Num0_Clob0_1_2_3_GTB20 = *clobtypes.NewMsgBatchCancel(
constants.Alice_Num0,
[]clobtypes.OrderBatch{
{
ClobPairId: 0,
ClientIds: []uint32{1, 2, 3},
},
},
20,
)
BatchCancel_Alice_Num1_Clob0_1_2_3_GTB20 = *clobtypes.NewMsgBatchCancel(
constants.Alice_Num1,
[]clobtypes.OrderBatch{
{
ClobPairId: 0,
ClientIds: []uint32{1, 2, 3},
},
},
20,
)
)

// We place 300 orders that match and 700 orders followed by their cancellations concurrently.
Expand Down
56 changes: 47 additions & 9 deletions protocol/x/clob/e2e/rate_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (

func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
tests := map[string]struct {
blockRateLimitConifg clobtypes.BlockRateLimitConfiguration
blockRateLimitConfig clobtypes.BlockRateLimitConfiguration
firstMsg sdktypes.Msg
secondMsg sdktypes.Msg
}{
"Short term orders with same subaccounts": {
blockRateLimitConifg: clobtypes.BlockRateLimitConfiguration{
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxShortTermOrdersAndCancelsPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Expand All @@ -36,7 +36,7 @@ func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
secondMsg: &PlaceOrder_Alice_Num0_Id0_Clob1_Buy5_Price10_GTB20,
},
"Short term orders with different subaccounts": {
blockRateLimitConifg: clobtypes.BlockRateLimitConfiguration{
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxShortTermOrdersAndCancelsPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Expand All @@ -48,7 +48,7 @@ func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
secondMsg: &PlaceOrder_Alice_Num1_Id0_Clob0_Buy5_Price10_GTB20,
},
"Stateful orders with same subaccounts": {
blockRateLimitConifg: clobtypes.BlockRateLimitConfiguration{
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxStatefulOrdersPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Expand All @@ -60,7 +60,7 @@ func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
secondMsg: &LongTermPlaceOrder_Alice_Num0_Id0_Clob1_Buy5_Price10_GTBT5,
},
"Stateful orders with different subaccounts": {
blockRateLimitConifg: clobtypes.BlockRateLimitConfiguration{
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxStatefulOrdersPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Expand All @@ -72,7 +72,7 @@ func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
secondMsg: &LongTermPlaceOrder_Alice_Num1_Id0_Clob0_Buy5_Price10_GTBT5,
},
"Short term order cancellations with same subaccounts": {
blockRateLimitConifg: clobtypes.BlockRateLimitConfiguration{
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxShortTermOrdersAndCancelsPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Expand All @@ -84,7 +84,7 @@ func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
secondMsg: &CancelOrder_Alice_Num0_Id0_Clob0_GTB20,
},
"Short term order cancellations with different subaccounts": {
blockRateLimitConifg: clobtypes.BlockRateLimitConfiguration{
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxShortTermOrdersAndCancelsPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Expand All @@ -95,6 +95,30 @@ func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
firstMsg: &CancelOrder_Alice_Num0_Id0_Clob1_GTB5,
secondMsg: &CancelOrder_Alice_Num1_Id0_Clob0_GTB20,
},
"Batch cancellations with same subaccounts": {
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxShortTermOrdersAndCancelsPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Limit: 1,
},
},
},
firstMsg: &BatchCancel_Alice_Num0_Clob0_1_2_3_GTB5,
secondMsg: &BatchCancel_Alice_Num0_Clob0_1_2_3_GTB20,
},
"Batch cancellations with different subaccounts": {
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxShortTermOrdersAndCancelsPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 2,
Limit: 1,
},
},
},
firstMsg: &BatchCancel_Alice_Num0_Clob0_1_2_3_GTB5,
secondMsg: &BatchCancel_Alice_Num1_Clob0_1_2_3_GTB20,
},
}

for name, tc := range tests {
Expand All @@ -107,7 +131,7 @@ func TestRateLimitingOrders_RateLimitsAreEnforced(t *testing.T) {
testapp.UpdateGenesisDocWithAppStateForModule(
&genesis,
func(genesisState *clobtypes.GenesisState) {
genesisState.BlockRateLimitConfig = tc.blockRateLimitConifg
genesisState.BlockRateLimitConfig = tc.blockRateLimitConfig
},
)
testapp.UpdateGenesisDocWithAppStateForModule(
Expand Down Expand Up @@ -514,6 +538,20 @@ func TestRateLimitingShortTermOrders_GuardedAgainstReplayAttacks(t *testing.T) {
firstValidGTB: &CancelOrder_Alice_Num0_Id0_Clob0_GTB20,
secondValidGTB: &CancelOrder_Alice_Num1_Id0_Clob0_GTB20,
},
"Batch cancellations": {
blockRateLimitConfig: clobtypes.BlockRateLimitConfiguration{
MaxShortTermOrdersAndCancelsPerNBlocks: []clobtypes.MaxPerNBlocksRateLimit{
{
NumBlocks: 1,
Limit: 3, // TODO FIX THIS ONCE WEIGHTS ARE DECIDED
},
},
},
replayLessGTB: &BatchCancel_Alice_Num0_Clob0_1_2_3_GTB5,
replayGreaterGTB: &BatchCancel_Alice_Num0_Clob0_1_2_3_GTB27,
firstValidGTB: &BatchCancel_Alice_Num0_Clob0_1_2_3_GTB20,
secondValidGTB: &BatchCancel_Alice_Num0_Clob0_1_2_3_GTB20,
},
}

for name, tc := range tests {
Expand Down Expand Up @@ -586,7 +624,7 @@ func TestRateLimitingShortTermOrders_GuardedAgainstReplayAttacks(t *testing.T) {
resp = tApp.CheckTx(secondCheckTx)
require.Conditionf(t, resp.IsErr, "Expected CheckTx to error. Response: %+v", resp)
require.Equal(t, clobtypes.ErrBlockRateLimitExceeded.ABCICode(), resp.Code)
require.Contains(t, resp.Log, "Rate of 2 exceeds configured block rate limit")
require.Contains(t, resp.Log, "exceeds configured block rate limit")
})
}
}

0 comments on commit e73f7c2

Please sign in to comment.