From fb08760035e8381ca6652e2ba66d28b80c841415 Mon Sep 17 00:00:00 2001 From: Krisztian Kovacs Date: Tue, 16 Jan 2024 14:01:15 +0100 Subject: [PATCH] feat(storage): add `block_hash()` getter Similar to `block_id()` but can omit a DB lookup if the block id is already a hash. --- crates/executor/src/execution_state.rs | 4 +-- crates/pathfinder/examples/feeder_gateway.rs | 5 ++-- .../src/p2p_network/sync_handlers.rs | 6 ++--- crates/pathfinder/src/state/sync.rs | 5 ++-- crates/storage/src/connection.rs | 4 +++ crates/storage/src/connection/block.rs | 27 +++++++++++++++++++ crates/storage/src/connection/transaction.rs | 14 +++++----- 7 files changed, 47 insertions(+), 18 deletions(-) diff --git a/crates/executor/src/execution_state.rs b/crates/executor/src/execution_state.rs index f53608a5dd..cbb94105a9 100644 --- a/crates/executor/src/execution_state.rs +++ b/crates/executor/src/execution_state.rs @@ -50,9 +50,9 @@ impl<'tx> ExecutionState<'tx> { if self.execute_on_parent_state && self.header.number.get() >= 10 { let block_number_whose_hash_becomes_available = pathfinder_common::BlockNumber::new_or_panic(self.header.number.get() - 10); - let (_, block_hash) = self + let block_hash = self .transaction - .block_id(block_number_whose_hash_becomes_available.into())? + .block_hash(block_number_whose_hash_becomes_available.into())? .context("Getting historical block hash")?; tracing::trace!(%block_number_whose_hash_becomes_available, %block_hash, "Setting historical block hash"); diff --git a/crates/pathfinder/examples/feeder_gateway.rs b/crates/pathfinder/examples/feeder_gateway.rs index 114764a18c..4ae1a3a60e 100644 --- a/crates/pathfinder/examples/feeder_gateway.rs +++ b/crates/pathfinder/examples/feeder_gateway.rs @@ -295,10 +295,9 @@ fn get_chain(tx: &pathfinder_storage::Transaction<'_>) -> anyhow::Result }; let genesis_hash = tx - .block_id(BlockNumber::GENESIS.into()) + .block_hash(BlockNumber::GENESIS.into()) .unwrap() - .context("Getting genesis hash")? - .1; + .context("Getting genesis hash")?; let chain = match genesis_hash { MAINNET_GENESIS_HASH => Chain::Mainnet, diff --git a/crates/pathfinder/src/p2p_network/sync_handlers.rs b/crates/pathfinder/src/p2p_network/sync_handlers.rs index 6fdc902a4f..0e9758718b 100644 --- a/crates/pathfinder/src/p2p_network/sync_handlers.rs +++ b/crates/pathfinder/src/p2p_network/sync_handlers.rs @@ -251,7 +251,7 @@ fn get_transactions_for_block( block_number: BlockNumber, responses: &mut Vec, ) -> anyhow::Result { - let Some((_, block_hash)) = tx.block_id(block_number.into())? else { + let Some(block_hash) = tx.block_hash(block_number.into())? else { return Ok(false); }; @@ -286,7 +286,7 @@ fn get_receipts_for_block( block_number: BlockNumber, responses: &mut Vec, ) -> anyhow::Result { - let Some((_, block_hash)) = tx.block_id(block_number.into())? else { + let Some(block_hash) = tx.block_hash(block_number.into())? else { return Ok(false); }; @@ -318,7 +318,7 @@ fn get_events_for_block( block_number: BlockNumber, responses: &mut Vec, ) -> anyhow::Result { - let Some((_, block_hash)) = tx.block_id(block_number.into())? else { + let Some(block_hash) = tx.block_hash(block_number.into())? else { return Ok(false); }; diff --git a/crates/pathfinder/src/state/sync.rs b/crates/pathfinder/src/state/sync.rs index 556f4d0c47..63f88afdb3 100644 --- a/crates/pathfinder/src/state/sync.rs +++ b/crates/pathfinder/src/state/sync.rs @@ -671,9 +671,8 @@ async fn l1_update( .context("Insert update")?; let l2_hash = transaction - .block_id(update.block_number.into()) - .context("Fetching block hash")? - .map(|(_, hash)| hash); + .block_hash(update.block_number.into()) + .context("Fetching block hash")?; if let Some(l2_hash) = l2_hash { if l2_hash == update.block_hash { diff --git a/crates/storage/src/connection.rs b/crates/storage/src/connection.rs index 9a56f4bf54..f04b56ac75 100644 --- a/crates/storage/src/connection.rs +++ b/crates/storage/src/connection.rs @@ -103,6 +103,10 @@ impl<'inner> Transaction<'inner> { block::block_id(self, block) } + pub fn block_hash(&self, block: BlockId) -> anyhow::Result> { + block::block_hash(self, block) + } + pub fn block_exists(&self, block: BlockId) -> anyhow::Result { block::block_exists(self, block) } diff --git a/crates/storage/src/connection/block.rs b/crates/storage/src/connection/block.rs index c45f23d794..da4029904d 100644 --- a/crates/storage/src/connection/block.rs +++ b/crates/storage/src/connection/block.rs @@ -185,6 +185,33 @@ pub(super) fn block_id( .map_err(|e| e.into()) } +pub(super) fn block_hash( + tx: &Transaction<'_>, + block: BlockId, +) -> anyhow::Result> { + match block { + BlockId::Latest => tx + .inner() + .query_row( + "SELECT hash FROM canonical_blocks ORDER BY number DESC LIMIT 1", + [], + |row| row.get_block_hash(0), + ) + .optional() + .map_err(|e| e.into()), + BlockId::Number(number) => tx + .inner() + .query_row( + "SELECT hash FROM canonical_blocks WHERE number = ?", + params![&number], + |row| row.get_block_hash(0), + ) + .optional() + .map_err(|e| e.into()), + BlockId::Hash(hash) => Ok(Some(hash)), + } +} + pub(super) fn block_exists(tx: &Transaction<'_>, block: BlockId) -> anyhow::Result { match block { BlockId::Latest => { diff --git a/crates/storage/src/connection/transaction.rs b/crates/storage/src/connection/transaction.rs index 5c1f7aa438..5899dcefab 100644 --- a/crates/storage/src/connection/transaction.rs +++ b/crates/storage/src/connection/transaction.rs @@ -122,7 +122,7 @@ pub(super) fn transaction_at_block( index: usize, ) -> anyhow::Result> { // Identify block hash - let Some((_, block_hash)) = tx.block_id(block)? else { + let Some(block_hash) = tx.block_hash(block)? else { return Ok(None); }; @@ -173,8 +173,8 @@ pub(super) fn transaction_count(tx: &Transaction<'_>, block: BlockId) -> anyhow: .context("Counting transactions"), BlockId::Latest => { // First get the latest block - let block = match tx.block_id(BlockId::Latest)? { - Some((number, _)) => number, + let block = match tx.block_hash(BlockId::Latest)? { + Some(hash) => hash, None => return Ok(0), }; @@ -187,7 +187,7 @@ pub(super) fn transaction_data_for_block( tx: &Transaction<'_>, block: BlockId, ) -> anyhow::Result>> { - let Some((_, block_hash)) = tx.block_id(block)? else { + let Some(block_hash) = tx.block_hash(block)? else { return Ok(None); }; @@ -230,7 +230,7 @@ pub(super) fn transactions_for_block( tx: &Transaction<'_>, block: BlockId, ) -> anyhow::Result>> { - let Some((_, block_hash)) = tx.block_id(block)? else { + let Some(block_hash) = tx.block_hash(block)? else { return Ok(None); }; @@ -263,7 +263,7 @@ pub(super) fn receipts_for_block( tx: &Transaction<'_>, block: BlockId, ) -> anyhow::Result>> { - let Some((_, block_hash)) = tx.block_id(block)? else { + let Some(block_hash) = tx.block_hash(block)? else { return Ok(None); }; @@ -295,7 +295,7 @@ pub(super) fn transaction_hashes_for_block( tx: &Transaction<'_>, block: BlockId, ) -> anyhow::Result>> { - let Some((_, block_hash)) = tx.block_id(block)? else { + let Some(block_hash) = tx.block_hash(block)? else { return Ok(None); };