Skip to content

Commit

Permalink
feat(rpc/v0.7): get_block_with_receipts stub
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Feb 21, 2024
1 parent 7eddde0 commit 8a3c53a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/rpc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use pathfinder_storage::Storage;
use std::num::NonZeroUsize;
use std::sync::Arc;

pub(crate) mod get_block_with_receipts;

type SequencerClient = starknet_gateway_client::Client;
use tokio::sync::watch as tokio_watch;

Expand Down
25 changes: 25 additions & 0 deletions crates/rpc/src/context/get_block_with_receipts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use pathfinder_common::{
receipt::Receipt, transaction::Transaction, BlockHeader, BlockId, PendingBlockHeader,
};

#[derive(Debug, Clone, PartialEq)]
pub(crate) enum BlockWithReceipts {
Full {
header: BlockHeader,
body: Vec<(Transaction, Receipt)>,
is_l1_accepted: bool,
},
Pending {
header: PendingBlockHeader,
body: Vec<(Transaction, Receipt)>,
},
}

impl super::RpcContext {
pub(crate) async fn get_block_with_receipts(
&self,
block: BlockId,
) -> anyhow::Result<Option<BlockWithReceipts>> {
todo!();
}
}
6 changes: 4 additions & 2 deletions crates/rpc/src/v07.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod dto;
pub mod method;

use crate::jsonrpc::{RpcRouter, RpcRouterBuilder};

Expand Down Expand Up @@ -34,9 +35,10 @@ pub fn register_routes() -> RpcRouterBuilder {
// .register("starknet_getTransactionByHash" , method::get_transaction_by_hash)
// .register("starknet_getTransactionReceipt" , method::get_transaction_receipt)
// .register("starknet_simulateTransactions" , method::simulate_transactions)
.register("starknet_specVersion" , || "0.7.0-rc0")
.register("starknet_specVersion", || "0.7.0-rc0")
// .register("starknet_traceBlockTransactions" , method::trace_block_transactions)
// .register("starknet_traceTransaction" , method::trace_transaction)
.register("starknet_getBlockWithReceipts", method::get_block_with_receipts)

.register("pathfinder_getProof" , crate::pathfinder::methods::get_proof)
.register("pathfinder_getProof", crate::pathfinder::methods::get_proof)
}
3 changes: 3 additions & 0 deletions crates/rpc/src/v07/method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod get_block_with_receipts;

pub use get_block_with_receipts::get_block_with_receipts;
81 changes: 81 additions & 0 deletions crates/rpc/src/v07/method/get_block_with_receipts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use pathfinder_common::BlockId;
use serde::Deserialize;

use crate::v07::dto;

use crate::context::{get_block_with_receipts::BlockWithReceipts, RpcContext};
use crate::v07::dto::transaction::TxnFinalityStatus;

#[derive(Deserialize, Debug, PartialEq, Eq)]
#[cfg_attr(test, derive(Copy, Clone))]
#[serde(deny_unknown_fields)]
pub struct Input {
block_id: BlockId,
}

#[derive(Debug, PartialEq)]
pub struct Output(BlockWithReceipts);

crate::error::generate_rpc_error_subset!(Error: BlockNotFound);

pub async fn get_block_with_receipts(context: RpcContext, input: Input) -> Result<Output, Error> {
context
.get_block_with_receipts(input.block_id)
.await?
.ok_or(Error::BlockNotFound)
.map(Output)
}

impl serde::Serialize for Output {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match &self.0 {
BlockWithReceipts::Full {
header,
body,
is_l1_accepted,
} => {
#[derive(serde::Serialize)]
struct Dto<'a> {
#[serde(flatten)]
header: dto::header::Header<'a>,
#[serde(flatten)]
body: dto::transaction::BlockBodyWithReceipts<'a>,
}

let finality_status = if *is_l1_accepted {
TxnFinalityStatus::AcceptedOnL1
} else {
TxnFinalityStatus::AcceptedOnL2
};

let header = dto::header::Header(header);
let body = dto::transaction::BlockBodyWithReceipts {
transaction_data: body,
finality_status,
};

Dto { header, body }.serialize(serializer)
}
BlockWithReceipts::Pending { header, body } => {
#[derive(serde::Serialize)]
struct Dto<'a> {
#[serde(flatten)]
header: dto::header::PendingHeader<'a>,
#[serde(flatten)]
body: dto::transaction::BlockBodyWithReceipts<'a>,
}

let header = dto::header::PendingHeader(header);
let body = dto::transaction::BlockBodyWithReceipts {
transaction_data: body,
finality_status: TxnFinalityStatus::AcceptedOnL2,
};

Dto { header, body }.serialize(serializer)
}
}
}
}

0 comments on commit 8a3c53a

Please sign in to comment.