Skip to content

Commit 4ddb2b2

Browse files
committed
Merge remote-tracking branch 'origin/main' into frontend-new-backend
2 parents e015f9e + badef4b commit 4ddb2b2

File tree

4 files changed

+55
-25
lines changed

4 files changed

+55
-25
lines changed

backend-rust/.sqlx/query-fc2f96416de3d86996d78076ff0a5ab9e9a8bf450265c6ece6e3b57ceb4c0d82.json renamed to backend-rust/.sqlx/query-3c68bcd07cc8ddee8de17f2996ece8aa625aa70aef64153347f79d84e9dd9a6b.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend-rust/.sqlx/query-c5a51f14dfc01a5a58aa0f6b7611953ec6a16b1a4aeef0e4dcd5e4fc24ad2ff1.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend-rust/src/graphql_api/block.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ use crate::{
77
use async_graphql::{
88
connection, types, ComplexObject, Context, Enum, Interface, Object, SimpleObject, Union,
99
};
10-
use futures::TryStreamExt;
1110

1211
#[derive(Default)]
1312
pub(crate) struct QueryBlocks;
1413

1514
#[Object]
1615
impl QueryBlocks {
17-
async fn block<'a>(&self, ctx: &Context<'a>, height_id: types::ID) -> ApiResult<Block> {
18-
let height: BlockHeight = height_id.try_into().map_err(ApiError::InvalidIdInt)?;
16+
async fn block<'a>(&self, ctx: &Context<'a>, id: types::ID) -> ApiResult<Block> {
17+
let height: BlockHeight = id.try_into().map_err(ApiError::InvalidIdInt)?;
1918
Block::query_by_height(get_pool(ctx)?, height).await
2019
}
2120

@@ -27,9 +26,10 @@ impl QueryBlocks {
2726
Block::query_by_hash(get_pool(ctx)?, block_hash).await
2827
}
2928

30-
async fn blocks<'a>(
29+
/// Query the list of blocks ordered descendingly by block height.
30+
async fn blocks(
3131
&self,
32-
ctx: &Context<'a>,
32+
ctx: &Context<'_>,
3333
#[graphql(desc = "Returns the first _n_ elements from the list.")] first: Option<u64>,
3434
#[graphql(desc = "Returns the elements in the list that come after the specified cursor.")]
3535
after: Option<String>,
@@ -46,11 +46,11 @@ impl QueryBlocks {
4646
before,
4747
config.block_connection_limit,
4848
)?;
49-
// The CCDScan front-end currently expects an ASC order of the nodes/edges
49+
// The CCDScan front-end currently expects an DESC order of the nodes/edges
5050
// returned (outer `ORDER BY`), while the inner `ORDER BY` is a trick to
5151
// get the correct nodes/edges selected based on the `after/before` key
5252
// specified.
53-
let mut row_stream = sqlx::query_as!(
53+
let rows = sqlx::query_as!(
5454
Block,
5555
"SELECT * FROM (
5656
SELECT
@@ -64,29 +64,36 @@ impl QueryBlocks {
6464
FROM blocks
6565
WHERE height > $1 AND height < $2
6666
ORDER BY
67-
(CASE WHEN $4 THEN height END) DESC,
68-
(CASE WHEN NOT $4 THEN height END) ASC
67+
(CASE WHEN $4 THEN height END) ASC,
68+
(CASE WHEN NOT $4 THEN height END) DESC
6969
LIMIT $3
70-
) ORDER BY height ASC",
70+
) ORDER BY height DESC",
7171
query.from,
7272
query.to,
7373
query.limit,
7474
query.desc
7575
)
76-
.fetch(pool);
77-
78-
let mut connection = connection::Connection::new(true, true);
79-
while let Some(block) = row_stream.try_next().await? {
80-
connection.edges.push(connection::Edge::new(block.height.to_string(), block));
76+
.fetch_all(pool)
77+
.await?;
78+
let has_prev_page = if let Some(first) = rows.first() {
79+
sqlx::query_scalar!("SELECT true FROM blocks WHERE height > $1 LIMIT 1", first.height)
80+
.fetch_optional(pool)
81+
.await?
82+
.flatten()
83+
.unwrap_or_default()
84+
} else {
85+
false
86+
};
87+
let has_next_page = if let Some(last) = rows.last() {
88+
// Genesis block have height 0, so we check whether the last block is higher.
89+
last.height > 0
90+
} else {
91+
false
92+
};
93+
let mut connection = connection::Connection::new(has_prev_page, has_next_page);
94+
for row in rows {
95+
connection.edges.push(connection::Edge::new(row.height.to_string(), row));
8196
}
82-
if last.is_some() {
83-
if let Some(edge) = connection.edges.last() {
84-
connection.has_previous_page = edge.node.height != 0;
85-
}
86-
} else if let Some(edge) = connection.edges.first() {
87-
connection.has_previous_page = edge.node.height != 0;
88-
}
89-
9097
Ok(connection)
9198
}
9299
}

frontend/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Fix
88

99
- Remove unused sub-query for account transactions in the accounts page.
10+
- Remove unused fields in query for block metrics.
1011

1112
## [1.6.0] - 2024-11-05
1213

0 commit comments

Comments
 (0)