diff --git a/crates/rpc/src/method/block_hash_and_number.rs b/crates/rpc/src/method/block_hash_and_number.rs index 3183d868a6..837f524c2f 100644 --- a/crates/rpc/src/method/block_hash_and_number.rs +++ b/crates/rpc/src/method/block_hash_and_number.rs @@ -10,6 +10,7 @@ pub struct Output { crate::error::generate_rpc_error_subset!(Error: NoBlocks); +/// Get the latest block hash and number. pub async fn block_hash_and_number(context: RpcContext) -> Result { let span = tracing::Span::current(); diff --git a/crates/rpc/src/method/block_number.rs b/crates/rpc/src/method/block_number.rs index e2a41eaa82..4fa84dc992 100644 --- a/crates/rpc/src/method/block_number.rs +++ b/crates/rpc/src/method/block_number.rs @@ -7,6 +7,7 @@ pub struct Output(pathfinder_common::BlockNumber); crate::error::generate_rpc_error_subset!(Error: NoBlocks); +/// Get the latest block number. pub async fn block_number(context: RpcContext) -> Result { let span = tracing::Span::current(); diff --git a/crates/rpc/src/method/chain_id.rs b/crates/rpc/src/method/chain_id.rs index c4643b2094..97a53d432e 100644 --- a/crates/rpc/src/method/chain_id.rs +++ b/crates/rpc/src/method/chain_id.rs @@ -4,6 +4,7 @@ crate::error::generate_rpc_error_subset!(Error); pub struct Output(pathfinder_common::ChainId); +/// Get the chain ID. pub async fn chain_id(context: RpcContext) -> Result { Ok(Output(context.chain_id)) } @@ -16,3 +17,23 @@ impl crate::dto::serialize::SerializeForVersion for Output { serializer.serialize(&crate::dto::ChainId(&self.0)) } } + +#[cfg(test)] +mod tests { + use pathfinder_common::ChainId; + use pathfinder_crypto::Felt; + + #[tokio::test] + async fn encoding() { + let value = "some_chain_id"; + let chain_id = Felt::from_be_slice(value.as_bytes()).unwrap(); + let chain_id = ChainId(chain_id); + + let encoded = serde_json::to_string(&chain_id).unwrap(); + + let expected = hex::encode(value); + let expected = format!(r#""0x{expected}""#); + + assert_eq!(encoded, expected); + } +} diff --git a/crates/rpc/src/method/get_block_transaction_count.rs b/crates/rpc/src/method/get_block_transaction_count.rs index 5cb5bcee7d..e1d5f7831b 100644 --- a/crates/rpc/src/method/get_block_transaction_count.rs +++ b/crates/rpc/src/method/get_block_transaction_count.rs @@ -5,7 +5,7 @@ use crate::context::RpcContext; #[derive(Debug, PartialEq, Eq)] pub struct Input { - block_id: BlockId, + block_id: pathfinder_common::BlockId, } impl crate::dto::DeserializeForVersion for Input { @@ -23,6 +23,7 @@ crate::error::generate_rpc_error_subset!(Error: BlockNotFound); #[derive(Debug)] pub struct Output(u64); +/// Get the number of transactions in a block. pub async fn get_block_transaction_count( context: RpcContext, input: Input, diff --git a/crates/rpc/src/method/get_class.rs b/crates/rpc/src/method/get_class.rs index b944936251..145c21a41c 100644 --- a/crates/rpc/src/method/get_class.rs +++ b/crates/rpc/src/method/get_class.rs @@ -10,8 +10,8 @@ crate::error::generate_rpc_error_subset!(Error: BlockNotFound, ClassHashNotFound #[derive(Debug, PartialEq, Eq)] pub struct Input { - block_id: BlockId, - class_hash: ClassHash, + block_id: pathfinder_common::BlockId, + class_hash: pathfinder_common::ClassHash, } impl crate::dto::DeserializeForVersion for Input { @@ -40,6 +40,7 @@ impl From for Output { } } +/// Get a contract class. pub async fn get_class(context: RpcContext, input: Input) -> Result { let span = tracing::Span::current(); let jh = tokio::task::spawn_blocking(move || -> Result { diff --git a/crates/rpc/src/method/get_class_at.rs b/crates/rpc/src/method/get_class_at.rs index b082806933..9dbe05b340 100644 --- a/crates/rpc/src/method/get_class_at.rs +++ b/crates/rpc/src/method/get_class_at.rs @@ -10,8 +10,8 @@ crate::error::generate_rpc_error_subset!(Error: BlockNotFound, ContractNotFound) #[derive(Debug, PartialEq, Eq)] pub struct Input { - block_id: BlockId, - contract_address: ContractAddress, + block_id: pathfinder_common::BlockId, + contract_address: pathfinder_common::ContractAddress, } impl crate::dto::DeserializeForVersion for Input { @@ -54,6 +54,7 @@ impl SerializeForVersion for Output { } } +/// Get a contract class. pub async fn get_class_at(context: RpcContext, input: Input) -> Result { let span = tracing::Span::current(); diff --git a/crates/rpc/src/v02/method.rs b/crates/rpc/src/v02/method.rs index b0a87941d1..109134a897 100644 --- a/crates/rpc/src/v02/method.rs +++ b/crates/rpc/src/v02/method.rs @@ -9,7 +9,6 @@ mod get_storage_at; pub(crate) mod get_transaction_by_block_id_and_index; pub(crate) mod get_transaction_by_hash; -pub(crate) use block_hash_and_number::{block_hash_and_number, block_number}; pub(crate) use chain_id::chain_id; pub(crate) use get_block_transaction_count::get_block_transaction_count; pub(crate) use get_class::get_class; diff --git a/crates/rpc/src/v06.rs b/crates/rpc/src/v06.rs index 14c77bcd92..f043acde32 100644 --- a/crates/rpc/src/v06.rs +++ b/crates/rpc/src/v06.rs @@ -3,24 +3,21 @@ use crate::jsonrpc::{RpcRouter, RpcRouterBuilder}; pub(crate) mod method; pub(crate) mod types; -use crate::v02::method as v02_method; -use crate::v03::method as v03_method; - #[rustfmt::skip] pub fn register_routes() -> RpcRouterBuilder { RpcRouter::builder(crate::RpcVersion::V06) - .register("starknet_blockHashAndNumber" , v02_method::block_hash_and_number) - .register("starknet_blockNumber" , v02_method::block_number) - .register("starknet_chainId" , v02_method::chain_id) - .register("starknet_getBlockTransactionCount" , v02_method::get_block_transaction_count) - .register("starknet_getClass" , v02_method::get_class) - .register("starknet_getClassAt" , v02_method::get_class_at) - .register("starknet_getClassHashAt" , v02_method::get_class_hash_at) - .register("starknet_getNonce" , v02_method::get_nonce) - .register("starknet_getStorageAt" , v02_method::get_storage_at) + .register("starknet_blockHashAndNumber" , crate::method::block_hash_and_number) + .register("starknet_blockNumber" , crate::method::block_number) + .register("starknet_chainId" , crate::method::chain_id) + .register("starknet_getBlockTransactionCount" , crate::method::get_block_transaction_count) + .register("starknet_getClass" , crate::method::get_class) + .register("starknet_getClassAt" , crate::method::get_class_at) + .register("starknet_getClassHashAt" , crate::method::get_class_hash_at) + .register("starknet_getNonce" , crate::method::get_nonce) + .register("starknet_getStorageAt" , crate::method::get_storage_at) - .register("starknet_getEvents" , v03_method::get_events) - .register("starknet_getStateUpdate" , v03_method::get_state_update) + .register("starknet_getEvents" , crate::method::get_events) + .register("starknet_getStateUpdate" , crate::method::get_state_update) .register("starknet_syncing" , method::syncing) .register("starknet_getTransactionStatus" , method::get_transaction_status)