-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Combine onchain order event handling logic #2828
Conversation
// Appending events is equivalent to replacing events out of range. | ||
self.replace_events( | ||
events, | ||
RangeInclusive::try_new(u64::MAX, u64::MAX).expect("valid range"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be problematic. We are passing here a u64::MAX
, but then here we are unsafely casting it to a i64
.
I am not so sure how the casting in Rust will affect here, but very likely it will parse to -1
, affecting the range and affecting the database data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, very good catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting issue should be solved. Major impact into database/potential panic.
I looked at better ways to work around this issue, but it looks like we store block number as a bigdecimal in the DB (which only support i64) but the main source of block number (ie. current block stream etc. return u64). So probably the cleanest change would be to alter the tables to store For now I replaced the unsafe cast with a safe one, but I'm not sure this is very safe going forward (I almost wiped the staging DB). |
@fleupold AFAIK, PostgreSQL does not explicitly support unsigned integer types 😞 |
What's more, we should maybe delete all the unsafe castings we have. It is very dangerous. For those who don't implement the trait We could even have a CI to check for unsafe castings 🤔 |
Yes, I can follow up with a PR that adds a lint rule that enforces
Hmh, I don't see a huge advantage of that over the proposed change (where we mindfully cast an overflow down to the largest possible i64, which should still be way above any reasonable block number). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks legit
&mut transaction, | ||
quotes.into_iter().flatten().collect::<Vec<_>>().as_slice(), | ||
// Appending events is equivalent to replacing events out of range. | ||
self.replace_events( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, in order to make it fully modular, we should have two functions delete_events()
and append_events()
(and replace_events()
calls them both). Conceptually we are calling a replace_events()
to append events, but we just use a range out of bounds so it doesn't replace any events, but the intention is not to replace events.
It is more of a concept/split of function nit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, I implemented this, but this changed the PR quite significantly. Therefore re-requesting review...
Description
When looking into adding a log statement when on chain orders are parsed (to debug time to happy moo), I noticed that the
append_events
andreplace_events
logic in the on chain order handler use exactly the same code except for the following block, which is only present inreplace_events
:Combining this logic remove code duplication and allows for easier adjusting of the logic.
Changes
append_events
logic by callingreplace_events
with an range that doesn't affect any orders.How to test
CI
Additional Notes to Reviewers
Please double check my reasoning that
replace_events
indeed mirror the exact logic fromappend_events