Skip to content

Commit

Permalink
test: deploy account test with mock server (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
ICavlek authored Sep 17, 2024
1 parent d845810 commit 15d8964
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 62 deletions.
127 changes: 73 additions & 54 deletions tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ use std::{thread, time};

use beerus::gen::{
client::Client, Address, BlockId, BlockTag, BroadcastedDeclareTxn,
BroadcastedTxn, Felt, Rpc, SimulationFlagForEstimateFee,
BroadcastedTxn, Felt, Rpc, SimulationFlagForEstimateFee, TxnHash,
};
use common::{
constants::{
dummy_transaction_v3, COMPILED_ACCOUNT_CONTRACT, DECLARE_ACCOUNT,
declare_transaction, deploy_transaction, estimate_fee_transaction,
invoke_transaction, COMPILED_ACCOUNT_CONTRACT, DECLARE_ACCOUNT,
},
katana::Katana,
matchers::StarknetMatcher::{
AddDeclareTransaction, AddDeclareTransactionMalicious, ChainId,
self, AddDeclareTransaction, AddDeclareTransactionMalicious,
AddDeployAccountTransaction, AddInvokeTransaction, ChainId,
ChainIdMalicious, ClassError, ClassMalicious, ClassSuccess,
EstimateFee, EstimateFeeMalicious, Nonce, NonceMalicious, SpecVersion,
SpecVersionMalicious,
EstimateFee, EstimateFeeMalicious, GetTransactionReceipt, Nonce,
NonceMalicious, SpecVersion, SpecVersionMalicious,
},
node::setup_client_with_mock_starknet_node,
};
Expand Down Expand Up @@ -139,36 +141,23 @@ async fn get_class_success() {

#[tokio::test]
async fn spec_version_estimate_fee() {
let declare_transaction = dummy_transaction_v3();
let (client, _starknet_node) =
setup_client_with_mock_starknet_node(vec![SpecVersion, EstimateFee])
.await;
let tx = estimate_fee_transaction();
assert!(client.specVersion().await.is_ok());
let res = client
.estimateFee(
vec![BroadcastedTxn::BroadcastedDeclareTxn(
BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
declare_transaction,
),
)],
vec![],
BlockId::BlockTag(BlockTag::Latest),
)
.estimateFee(vec![tx], vec![], BlockId::BlockTag(BlockTag::Latest))
.await;
assert!(res.is_ok());
}

#[tokio::test]
async fn add_declare_transaction() {
let declare_transaction = dummy_transaction_v3();
let tx = declare_transaction();
let (client, _starknet_node) =
setup_client_with_mock_starknet_node(vec![AddDeclareTransaction]).await;
assert!(client
.addDeclareTransaction(BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
declare_transaction
))
.await
.is_ok());
assert!(client.addDeclareTransaction(tx).await.is_ok());
}

#[tokio::test]
Expand All @@ -186,31 +175,19 @@ async fn declare_account_mock() {
let block_id = BlockId::BlockTag(BlockTag::Latest);
let class_hash = Felt::try_new("0x0").unwrap();
let contract_address = Address(class_hash.clone());
let declare_transaction = dummy_transaction_v3();
let estimate_tx = estimate_fee_transaction();
let declare_tx = declare_transaction();

assert!(client.chainId().await.is_ok());
assert!(client.getClass(block_id.clone(), class_hash).await.is_err());
assert!(client.chainId().await.is_ok());
assert!(client.getNonce(block_id.clone(), contract_address).await.is_ok());
assert!(client.specVersion().await.is_ok());
assert!(client
.estimateFee(
vec![BroadcastedTxn::BroadcastedDeclareTxn(
BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
declare_transaction.clone(),
),
)],
vec![],
block_id
)
.await
.is_ok());
assert!(client
.addDeclareTransaction(BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
declare_transaction
))
.estimateFee(vec![estimate_tx], vec![], block_id)
.await
.is_ok());
assert!(client.addDeclareTransaction(declare_tx).await.is_ok());
}

#[tokio::test]
Expand All @@ -227,28 +204,70 @@ async fn malicious_data_results_in_err() {
let block_id = BlockId::BlockTag(BlockTag::Latest);
let class_hash = Felt::try_new("0x0").unwrap();
let contract_address = Address(class_hash.clone());
let declare_transaction = dummy_transaction_v3();
let declare_tx = declare_transaction();
let estimate_tx = estimate_fee_transaction();

assert!(client
.addDeclareTransaction(BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
declare_transaction.clone()
))
.await
.is_err());
assert!(client.addDeclareTransaction(declare_tx).await.is_err());
assert!(client.chainId().await.is_err());
assert!(client
.estimateFee(
vec![BroadcastedTxn::BroadcastedDeclareTxn(
BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
declare_transaction.clone(),
),
)],
vec![],
block_id.clone()
)
.estimateFee(vec![estimate_tx], vec![], block_id.clone())
.await
.is_err());
assert!(client.getClass(block_id.clone(), class_hash).await.is_err());
assert!(client.getNonce(block_id, contract_address).await.is_err());
assert!(client.specVersion().await.is_err());
}

#[tokio::test]
async fn deploy_account_mock() {
let mut account_deploy: Vec<StarknetMatcher> =
vec![ChainId, Nonce, SpecVersion, EstimateFee];
let mut invoke_eth_transfer: Vec<StarknetMatcher> = vec![
ChainId,
ChainId,
Nonce,
SpecVersion,
EstimateFee,
AddInvokeTransaction,
];
let mut account_deploy_last: Vec<StarknetMatcher> =
vec![Nonce, AddDeployAccountTransaction, GetTransactionReceipt];
account_deploy.append(&mut invoke_eth_transfer);
account_deploy.append(&mut account_deploy_last);
let (client, _starknet_node) =
setup_client_with_mock_starknet_node(account_deploy).await;
let block_id = BlockId::BlockTag(BlockTag::Latest);
let class_hash = Felt::try_new("0x0").unwrap();
let contract_address = Address(class_hash.clone());
let estimate_tx = estimate_fee_transaction();
let invoke_tx = invoke_transaction();
let deploy_tx = deploy_transaction();
let tx_hash = TxnHash(class_hash);

assert!(client.chainId().await.is_ok());
assert!(client
.getNonce(block_id.clone(), contract_address.clone())
.await
.is_ok());
assert!(client.specVersion().await.is_ok());
assert!(client
.estimateFee(vec![estimate_tx.clone()], vec![], block_id.clone())
.await
.is_ok());

assert!(client.chainId().await.is_ok());
assert!(client.chainId().await.is_ok());
assert!(client
.getNonce(block_id.clone(), contract_address.clone())
.await
.is_ok());
assert!(client.specVersion().await.is_ok());
assert!(client
.estimateFee(vec![estimate_tx], vec![], block_id.clone())
.await
.is_ok());
assert!(client.addInvokeTransaction(invoke_tx).await.is_ok());
assert!(client.getNonce(block_id, contract_address).await.is_ok());
assert!(client.addDeployAccountTransaction(deploy_tx).await.is_ok());
assert!(client.getTransactionReceipt(tx_hash).await.is_ok());
}
95 changes: 90 additions & 5 deletions tests/common/constants.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use beerus::gen::{
Address, BroadcastedDeclareTxnV3, BroadcastedDeclareTxnV3Type,
BroadcastedDeclareTxnV3Version, ContractClass,
ContractClassEntryPointsByType, DaMode, Felt, ResourceBounds,
ResourceBoundsMapping, SierraEntryPoint, U128, U64,
Address, BroadcastedDeclareTxn, BroadcastedDeclareTxnV3,
BroadcastedDeclareTxnV3Type, BroadcastedDeclareTxnV3Version,
BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, BroadcastedTxn,
ContractClass, ContractClassEntryPointsByType, DaMode, DeployAccountTxn,
DeployAccountTxnV3, DeployAccountTxnV3Type, DeployAccountTxnV3Version,
Felt, InvokeTxn, InvokeTxnV3, InvokeTxnV3Type, InvokeTxnV3Version,
ResourceBounds, ResourceBoundsMapping, SierraEntryPoint, U128, U64,
};

#[allow(dead_code)]
Expand All @@ -12,7 +15,35 @@ pub const COMPILED_ACCOUNT_CONTRACT: &str =
pub const DECLARE_ACCOUNT: &str = include_str!("../clob/declare_account.txt");

#[allow(dead_code)]
pub fn dummy_transaction_v3() -> BroadcastedDeclareTxnV3 {
pub fn declare_transaction() -> BroadcastedDeclareTxn {
BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
dummy_declare_transaction_v3(),
)
}

#[allow(dead_code)]
pub fn estimate_fee_transaction() -> BroadcastedTxn {
BroadcastedTxn::BroadcastedDeclareTxn(
BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
dummy_declare_transaction_v3(),
),
)
}

#[allow(dead_code)]
pub fn invoke_transaction() -> BroadcastedInvokeTxn {
BroadcastedInvokeTxn(InvokeTxn::InvokeTxnV3(dummy_invoke_transaction_v3()))
}

#[allow(dead_code)]
pub fn deploy_transaction() -> BroadcastedDeployAccountTxn {
BroadcastedDeployAccountTxn(DeployAccountTxn::DeployAccountTxnV3(
dummy_deploy_transaction_v3(),
))
}

#[allow(dead_code)]
fn dummy_declare_transaction_v3() -> BroadcastedDeclareTxnV3 {
BroadcastedDeclareTxnV3 {
account_deployment_data: vec![Felt::try_new("0x0").unwrap()],
compiled_class_hash: Felt::try_new("0x0").unwrap(),
Expand Down Expand Up @@ -60,3 +91,57 @@ pub fn dummy_transaction_v3() -> BroadcastedDeclareTxnV3 {
tip: U64::try_new("0x0").unwrap(),
}
}

#[allow(dead_code)]
fn dummy_invoke_transaction_v3() -> InvokeTxnV3 {
InvokeTxnV3 {
account_deployment_data: vec![Felt::try_new("0x0").unwrap()],
calldata: vec![Felt::try_new("0x1").unwrap()],
fee_data_availability_mode: DaMode::L1,
nonce: Felt::try_new("0x2").unwrap(),
nonce_data_availability_mode: DaMode::L1,
paymaster_data: vec![Felt::try_new("0x1").unwrap()],
r#type: InvokeTxnV3Type::Invoke,
resource_bounds: ResourceBoundsMapping {
l1_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
l2_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
},
sender_address: Address(Felt::try_new("0x3").unwrap()),
signature: vec![Felt::try_new("0x4").unwrap()],
tip: U64::try_new("0x0").unwrap(),
version: InvokeTxnV3Version::V0x3,
}
}

#[allow(dead_code)]
fn dummy_deploy_transaction_v3() -> DeployAccountTxnV3 {
DeployAccountTxnV3 {
class_hash: Felt::try_new("0x0").unwrap(),
constructor_calldata: vec![Felt::try_new("0x1").unwrap()],
contract_address_salt: Felt::try_new("0x2").unwrap(),
fee_data_availability_mode: DaMode::L1,
nonce: Felt::try_new("0x3").unwrap(),
nonce_data_availability_mode: DaMode::L1,
paymaster_data: vec![Felt::try_new("0x4").unwrap()],
r#type: DeployAccountTxnV3Type::DeployAccount,
resource_bounds: ResourceBoundsMapping {
l1_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
l2_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
},
signature: vec![Felt::try_new("0x5").unwrap()],
tip: U64::try_new("0x0").unwrap(),
version: DeployAccountTxnV3Version::V0x3,
}
}
Loading

0 comments on commit 15d8964

Please sign in to comment.