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

feat: switch blob ID calculation from data offset to configurable offset #1593

Merged
merged 33 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
18a74ae
extract configurable offset instead of data offset
Salka1988 Jan 31, 2025
d061632
ref
Salka1988 Jan 31, 2025
f04b290
fmt
Salka1988 Jan 31, 2025
f7a98ab
fmt
Salka1988 Jan 31, 2025
b44da3a
Merge branch 'refs/heads/master' into feat/support_for_verified_abi
Salka1988 Jan 31, 2025
bc52e12
Merge branch 'master' into feat/support_for_verified_abi
hal3e Feb 3, 2025
45efba2
add legacy check
Salka1988 Feb 4, 2025
9be677d
Merge branch 'feat/support_for_verified_abi' of github.com:FuelLabs/f…
Salka1988 Feb 4, 2025
14245d6
add fix
Salka1988 Feb 4, 2025
aa38185
add fix
Salka1988 Feb 4, 2025
bcda863
add fix
Salka1988 Feb 4, 2025
d4dd2d2
Merge branch 'refs/heads/master' into feat/support_for_verified_abi
Salka1988 Feb 4, 2025
393af53
add suggestions
Salka1988 Feb 4, 2025
85894d9
add fmt
Salka1988 Feb 4, 2025
2717115
rename
Salka1988 Feb 4, 2025
526ef65
add tests
Salka1988 Feb 4, 2025
64dcf81
Update packages/fuels-programs/src/assembly/script_and_predicate_load…
Salka1988 Feb 5, 2025
691cff4
Update e2e/tests/binary_format.rs
Salka1988 Feb 5, 2025
e9d80c7
Update packages/fuels-programs/src/executable.rs
Salka1988 Feb 5, 2025
663ca6c
add fix
Salka1988 Feb 5, 2025
634e5e0
add deprecated
Salka1988 Feb 5, 2025
76f6b40
update pr comments
Salka1988 Feb 7, 2025
b6258f5
update
Salka1988 Feb 7, 2025
39b2657
update
Salka1988 Feb 7, 2025
632854f
update
Salka1988 Feb 9, 2025
9365c02
add fix
Salka1988 Feb 10, 2025
6c519fd
Update e2e/tests/binary_format.rs
Salka1988 Feb 10, 2025
f838b01
Update packages/fuels-programs/src/executable.rs
Salka1988 Feb 10, 2025
40e0a09
Update packages/fuels-programs/src/debug.rs
Salka1988 Feb 10, 2025
278d3fc
reverte code
Salka1988 Feb 12, 2025
4ad8fa6
add fn
Salka1988 Feb 12, 2025
705d05d
remove handwritten script test
Salka1988 Feb 12, 2025
52e12b3
Merge branch 'refs/heads/master' into feat/support_for_verified_abi
Salka1988 Feb 12, 2025
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
Binary file not shown.
80 changes: 80 additions & 0 deletions e2e/tests/binary_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#[cfg(test)]
mod tests {
use fuels::programs::executable::{Executable, Regular};
use std::convert::TryInto;
use std::ops::Range;

const DATA_OFFSET_LOCATION: Range<usize> = 8..16;
const CONFIGURABLES_OFFSET_LOCATION: Range<usize> = 16..24;

const LEGACY_BINARY_PATH: &str =
"../e2e/assets/precompiled_sway/legacy_format_simple_contract.bin";
const NEW_BINARY_PATH: &str =
"../e2e/sway/bindings/simple_contract/out/release/simple_contract.bin";

#[test]
fn no_configurables_offset_for_old_sway_binaries() {
// given
let (_, executable) = load(LEGACY_BINARY_PATH);

// when
let configurables_offset = executable.configurables_offset_in_code().unwrap();

// then
assert_eq!(configurables_offset, None);
}

#[test]
fn correct_data_offset_for_old_sway_binaries() {
// given
let (binary, executable) = load(LEGACY_BINARY_PATH);
let expected_data_offset = read_offset(&binary, DATA_OFFSET_LOCATION);

// when
let data_offset = executable.data_offset_in_code().unwrap();

// then
assert_eq!(data_offset, expected_data_offset);
}

#[test]
fn correct_data_offset_for_new_sway_binaries() {
// given
let (binary, executable) = load(NEW_BINARY_PATH);
let expected_data_offset = read_offset(&binary, DATA_OFFSET_LOCATION);

// when
let data_offset = executable.data_offset_in_code().unwrap();

// then
assert_eq!(data_offset, expected_data_offset);
}

#[test]
fn correct_configurables_offset_for_new_sway_binaries() {
// given
let (binary, executable) = load(NEW_BINARY_PATH);
let expected_configurables_offset = read_offset(&binary, CONFIGURABLES_OFFSET_LOCATION);

// when
let configurables_offset = executable.configurables_offset_in_code();

// then
let configurables_offset = configurables_offset
.expect("to successfully detect a modern binary is used")
.expect("to extract the configurables_offset");
assert_eq!(configurables_offset, expected_configurables_offset);
}

pub fn read_offset(binary: &[u8], range: Range<usize>) -> usize {
assert_eq!(range.clone().count(), 8, "must be a range of 8 B");
let data: [u8; 8] = binary[range].try_into().unwrap();
u64::from_be_bytes(data) as usize
}

fn load(path: &str) -> (Vec<u8>, Executable<Regular>) {
let binary = std::fs::read(path).unwrap();
let executable = Executable::from_bytes(binary.clone());
(binary, executable)
}
}
28 changes: 15 additions & 13 deletions e2e/tests/debug_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,29 +386,31 @@ async fn debugs_sway_script_with_no_configurables() -> Result<()> {
.await
.unwrap();

let abi =
std::fs::read_to_string("./sway/scripts/basic_script/out/release/basic_script-abi.json")?;

let decoder = ABIFormatter::from_json_abi(abi)?;

let ScriptType::Other(desc) = ScriptType::detect(&tb.script, &tb.script_data).unwrap() else {
panic!("expected a script")
};

assert_eq!(
decoder
.decode_configurables(desc.data_section().unwrap())
.unwrap(),
vec![]
);
assert!(desc.data_section().is_none());

Ok(())
}
fn generate_modern_sway_binary(len: usize) -> Vec<u8> {
assert!(
len > 24,
"needs at least 24B to fit in the indicator_of_modern_binary, data & configurables offsets"
);

let mut custom_script = vec![0; len];
let indicator_of_modern_binary = fuel_asm::op::jmpf(0x00, 0x04);

custom_script[4..8].copy_from_slice(&indicator_of_modern_binary.to_bytes());
custom_script
}

#[tokio::test]
async fn data_section_offset_not_set_if_out_of_bounds() -> Result<()> {
let mut custom_script = vec![0; 1000];
custom_script[8..16].copy_from_slice(&u64::MAX.to_be_bytes());
let mut custom_script = generate_modern_sway_binary(100);
custom_script[16..24].copy_from_slice(&u64::MAX.to_be_bytes());

let ScriptType::Other(desc) = ScriptType::detect(&custom_script, &[]).unwrap() else {
panic!("expected a script")
Expand Down
8 changes: 6 additions & 2 deletions e2e/tests/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,14 @@ async fn loader_can_be_presented_as_a_normal_script_with_shifted_configurables()

let configurables: Configurables = configurables.into();

let offset = regular
.configurables_offset_in_code()?
.unwrap_or_else(|| regular.data_offset_in_code().unwrap());

let shifted_configurables = configurables
.with_shifted_offsets(-(regular.data_offset_in_code().unwrap() as i64))
.with_shifted_offsets(-(offset as i64))
.unwrap()
.with_shifted_offsets(loader.data_offset_in_code() as i64)
.with_shifted_offsets(loader.configurables_offset_in_code() as i64)
.unwrap();

let loader_posing_as_normal_script =
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-programs/src/assembly/contract_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use fuel_tx::{AssetId, ContractId};
use fuels_core::{constants::WORD_SIZE, error, types::errors::Result};

use super::cursor::WasmFriendlyCursor;
#[derive(Debug)]

pub struct ContractCallInstructions {
instructions: Vec<Instruction>,
gas_fwd: bool,
Expand Down
Loading
Loading