Skip to content

Commit

Permalink
Block statistics (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
td202 committed Nov 15, 2024
1 parent bb4da34 commit 27c915d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 21 deletions.

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

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

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

63 changes: 51 additions & 12 deletions backend-rust/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ impl BaseQuery {
let mut row_stream = sqlx::query_as!(
Block,
"SELECT * FROM (
SELECT hash, height, slot_time, baker_id, total_amount
SELECT hash, height, slot_time, block_time, finalization_time, baker_id, \
total_amount
FROM blocks
WHERE height > $1 AND height < $2
ORDER BY
Expand Down Expand Up @@ -1173,23 +1174,30 @@ struct Versions {

#[derive(Debug, sqlx::FromRow)]
pub struct Block {
hash: BlockHash,
height: BlockHeight,
hash: BlockHash,
height: BlockHeight,
/// Time of the block being baked.
slot_time: DateTime,
// block_time: i32,
// finalization_time: Option<i32>,
slot_time: DateTime,
/// Number of milliseconds between the `slot_time` of this block and its
/// parent.
block_time: i32,
/// If this block is finalized, the number of milliseconds between the
/// `slot_time` of this block and the first block that contains a
/// finalization proof or quorum certificate that justifies this block
/// being finalized.
finalization_time: Option<i32>,
// finalized_by: Option<BlockHeight>,
baker_id: Option<BakerId>,
total_amount: Amount,
baker_id: Option<BakerId>,
total_amount: Amount,
// total_staked: Amount,
}

impl Block {
async fn query_by_height(pool: &PgPool, height: BlockHeight) -> ApiResult<Self> {
sqlx::query_as!(
Block,
"SELECT hash, height, slot_time, baker_id, total_amount FROM blocks WHERE height=$1",
"SELECT hash, height, slot_time, block_time, finalization_time, baker_id, \
total_amount FROM blocks WHERE height=$1",
height
)
.fetch_optional(pool)
Expand All @@ -1200,7 +1208,8 @@ impl Block {
async fn query_by_hash(pool: &PgPool, block_hash: BlockHash) -> ApiResult<Self> {
sqlx::query_as!(
Block,
"SELECT hash, height, slot_time, baker_id, total_amount FROM blocks WHERE hash=$1",
"SELECT hash, height, slot_time, block_time, finalization_time, baker_id, \
total_amount FROM blocks WHERE hash=$1",
block_hash
)
.fetch_optional(pool)
Expand Down Expand Up @@ -1232,6 +1241,17 @@ impl Block {
/// Whether the block is finalized.
async fn finalized(&self) -> bool { true }

/// The block statistics:
/// - The time difference from the parent block.
/// - The time difference to the block that justifies the block being
/// finalized.
async fn block_statistics(&self) -> BlockStatistics {
BlockStatistics {
block_time: self.block_time as f64 / 1000.0,
finalization_time: self.finalization_time.map(|f| f as f64 / 1000.0),
}
}

/// Number of transactions included in this block.
async fn transaction_count<'a>(&self, ctx: &Context<'a>) -> ApiResult<i64> {
let result =
Expand Down Expand Up @@ -2174,8 +2194,27 @@ struct BalanceStatistics {

#[derive(SimpleObject)]
struct BlockStatistics {
block_time: f32,
finalization_time: f32,
/// Number of seconds between block slot time of this block and previous
/// block.
block_time: f64,
/// Number of seconds between the block slot time of this block and the
/// block containing the finalization proof for this block.
///
/// This is an objective measure of the finalization time (determined by
/// chain data alone) and will at least be the block time. The actual
/// finalization time will usually be lower than that but can only be
/// determined in a subjective manner by each node: That is the time a
/// node has first seen a block finalized. This is defined as the
/// difference between when a finalization proof is first constructed,
/// and the block slot time. However the time when a finalization proof
/// is first constructed is subjective, some nodes will receive the
/// necessary messages before others. Also, this number cannot be
/// reconstructed for blocks finalized before extracting data from the
/// node.
///
/// Value will initially be `None` until the block containing the
/// finalization proof for this block is itself finalized.
finalization_time: Option<f64>,
}

#[derive(Interface)]
Expand Down

0 comments on commit 27c915d

Please sign in to comment.