-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_rate_limiter.go
212 lines (192 loc) · 7.9 KB
/
order_rate_limiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package rate_limit
import (
"github.com/jinxprotocol/v4-chain/protocol/lib"
"github.com/jinxprotocol/v4-chain/protocol/lib/metrics"
"github.com/jinxprotocol/v4-chain/protocol/x/clob/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// A RateLimiter which rate limits types.MsgPlaceOrder.
//
// The rate limiting keeps track of short term and stateful orders placed during
// CheckTx.
type placeOrderRateLimiter struct {
checkStateShortTermOrderRateLimiter RateLimiter[string]
checkStateStatefulOrderRateLimiter RateLimiter[string]
// The set of rate limited accounts is only stored for telemetry purposes.
rateLimitedAccounts map[string]bool
}
var _ RateLimiter[*types.MsgPlaceOrder] = (*placeOrderRateLimiter)(nil)
// NewPlaceOrderRateLimiter returns a RateLimiter which rate limits types.MsgPlaceOrder based upon the provided
// types.BlockRateLimitConfiguration. The rate limiter currently supports limiting based upon:
// - how many short term orders per account (by using string).
// - how many stateful order per account (by using string).
//
// The rate limiting must only be used during `CheckTx` because the rate limiting information is not recovered
// on application restart preventing it from being deterministic during `DeliverTx`.
//
// Depending upon the provided types.BlockRateLimitConfiguration, the returned RateLimiter may rely on:
// - `ctx.BlockHeight()` in RateLimit to track which block the rate limit should apply to.
// - `ctx.BlockHeight()` in PruneRateLimits and should be invoked during `EndBlocker`. If invoked
// during `PrepareCheckState` one must supply a `ctx` with the previous block height via
// `ctx.WithBlockHeight(ctx.BlockHeight()-1)`.
func NewPlaceOrderRateLimiter(config types.BlockRateLimitConfiguration) RateLimiter[*types.MsgPlaceOrder] {
if err := config.Validate(); err != nil {
panic(err)
}
// Return the no-op rate limiter if the configuration is empty.
if len(config.MaxShortTermOrdersPerNBlocks)+len(config.MaxStatefulOrdersPerNBlocks) == 0 {
return noOpRateLimiter[*types.MsgPlaceOrder]{}
}
r := placeOrderRateLimiter{
rateLimitedAccounts: make(map[string]bool, 0),
}
if len(config.MaxShortTermOrdersPerNBlocks) == 0 {
r.checkStateShortTermOrderRateLimiter = NewNoOpRateLimiter[string]()
} else if len(config.MaxShortTermOrdersPerNBlocks) == 1 &&
config.MaxShortTermOrdersPerNBlocks[0].NumBlocks == 1 {
r.checkStateShortTermOrderRateLimiter = NewSingleBlockRateLimiter[string](
"MaxShortTermOrdersPerNBlocks",
config.MaxShortTermOrdersPerNBlocks[0],
)
} else {
r.checkStateShortTermOrderRateLimiter = NewMultiBlockRateLimiter[string](
"MaxShortTermOrdersPerNBlocks",
config.MaxShortTermOrdersPerNBlocks,
)
}
if len(config.MaxStatefulOrdersPerNBlocks) == 0 {
r.checkStateStatefulOrderRateLimiter = NewNoOpRateLimiter[string]()
} else if len(config.MaxStatefulOrdersPerNBlocks) == 1 &&
config.MaxStatefulOrdersPerNBlocks[0].NumBlocks == 1 {
r.checkStateStatefulOrderRateLimiter = NewSingleBlockRateLimiter[string](
"MaxStatefulOrdersPerNBlocks",
config.MaxStatefulOrdersPerNBlocks[0],
)
} else {
r.checkStateStatefulOrderRateLimiter = NewMultiBlockRateLimiter[string](
"MaxStatefulOrdersPerNBlocks",
config.MaxStatefulOrdersPerNBlocks,
)
}
return &r
}
func (r *placeOrderRateLimiter) RateLimit(ctx sdk.Context, msg *types.MsgPlaceOrder) (err error) {
lib.AssertCheckTxMode(ctx)
if msg.Order.IsShortTermOrder() {
err = r.checkStateShortTermOrderRateLimiter.RateLimit(
ctx,
msg.Order.OrderId.SubaccountId.Owner,
)
} else {
msg.Order.MustBeStatefulOrder()
err = r.checkStateStatefulOrderRateLimiter.RateLimit(ctx, msg.Order.OrderId.SubaccountId.Owner)
}
if err != nil {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, metrics.RateLimit, metrics.PlaceOrder, metrics.Count},
1,
msg.Order.GetOrderLabels(),
)
r.rateLimitedAccounts[msg.Order.OrderId.SubaccountId.Owner] = true
}
return err
}
func (r *placeOrderRateLimiter) PruneRateLimits(ctx sdk.Context) {
telemetry.IncrCounter(
float32(len(r.rateLimitedAccounts)),
types.ModuleName,
metrics.RateLimit,
metrics.PlaceOrderAccounts,
metrics.Count,
)
// Note that this method for clearing the map is optimized by the go compiler significantly
// and will leave the relative size of the map the same so that it doesn't need to be resized
// often.
for key := range r.rateLimitedAccounts {
delete(r.rateLimitedAccounts, key)
}
r.checkStateShortTermOrderRateLimiter.PruneRateLimits(ctx)
r.checkStateStatefulOrderRateLimiter.PruneRateLimits(ctx)
}
// A RateLimiter which rate limits types.MsgCancelOrder.
//
// The rate limiting keeps track of short term order cancellations during CheckTx.
type cancelOrderRateLimiter struct {
checkStateShortTermRateLimiter RateLimiter[string]
// The set of rate limited accounts is only stored for telemetry purposes.
rateLimitedAccounts map[string]bool
}
var _ RateLimiter[*types.MsgCancelOrder] = (*cancelOrderRateLimiter)(nil)
// NewCancelOrderRateLimiter returns a RateLimiter which rate limits types.MsgCancelOrder based upon the provided
// types.BlockRateLimitConfiguration. The rate limiter currently supports limiting based upon:
// - how many short term order cancellations per account (by using string).
//
// The rate limiting must only be used during `CheckTx` because the rate limiting information is not recovered
// on application restart preventing it from being deterministic during `DeliverTx`.
//
// Depending upon the provided types.BlockRateLimitConfiguration, the returned RateLimiter may rely on:
// - `ctx.BlockHeight()` in RateLimit to track which block the rate limit should apply to.
// - `ctx.BlockHeight()` in PruneRateLimits and should be invoked during `EndBlocker`. If invoked
// during `PrepareCheckState` one must supply a `ctx` with the previous block height via
// `ctx.WithBlockHeight(ctx.BlockHeight()-1)`.
func NewCancelOrderRateLimiter(config types.BlockRateLimitConfiguration) RateLimiter[*types.MsgCancelOrder] {
if err := config.Validate(); err != nil {
panic(err)
}
// Return the no-op rate limiter if the configuration is empty.
if len(config.MaxShortTermOrderCancellationsPerNBlocks) == 0 {
return noOpRateLimiter[*types.MsgCancelOrder]{}
}
rateLimiter := cancelOrderRateLimiter{
rateLimitedAccounts: make(map[string]bool, 0),
}
if len(config.MaxShortTermOrderCancellationsPerNBlocks) == 1 &&
config.MaxShortTermOrderCancellationsPerNBlocks[0].NumBlocks == 1 {
rateLimiter.checkStateShortTermRateLimiter = NewSingleBlockRateLimiter[string](
"MaxShortTermOrdersPerNBlocks",
config.MaxShortTermOrderCancellationsPerNBlocks[0],
)
return &rateLimiter
} else {
rateLimiter.checkStateShortTermRateLimiter = NewMultiBlockRateLimiter[string](
"MaxShortTermOrdersPerNBlocks",
config.MaxShortTermOrderCancellationsPerNBlocks,
)
return &rateLimiter
}
}
func (r *cancelOrderRateLimiter) RateLimit(ctx sdk.Context, msg *types.MsgCancelOrder) (err error) {
lib.AssertCheckTxMode(ctx)
if msg.OrderId.IsShortTermOrder() {
err = r.checkStateShortTermRateLimiter.RateLimit(
ctx,
msg.OrderId.SubaccountId.Owner,
)
}
if err != nil {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, metrics.RateLimit, metrics.CancelOrder, metrics.Count},
1,
msg.OrderId.GetOrderIdLabels(),
)
r.rateLimitedAccounts[msg.OrderId.SubaccountId.Owner] = true
}
return err
}
func (r *cancelOrderRateLimiter) PruneRateLimits(ctx sdk.Context) {
telemetry.IncrCounter(
float32(len(r.rateLimitedAccounts)),
types.ModuleName,
metrics.RateLimit,
metrics.CancelOrderAccounts,
metrics.Count,
)
// Note that this method for clearing the map is optimized by the go compiler significantly
// and will leave the relative size of the map the same so that it doesn't need to be resized
// often.
for key := range r.rateLimitedAccounts {
delete(r.rateLimitedAccounts, key)
}
r.checkStateShortTermRateLimiter.PruneRateLimits(ctx)
}