From f114258fc82135294fbb34a34dc72e0ca6b5470f Mon Sep 17 00:00:00 2001 From: Will Yang Date: Fri, 28 Jun 2024 13:43:02 -0700 Subject: [PATCH] rename back to scan_limit --- crates/sui-graphql-rpc/src/raw_query.rs | 33 ++++++----- crates/sui-graphql-rpc/src/types/address.rs | 4 +- crates/sui-graphql-rpc/src/types/coin.rs | 4 +- .../src/types/coin_metadata.rs | 4 +- crates/sui-graphql-rpc/src/types/epoch.rs | 4 +- .../sui-graphql-rpc/src/types/move_object.rs | 4 +- .../sui-graphql-rpc/src/types/move_package.rs | 12 +--- crates/sui-graphql-rpc/src/types/object.rs | 10 ++-- crates/sui-graphql-rpc/src/types/query.rs | 4 +- crates/sui-graphql-rpc/src/types/stake.rs | 4 +- .../src/types/suins_registration.rs | 12 +--- .../src/types/transaction_block.rs | 56 +++++++++---------- 12 files changed, 70 insertions(+), 81 deletions(-) diff --git a/crates/sui-graphql-rpc/src/raw_query.rs b/crates/sui-graphql-rpc/src/raw_query.rs index 60795b23a372f1..e91645bf3c23b4 100644 --- a/crates/sui-graphql-rpc/src/raw_query.rs +++ b/crates/sui-graphql-rpc/src/raw_query.rs @@ -179,6 +179,25 @@ macro_rules! or_filter { }}; } +/// Accepts two `Rawquery` instances and a third expression consisting of which columns to join on. +#[macro_export] +macro_rules! inner_join { + ($lhs:expr, $rhs:ident => ($rhs_query:expr, $alias:expr), using: [$using:expr $(, $more_using:expr)*]) => {{ + use $crate::raw_query::RawQuery; + + let (lhs_sql, mut binds) = $lhs.finish(); + let (rhs_sql, rhs_binds) = $rhs_query.finish(); + + binds.extend(rhs_binds); + + let aliased = format!("({}) AS {}", rhs_sql, $alias); + let using_clause = format!("USING ({})", stringify!($using $(, $more_using)*)); + let sql = format!("{} INNER JOIN {} {}", lhs_sql, aliased, using_clause); + + RawQuery::new(sql, binds) + }}; +} + /// Accepts a `SELECT FROM` format string and optional subqueries. If subqueries are provided, there /// should be curly braces `{}` in the format string to interpolate each subquery's sql string into. /// Concatenates subqueries to the `SELECT FROM` clause, and creates a new `RawQuery` from the @@ -192,20 +211,6 @@ macro_rules! query { $crate::raw_query::RawQuery::new($select, vec![]) }; - // Handle a select clause with ongoing subquery and a new subquery with alias - ($select:expr, $ongoing_subquery:expr, aliased => ($new_subquery:expr, $alias:expr)) => {{ - use $crate::raw_query::RawQuery; - let (ongoing_sql, mut ongoing_binds) = $ongoing_subquery.finish(); - let (new_sql, new_binds) = $new_subquery.finish(); - - ongoing_binds.extend(new_binds); - - let new_subquery_sql = format!("({}) AS {}", new_sql, $alias); - let select_formatted = format!($select, ongoing_sql, new_subquery_sql); - - RawQuery::new(select_formatted, ongoing_binds) - }}; - // Expects a select clause and one or more subqueries. The select clause should contain curly // braces for subqueries to be interpolated into. Use when the subqueries can be aliased // directly in the select statement. diff --git a/crates/sui-graphql-rpc/src/types/address.rs b/crates/sui-graphql-rpc/src/types/address.rs index a72a894c9b4d18..a84065e3401020 100644 --- a/crates/sui-graphql-rpc/src/types/address.rs +++ b/crates/sui-graphql-rpc/src/types/address.rs @@ -144,7 +144,7 @@ impl Address { before: Option, relation: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { use AddressTransactionBlockRelationship as R; let page = Page::from_params(ctx.data_unchecked(), first, after, last, before)?; @@ -169,7 +169,7 @@ impl Address { page, filter, self.checkpoint_viewed_at, - Some(within_checkpoints.unwrap_or(10000000)), + Some(scan_limit.unwrap_or(10000000)), ) .await .extend() diff --git a/crates/sui-graphql-rpc/src/types/coin.rs b/crates/sui-graphql-rpc/src/types/coin.rs index cfce6918db3459..29cf5059d62d61 100644 --- a/crates/sui-graphql-rpc/src/types/coin.rs +++ b/crates/sui-graphql-rpc/src/types/coin.rs @@ -200,7 +200,7 @@ impl Coin { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { ObjectImpl(&self.super_.super_) .received_transaction_blocks( @@ -210,7 +210,7 @@ impl Coin { last, before, filter, - within_checkpoints, + scan_limit, ) .await } diff --git a/crates/sui-graphql-rpc/src/types/coin_metadata.rs b/crates/sui-graphql-rpc/src/types/coin_metadata.rs index 8d7da1f2ddad3e..62f2a5a6a1d6ef 100644 --- a/crates/sui-graphql-rpc/src/types/coin_metadata.rs +++ b/crates/sui-graphql-rpc/src/types/coin_metadata.rs @@ -189,7 +189,7 @@ impl CoinMetadata { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { ObjectImpl(&self.super_.super_) .received_transaction_blocks( @@ -199,7 +199,7 @@ impl CoinMetadata { last, before, filter, - within_checkpoints, + scan_limit, ) .await } diff --git a/crates/sui-graphql-rpc/src/types/epoch.rs b/crates/sui-graphql-rpc/src/types/epoch.rs index 96822d76284d35..bf3bff259fb29d 100644 --- a/crates/sui-graphql-rpc/src/types/epoch.rs +++ b/crates/sui-graphql-rpc/src/types/epoch.rs @@ -250,7 +250,7 @@ impl Epoch { return Ok(Connection::new(false, false)); }; - let within_checkpoints = self + let scan_limit = self .stored .last_checkpoint_id .map(|id| id as u64) @@ -262,7 +262,7 @@ impl Epoch { page, filter, self.checkpoint_viewed_at, - Some(within_checkpoints), + Some(scan_limit), ) .await .extend() diff --git a/crates/sui-graphql-rpc/src/types/move_object.rs b/crates/sui-graphql-rpc/src/types/move_object.rs index 68063b89a98256..c5019ce3f5e2df 100644 --- a/crates/sui-graphql-rpc/src/types/move_object.rs +++ b/crates/sui-graphql-rpc/src/types/move_object.rs @@ -268,7 +268,7 @@ impl MoveObject { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { ObjectImpl(&self.super_) .received_transaction_blocks( @@ -278,7 +278,7 @@ impl MoveObject { last, before, filter, - within_checkpoints, + scan_limit, ) .await } diff --git a/crates/sui-graphql-rpc/src/types/move_package.rs b/crates/sui-graphql-rpc/src/types/move_package.rs index e3f8a1df10cd42..1e825344f62da1 100644 --- a/crates/sui-graphql-rpc/src/types/move_package.rs +++ b/crates/sui-graphql-rpc/src/types/move_package.rs @@ -243,18 +243,10 @@ impl MovePackage { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { ObjectImpl(&self.super_) - .received_transaction_blocks( - ctx, - first, - after, - last, - before, - filter, - within_checkpoints, - ) + .received_transaction_blocks(ctx, first, after, last, before, filter, scan_limit) .await } diff --git a/crates/sui-graphql-rpc/src/types/object.rs b/crates/sui-graphql-rpc/src/types/object.rs index 1851c22ae3e6f6..cc086bcac69964 100644 --- a/crates/sui-graphql-rpc/src/types/object.rs +++ b/crates/sui-graphql-rpc/src/types/object.rs @@ -241,7 +241,7 @@ pub(crate) struct HistoricalObjectCursor { arg(name = "last", ty = "Option"), arg(name = "before", ty = "Option"), arg(name = "filter", ty = "Option"), - arg(name = "within_checkpoints", ty = "Option"), + arg(name = "scan_limit", ty = "Option"), ty = "Connection", desc = "The transaction blocks that sent objects to this object." ), @@ -433,7 +433,7 @@ impl Object { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { ObjectImpl(self) .received_transaction_blocks( @@ -443,7 +443,7 @@ impl Object { last, before, filter, - within_checkpoints, + scan_limit, ) .await } @@ -602,7 +602,7 @@ impl ObjectImpl<'_> { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { let page = Page::from_params(ctx.data_unchecked(), first, after, last, before)?; @@ -621,7 +621,7 @@ impl ObjectImpl<'_> { page, filter, self.0.checkpoint_viewed_at, - Some(within_checkpoints.unwrap_or(10000000)), + Some(scan_limit.unwrap_or(10000000)), ) .await .extend() diff --git a/crates/sui-graphql-rpc/src/types/query.rs b/crates/sui-graphql-rpc/src/types/query.rs index 969c822bbc586c..4f580ddc35d9bb 100644 --- a/crates/sui-graphql-rpc/src/types/query.rs +++ b/crates/sui-graphql-rpc/src/types/query.rs @@ -313,7 +313,7 @@ impl Query { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { let Watermark { checkpoint, .. } = *ctx.data()?; @@ -323,7 +323,7 @@ impl Query { page, filter.unwrap_or_default(), checkpoint, - Some(within_checkpoints.unwrap_or(10000000)), + Some(scan_limit.unwrap_or(10000000)), ) .await .extend() diff --git a/crates/sui-graphql-rpc/src/types/stake.rs b/crates/sui-graphql-rpc/src/types/stake.rs index 8c0e338a990cf4..14f4ee6c2c05b8 100644 --- a/crates/sui-graphql-rpc/src/types/stake.rs +++ b/crates/sui-graphql-rpc/src/types/stake.rs @@ -208,7 +208,7 @@ impl StakedSui { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { ObjectImpl(&self.super_.super_) .received_transaction_blocks( @@ -218,7 +218,7 @@ impl StakedSui { last, before, filter, - within_checkpoints, + scan_limit, ) .await } diff --git a/crates/sui-graphql-rpc/src/types/suins_registration.rs b/crates/sui-graphql-rpc/src/types/suins_registration.rs index 20151fc794c610..08377fc04537e3 100644 --- a/crates/sui-graphql-rpc/src/types/suins_registration.rs +++ b/crates/sui-graphql-rpc/src/types/suins_registration.rs @@ -245,18 +245,10 @@ impl SuinsRegistration { last: Option, before: Option, filter: Option, - within_checkpoints: Option, + scan_limit: Option, ) -> Result> { ObjectImpl(&self.super_.super_) - .received_transaction_blocks( - ctx, - first, - after, - last, - before, - filter, - within_checkpoints, - ) + .received_transaction_blocks(ctx, first, after, last, before, filter, scan_limit) .await } diff --git a/crates/sui-graphql-rpc/src/types/transaction_block.rs b/crates/sui-graphql-rpc/src/types/transaction_block.rs index 98604aa0e7148b..c5323cb06643b1 100644 --- a/crates/sui-graphql-rpc/src/types/transaction_block.rs +++ b/crates/sui-graphql-rpc/src/types/transaction_block.rs @@ -32,7 +32,7 @@ use crate::{ consistency::Checkpointed, data::{self, DataLoader, Db, DbConnection, QueryExecutor}, error::Error, - filter, query, + filter, inner_join, query, raw_query::RawQuery, server::watermark_task::Watermark, tx_lookups::{ @@ -307,13 +307,14 @@ impl TransactionBlock { /// the cursor if they are consistent. /// /// Filters that involve a combination of `recvAddress`, `inputObject`, `changedObject`, and - /// `function` should provide a value for `within_checkpoints`. + /// `function` should provide a value for `scan_limit`. This indicates how many transactions to + /// scan through per the filter conditions. pub(crate) async fn paginate( ctx: &Context<'_>, page: Page, filter: TransactionBlockFilter, checkpoint_viewed_at: u64, - within_checkpoints: Option, + scan_limit: Option, ) -> Result, Error> { filter.is_consistent()?; // If cursors are provided, defer to the `checkpoint_viewed_at` in the cursor if they are @@ -344,10 +345,10 @@ impl TransactionBlock { ) .unwrap(); - // If `within_checkpoints` is set, we need to adjust the lower and upper bounds. It is up to - // the caller of `TransactionBlock::paginate` to determine whether `within_checkpoints` is + // If `scan_limit` is set, we need to adjust the lower and upper bounds. It is up to + // the caller of `TransactionBlock::paginate` to determine whether `scan_limit` is // required. - if let Some(scan_limit) = within_checkpoints { + if let Some(scan_limit) = scan_limit { if page.is_from_front() { hi_cp = std::cmp::min(hi_cp, lo_cp.saturating_add(scan_limit)); } else { @@ -362,7 +363,6 @@ impl TransactionBlock { let (prev, next, transactions): (bool, bool, Vec) = db .execute_repeatable(move |conn| { - // The min or first `tx_sequence_number` of a checkpoint is the previous // checkpoint's `network_total_transactions`. Because this refers to a historical // checkpoint, if we yield a `None` result, we can return early. @@ -370,11 +370,12 @@ impl TransactionBlock { 0 => 0, _ => { let sequence_number: Option = conn - .first(move || { - cp::checkpoints - .select(cp::network_total_transactions) - .filter(cp::sequence_number.eq((lo_cp - 1) as i64)) - }).optional()?; + .first(move || { + cp::checkpoints + .select(cp::network_total_transactions) + .filter(cp::sequence_number.eq((lo_cp - 1) as i64)) + }) + .optional()?; match sequence_number { Some(sequence_number) => sequence_number as u64, @@ -393,7 +394,8 @@ impl TransactionBlock { cp::checkpoints .select(cp::network_total_transactions) .filter(cp::sequence_number.eq(hi_cp as i64)) - }).optional()?; + }) + .optional()?; match sequence_number { Some(sequence_number) => (sequence_number as u64).saturating_sub(1), @@ -408,11 +410,17 @@ impl TransactionBlock { // If `transaction_ids` is specified, we can use that in lieu of the range under the // assumption that it will further constrain the rows we look up. if let Some(txs) = &filter.transaction_ids { - let transaction_ids: Vec = conn.results(move || select_ids(txs, &bound).into_boxed())?; + let transaction_ids: Vec = + conn.results(move || select_ids(txs, &bound).into_boxed())?; if transaction_ids.is_empty() { return Ok::<_, diesel::result::Error>((false, false, vec![])); } - bound = TxLookupBound::from_set(transaction_ids.into_iter().map(|tx| tx.tx_sequence_number as u64).collect()); + bound = TxLookupBound::from_set( + transaction_ids + .into_iter() + .map(|tx| tx.tx_sequence_number as u64) + .collect(), + ); } let mut subqueries = vec![]; @@ -458,11 +466,9 @@ impl TransactionBlock { subqueries.push((select_sender(sender, &bound), "tx_senders")); } // And if there are no filters at all, we can operate directly on the main table + // TODO (wlmyng): at this point we can directly make the query. we just need to produce the `tx_sequence_numbers: Vec` else { - subqueries.push(( - select_tx(None, &bound, "transactions"), - "transactions", - )); + subqueries.push((select_tx(None, &bound, "transactions"), "transactions")); } } @@ -477,13 +483,9 @@ impl TransactionBlock { let mut subquery = subqueries.pop().unwrap().0; if !subqueries.is_empty() { - subquery = query!( - "SELECT tx_sequence_number FROM ({}) AS initial", - subquery - ); + subquery = query!("SELECT tx_sequence_number FROM ({}) AS initial", subquery); while let Some((subselect, alias)) = subqueries.pop() { - subquery = - query!("{} INNER JOIN {} USING (tx_sequence_number)", subquery, aliased => (subselect, alias)); + subquery = inner_join!(subquery, rhs => (subselect, alias), using: ["tx_sequence_number"]); } } @@ -494,9 +496,7 @@ impl TransactionBlock { let tx_sequence_numbers = results .into_iter() - .map(|x| { - x.tx_sequence_number - }) + .map(|x| x.tx_sequence_number) .collect::>(); // then just do a multi-get