Commit 8f73fa3
Edward (Mike's bot)
fix(order-book): widen money-math products to u128 to avoid spurious u64 overflow
place_order and cancel_order both do price * quantity in u64. u64 * u64
overflows at ~1.8e19 base units \u2014 perfectly reachable once you scale by
token decimals (an 18-decimal quote mint hits it at modest mid-cap prices
and quantities). u64::checked_mul would then refuse a legitimate order
with NumericalOverflow even though the final lock amount fits a u64
balance fine.
Promote both operands to u128 before the multiply, then narrow back to
u64 with try_into. Same pattern the gross_quote-fee-quote chain was
already using \u2014 now used consistently for:
- bid lock (price * quantity)
- per-fill gross_quote (fill_price * fill_quantity)
- per-fill locked_for_this_fill rebate base (price * fill_quantity)
- cancel_order Bid refund (order.price * remaining)
Also: replace order.filled_quantity = quantity.saturating_sub(taker_remaining)
with checked_sub. saturating_* on the matching engine's bookkeeping is a
silent-clamp hazard; if taker_remaining > quantity that's a real bug and
the program should abort, not write a misleading filled_quantity.
Also: add require!(fee_quote <= gross_quote) after the fee calculation
as a defence-in-depth invariant. fee_basis_points is bounded at init,
so the require is unreachable in normal operation \u2014 but a stale bound
assumption would otherwise let a misconfigured market overdraw the
maker's net payout silently.1 parent 7c3f9c8 commit 8f73fa3
2 files changed
Lines changed: 53 additions & 15 deletions
Lines changed: 10 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
34 | 40 | | |
35 | 41 | | |
36 | 42 | | |
| |||
Lines changed: 43 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
53 | 61 | | |
54 | 62 | | |
55 | 63 | | |
56 | 64 | | |
57 | 65 | | |
58 | 66 | | |
59 | | - | |
60 | | - | |
61 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
62 | 72 | | |
63 | 73 | | |
64 | 74 | | |
| |||
173 | 183 | | |
174 | 184 | | |
175 | 185 | | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
180 | 195 | | |
181 | 196 | | |
182 | 197 | | |
| |||
186 | 201 | | |
187 | 202 | | |
188 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
189 | 210 | | |
190 | 211 | | |
191 | 212 | | |
| |||
203 | 224 | | |
204 | 225 | | |
205 | 226 | | |
206 | | - | |
207 | | - | |
208 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
209 | 235 | | |
210 | 236 | | |
211 | 237 | | |
| |||
352 | 378 | | |
353 | 379 | | |
354 | 380 | | |
355 | | - | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
356 | 388 | | |
357 | 389 | | |
358 | 390 | | |
| |||
0 commit comments