Skip to content

Commit

Permalink
feat(rpc): decouple v06 from v02 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
t00ts committed Nov 19, 2024
1 parent 0451be8 commit 8a4afce
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 20 deletions.
1 change: 1 addition & 0 deletions crates/rpc/src/method/block_hash_and_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Output, Error> {
let span = tracing::Span::current();

Expand Down
1 change: 1 addition & 0 deletions crates/rpc/src/method/block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Output, Error> {
let span = tracing::Span::current();

Expand Down
21 changes: 21 additions & 0 deletions crates/rpc/src/method/chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Output, Error> {
Ok(Output(context.chain_id))
}
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion crates/rpc/src/method/get_block_transaction_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions crates/rpc/src/method/get_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -40,6 +40,7 @@ impl From<ContractClass> for Output {
}
}

/// Get a contract class.
pub async fn get_class(context: RpcContext, input: Input) -> Result<Output, Error> {
let span = tracing::Span::current();
let jh = tokio::task::spawn_blocking(move || -> Result<Output, Error> {
Expand Down
5 changes: 3 additions & 2 deletions crates/rpc/src/method/get_class_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,6 +54,7 @@ impl SerializeForVersion for Output {
}
}

/// Get a contract class.
pub async fn get_class_at(context: RpcContext, input: Input) -> Result<ContractClass, Error> {
let span = tracing::Span::current();

Expand Down
1 change: 0 additions & 1 deletion crates/rpc/src/v02/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 11 additions & 14 deletions crates/rpc/src/v06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8a4afce

Please sign in to comment.