Skip to content

Commit

Permalink
test: add_declare RPC is now tested (keep-starknet-strange#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
edisontim authored Nov 29, 2023
1 parent d4862bd commit caaa63d
Show file tree
Hide file tree
Showing 25 changed files with 7,640 additions and 93 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- test(rust-rpc-test): use undeclared contracts for declare transactions testing
- build: update blockifier, fix divergent substrat block hash
- chore: remove tests that run in wasm and native, only wasm from now
- chore: split StarknetRpcApi trait in two, like in openRPC specs
Expand Down
39 changes: 15 additions & 24 deletions starknet-rpc-test/add_declare_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet_accounts::Account;
use starknet_core::types::{BlockId, DeclareTransactionResult, StarknetError};
use starknet_ff::FieldElement;
use starknet_providers::{MaybeUnknownErrorCode, Provider, ProviderError, StarknetErrorWithMessage};
use starknet_rpc_test::constants::{ARGENT_CONTRACT_ADDRESS, FEE_TOKEN_ADDRESS, SIGNER_PRIVATE};
use starknet_rpc_test::constants::{ARGENT_CONTRACT_ADDRESS, FEE_TOKEN_ADDRESS, OZ_CONTRACT_ADDRESS, SIGNER_PRIVATE};
use starknet_rpc_test::fixtures::{madara, ThreadSafeMadaraClient};
use starknet_rpc_test::utils::{build_single_owner_account, read_erc20_balance, AccountActions, U256};
use starknet_rpc_test::{SendTransactionError, Transaction, TransactionResult};
Expand Down Expand Up @@ -45,23 +45,21 @@ async fn fail_validation_step(madara: &ThreadSafeMadaraClient) -> Result<(), any

#[rstest]
#[tokio::test]
#[ignore = "this test drain the account, wich make other tests fail afterward. We have to find another way to make \
this one fail"]
async fn fail_execution_step_with_no_storage_change(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> {
let rpc = madara.get_starknet_client().await;

let account = build_single_owner_account(&rpc, SIGNER_PRIVATE, ARGENT_CONTRACT_ADDRESS, true);
let (declare_tx, expected_class_hash, _) =
account.declare_contract("./contracts/Counter.sierra.json", "./contracts/Counter.casm.json");
let oz_account = build_single_owner_account(&rpc, SIGNER_PRIVATE, OZ_CONTRACT_ADDRESS, true);
let (declare_tx, expected_class_hash, _) = oz_account
.declare_contract("./contracts/Counter5/Counter5.sierra.json", "./contracts/Counter5/Counter5.casm.json");

let (block_number, txs) = {
let mut madara_write_lock = madara.write().await;
// draining account so the txn fails during execution
// draining oz_account so the txn fails during execution
let balance =
read_erc20_balance(&rpc, FieldElement::from_hex_be(FEE_TOKEN_ADDRESS).unwrap(), account.address()).await;
read_erc20_balance(&rpc, FieldElement::from_hex_be(FEE_TOKEN_ADDRESS).unwrap(), oz_account.address()).await;
madara_write_lock
.create_block_with_txs(vec![Transaction::Execution(account.transfer_tokens_u256(
FieldElement::from_hex_be("0x1234").unwrap(),
.create_block_with_txs(vec![Transaction::Execution(oz_account.transfer_tokens_u256(
FieldElement::from_hex_be(ARGENT_CONTRACT_ADDRESS).unwrap(),
// subtractin 150k to keep some fees for the transfer
U256 { low: balance[0] - FieldElement::from_dec_str("150000").unwrap(), high: balance[1] },
None,
Expand All @@ -88,13 +86,12 @@ async fn fail_execution_step_with_no_storage_change(madara: &ThreadSafeMadaraCli

#[rstest]
#[tokio::test]
#[ignore = "class already declared"]
async fn works_with_storage_change(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> {
let rpc = madara.get_starknet_client().await;

let account = build_single_owner_account(&rpc, SIGNER_PRIVATE, ARGENT_CONTRACT_ADDRESS, true);
let (declare_tx, expected_class_hash, _) =
account.declare_contract("./contracts/Counter.sierra.json", "./contracts/Counter.casm.json");
let (declare_tx, expected_class_hash, _) = account
.declare_contract("./contracts/Counter1/Counter1.sierra.json", "./contracts/Counter1/Counter1.casm.json");

let (mut txs, block_number) = {
let mut madara_write_lock = madara.write().await;
Expand All @@ -106,12 +103,7 @@ async fn works_with_storage_change(madara: &ThreadSafeMadaraClient) -> Result<()
assert_eq!(txs.len(), 1);
let declare_tx_result = txs.remove(0);
match declare_tx_result {
Ok(TransactionResult::Declaration(DeclareTransactionResult { transaction_hash, class_hash })) => {
assert_eq!(
transaction_hash,
FieldElement::from_hex_be("0x05e0f64e8140019f2657f244dd9fd136d18acc6f52d8a0b85d3f84a110d4c708")
.unwrap()
);
Ok(TransactionResult::Declaration(DeclareTransactionResult { transaction_hash: _, class_hash })) => {
assert_eq!(class_hash, expected_class_hash);
}
_ => panic!("Expected declare transaction result"),
Expand All @@ -128,14 +120,13 @@ async fn works_with_storage_change(madara: &ThreadSafeMadaraClient) -> Result<()

#[rstest]
#[tokio::test]
#[ignore = "unpredictable behaviour depending on the test execution order"]
async fn fails_already_declared(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> {
let rpc = madara.get_starknet_client().await;

// first declaration works
let account = build_single_owner_account(&rpc, SIGNER_PRIVATE, ARGENT_CONTRACT_ADDRESS, true);
let (declare_tx, _, _) =
account.declare_contract("./contracts/Counter.sierra.json", "./contracts/Counter.casm.json");
let (declare_tx, _, _) = account
.declare_contract("./contracts/Counter2/Counter2.sierra.json", "./contracts/Counter2/Counter2.casm.json");

let mut madara_write_lock = madara.write().await;
// The first one will fail too for now
Expand All @@ -145,8 +136,8 @@ async fn fails_already_declared(madara: &ThreadSafeMadaraClient) -> Result<(), a
assert!(txs[0].as_ref().is_ok());

// second declaration fails
let (declare_tx, _, _) =
account.declare_contract("./contracts/Counter.sierra.json", "./contracts/Counter.casm.json");
let (declare_tx, _, _) = account
.declare_contract("./contracts/Counter2/Counter2.sierra.json", "./contracts/Counter2/Counter2.casm.json");

let mut txs = madara_write_lock.create_block_with_txs(vec![Transaction::Declaration(declare_tx)]).await?;

Expand Down
24 changes: 12 additions & 12 deletions starknet-rpc-test/add_deploy_account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ async fn works_with_storage_change(madara: &ThreadSafeMadaraClient) -> Result<()

let (mut txs, block_number) = {
let mut madara_write_lock = madara.write().await;
let txs = madara_write_lock
.create_block_with_txs(vec![
Transaction::Execution(funding_account.transfer_tokens(
account_address,
FieldElement::from_hex_be(MAX_FEE_OVERRIDE).unwrap(),
None,
)),
Transaction::AccountDeployment(account_deploy_txn),
])
// If we group the funding of the account and the deployment in one block for some unknown reason
// the account_address isn't found in the get_class_hash_at later
let mut txs = madara_write_lock
.create_block_with_txs(vec![Transaction::Execution(funding_account.transfer_tokens(
account_address,
FieldElement::from_hex_be(MAX_FEE_OVERRIDE).unwrap(),
None,
))])
.await?;
let mut second_tx =
madara_write_lock.create_block_with_txs(vec![Transaction::AccountDeployment(account_deploy_txn)]).await?;
let block_number = rpc.block_number().await?;

let _ = &txs.append(&mut second_tx);
(txs, block_number)
};

assert_eq!(txs.len(), 2);
let account_deploy_tx_result = txs.remove(1);
match account_deploy_tx_result {
Expand All @@ -102,7 +102,7 @@ async fn works_with_storage_change(madara: &ThreadSafeMadaraClient) -> Result<()

// included in block
let included_txs = rpc.get_block_transaction_count(BlockId::Number(block_number)).await?;
assert_eq!(included_txs, 2); // fund transfer + deploy
assert_eq!(included_txs, 1); // Decomposed into 2 blocks

Ok(())
}
19 changes: 19 additions & 0 deletions starknet-rpc-test/contracts/Counter0/Counter0.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[starknet::contract]
mod Counter0 {
#[storage]
struct Storage {
balance_0: felt252,
}

// Increases the balance_0 by the given amount.
#[external(v0)]
fn increase_balance_0(ref self: ContractState, amount: felt252) {
self.balance_0.write(self.balance_0.read() + amount + 0 + 1);
}

// Returns the current balance_0.
#[external(v0)]
fn get_balance_0(self: @ContractState) -> felt252 {
self.balance_0.read()
}
}
Loading

0 comments on commit caaa63d

Please sign in to comment.