Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
test: validate_unsigned noce logic for User transactions (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys authored Oct 12, 2023
1 parent 6a1df16 commit a235719
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 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: validate Nonce for unsigned user txs
- fix: fixed declare V0 placeholder with the hash of an empty list of felts
- feat(cli): `run` is the by default command when running the `madara` bin
- refacto(cli): `run` and `setup` commands are defined in their own files
Expand Down
36 changes: 33 additions & 3 deletions crates/pallets/starknet/src/tests/declare_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use mp_felt::Felt252Wrapper;
use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::{DeclareTransactionV1, DeclareTransactionV2};
use sp_runtime::traits::ValidateUnsigned;
use sp_runtime::transaction_validity::{TransactionSource, TransactionValidityError, ValidTransaction};
use starknet_api::api_core::ClassHash;
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction,
};
use starknet_api::api_core::{ClassHash, Nonce};
use starknet_api::hash::StarkFelt;
use starknet_crypto::FieldElement;

use super::mock::default_mock::*;
use super::mock::*;
use super::utils::{get_contract_class, sign_message_hash};
use crate::tests::get_declare_dummy;
use crate::tests::{get_declare_dummy, set_nonce};
use crate::{Config, Error};

#[test]
Expand Down Expand Up @@ -364,3 +367,30 @@ fn test_verify_require_tag() {
assert_eq!(validate_result, valid_transaction_expected)
});
}

#[test]
fn test_verify_nonce_in_unsigned_tx() {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);

let transaction = get_declare_dummy(
Starknet::chain_id(),
Felt252Wrapper::ONE,
AccountType::V0(AccountTypeV0Inner::NoValidate),
);
let erc20_class = get_contract_class("ERC20.json", 0);

let tx_sender = (*transaction.sender_address()).into();
let tx_source = TransactionSource::InBlock;
let call = crate::Call::declare { transaction, contract_class: erc20_class };

assert!(Starknet::validate_unsigned(tx_source, &call).is_ok());

set_nonce::<MockRuntime>(&tx_sender, &Nonce(StarkFelt::from(2u64)));

assert_eq!(
Starknet::validate_unsigned(tx_source, &call),
Err(TransactionValidityError::Invalid(InvalidTransaction::Stale))
);
});
}
29 changes: 26 additions & 3 deletions crates/pallets/starknet/src/tests/deploy_account_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use mp_felt::Felt252Wrapper;
use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::DeployAccountTransaction;
use sp_runtime::traits::ValidateUnsigned;
use sp_runtime::transaction_validity::TransactionSource;
use starknet_api::api_core::ContractAddress;
use sp_runtime::transaction_validity::{InvalidTransaction, TransactionSource, TransactionValidityError};
use starknet_api::api_core::{ContractAddress, Nonce};
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::{Event as StarknetEvent, EventContent, EventData, EventKey};
use starknet_crypto::FieldElement;
Expand All @@ -13,7 +13,7 @@ use super::mock::default_mock::*;
use super::mock::*;
use super::utils::sign_message_hash;
use crate::tests::constants::{ACCOUNT_PUBLIC_KEY, SALT};
use crate::tests::{get_deploy_account_dummy, set_infinite_tokens};
use crate::tests::{get_deploy_account_dummy, set_infinite_tokens, set_nonce};
use crate::{Config, Error, Event, StorageView};

#[test]
Expand Down Expand Up @@ -335,3 +335,26 @@ fn set_signer(address: ContractAddress, account_type: AccountType) {
StarkFelt::try_from(ACCOUNT_PUBLIC_KEY).unwrap(),
);
}

#[test]
fn test_verify_nonce_in_unsigned_tx() {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);

let transaction =
get_deploy_account_dummy(Felt252Wrapper::ZERO, *SALT, AccountType::V0(AccountTypeV0Inner::NoValidate));

let tx_sender = transaction.account_address().into();
let tx_source = TransactionSource::InBlock;
let call = crate::Call::deploy_account { transaction };

assert!(Starknet::validate_unsigned(tx_source, &call).is_ok());

set_nonce::<MockRuntime>(&tx_sender, &Nonce(StarkFelt::from(1u64)));

assert_eq!(
Starknet::validate_unsigned(tx_source, &call),
Err(TransactionValidityError::Invalid(InvalidTransaction::Stale))
);
});
}
32 changes: 28 additions & 4 deletions crates/pallets/starknet/src/tests/invoke_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::{InvokeTransaction, InvokeTransactionV1};
use pretty_assertions::assert_eq;
use sp_runtime::traits::ValidateUnsigned;
use sp_runtime::transaction_validity::{TransactionSource, TransactionValidityError, ValidTransaction};
use starknet_api::api_core::{ContractAddress, PatriciaKey};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction,
};
use starknet_api::api_core::{ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Event as StarknetEvent, EventContent, EventData, EventKey, Fee, TransactionHash};
Expand All @@ -20,9 +22,9 @@ use super::utils::sign_message_hash;
use crate::message::Message;
use crate::tests::{
get_invoke_argent_dummy, get_invoke_braavos_dummy, get_invoke_dummy, get_invoke_emit_event_dummy,
get_invoke_nonce_dummy, get_invoke_openzeppelin_dummy, get_storage_read_write_dummy,
get_invoke_nonce_dummy, get_invoke_openzeppelin_dummy, get_storage_read_write_dummy, set_nonce,
};
use crate::{Config, Error, Event, StorageView};
use crate::{Call, Config, Error, Event, StorageView};

#[test]
fn given_hardcoded_contract_run_invoke_tx_fails_sender_not_deployed() {
Expand Down Expand Up @@ -491,3 +493,25 @@ fn test_verify_require_tag() {
assert_eq!(validate_result.unwrap(), valid_transaction_expected.unwrap())
});
}

#[test]
fn test_verify_nonce_in_unsigned_tx() {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);

let transaction = get_invoke_dummy(Felt252Wrapper::ZERO);

let tx_sender = transaction.sender_address.into();
let tx_source = TransactionSource::InBlock;
let call = Call::invoke { transaction: transaction.into() };

assert!(Starknet::validate_unsigned(tx_source, &call).is_ok());

set_nonce::<MockRuntime>(&tx_sender, &Nonce(StarkFelt::from(1u64)));

assert_eq!(
Starknet::validate_unsigned(tx_source, &call),
Err(TransactionValidityError::Invalid(InvalidTransaction::Stale))
);
});
}
9 changes: 7 additions & 2 deletions crates/pallets/starknet/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use blockifier::state::state_api::State;
use mp_felt::Felt252Wrapper;
use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::{DeclareTransaction, DeclareTransactionV1, DeployAccountTransaction, InvokeTransactionV1};
use starknet_api::api_core::ContractAddress;
use starknet_api::api_core::{ContractAddress, Nonce};
use starknet_api::hash::StarkFelt;

use self::mock::default_mock::{MockRuntime, Starknet};
use self::mock::{get_account_address, AccountType};
use crate::blockifier_state_adapter::BlockifierStateAdapter;
use crate::tests::mock::account_helper;
use crate::tests::utils::sign_message_hash;
use crate::Config;
use crate::{Config, Nonces};

mod account_helper;
mod call_contract;
Expand Down Expand Up @@ -219,3 +219,8 @@ pub fn set_infinite_tokens<T: Config>(contract_address: &ContractAddress) {
state_adapter.set_storage_at(fee_token_address, low_key, StarkFelt::from(u64::MAX as u128));
state_adapter.set_storage_at(fee_token_address, high_key, StarkFelt::from(u64::MAX as u128));
}

/// Sets nonce for the given address.
pub fn set_nonce<T: Config>(address: &ContractAddress, nonce: &Nonce) {
Nonces::<T>::insert(address, nonce)
}

0 comments on commit a235719

Please sign in to comment.