Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add test for compute_calls_instructions_len #1183

Merged
merged 20 commits into from
Dec 15, 2023
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
019fdaa
chore: add test for compute_calls_instructions_len
Br1ght0ne Nov 1, 2023
307a791
Add test combinations based on output_param and gas_offset
Br1ght0ne Nov 7, 2023
fd7b8d6
Merge branch 'master' into oleksii/test-compute-instruction-len
Br1ght0ne Nov 7, 2023
103499a
Merge branch 'master' into oleksii/test-compute-instruction-len
MujkicA Nov 8, 2023
1c43888
Merge branch 'master' into oleksii/test-compute-instruction-len
iqdecay Nov 8, 2023
d458775
Merge branch 'master' into oleksii/test-compute-instruction-len
Br1ght0ne Nov 12, 2023
4de9850
Merge branch 'master' into oleksii/test-compute-instruction-len
Br1ght0ne Nov 20, 2023
a41391b
Merge branch 'master' into oleksii/test-compute-instruction-len
iqdecay Nov 20, 2023
1714ad2
Address review comments
Br1ght0ne Nov 27, 2023
b4b4799
Merge branch 'master' into oleksii/test-compute-instruction-len
Br1ght0ne Nov 27, 2023
0c08092
cargo fmt
Br1ght0ne Nov 27, 2023
e98f37e
Merge branch 'master' into oleksii/test-compute-instruction-len
Br1ght0ne Nov 29, 2023
21a16fc
Merge branch 'master' into oleksii/test-compute-instruction-len
MujkicA Nov 30, 2023
0dab228
Merge branch 'master' into oleksii/test-compute-instruction-len
hal3e Dec 7, 2023
29ae175
Merge branch 'master' into oleksii/test-compute-instruction-len
MujkicA Dec 11, 2023
377536a
Merge branch 'master' into oleksii/test-compute-instruction-len
segfault-magnet Dec 12, 2023
e179563
Promote instruction counts to constants
Br1ght0ne Dec 13, 2023
1214ed5
Merge branch 'master' into oleksii/test-compute-instruction-len
MujkicA Dec 13, 2023
6e0b1db
Merge branch 'master' into oleksii/test-compute-instruction-len
Br1ght0ne Dec 13, 2023
0d6f80e
Merge branch 'master' into oleksii/test-compute-instruction-len
iqdecay Dec 14, 2023
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
99 changes: 99 additions & 0 deletions packages/fuels-programs/src/call_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,4 +947,103 @@ mod test {
expected_asset_id_amounts
)
}

mod compute_calls_instructions_len {
use fuel_asm::Instruction;
use fuels_core::types::{enum_variants::EnumVariants, param_types::ParamType};

use crate::{call_utils::compute_calls_instructions_len, contract::ContractCall};

// movi, movi, lw, movi + call (for gas)
const BASE_INSTRUCTION_COUNT: usize = 5;

#[test]
fn test_simple() {
let call = ContractCall::new_with_random_id();
let instructions_len = compute_calls_instructions_len(&[call]).unwrap();
assert_eq!(instructions_len, Instruction::SIZE * BASE_INSTRUCTION_COUNT);
}

#[test]
fn test_with_gas_offset() {
let mut call = ContractCall::new_with_random_id();
call.call_parameters = call.call_parameters.with_gas_forwarded(0);
let instructions_len = compute_calls_instructions_len(&[call]).unwrap();
assert_eq!(
instructions_len,
// 2 instructions (movi and lw) added in get_single_call_instructions when gas_offset is set
Instruction::SIZE * (BASE_INSTRUCTION_COUNT + 2)
Br1ght0ne marked this conversation as resolved.
Show resolved Hide resolved
);
}

#[test]
fn test_with_heap_type() {
let output_params = vec![
ParamType::Vector(Box::new(ParamType::U8)),
ParamType::String,
ParamType::Bytes,
];
for output_param in output_params {
let mut call = ContractCall::new_with_random_id();
call.output_param = output_param;
let instructions_len = compute_calls_instructions_len(&[call]).unwrap();
assert_eq!(
instructions_len,
// 4 instructions (lw, lw, muli, retd) added by extract_data_receipt
Instruction::SIZE * (BASE_INSTRUCTION_COUNT + 4)
Br1ght0ne marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

#[test]
fn test_with_gas_offset_and_heap_type() {
let mut call = ContractCall::new_with_random_id();
call.call_parameters = call.call_parameters.with_gas_forwarded(0);
call.output_param = ParamType::Vector(Box::new(ParamType::U8));
let instructions_len = compute_calls_instructions_len(&[call]).unwrap();
assert_eq!(
instructions_len,
// combines extra instructions from two above tests
Instruction::SIZE * (BASE_INSTRUCTION_COUNT + 2 + 4)
Br1ght0ne marked this conversation as resolved.
Show resolved Hide resolved
);
}

#[test]
fn test_with_enum_with_heap_and_non_heap_variant() {
let variant_sets = vec![
vec![ParamType::Vector(Box::new(ParamType::U8)), ParamType::U8],
vec![ParamType::String, ParamType::U8],
vec![ParamType::Bytes, ParamType::U8],
];
for variant_set in variant_sets {
let mut call = ContractCall::new_with_random_id();
call.output_param = ParamType::Enum {
variants: EnumVariants::new(variant_set).unwrap(),
generics: Vec::new(),
};
let instructions_len = compute_calls_instructions_len(&[call]).unwrap();
assert_eq!(
instructions_len,
// 4 instructions (movi, lw, jnef, retd) added by extract_heap_data
// and 4 (lw, lw, muli, retd) added by extract_data_receipt
Instruction::SIZE * (BASE_INSTRUCTION_COUNT + 8)
Br1ght0ne marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

#[test]
fn test_with_enum_with_only_non_heap_variants() {
let mut call = ContractCall::new_with_random_id();
call.output_param = ParamType::Enum {
variants: EnumVariants::new(vec![ParamType::Bool, ParamType::U8]).unwrap(),
generics: Vec::new(),
};
let instructions_len = compute_calls_instructions_len(&[call]).unwrap();
assert_eq!(
instructions_len,
// no extra instructions if there are no heap type variants
Instruction::SIZE * BASE_INSTRUCTION_COUNT
);
}
}
}
Loading