Skip to content

Commit

Permalink
test(JSON-RPC): test spec for execution methods (#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahakShama authored Dec 26, 2023
1 parent 370e5f4 commit fcee6b4
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@
"description": "Returns the execution traces of all transactions included in the given block",
"params": [
{
"name": "block_hash",
"summary": "The hash of the requested block",
"name": "block_id",
"description": "The hash of the requested block, or number (height) of the requested block, or a block tag",
"required": true,
"schema": {
"$ref": "#/components/schemas/BLOCK_HASH"
"$ref": "#/components/schemas/BLOCK_ID"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@
"$ref": "./starknet_api_openrpc.json#/components/schemas/FEE_ESTIMATE"
},
"BROADCASTED_TXN": {
"$ref": "./api/starknet_api_openrpc.json#/components/schemas/BROADCASTED_TXN"
"$ref": "./starknet_api_openrpc.json#/components/schemas/BROADCASTED_TXN"
},
"STATE_DIFF": {
"$ref": "./api/starknet_api_openrpc.json#/components/schemas/STATE_DIFF"
"$ref": "./starknet_api_openrpc.json#/components/schemas/STATE_DIFF"
}
},
"errors": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@
"$ref": "./starknet_api_openrpc.json#/components/schemas/FEE_ESTIMATE"
},
"BROADCASTED_TXN": {
"$ref": "./api/starknet_api_openrpc.json#/components/schemas/BROADCASTED_TXN"
"$ref": "./starknet_api_openrpc.json#/components/schemas/BROADCASTED_TXN"
},
"STATE_DIFF": {
"$ref": "./api/starknet_api_openrpc.json#/components/schemas/STATE_DIFF"
"$ref": "./starknet_api_openrpc.json#/components/schemas/STATE_DIFF"
},
"EXECUTION_RESOURCES": {
"$ref": "./api/starknet_api_openrpc.json#/components/schemas/EXECUTION_RESOURCES"
"$ref": "./starknet_api_openrpc.json#/components/schemas/EXECUTION_RESOURCES"
}
},
"errors": {
Expand Down
22 changes: 18 additions & 4 deletions crates/papyrus_rpc/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,11 @@ fn fix_errors(spec: &mut serde_json::Value) {
}
}

#[allow(dead_code)]
pub fn method_name_to_spec_method_name(method_name: &str) -> String {
let re = Regex::new((VERSION_PATTERN.to_string() + "_").as_str()).unwrap();
re.replace_all(method_name, "").to_string()
}

#[allow(dead_code)]
pub async fn call_api_then_assert_and_validate_schema_for_err<
R: JsonRpcServerImpl,
T: for<'a> Deserialize<'a> + std::fmt::Debug,
Expand Down Expand Up @@ -383,7 +381,6 @@ pub async fn call_api_then_assert_and_validate_schema_for_err<
);
}

#[allow(dead_code)]
pub async fn call_api_then_assert_and_validate_schema_for_result<
R: JsonRpcServerImpl,
T: for<'a> Deserialize<'a> + std::fmt::Debug + std::cmp::PartialEq,
Expand All @@ -395,10 +392,26 @@ pub async fn call_api_then_assert_and_validate_schema_for_result<
spec_file: SpecFile,
expected_res: &T,
) {
assert_eq!(
call_and_validate_schema_for_result::<_, T>(module, method, params, version_id, spec_file)
.await,
*expected_res
);
}

pub async fn call_and_validate_schema_for_result<
R: JsonRpcServerImpl,
T: for<'a> Deserialize<'a> + std::fmt::Debug,
>(
module: &RpcModule<R>,
method: &str,
params: Vec<Box<dyn SerializeJsonValue>>,
version_id: &VersionId,
spec_file: SpecFile,
) -> T {
validate_schema_for_method_params(method, &params, version_id, spec_file);
let params = params_vec_to_named_params(method, params, version_id, spec_file);
let (json_response, res) = raw_call::<_, _, T>(module, method, &params).await;
assert_eq!(res.unwrap(), *expected_res);
assert!(
validate_schema(
&get_starknet_spec_api_schema_for_method_results(
Expand All @@ -417,6 +430,7 @@ pub async fn call_api_then_assert_and_validate_schema_for_result<
)
.unwrap(),
);
res.unwrap()
}

pub fn get_method_names_from_spec(version_id: &VersionId) -> Vec<String> {
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_rpc/src/v0_4/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub trait JsonRpc {
#[method(name = "estimateFee")]
async fn estimate_fee(
&self,
transactions: Vec<BroadcastedTransaction>,
request: Vec<BroadcastedTransaction>,
block_id: BlockId,
) -> RpcResult<Vec<FeeEstimate>>;

Expand Down
113 changes: 61 additions & 52 deletions crates/papyrus_rpc/src/v0_4/execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ use super::transaction::{
};
use crate::api::{BlockHashOrNumber, BlockId, CallRequest, Tag};
use crate::test_utils::{
call_and_validate_schema_for_result,
call_api_then_assert_and_validate_schema_for_result,
get_starknet_spec_api_schema_for_components,
get_starknet_spec_api_schema_for_method_results,
Expand Down Expand Up @@ -340,18 +341,18 @@ async fn call_estimate_fee() {
..Default::default()
}));

let res = module
.call::<_, Vec<FeeEstimate>>(
"starknet_V0_4_estimateFee",
(
vec![invoke.clone()],
BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(0))),
),
)
.await
.unwrap();

assert_eq!(res, vec![EXPECTED_FEE_ESTIMATE.clone()]);
call_api_then_assert_and_validate_schema_for_result(
&module,
"starknet_V0_4_estimateFee",
vec![
Box::new(vec![invoke.clone()]),
Box::new(BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(0)))),
],
&VERSION_0_4,
SpecFile::StarknetApiOpenrpc,
&vec![EXPECTED_FEE_ESTIMATE.clone()],
)
.await;

// Test that calling the same transaction with a different block context with a different gas
// price produces a different fee.
Expand Down Expand Up @@ -461,13 +462,14 @@ async fn test_call_simulate(
};
let invoke = BroadcastedTransaction::Invoke(InvokeTransaction::Version1(invoke_v1.clone()));

let mut res = module
.call::<_, Vec<SimulatedTransaction>>(
"starknet_V0_4_simulateTransactions",
(block_id, vec![invoke], Vec::<SimulationFlag>::new()),
)
.await
.unwrap();
let mut res = call_and_validate_schema_for_result::<_, Vec<SimulatedTransaction>>(
module,
"starknet_V0_4_simulateTransactions",
vec![Box::new(block_id), Box::new(vec![invoke]), Box::<Vec<SimulationFlag>>::default()],
&VERSION_0_4,
SpecFile::TraceApi,
)
.await;

assert_eq!(res.len(), 1);

Expand Down Expand Up @@ -542,17 +544,18 @@ async fn call_simulate_skip_validate() {
..Default::default()
}));

let mut res = module
.call::<_, Vec<SimulatedTransaction>>(
"starknet_V0_4_simulateTransactions",
(
BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(0))),
vec![invoke],
vec![SimulationFlag::SkipValidate],
),
)
.await
.unwrap();
let mut res = call_and_validate_schema_for_result::<_, Vec<SimulatedTransaction>>(
&module,
"starknet_V0_4_simulateTransactions",
vec![
Box::new(BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(0)))),
Box::new(vec![invoke]),
Box::new(vec![SimulationFlag::SkipValidate]),
],
&VERSION_0_4,
SpecFile::TraceApi,
)
.await;

assert_eq!(res.len(), 1);

Expand Down Expand Up @@ -591,17 +594,18 @@ async fn call_simulate_skip_fee_charge() {
..Default::default()
}));

let mut res = module
.call::<_, Vec<SimulatedTransaction>>(
"starknet_V0_4_simulateTransactions",
(
BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(0))),
vec![invoke],
vec![SimulationFlag::SkipFeeCharge],
),
)
.await
.unwrap();
let mut res = call_and_validate_schema_for_result::<_, Vec<SimulatedTransaction>>(
&module,
"starknet_V0_4_simulateTransactions",
vec![
Box::new(BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(0)))),
Box::new(vec![invoke]),
Box::new(vec![SimulationFlag::SkipFeeCharge]),
],
&VERSION_0_4,
SpecFile::TraceApi,
)
.await;

assert_eq!(res.len(), 1);

Expand Down Expand Up @@ -698,10 +702,14 @@ async fn trace_block_transactions_regular_and_pending() {
.commit()
.unwrap();

let tx_1_trace = module
.call::<_, TransactionTrace>("starknet_V0_4_traceTransaction", [tx_hash1])
.await
.unwrap();
let tx_1_trace = call_and_validate_schema_for_result::<_, TransactionTrace>(
&module,
"starknet_V0_4_traceTransaction",
vec![Box::new(tx_hash1)],
&VERSION_0_4,
SpecFile::TraceApi,
)
.await;

assert_matches!(tx_1_trace, TransactionTrace::Invoke(_));

Expand All @@ -712,13 +720,14 @@ async fn trace_block_transactions_regular_and_pending() {

assert_matches!(tx_2_trace, TransactionTrace::Invoke(_));

let res = module
.call::<_, Vec<TransactionTraceWithHash>>(
"starknet_V0_4_traceBlockTransactions",
[BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(2)))],
)
.await
.unwrap();
let res = call_and_validate_schema_for_result::<_, Vec<TransactionTraceWithHash>>(
&module,
"starknet_V0_4_traceBlockTransactions",
vec![Box::new(BlockId::HashOrNumber(BlockHashOrNumber::Number(BlockNumber(2))))],
&VERSION_0_4,
SpecFile::TraceApi,
)
.await;

assert_eq!(res.len(), 2);
assert_eq!(res[0].trace_root, tx_1_trace);
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_rpc/src/v0_5/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub trait JsonRpc {
#[method(name = "estimateFee")]
async fn estimate_fee(
&self,
transactions: Vec<BroadcastedTransaction>,
request: Vec<BroadcastedTransaction>,
block_id: BlockId,
) -> RpcResult<Vec<FeeEstimate>>;

Expand Down
Loading

0 comments on commit fcee6b4

Please sign in to comment.