-
Notifications
You must be signed in to change notification settings - Fork 186
(fast4) inject final order_uid in proposed_trade_executions #4859
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,6 +218,7 @@ impl<T: Send + Sync + Clone, W: Send + Sync> OnchainOrderParser<T, W> { | |
| order_placement_events: Vec<(ContractEvent, Log)>, | ||
| ) -> Result<( | ||
| Vec<W>, | ||
| Vec<i64>, | ||
| Vec<Option<database::orders::Quote>>, | ||
| Vec<(database::events::EventIndex, OnchainOrderPlacement)>, | ||
| Vec<Order>, | ||
|
|
@@ -265,7 +266,7 @@ impl<T: Send + Sync + Clone, W: Send + Sync> OnchainOrderParser<T, W> { | |
| .await; | ||
|
|
||
| let data_tuple = onchain_order_data.into_iter().map( | ||
| |(event_index, quote, onchain_order_placement, order, tx_hash)| { | ||
| |(event_index, quote_id, quote, onchain_order_placement, order, tx_hash)| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with this amount of parameters, I think it's time we make this a function |
||
| ( | ||
| self.custom_onchain_data_parser | ||
| .customized_event_data_for_event_index( | ||
|
|
@@ -274,6 +275,7 @@ impl<T: Send + Sync + Clone, W: Send + Sync> OnchainOrderParser<T, W> { | |
| &custom_data_hashmap, | ||
| &onchain_order_placement, | ||
| ), | ||
| quote_id, | ||
| quote, | ||
| (event_index, onchain_order_placement), | ||
| order, | ||
|
|
@@ -318,9 +320,9 @@ impl<T: Send + Sync + Clone, W: Send + Sync> OnchainOrderParser<T, W> { | |
| .collect(); | ||
| let invalidation_events = get_invalidation_events(events)?; | ||
| let invalided_order_uids = extract_invalidated_order_uids(invalidation_events)?; | ||
| let (custom_onchain_data, quotes, broadcasted_order_data, mut orders, tx_hashes) = self | ||
| .extract_custom_and_general_order_data(order_placement_events) | ||
| .await?; | ||
| let (custom_onchain_data, quote_ids, quotes, broadcasted_order_data, mut orders, tx_hashes) = | ||
| self.extract_custom_and_general_order_data(order_placement_events) | ||
| .await?; | ||
|
|
||
| database::onchain_invalidations::insert_onchain_invalidations( | ||
| transaction, | ||
|
|
@@ -364,6 +366,29 @@ impl<T: Send + Sync + Clone, W: Send + Sync> OnchainOrderParser<T, W> { | |
| .await | ||
| .context("insert_orders failed")?; | ||
|
|
||
| // Promote fast-path quotes for onchain orders (mirrors the trait-based | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't we missing applying the |
||
| // path in the orderbook). For each successfully-quoted order tied to a | ||
| // fast-path `auction_id`, drop the transient `quotes` row and rewrite | ||
| // the placeholder `proposed_trade_executions.order_uid` to the real | ||
| // one. | ||
| for (quote_id, quote, order) in izip!("e_ids, "es, &orders) { | ||
| let Some(quote) = quote else { | ||
| continue; | ||
| }; | ||
| let Some(auction_id) = quote.auction_id else { | ||
| continue; | ||
| }; | ||
| // The order_quotes row already carries the auction_id (populated | ||
| // inline above by `insert_quotes`), so all that's left is to drop | ||
| // the transient `quotes` row and patch competition tables. | ||
| database::quotes::delete_and_return_row(transaction, *quote_id) | ||
| .await | ||
| .context("failed to delete promoted onchain quote")?; | ||
| database::fast_path::finalize_quote_competition(transaction, auction_id, order.uid) | ||
| .await | ||
| .context("failed to patch competition rows for onchain order")?; | ||
| } | ||
|
Comment on lines
+374
to
+390
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop patches |
||
|
|
||
| for order in &invalided_order_uids { | ||
| tracing::debug!(?order, "invalidated order"); | ||
| } | ||
|
|
@@ -442,6 +467,7 @@ fn extract_invalidated_order_uids( | |
|
|
||
| type GeneralOnchainOrderPlacementData = ( | ||
| EventIndex, | ||
| i64, | ||
| Option<database::orders::Quote>, | ||
| OnchainOrderPlacement, | ||
| Order, | ||
|
|
@@ -513,7 +539,14 @@ where | |
| None | ||
| } | ||
| }; | ||
| Ok((event_index, quote, order_data.0, order_data.1, tx_hash)) | ||
| Ok(( | ||
| event_index, | ||
| quote_id, | ||
| quote, | ||
| order_data.0, | ||
| order_data.1, | ||
| tx_hash, | ||
| )) | ||
| }, | ||
| ); | ||
| let onchain_order_placement_data: Vec<Result<GeneralOnchainOrderPlacementData>> = | ||
|
|
@@ -1319,9 +1352,10 @@ mod test { | |
| metadata: quote.data.metadata.clone().try_into().unwrap(), | ||
| auction_id: quote.data.auction_id, | ||
| }; | ||
| assert_eq!(result.1, vec![Some(expected_quote)]); | ||
| assert_eq!(result.1, vec![0i64]); | ||
| assert_eq!(result.2, vec![Some(expected_quote)]); | ||
| assert_eq!( | ||
| result.2, | ||
| result.3, | ||
| vec![( | ||
| expected_event_index, | ||
| OnchainOrderPlacement { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //! Database queries for the fast-path settlement feature. | ||
| //! | ||
| //! Fast-path orders reuse a quote's synthetic solver competition as the | ||
| //! actual settlement. This module owns the promotion step that patches | ||
| //! the placeholder rows written at quote time to reference the real | ||
| //! `order_uid` ([`finalize_quote_competition`]). | ||
|
|
||
| use { | ||
| crate::{OrderUid, PgTransaction, auction::AuctionId}, | ||
| std::ops::DerefMut, | ||
| tracing::instrument, | ||
| }; | ||
|
|
||
| /// Because the final order uid is not known when we store the quote | ||
| /// competition data we use `0x000...000` as a sentinel value. | ||
| /// When an order gets placed referencing a quote competition this function | ||
| /// replaces the placeholder value with the now final order uid. | ||
| #[instrument(skip_all)] | ||
| pub async fn finalize_quote_competition( | ||
| ex: &mut PgTransaction<'_>, | ||
| auction_id: AuctionId, | ||
| order_uid: OrderUid, | ||
| ) -> Result<(), sqlx::Error> { | ||
| const QUERY: &str = r#" | ||
| WITH patch_te AS ( | ||
| UPDATE proposed_trade_executions | ||
| SET order_uid = $1 | ||
| WHERE auction_id = $2 AND order_uid = $3 | ||
| ) | ||
| UPDATE competition_auctions | ||
| SET order_uids = ARRAY[$1] | ||
| WHERE id = $2 | ||
| "#; | ||
| sqlx::query(QUERY) | ||
| .bind(order_uid) | ||
| .bind(auction_id) | ||
| .bind(OrderUid::default()) | ||
| .execute(ex.deref_mut()) | ||
| .await?; | ||
| Ok(()) | ||
| } |
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 return type looks pretty complicated. Can we use a QuoteId alias instead of i64 here or refactor it to return a named struct?