Skip to content

Commit

Permalink
No fee pallet option (keep-starknet-strange#868)
Browse files Browse the repository at this point in the history
Co-authored-by: apoorveth <[email protected]>
  • Loading branch information
apoorvsadana and apoorvsadana authored Jul 18, 2023
1 parent 6d92e60 commit c46c02e
Show file tree
Hide file tree
Showing 30 changed files with 412 additions and 530 deletions.
1 change: 1 addition & 0 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ runtime-benchmarks = [
# in the near future.
try-runtime = ["madara-runtime/try-runtime", "try-runtime-cli/try-runtime"]
madara-state-root = ["madara-runtime/madara-state-root"]
disable-transaction-fee = ["madara-runtime/disable-transaction-fee"]
11 changes: 10 additions & 1 deletion crates/pallets/starknet/src/blockifier_state_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use mp_starknet::crypto::commitment::{calculate_class_commitment_leaf_hash, calc
use mp_starknet::execution::types::{
ClassHashWrapper, CompiledClassHashWrapper, ContractAddressWrapper, Felt252Wrapper,
};
use mp_starknet::state::StateChanges;
use mp_starknet::state::{FeeConfig, StateChanges};
use sp_core::Get;
use starknet_api::api_core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::hash::StarkFelt;
Expand Down Expand Up @@ -42,6 +42,15 @@ where
}
}

impl<T> FeeConfig for BlockifierStateAdapter<T>
where
T: Config,
{
fn is_transaction_fee_disabled(&self) -> bool {
T::DisableTransactionFee::get()
}
}

impl<T: Config> Default for BlockifierStateAdapter<T> {
fn default() -> Self {
Self { storage_update: BTreeMap::default(), class_hash_update: usize::default(), _phantom: PhantomData }
Expand Down
6 changes: 3 additions & 3 deletions crates/pallets/starknet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ pub mod pallet {
/// As this is a very time-consuming process we preferred to let it optional for now
/// Not every application needs it but if you need to use it you can enable it
type EnableStateRoot: Get<bool>;
/// A bool to disable transaction fees and make all transactions free
#[pallet::constant]
type DisableTransactionFee: Get<bool>;
#[pallet::constant]
type InvokeTxMaxNSteps: Get<u32>;
#[pallet::constant]
Expand Down Expand Up @@ -1105,9 +1108,6 @@ impl<T: Config> Pallet<T> {

/// Estimate the fee associated with transaction
pub fn estimate_fee(transaction: Transaction) -> Result<(u64, u64), DispatchError> {
// Check if contract is deployed
ensure!(ContractClassHashes::<T>::contains_key(transaction.sender_address), Error::<T>::AccountNotDeployed);

match transaction.execute(
&mut BlockifierStateAdapter::<T>::default(),
&Self::get_block_context(),
Expand Down
7 changes: 4 additions & 3 deletions crates/pallets/starknet/src/tests/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use starknet_api::api_core::{ChainId, ContractAddress};
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::hash::StarkFelt;

use super::mock::default_mock::*;
use super::mock::*;
use crate::tests::constants::FEE_TOKEN_ADDRESS;
use crate::tests::get_invoke_dummy;
use crate::{pallet, SeqAddrUpdate, SequencerAddress};

#[test]
fn store_block_no_pending_transactions_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
// initialize first block
let header = System::finalize();
const BLOCK_NUMBER: u64 = 1;
Expand All @@ -44,7 +45,7 @@ fn store_block_no_pending_transactions_works() {

#[test]
fn store_block_with_pending_transactions_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
// initialize first block
let header = System::finalize();
const BLOCK_NUMBER: u64 = 1;
Expand Down Expand Up @@ -88,7 +89,7 @@ fn store_block_with_pending_transactions_works() {

#[test]
fn get_block_context_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
// initialize first block
let header = System::finalize();
const BLOCK_NUMBER: u64 = 1;
Expand Down
12 changes: 5 additions & 7 deletions crates/pallets/starknet/src/tests/call_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use sp_core::ConstU32;
use sp_runtime::BoundedVec;

use super::constants::TOKEN_CONTRACT_CLASS_HASH;
use super::mock::default_mock::*;
use super::mock::*;
use crate::tests::utils::build_get_balance_contract_call;

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

let origin = RuntimeOrigin::none();
Expand Down Expand Up @@ -50,12 +52,8 @@ fn given_call_contract_call_works() {
Felt252Wrapper::from_hex_be("00dc58c1280862c95964106ef9eba5d9ed8c0c16d05883093e4540f22b829dff").unwrap();

// Call balanceOf
let balance_of_selector =
Felt252Wrapper::from_hex_be("0x02e4263afad30923c891518314c3c95dbe830a16874e8abc5777a9a20b54c76e").unwrap();
let calldata = bounded_vec![
sender_account // owner address
];
let res = Starknet::call_contract(expected_erc20_address, balance_of_selector, calldata);
let call_args = build_get_balance_contract_call(sender_account, expected_erc20_address);
let res = Starknet::call_contract(call_args.0,call_args.1,call_args.2);
assert_ok!(res.clone());
pretty_assertions::assert_eq!(
res.unwrap(),
Expand Down
27 changes: 14 additions & 13 deletions crates/pallets/starknet/src/tests/declare_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use sp_runtime::traits::ValidateUnsigned;
use sp_runtime::transaction_validity::{TransactionSource, TransactionValidityError, ValidTransaction};
use starknet_crypto::FieldElement;

use super::mock::default_mock::*;
use super::mock::*;
use super::utils::{get_contract_class, sign_message_hash};
use crate::Error;

#[test]
fn given_contract_declare_tx_works_once_not_twice() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();
let account_addr = get_account_address(AccountType::V0(AccountTypeV0Inner::NoValidate));
Expand Down Expand Up @@ -42,7 +43,7 @@ fn given_contract_declare_tx_works_once_not_twice() {

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

let none_origin = RuntimeOrigin::none();
Expand Down Expand Up @@ -73,7 +74,7 @@ fn given_contract_declare_tx_fails_sender_not_deployed() {

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

let none_origin = RuntimeOrigin::none();
Expand Down Expand Up @@ -104,7 +105,7 @@ fn given_contract_declare_tx_fails_wrong_tx_version() {

#[test]
fn given_contract_declare_on_openzeppelin_account_then_it_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();

Expand Down Expand Up @@ -146,7 +147,7 @@ fn given_contract_declare_on_openzeppelin_account_then_it_works() {

#[test]
fn given_contract_declare_on_openzeppelin_account_with_incorrect_signature_then_it_fails() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();

Expand Down Expand Up @@ -180,7 +181,7 @@ fn given_contract_declare_on_openzeppelin_account_with_incorrect_signature_then_

#[test]
fn given_contract_declare_on_braavos_account_then_it_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();

Expand Down Expand Up @@ -222,7 +223,7 @@ fn given_contract_declare_on_braavos_account_then_it_works() {

#[test]
fn given_contract_declare_on_braavos_account_with_incorrect_signature_then_it_fails() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();

Expand Down Expand Up @@ -256,7 +257,7 @@ fn given_contract_declare_on_braavos_account_with_incorrect_signature_then_it_fa

#[test]
fn given_contract_declare_on_argent_account_then_it_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();

Expand Down Expand Up @@ -298,7 +299,7 @@ fn given_contract_declare_on_argent_account_then_it_works() {

#[test]
fn given_contract_declare_on_argent_account_with_incorrect_signature_then_it_fails() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();

Expand Down Expand Up @@ -332,7 +333,7 @@ fn given_contract_declare_on_argent_account_with_incorrect_signature_then_it_fai

#[test]
fn given_contract_declare_on_cairo_1_no_validate_account_then_it_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();

Expand Down Expand Up @@ -376,7 +377,7 @@ fn given_contract_declare_on_cairo_1_no_validate_account_then_it_works() {

#[test]
fn test_verify_tx_longevity() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let account_addr = get_account_address(AccountType::V0(AccountTypeV0Inner::NoValidate));

Expand Down Expand Up @@ -404,7 +405,7 @@ fn test_verify_tx_longevity() {

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

let account_addr = get_account_address(AccountType::V0(AccountTypeV0Inner::NoValidate));
Expand Down Expand Up @@ -443,7 +444,7 @@ fn test_verify_no_require_tag() {

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

let account_addr = get_account_address(AccountType::V0(AccountTypeV0Inner::NoValidate));
Expand Down
33 changes: 17 additions & 16 deletions crates/pallets/starknet/src/tests/deploy_account_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use mp_starknet::transaction::types::{DeployAccountTransaction, EventWrapper};
use sp_runtime::traits::ValidateUnsigned;
use sp_runtime::transaction_validity::TransactionSource;

use super::mock::default_mock::*;
use super::mock::*;
use super::utils::sign_message_hash;
use crate::tests::constants::ACCOUNT_PUBLIC_KEY;
use crate::{Error, Event, StorageView};

#[test]
fn given_contract_run_deploy_account_tx_works() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();
// TEST ACCOUNT CONTRACT
Expand Down Expand Up @@ -63,7 +64,7 @@ fn given_contract_run_deploy_account_tx_works() {

#[test]
fn given_contract_run_deploy_account_tx_twice_fails() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let salt = "0x03b37cbe4e9eac89d54c5f7cc6329a63a63e8c8db2bf936f981041e086752463";
let (address, account_class_hash, calldata) =
Expand Down Expand Up @@ -101,7 +102,7 @@ fn given_contract_run_deploy_account_tx_twice_fails() {

#[test]
fn given_contract_run_deploy_account_tx_undeclared_then_it_fails() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
let none_origin = RuntimeOrigin::none();
let account_class_hash = get_account_class_hash(AccountType::V0(AccountTypeV0Inner::Argent));
Expand All @@ -125,7 +126,7 @@ fn given_contract_run_deploy_account_tx_undeclared_then_it_fails() {

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

let none_origin = RuntimeOrigin::none();
Expand Down Expand Up @@ -163,7 +164,7 @@ fn given_contract_run_deploy_account_tx_fails_wrong_tx_version() {

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

let none_origin = RuntimeOrigin::none();
Expand All @@ -189,7 +190,7 @@ fn given_contract_run_deploy_account_openzeppelin_tx_works() {
signature: bounded_vec!(),
is_query: false,
};
let mp_transaction = transaction.clone().from_deploy(get_chain_id()).unwrap();
let mp_transaction = transaction.clone().from_deploy(Starknet::chain_id()).unwrap();

let tx_hash = mp_transaction.hash;
transaction.signature = sign_message_hash(tx_hash);
Expand All @@ -205,7 +206,7 @@ fn given_contract_run_deploy_account_openzeppelin_tx_works() {

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

let none_origin = RuntimeOrigin::none();
Expand All @@ -232,7 +233,7 @@ fn given_contract_run_deploy_account_openzeppelin_with_incorrect_signature_then_
is_query: false,
};

let address = transaction.clone().from_deploy(get_chain_id()).unwrap().sender_address;
let address = transaction.clone().from_deploy(Starknet::chain_id()).unwrap().sender_address;
set_signer(address, AccountType::V0(AccountTypeV0Inner::Openzeppelin));

assert_err!(
Expand All @@ -244,7 +245,7 @@ fn given_contract_run_deploy_account_openzeppelin_with_incorrect_signature_then_

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

let none_origin = RuntimeOrigin::none();
Expand All @@ -271,7 +272,7 @@ fn given_contract_run_deploy_account_argent_tx_works() {
is_query: false,
};

let mp_transaction = transaction.clone().from_deploy(get_chain_id()).unwrap();
let mp_transaction = transaction.clone().from_deploy(Starknet::chain_id()).unwrap();

let tx_hash = mp_transaction.hash;
transaction.signature = sign_message_hash(tx_hash);
Expand All @@ -287,7 +288,7 @@ fn given_contract_run_deploy_account_argent_tx_works() {

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

let none_origin = RuntimeOrigin::none();
Expand All @@ -314,7 +315,7 @@ fn given_contract_run_deploy_account_argent_with_incorrect_signature_then_it_fai
is_query: false,
};

let address = transaction.clone().from_deploy(get_chain_id()).unwrap().sender_address;
let address = transaction.clone().from_deploy(Starknet::chain_id()).unwrap().sender_address;
set_signer(address, AccountType::V0(AccountTypeV0Inner::Argent));

assert_err!(
Expand All @@ -326,7 +327,7 @@ fn given_contract_run_deploy_account_argent_with_incorrect_signature_then_it_fai

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

let none_origin = RuntimeOrigin::none();
Expand Down Expand Up @@ -363,7 +364,7 @@ fn given_contract_run_deploy_account_braavos_tx_works() {
is_query: false,
};

let address = transaction.clone().from_deploy(get_chain_id()).unwrap().sender_address;
let address = transaction.clone().from_deploy(Starknet::chain_id()).unwrap().sender_address;
set_infinite_tokens(address);
set_signer(address, AccountType::V0(AccountTypeV0Inner::Braavos));

Expand All @@ -374,7 +375,7 @@ fn given_contract_run_deploy_account_braavos_tx_works() {

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

let none_origin = RuntimeOrigin::none();
Expand Down Expand Up @@ -416,7 +417,7 @@ fn given_contract_run_deploy_account_braavos_with_incorrect_signature_then_it_fa

#[test]
fn test_verify_tx_longevity() {
new_test_ext().execute_with(|| {
new_test_ext::<MockRuntime>().execute_with(|| {
basic_test_setup(2);
// TEST ACCOUNT CONTRACT
// - ref testnet tx(0x0751b4b5b95652ad71b1721845882c3852af17e2ed0c8d93554b5b292abb9810)
Expand Down
Loading

0 comments on commit c46c02e

Please sign in to comment.