Skip to content

Commit

Permalink
Add logic to query
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Jan 29, 2025
1 parent 142a5ca commit 339a514
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 60 deletions.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 26 additions & 9 deletions backend-rust/src/graphql_api/baker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Baker {
#[graphql(desc = "Returns the last _n_ elements from the list.")] last: Option<u64>,
#[graphql(desc = "Returns the elements in the list that come before the specified cursor.")]
before: Option<String>,
) -> ApiResult<connection::Connection<String, Transaction>> {
) -> ApiResult<connection::Connection<String, InterimTransaction>> {
let config = get_config(ctx)?;
let pool = get_pool(ctx)?;
let query = ConnectionQuery::<i64>::new(
Expand All @@ -167,10 +167,12 @@ impl Baker {
config.transactions_per_block_connection_limit,
)?;

// Retrieves the transactions initated by a baker account. The transactions are
// ordered in ascending order (outer `ORDER BY`). If the `last` input
// parameter is set, the inner `ORDER BY` reverses the transaction order
// to allow the range be applied starting from the last element.
// Retrieves the transactions related to a baker account ('AddBaker',
// 'RemoveBaker', 'UpdateBakerStake', 'UpdateBakerRestakeEarnings',
// 'UpdateBakerKeys', 'ConfigureBaker'). The transactions are ordered in
// descending order (outer `ORDER BY`). If the `last` input parameter is
// set, the inner `ORDER BY` reverses the transaction order to allow the
// range be applied starting from the last element.
let mut row_stream = sqlx::query_as!(
Transaction,
r#"
Expand All @@ -192,11 +194,13 @@ impl Baker {
FROM transactions
WHERE transactions.sender_index = $5
AND index > $1 AND index < $2
AND type_account IN ('AddBaker', 'RemoveBaker', 'UpdateBakerStake',
'UpdateBakerRestakeEarnings', 'UpdateBakerKeys', 'ConfigureBaker')
ORDER BY
CASE WHEN $3 THEN index END DESC,
CASE WHEN NOT $3 THEN index END ASC
CASE WHEN NOT $3 THEN index END DESC,
CASE WHEN $3 THEN index END ASC
LIMIT $4
) ORDER BY index ASC"#,
) ORDER BY index DESC"#,
query.from,
query.to,
query.desc,
Expand All @@ -220,7 +224,12 @@ impl Baker {
Some(current_min) => min(current_min, tx.index),
});

connection.edges.push(connection::Edge::new(tx.index.to_string(), tx));
connection.edges.push(connection::Edge::new(
tx.index.to_string(),
InterimTransaction {
transaction: tx,
},
));
}

if let (Some(page_min_id), Some(page_max_id)) = (page_min_index, page_max_index) {
Expand All @@ -244,6 +253,14 @@ impl Baker {
}
}

// Future improvement (API breaking changes): The function `Baker::transactions`
// can directly return a `Transaction` instead of the `IterimTransaction` type
// here.
#[derive(SimpleObject)]
struct InterimTransaction {
transaction: Transaction,
}

#[derive(Union)]
enum BakerState<'a> {
ActiveBakerState(ActiveBakerState<'a>),
Expand Down
28 changes: 0 additions & 28 deletions backend-rust/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,34 +1239,6 @@ impl PreparedBlockItem {
.fetch_one(tx.as_mut())
.await?;

if let Some(account_transaction) = self.account_type {
// Mark baker-related transactions by incrementing the `index_per_baker_id`
// value.
if matches!(
account_transaction,
AccountTransactionType::AddBaker
| AccountTransactionType::RemoveBaker
| AccountTransactionType::UpdateBakerStake
| AccountTransactionType::UpdateBakerRestakeEarnings
| AccountTransactionType::UpdateBakerKeys
| AccountTransactionType::ConfigureBaker
) {
sqlx::query!(
"UPDATE transactions
SET index_per_baker_id = (
SELECT COALESCE(MAX(index_per_baker_id) + 1, 0)
FROM transactions
WHERE sender_index = (SELECT index FROM accounts WHERE address = $2)
)
WHERE transactions.index = $1",
tx_idx,
self.sender
)
.execute(tx.as_mut())
.await?;
}
}

// Note that this does not include account creation. We handle that when saving
// the account creation event.
sqlx::query!(
Expand Down
7 changes: 1 addition & 6 deletions backend-rust/src/migrations/m0001-initialize.sql
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,7 @@ CREATE TABLE transactions(
JSONB,
-- Transaction details. Reject reason if success is false.
reject
JSONB,
-- Every time this is a `baker-related` transaction, this index is incremented for that `baker_id (= account_index)`.
-- This value is `NULL` when the transaction is not a `baker-related` transaction.
-- This value is used to quickly filter/sort transactions associated to a baker.
index_per_baker_id
BIGINT
JSONB
);

-- Important for quickly filtering transactions in a given block.
Expand Down

0 comments on commit 339a514

Please sign in to comment.