Skip to content

Commit

Permalink
feat(storage): add block_hash() getter
Browse files Browse the repository at this point in the history
Similar to `block_id()` but can omit a DB lookup if the block id is
already a hash.
  • Loading branch information
kkovaacs committed Jan 22, 2024
1 parent 76a781e commit fb08760
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/executor/src/execution_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 2 additions & 3 deletions crates/pathfinder/examples/feeder_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,9 @@ fn get_chain(tx: &pathfinder_storage::Transaction<'_>) -> anyhow::Result<Chain>
};

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,
Expand Down
6 changes: 3 additions & 3 deletions crates/pathfinder/src/p2p_network/sync_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ fn get_transactions_for_block(
block_number: BlockNumber,
responses: &mut Vec<TransactionsResponse>,
) -> anyhow::Result<bool> {
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);
};

Expand Down Expand Up @@ -286,7 +286,7 @@ fn get_receipts_for_block(
block_number: BlockNumber,
responses: &mut Vec<ReceiptsResponse>,
) -> anyhow::Result<bool> {
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);
};

Expand Down Expand Up @@ -318,7 +318,7 @@ fn get_events_for_block(
block_number: BlockNumber,
responses: &mut Vec<EventsResponse>,
) -> anyhow::Result<bool> {
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);
};

Expand Down
5 changes: 2 additions & 3 deletions crates/pathfinder/src/state/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions crates/storage/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ impl<'inner> Transaction<'inner> {
block::block_id(self, block)
}

pub fn block_hash(&self, block: BlockId) -> anyhow::Result<Option<BlockHash>> {
block::block_hash(self, block)
}

pub fn block_exists(&self, block: BlockId) -> anyhow::Result<bool> {
block::block_exists(self, block)
}
Expand Down
27 changes: 27 additions & 0 deletions crates/storage/src/connection/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<BlockHash>> {
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<bool> {
match block {
BlockId::Latest => {
Expand Down
14 changes: 7 additions & 7 deletions crates/storage/src/connection/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub(super) fn transaction_at_block(
index: usize,
) -> anyhow::Result<Option<gateway::Transaction>> {
// Identify block hash
let Some((_, block_hash)) = tx.block_id(block)? else {
let Some(block_hash) = tx.block_hash(block)? else {
return Ok(None);
};

Expand Down Expand Up @@ -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),
};

Expand All @@ -187,7 +187,7 @@ pub(super) fn transaction_data_for_block(
tx: &Transaction<'_>,
block: BlockId,
) -> anyhow::Result<Option<Vec<(gateway::Transaction, gateway::Receipt)>>> {
let Some((_, block_hash)) = tx.block_id(block)? else {
let Some(block_hash) = tx.block_hash(block)? else {
return Ok(None);
};

Expand Down Expand Up @@ -230,7 +230,7 @@ pub(super) fn transactions_for_block(
tx: &Transaction<'_>,
block: BlockId,
) -> anyhow::Result<Option<Vec<gateway::Transaction>>> {
let Some((_, block_hash)) = tx.block_id(block)? else {
let Some(block_hash) = tx.block_hash(block)? else {
return Ok(None);
};

Expand Down Expand Up @@ -263,7 +263,7 @@ pub(super) fn receipts_for_block(
tx: &Transaction<'_>,
block: BlockId,
) -> anyhow::Result<Option<Vec<gateway::Receipt>>> {
let Some((_, block_hash)) = tx.block_id(block)? else {
let Some(block_hash) = tx.block_hash(block)? else {
return Ok(None);
};

Expand Down Expand Up @@ -295,7 +295,7 @@ pub(super) fn transaction_hashes_for_block(
tx: &Transaction<'_>,
block: BlockId,
) -> anyhow::Result<Option<Vec<TransactionHash>>> {
let Some((_, block_hash)) = tx.block_id(block)? else {
let Some(block_hash) = tx.block_hash(block)? else {
return Ok(None);
};

Expand Down

0 comments on commit fb08760

Please sign in to comment.