Skip to content
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

Merged
merged 7 commits into from
Jul 26, 2024
Merged

Conversation

fleupold
Copy link
Contributor

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 and replace_events logic in the on chain order handler use exactly the same code except for the following block, which is only present in replace_events:

database::onchain_broadcasted_orders::mark_as_reorged(
    &mut transaction,
    *range.start() as i64,
)
.await
.context("mark_onchain_order_events failed")?;

database::onchain_invalidations::delete_invalidations(
    &mut transaction,
    *range.start() as i64,
)
.await
.context("invalidating_onchain_order_events failed")?;

Combining this logic remove code duplication and allows for easier adjusting of the logic.

Changes

  • Replace the append_events logic by calling replace_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 from append_events

@fleupold fleupold requested a review from a team as a code owner July 24, 2024 09:55
// Appending events is equivalent to replacing events out of range.
self.replace_events(
events,
RangeInclusive::try_new(u64::MAX, u64::MAX).expect("valid range"),
Copy link
Contributor

@m-lord-renkse m-lord-renkse Jul 24, 2024

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, very good catch

Copy link
Contributor

@m-lord-renkse m-lord-renkse left a 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.

@fleupold
Copy link
Contributor Author

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 numeric instead of bigint

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).

@m-lord-renkse
Copy link
Contributor

m-lord-renkse commented Jul 24, 2024

@fleupold AFAIK, PostgreSQL does not explicitly support unsigned integer types 😞
We can create an internal type (with an i64 inside) which is forced to always be positive. And implement the sqlx derives so it can be used in the database 🤔

@m-lord-renkse
Copy link
Contributor

m-lord-renkse commented Jul 24, 2024

What's more, we should maybe delete all the unsafe castings we have. It is very dangerous.
For types which have immediate casting (like u32 to u64), the From<> trait is already automatically derived (u64::from() vs x as u64), but these castings are fine with doing it with as.

For those who don't implement the trait From<> (because they do the TryFrom<> trait), then the unsafe casting should be definitely removed.

We could even have a CI to check for unsafe castings 🤔

@fleupold
Copy link
Contributor Author

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 cast_possible_wrap = "deny".

For types which have immediate casting (like u32 to u64), the From<> trait is already automatically derived (u64::from() vs x as u64), but these castings are fine with doing it with as.

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).

Copy link
Contributor

@squadgazzz squadgazzz left a 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(
Copy link
Contributor

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.

Copy link
Contributor Author

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...

@fleupold fleupold enabled auto-merge (squash) July 26, 2024 08:26
@fleupold fleupold merged commit e636805 into main Jul 26, 2024
10 checks passed
@fleupold fleupold deleted the combine_event_handling_logic branch July 26, 2024 08:33
@github-actions github-actions bot locked and limited conversation to collaborators Jul 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants