Skip to content

Commit

Permalink
feat(rpc/v0.7): implement get_block_with_receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Feb 21, 2024
1 parent 8a3c53a commit 72c1462
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
61 changes: 58 additions & 3 deletions crates/rpc/src/context/get_block_with_receipts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context;
use pathfinder_common::{
receipt::Receipt, transaction::Transaction, BlockHeader, BlockId, PendingBlockHeader,
};
Expand All @@ -11,15 +12,69 @@ pub(crate) enum BlockWithReceipts {
},
Pending {
header: PendingBlockHeader,
// TODO: consider making this an Arc so we can share this better.
body: Vec<(Transaction, Receipt)>,
},
}

impl super::RpcContext {
pub(crate) async fn get_block_with_receipts(
&self,
block: BlockId,
self,
block_id: BlockId,
) -> anyhow::Result<Option<BlockWithReceipts>> {
todo!();
let span = tracing::Span::current();
tokio::task::spawn_blocking(move || {
let _g = span.enter();
let mut db = self
.storage
.connection()
.context("Creating database connection")?;

let db = db.transaction().context("Creating database transaction")?;

let block_id = match block_id {
BlockId::Pending => {
let pending = self
.pending_data
.get(&db)
.context("Querying pending data")?;

let body = pending
.block
.transactions
.iter()
.zip(pending.block.transaction_receipts.iter())
.map(|(t, r)| (t.clone(), r.clone()))
.collect();

return Ok(Some(BlockWithReceipts::Pending {
header: pending.pending_header(),
body,
}));
}
other => other.try_into().expect("Only pending cast should fail"),
};

let Some(header) = db.block_header(block_id).context("Fetching block header")? else {
return Ok(None);
};

let body = db
.transaction_data_for_block(block_id)
.context("Fetching transaction data")?
.context("Transaction data missing")?;

let is_l1_accepted = db
.block_is_l1_accepted(block_id)
.context("Fetching block finality")?;

Ok(Some(BlockWithReceipts::Full {
header,
body,
is_l1_accepted,
}))
})
.await
.context("Joining blocking task")?
}
}
13 changes: 12 additions & 1 deletion crates/rpc/src/pending.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use anyhow::Context;
use pathfinder_common::{BlockHeader, BlockNumber, StateUpdate};
use pathfinder_common::{BlockHeader, BlockNumber, PendingBlockHeader, StateUpdate};
use pathfinder_storage::Transaction;
use starknet_gateway_types::reply::{PendingBlock, Status};

Expand All @@ -20,6 +20,17 @@ pub struct PendingData {
}

impl PendingData {
pub fn pending_header(&self) -> PendingBlockHeader {
PendingBlockHeader {
parent_hash: self.block.parent_hash,
timestamp: self.block.timestamp,
eth_l1_gas_price: self.block.eth_l1_gas_price(),
strk_l1_gas_price: self.block.strk_l1_gas_price().unwrap_or_default(),
sequencer_address: self.block.sequencer_address,
starknet_version: self.block.starknet_version.clone(),
}
}

pub fn header(&self) -> BlockHeader {
// Be explicit about fields so that we are forced to check
// if any new fields are added.
Expand Down

0 comments on commit 72c1462

Please sign in to comment.