If the hint order id has been reused and the previous order type is matched
the current code iterates from the head of the linked list under the assumption that since the previous order has been matched it must have been at the top of the orderbook which would mean the new order with a similar price would also be somewhere near the top of the orderbook
.
https://github.com/Cyfrin/2023-09-ditto/blob/a93b4276420a092913f43169a353a6198d3c21b9/contracts/libraries/LibOrders.sol#L927-L947
But it is possible that the initial order was cancelled and the id reused multiple times with the previous order being close to the market price resulting in a match. This can lead to a possible exhaustion of gas if the user's order has a price far from the top of the orderbook.
Current state of bids in orderbook:
Top bid 2000
Total bids 1000
Bids ids are from 100 to 999. No order is cancelled and reusable.
A user wants to bid at 1700 which would be the 800th order pricewise.
User calls createBid
passing in [799,798]
for the orderHintArray.
The following tx's occur in the same block before the user's createBid
call in the following order.
Order id 799
gets cancelled.
Another user creates a limit order at 2001
which now has order id 799
since it is reused.
A market/new limit ask order fills the bid.
Another user creates a limit order at price 1800
.
In createBid
when finding the hint id, the condition prevOrderType == O.Matched
will pass and the hintId returned will be the HEAD
.
The loop starts to check for the price match from HEAD
and exhausts gas before iterating over 800 bids.
Order creation can run out-of-gas on particular flow
Add the following change in test/AskSellOrders.t.sol and run
I think the probability of the above scenario is higher than that of multiple user's cancelling their orders. Hence moving to the next hint order as soon as the current hint order has been found to be reused could be better and will cost less gas on error.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.