Skip to content

Commit

Permalink
remove gas_forwaded from data when not used
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Sep 5, 2023
1 parent 443eea1 commit 377698d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
45 changes: 33 additions & 12 deletions packages/fuels-programs/src/call_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,29 +247,43 @@ pub(crate) fn build_script_data_from_contract_calls(
let gas_forwarded = call.call_parameters.gas_forwarded();

let call_param_offsets = CallOpcodeParamsOffset {
gas_forwarded_offset: gas_forwarded.map(|_| segment_offset),
amount_offset: segment_offset + WORD_SIZE,
asset_id_offset: segment_offset + 2 * WORD_SIZE,
call_data_offset: segment_offset + 2 * WORD_SIZE + AssetId::LEN,
amount_offset: segment_offset,
asset_id_offset: segment_offset + WORD_SIZE,
call_data_offset: segment_offset + WORD_SIZE + AssetId::LEN,
gas_forwarded_offset: gas_forwarded
.map(|_| segment_offset + WORD_SIZE + AssetId::LEN + ContractId::LEN + WORD_SIZE),
};
param_offsets.push(call_param_offsets);

script_data.extend(gas_forwarded.unwrap_or_default().to_be_bytes());
script_data.extend(call.call_parameters.amount().to_be_bytes());
script_data.extend(call.call_parameters.asset_id().iter());
script_data.extend(call.contract_id.hash().as_ref());
script_data.extend(call.encoded_selector);

let gas_forwarded_size = gas_forwarded
.map(|gf| {
script_data.extend(gf.to_be_bytes());

WORD_SIZE
})
.unwrap_or_default();

// If the method call takes custom inputs or has more than
// one argument, we need to calculate the `call_data_offset`,
// which points to where the data for the custom types start in the
// transaction. If it doesn't take any custom inputs, this isn't necessary.
let encoded_args_start_offset = if call.compute_custom_input_offset {
// Custom inputs are stored after the previously added parameters,
// including custom_input_offset
let custom_input_offset =
segment_offset + 2 * WORD_SIZE + AssetId::LEN + ContractId::LEN + 2 * WORD_SIZE;
let custom_input_offset = segment_offset
+ WORD_SIZE // amount size
+ AssetId::LEN
+ ContractId::LEN
+ WORD_SIZE // encoded_selector size
+ gas_forwarded_size
+ WORD_SIZE; // custom_input_offset size
script_data.extend((custom_input_offset as Word).to_be_bytes());

custom_input_offset
} else {
segment_offset
Expand Down Expand Up @@ -556,6 +570,7 @@ mod test {
async fn test_script_data() {
// Arrange
const SELECTOR_LEN: usize = WORD_SIZE;
const GAS_FORWARDED_SIZE: usize = WORD_SIZE;
const NUM_CALLS: usize = 3;

let contract_ids = vec![
Expand Down Expand Up @@ -624,25 +639,31 @@ mod test {
}

// Calls 1 and 3 have their input arguments after the selector
let call_1_arg_offset = param_offsets[0].call_data_offset + ContractId::LEN + SELECTOR_LEN;
let call_1_arg_offset =
param_offsets[0].call_data_offset + ContractId::LEN + SELECTOR_LEN + GAS_FORWARDED_SIZE;
let call_1_arg = script_data[call_1_arg_offset..call_1_arg_offset + WORD_SIZE].to_vec();
assert_eq!(call_1_arg, args[0].resolve(0));

let call_3_arg_offset = param_offsets[2].call_data_offset + ContractId::LEN + SELECTOR_LEN;
let call_3_arg_offset =
param_offsets[2].call_data_offset + ContractId::LEN + SELECTOR_LEN + GAS_FORWARDED_SIZE;
let call_3_arg = script_data[call_3_arg_offset..call_3_arg_offset + WORD_SIZE].to_vec();
assert_eq!(call_3_arg, args[2].resolve(0));

// Call 2 has custom inputs and custom_input_offset
let call_2_arg_offset = param_offsets[1].call_data_offset + ContractId::LEN + SELECTOR_LEN;
let call_2_arg_offset =
param_offsets[1].call_data_offset + ContractId::LEN + SELECTOR_LEN + GAS_FORWARDED_SIZE;
let custom_input_offset =
script_data[call_2_arg_offset..call_2_arg_offset + WORD_SIZE].to_vec();
assert_eq!(
custom_input_offset,
(call_2_arg_offset + WORD_SIZE).to_be_bytes()
);

let custom_input_offset =
param_offsets[1].call_data_offset + ContractId::LEN + SELECTOR_LEN + WORD_SIZE;
let custom_input_offset = param_offsets[1].call_data_offset
+ ContractId::LEN
+ SELECTOR_LEN
+ GAS_FORWARDED_SIZE
+ WORD_SIZE;
let custom_input =
script_data[custom_input_offset..custom_input_offset + WORD_SIZE].to_vec();
assert_eq!(custom_input, args[1].resolve(0));
Expand Down
4 changes: 2 additions & 2 deletions packages/fuels/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ async fn test_contract_call_fee_estimation() -> Result<()> {

let expected_min_gas_price = 0; // This is the default min_gas_price from the ConsensusParameters
let expected_gas_used = 397;
let expected_metered_bytes_size = 720;
let expected_total_fee = 328;
let expected_metered_bytes_size = 712;
let expected_total_fee = 325;

let estimated_transaction_cost = contract_instance
.methods()
Expand Down

0 comments on commit 377698d

Please sign in to comment.