From e5fc93d1016244262ae493641e11237af0df9969 Mon Sep 17 00:00:00 2001 From: t00ts Date: Wed, 20 Nov 2024 11:15:08 +0400 Subject: [PATCH] feat(rpc): remove `serde` derives from rpc methods --- crates/rpc/src/method/call.rs | 33 ++++++++++++++----- .../rpc/src/method/get_block_with_receipts.rs | 2 -- .../src/method/get_block_with_tx_hashes.rs | 2 -- crates/rpc/src/method/get_block_with_txs.rs | 2 -- .../get_transaction_by_block_id_and_index.rs | 16 +++++---- .../rpc/src/method/get_transaction_by_hash.rs | 17 ++++++---- 6 files changed, 45 insertions(+), 27 deletions(-) diff --git a/crates/rpc/src/method/call.rs b/crates/rpc/src/method/call.rs index eb3a0ce803..5091685e99 100644 --- a/crates/rpc/src/method/call.rs +++ b/crates/rpc/src/method/call.rs @@ -67,22 +67,32 @@ impl From 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, } -// TODO: Not used yet, just an example for now. +impl crate::dto::DeserializeForVersion for FunctionCall { + fn deserialize(value: crate::dto::Value) -> Result { + 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 { value.deserialize_map(|value| { @@ -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::(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"), @@ -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::(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"), diff --git a/crates/rpc/src/method/get_block_with_receipts.rs b/crates/rpc/src/method/get_block_with_receipts.rs index 5779209aa9..0e48d18329 100644 --- a/crates/rpc/src/method/get_block_with_receipts.rs +++ b/crates/rpc/src/method/get_block_with_receipts.rs @@ -19,8 +19,6 @@ pub enum Output { Pending(Arc), } -#[derive(serde::Deserialize)] -#[serde(deny_unknown_fields)] pub struct Input { pub block_id: BlockId, } diff --git a/crates/rpc/src/method/get_block_with_tx_hashes.rs b/crates/rpc/src/method/get_block_with_tx_hashes.rs index 0b9a106be6..8445ea6af5 100644 --- a/crates/rpc/src/method/get_block_with_tx_hashes.rs +++ b/crates/rpc/src/method/get_block_with_tx_hashes.rs @@ -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, } diff --git a/crates/rpc/src/method/get_block_with_txs.rs b/crates/rpc/src/method/get_block_with_txs.rs index e0ae1f7856..ee0818542f 100644 --- a/crates/rpc/src/method/get_block_with_txs.rs +++ b/crates/rpc/src/method/get_block_with_txs.rs @@ -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, } diff --git a/crates/rpc/src/method/get_transaction_by_block_id_and_index.rs b/crates/rpc/src/method/get_transaction_by_block_id_and_index.rs index 89fd502014..f682fb0d79 100644 --- a/crates/rpc/src/method/get_transaction_by_block_id_and_index.rs +++ b/crates/rpc/src/method/get_transaction_by_block_id_and_index.rs @@ -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, @@ -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::(positional).unwrap(); + let positional = crate::dto::Value::new(positional_json, crate::RpcVersion::V08); + + let input = Input::deserialize(positional).unwrap(); assert_eq!( input, Input { @@ -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::(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 { diff --git a/crates/rpc/src/method/get_transaction_by_hash.rs b/crates/rpc/src/method/get_transaction_by_hash.rs index a3e15f5e61..f5c8b1f556 100644 --- a/crates/rpc/src/method/get_transaction_by_hash.rs +++ b/crates/rpc/src/method/get_transaction_by_hash.rs @@ -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, } @@ -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::(positional).unwrap(); + let input = Input::deserialize(positional).unwrap(); assert_eq!( input, Input { @@ -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::(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 {