diff --git a/Cargo.lock b/Cargo.lock index f65713799f..d4247853f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6158,6 +6158,7 @@ version = "0.4.0-dev.3" dependencies = [ "bytes", "indexmap 2.2.6", + "lazy_static", "papyrus_common", "primitive-types", "prost", @@ -6165,6 +6166,7 @@ dependencies = [ "prost-types", "starknet-types-core", "starknet_api", + "test_utils", "thiserror", ] diff --git a/crates/papyrus_protobuf/Cargo.toml b/crates/papyrus_protobuf/Cargo.toml index 883907e7f3..1a0e7e6190 100644 --- a/crates/papyrus_protobuf/Cargo.toml +++ b/crates/papyrus_protobuf/Cargo.toml @@ -8,6 +8,7 @@ license-file.workspace = true [dependencies] bytes.workspace = true indexmap.workspace = true +lazy_static.workspace = true primitive-types.workspace = true prost.workspace = true prost-types.workspace = true @@ -16,5 +17,8 @@ starknet-types-core.workspace = true thiserror.workspace = true papyrus_common = { path = "../papyrus_common", version = "0.4.0-dev.2" } +[dev-dependencies] +test_utils = { path = "../test_utils" } + [build-dependencies] prost-build.workspace = true diff --git a/crates/papyrus_protobuf/src/converters/transaction.rs b/crates/papyrus_protobuf/src/converters/transaction.rs index 596426dba4..57c41ade81 100644 --- a/crates/papyrus_protobuf/src/converters/transaction.rs +++ b/crates/papyrus_protobuf/src/converters/transaction.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +#[path = "transaction_test.rs"] +mod transaction_test; use std::convert::{TryFrom, TryInto}; use prost::Message; diff --git a/crates/papyrus_protobuf/src/converters/transaction_test.rs b/crates/papyrus_protobuf/src/converters/transaction_test.rs new file mode 100644 index 0000000000..90ac0dcd7f --- /dev/null +++ b/crates/papyrus_protobuf/src/converters/transaction_test.rs @@ -0,0 +1,189 @@ +use lazy_static::lazy_static; +use starknet_api::transaction::{ + Builtin, + DeclareTransaction, + DeclareTransactionOutput, + DeployAccountTransaction, + DeployAccountTransactionOutput, + DeployTransactionOutput, + ExecutionResources, + GasVector, + InvokeTransaction, + InvokeTransactionOutput, + L1HandlerTransactionOutput, + Resource, + ResourceBounds, + ResourceBoundsMapping, + Transaction as StarknetApiTransaction, + TransactionOutput, +}; +use test_utils::{get_rng, GetTestInstance}; + +use crate::sync::DataOrFin; + +macro_rules! create_transaction_output { + ($tx_output_type:ty, $tx_output_enum_variant:ident) => {{ + let mut rng = get_rng(); + let mut transaction_output = <$tx_output_type>::get_test_instance(&mut rng); + transaction_output.execution_resources = EXECUTION_RESOURCES.clone(); + transaction_output.events = vec![]; + TransactionOutput::$tx_output_enum_variant(transaction_output) + }}; +} + +#[test] +fn test_l1_handler_transaction() { + let mut rng = get_rng(); + let transaction = starknet_api::transaction::L1HandlerTransaction::get_test_instance(&mut rng); + let transaction = StarknetApiTransaction::L1Handler(transaction); + + let transaction_output = create_transaction_output!(L1HandlerTransactionOutput, L1Handler); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_deploy_transaction() { + let mut rng = get_rng(); + let transaction = starknet_api::transaction::DeployTransaction::get_test_instance(&mut rng); + let transaction = StarknetApiTransaction::Deploy(transaction); + + let transaction_output = create_transaction_output!(DeployTransactionOutput, Deploy); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_declare_transaction_v0() { + let mut rng = get_rng(); + let transaction = + starknet_api::transaction::DeclareTransactionV0V1::get_test_instance(&mut rng); + let transaction = StarknetApiTransaction::Declare(DeclareTransaction::V0(transaction)); + + let transaction_output = create_transaction_output!(DeclareTransactionOutput, Declare); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_declare_transaction_v1() { + let mut rng = get_rng(); + let transaction = + starknet_api::transaction::DeclareTransactionV0V1::get_test_instance(&mut rng); + let transaction = StarknetApiTransaction::Declare(DeclareTransaction::V1(transaction)); + + let transaction_output = create_transaction_output!(DeclareTransactionOutput, Declare); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_declare_transaction_v2() { + let mut rng = get_rng(); + let transaction = starknet_api::transaction::DeclareTransactionV2::get_test_instance(&mut rng); + let transaction = StarknetApiTransaction::Declare(DeclareTransaction::V2(transaction)); + + let transaction_output = create_transaction_output!(DeclareTransactionOutput, Declare); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_declare_transaction_v3() { + let mut rng = get_rng(); + let mut transaction = + starknet_api::transaction::DeclareTransactionV3::get_test_instance(&mut rng); + transaction.resource_bounds = RESOURCE_BOUNDS_MAPPING.clone(); + let transaction = StarknetApiTransaction::Declare(DeclareTransaction::V3(transaction)); + + let transaction_output = create_transaction_output!(DeclareTransactionOutput, Declare); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_invoke_transaction_v0() { + let mut rng = get_rng(); + let transaction = starknet_api::transaction::InvokeTransactionV0::get_test_instance(&mut rng); + let transaction = StarknetApiTransaction::Invoke(InvokeTransaction::V0(transaction)); + + let transaction_output = create_transaction_output!(InvokeTransactionOutput, Invoke); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_invoke_transaction_v1() { + let mut rng = get_rng(); + let transaction = starknet_api::transaction::InvokeTransactionV1::get_test_instance(&mut rng); + let transaction = StarknetApiTransaction::Invoke(InvokeTransaction::V1(transaction)); + + let transaction_output = create_transaction_output!(InvokeTransactionOutput, Invoke); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_invoke_transaction_v3() { + let mut rng = get_rng(); + let mut transaction = + starknet_api::transaction::InvokeTransactionV3::get_test_instance(&mut rng); + transaction.resource_bounds = RESOURCE_BOUNDS_MAPPING.clone(); + let transaction = StarknetApiTransaction::Invoke(InvokeTransaction::V3(transaction)); + + let transaction_output = create_transaction_output!(InvokeTransactionOutput, Invoke); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_deploy_account_transaction_v1() { + let mut rng = get_rng(); + let transaction = + starknet_api::transaction::DeployAccountTransactionV1::get_test_instance(&mut rng); + let transaction = + StarknetApiTransaction::DeployAccount(DeployAccountTransaction::V1(transaction)); + + let transaction_output = + create_transaction_output!(DeployAccountTransactionOutput, DeployAccount); + transaction_test(transaction, transaction_output); +} + +#[test] +fn test_deploy_account_transaction_v3() { + let mut rng = get_rng(); + let mut transaction = + starknet_api::transaction::DeployAccountTransactionV3::get_test_instance(&mut rng); + transaction.resource_bounds = RESOURCE_BOUNDS_MAPPING.clone(); + let transaction = + StarknetApiTransaction::DeployAccount(DeployAccountTransaction::V3(transaction)); + + let transaction_output = + create_transaction_output!(DeployAccountTransactionOutput, DeployAccount); + transaction_test(transaction, transaction_output); +} + +fn transaction_test(transaction: StarknetApiTransaction, transaction_output: TransactionOutput) { + let data = DataOrFin(Some((transaction, transaction_output))); + let bytes_data = Vec::::from(data.clone()); + let res_data = DataOrFin::try_from(bytes_data).unwrap(); + assert_eq!(data, res_data); +} + +lazy_static! { + static ref EXECUTION_RESOURCES: ExecutionResources = ExecutionResources { + steps: 0, + builtin_instance_counter: std::collections::HashMap::from([ + (Builtin::RangeCheck, 1), + (Builtin::Pedersen, 2), + (Builtin::Poseidon, 3), + (Builtin::EcOp, 4), + (Builtin::Ecdsa, 5), + (Builtin::Bitwise, 6), + (Builtin::Keccak, 7), + (Builtin::SegmentArena, 0), + ]), + memory_holes: 0, + da_gas_consumed: GasVector::default(), + gas_consumed: GasVector::default(), + }; + static ref RESOURCE_BOUNDS_MAPPING: ResourceBoundsMapping = ResourceBoundsMapping( + [ + (Resource::L1Gas, ResourceBounds { max_amount: 0x5, max_price_per_unit: 0x6 }), + (Resource::L2Gas, ResourceBounds { max_amount: 0x5, max_price_per_unit: 0x6 }), + ] + .into_iter() + .collect(), + ); +}