Skip to content

Commit

Permalink
feat(rpc): remove serde derives from rpc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
t00ts committed Dec 12, 2024
1 parent a0efe02 commit e5fc93d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
33 changes: 24 additions & 9 deletions crates/rpc/src/method/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,32 @@ impl From<CallError> for ApplicationError {
}
}

#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[derive(Debug, PartialEq, Eq)]
pub struct Input {
pub request: FunctionCall,
pub block_id: BlockId,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[derive(Debug, PartialEq, Eq)]
pub struct FunctionCall {
pub contract_address: ContractAddress,
pub entry_point_selector: EntryPoint,
pub calldata: Vec<CallParam>,
}

// TODO: Not used yet, just an example for now.
impl crate::dto::DeserializeForVersion for FunctionCall {
fn deserialize(value: crate::dto::Value) -> Result<Self, serde_json::Error> {
value.deserialize_map(|value| {
Ok(Self {
contract_address: value.deserialize_serde("contract_address")?,
entry_point_selector: value.deserialize_serde("entry_point_selector")?,
calldata: value
.deserialize_array("calldata", crate::dto::Value::deserialize_serde)?,
})
})
}
}

impl crate::dto::DeserializeForVersion for Input {
fn deserialize(value: crate::dto::Value) -> Result<Self, serde_json::Error> {
value.deserialize_map(|value| {
Expand Down Expand Up @@ -181,15 +191,18 @@ mod tests {
use serde_json::json;

use super::*;
use crate::dto::DeserializeForVersion;

#[test]
fn positional_args() {
let positional = json!([
let positional_json = json!([
{ "contract_address": "0xabcde", "entry_point_selector": "0xee", "calldata": ["0x1234", "0x2345"] },
{ "block_hash": "0xbbbbbbbb" }
]);

let input = serde_json::from_value::<Input>(positional).unwrap();
let positional = crate::dto::Value::new(positional_json, crate::RpcVersion::V08);

let input = Input::deserialize(positional).unwrap();
let expected = Input {
request: FunctionCall {
contract_address: contract_address!("0xabcde"),
Expand All @@ -203,12 +216,14 @@ mod tests {

#[test]
fn named_args() {
let named = json!({
let named_json = json!({
"request": { "contract_address": "0xabcde", "entry_point_selector": "0xee", "calldata": ["0x1234", "0x2345"] },
"block_id": { "block_hash": "0xbbbbbbbb" }
});

let input = serde_json::from_value::<Input>(named).unwrap();
let named = crate::dto::Value::new(named_json, crate::RpcVersion::V08);

let input = Input::deserialize(named).unwrap();
let expected = Input {
request: FunctionCall {
contract_address: contract_address!("0xabcde"),
Expand Down
2 changes: 0 additions & 2 deletions crates/rpc/src/method/get_block_with_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ pub enum Output {
Pending(Arc<PendingBlock>),
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Input {
pub block_id: BlockId,
}
Expand Down
2 changes: 0 additions & 2 deletions crates/rpc/src/method/get_block_with_tx_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use crate::context::RpcContext;

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

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Input {
pub block_id: BlockId,
}
Expand Down
2 changes: 0 additions & 2 deletions crates/rpc/src/method/get_block_with_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::context::RpcContext;

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

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Input {
pub block_id: BlockId,
}
Expand Down
16 changes: 10 additions & 6 deletions crates/rpc/src/method/get_transaction_by_block_id_and_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use pathfinder_common::{BlockId, TransactionIndex};
use crate::context::RpcContext;
use crate::types::transaction::TransactionWithHash;

#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[derive(Debug, PartialEq, Eq)]
pub struct Input {
block_id: BlockId,
index: TransactionIndex,
Expand Down Expand Up @@ -100,15 +99,18 @@ mod tests {
use serde_json::json;

use super::*;
use crate::dto::DeserializeForVersion;

#[test]
fn positional_args() {
let positional = json!([
let positional_json = json!([
{"block_hash": "0xdeadbeef"},
1
]);

let input = serde_json::from_value::<Input>(positional).unwrap();
let positional = crate::dto::Value::new(positional_json, crate::RpcVersion::V08);

let input = Input::deserialize(positional).unwrap();
assert_eq!(
input,
Input {
Expand All @@ -120,12 +122,14 @@ mod tests {

#[test]
fn named_args() {
let named_args = json!({
let named_args_json = json!({
"block_id": {"block_hash": "0xdeadbeef"},
"index": 1
});

let input = serde_json::from_value::<Input>(named_args).unwrap();
let named = crate::dto::Value::new(named_args_json, crate::RpcVersion::V08);

let input = Input::deserialize(named).unwrap();
assert_eq!(
input,
Input {
Expand Down
17 changes: 11 additions & 6 deletions crates/rpc/src/method/get_transaction_by_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use pathfinder_common::TransactionHash;

use crate::context::RpcContext;

#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[derive(Debug, PartialEq, Eq)]
pub struct Input {
transaction_hash: TransactionHash,
}
Expand Down Expand Up @@ -86,12 +85,15 @@ mod tests {
use serde_json::json;

use super::*;
use crate::dto::DeserializeForVersion;

#[test]
fn positional_args() {
let positional = json!(["0xdeadbeef"]);
let positional_json = json!(["0xdeadbeef"]);

let positional = crate::dto::Value::new(positional_json, crate::RpcVersion::V08);

let input = serde_json::from_value::<Input>(positional).unwrap();
let input = Input::deserialize(positional).unwrap();
assert_eq!(
input,
Input {
Expand All @@ -102,10 +104,13 @@ mod tests {

#[test]
fn named_args() {
let named_args = json!({
let named_args_json = json!({
"transaction_hash": "0xdeadbeef"
});
let input = serde_json::from_value::<Input>(named_args).unwrap();

let named = crate::dto::Value::new(named_args_json, crate::RpcVersion::V08);

let input = Input::deserialize(named).unwrap();
assert_eq!(
input,
Input {
Expand Down

0 comments on commit e5fc93d

Please sign in to comment.