Skip to content

Commit

Permalink
test(protobuf): add transaction conversion test (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware authored Jun 13, 2024
1 parent 719f7d2 commit c2ed014
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/papyrus_protobuf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 3 additions & 0 deletions crates/papyrus_protobuf/src/converters/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(test)]
#[path = "transaction_test.rs"]
mod transaction_test;
use std::convert::{TryFrom, TryInto};

use prost::Message;
Expand Down
189 changes: 189 additions & 0 deletions crates/papyrus_protobuf/src/converters/transaction_test.rs
Original file line number Diff line number Diff line change
@@ -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::<u8>::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(),
);
}

0 comments on commit c2ed014

Please sign in to comment.