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

fix: change order of arguments in fee transfer's calldata #783

Merged
merged 8 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transaction/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ mod tests {
// We expect a fee transfer failure because the fee token contract is not set up
assert_matches!(
internal_declare.execute(&mut state, &BlockContext::default()),
Err(TransactionError::FeeError(e)) if e == "Fee transfer failure"
Err(TransactionError::FeeTransferError(_))
);
}
}
4 changes: 4 additions & 0 deletions src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub enum TransactionError {
InvokeFunctionZeroHasNonce,
#[error("Invalid transaction nonce. Expected: {0} got {1}")]
InvalidTransactionNonce(String, String),
#[error("Actual fee exceeds max fee. Actual: {0}, Max: {1}")]
ActualFeeExceedsMaxFee(u128, u128),
#[error("Fee transfer failure: {0}")]
FeeTransferError(Box<TransactionError>),
#[error("{0}")]
FeeError(String),
#[error("Cairo resource names must be contained in fee weights dict")]
Expand Down
11 changes: 6 additions & 5 deletions src/transaction/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ pub(crate) fn execute_fee_transfer<S: State + StateReader>(
actual_fee: u128,
) -> Result<CallInfo, TransactionError> {
if actual_fee > tx_execution_context.max_fee {
return Err(TransactionError::FeeError(
"Actual fee exceeded max fee.".to_string(),
return Err(TransactionError::ActualFeeExceedsMaxFee(
actual_fee,
tx_execution_context.max_fee,
));
}

let fee_token_address = block_context.starknet_os_config.fee_token_address.clone();

let calldata = [
block_context.block_info.sequencer_address.0.clone(),
0.into(),
Felt252::from(actual_fee),
Felt252::from(actual_fee), // U256.low
0.into(), // U256.high
]
.to_vec();

Expand All @@ -61,7 +62,7 @@ pub(crate) fn execute_fee_transfer<S: State + StateReader>(
false,
);
// TODO: Avoid masking the error from the fee transfer.
fee_transfer_exec.map_err(|_| TransactionError::FeeError("Fee transfer failure".to_string()))
fee_transfer_exec.map_err(|e| TransactionError::FeeTransferError(Box::new(e)))
}

// ----------------------------------------------------------------------------------------
Expand Down
59 changes: 31 additions & 28 deletions src/transaction/invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,29 @@ mod tests {
}

#[test]
// Test fee calculation is done correctly but payment to sequencer fails due to been WIP.
fn test_execute_invoke_fee_payment_to_sequencer_should_fail() {
// Test fee calculation is done correctly but payment to sequencer fails due
// to the token contract not being deployed
fn test_invoke_with_non_deployed_fee_token_should_fail() {
let contract_address = Address(0.into());

// Instantiate CachedState
let mut state_reader = InMemoryStateReader::default();
// Set contract_class
let class_hash = [1; 32];
let contract_class =
ContractClass::try_from(PathBuf::from("starknet_programs/fibonacci.json")).unwrap();
// Set contact_state
let nonce = Felt252::zero();

state_reader
.address_to_class_hash_mut()
.insert(contract_address.clone(), class_hash);
state_reader
.address_to_nonce
.insert(contract_address.clone(), nonce);

let internal_invoke_function = InvokeFunction {
contract_address: Address(0.into()),
contract_address,
entry_point_selector: Felt252::from_str_radix(
"112e35f48499939272000bd72eb840e502ca4c3aefa8800992e8defb746e0c9",
16,
Expand All @@ -728,23 +747,6 @@ mod tests {
skip_fee_transfer: false,
};

// Instantiate CachedState
let mut state_reader = InMemoryStateReader::default();
// Set contract_class
let class_hash = [1; 32];
let contract_class =
ContractClass::try_from(PathBuf::from("starknet_programs/fibonacci.json")).unwrap();
// Set contact_state
let contract_address = Address(0.into());
let nonce = Felt252::zero();

state_reader
.address_to_class_hash_mut()
.insert(contract_address.clone(), class_hash);
state_reader
.address_to_nonce
.insert(contract_address, nonce);

let mut state = CachedState::new(state_reader.clone(), None, None);

// Initialize state.contract_classes
Expand All @@ -761,10 +763,9 @@ mod tests {
(String::from("range_check_builtin"), 70.into()),
]);

let expected_error = internal_invoke_function.execute(&mut state, &block_context, 0);
let error_msg = "Fee transfer failure".to_string();
assert!(expected_error.is_err());
assert_matches!(expected_error.unwrap_err(), TransactionError::FeeError(msg) if msg == error_msg);
let result = internal_invoke_function.execute(&mut state, &block_context, 0);
assert!(result.is_err());
assert_matches!(result.unwrap_err(), TransactionError::FeeTransferError(_));
}

#[test]
Expand Down Expand Up @@ -824,10 +825,12 @@ mod tests {
]);
block_context.starknet_os_config.gas_price = 1;

let expected_error = internal_invoke_function.execute(&mut state, &block_context, 0);
let error_msg = "Actual fee exceeded max fee.".to_string();
assert!(expected_error.is_err());
assert_matches!(expected_error.unwrap_err(), TransactionError::FeeError(actual_error_msg) if actual_error_msg == error_msg);
let error = internal_invoke_function.execute(&mut state, &block_context, 0);
assert!(error.is_err());
assert_matches!(
error.unwrap_err(),
TransactionError::ActualFeeExceedsMaxFee(_, _)
);
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ pub fn get_deployed_address_class_hash_at_address<S: StateReader>(
.to_owned();

if class_hash == *UNINITIALIZED_CLASS_HASH {
return Err(TransactionError::NotDeployedContract(class_hash));
return Err(TransactionError::NotDeployedContract(felt_to_hash(
&contract_address.0,
)));
}
Ok(class_hash)
}
Expand Down
Loading