Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Nov 3, 2024
1 parent d2ce3b1 commit 87257e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions forc-util/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ use std::fs::{self, File};
use std::io::Read;
use std::path::Path;

// The index of the beginning of the half-word (4 bytes) that contains the configurables section offset.
const CONFIGURABLES_OFFSET_INSTR_LO: usize = 4;
// The index of the end of the half-word (4 bytes) that contains the configurables section offset.
const CONFIGURABLES_OFFSET_INSTR_HI: usize = 5;

/// Parses a bytecode file into an iterator of instructions and their corresponding bytes.
pub fn parse_bytecode_to_instructions<P>(
path: P,
) -> anyhow::Result<
impl Iterator<
Item = (
Result<fuel_asm::Instruction, fuel_asm::InvalidOpcode>,
&'static [u8],
),
>,
Vec<(
Result<fuel_asm::Instruction, fuel_asm::InvalidOpcode>,
Vec<u8>,
)>,
>
where
P: AsRef<Path> + Clone,
Expand All @@ -25,12 +28,10 @@ where
let mut buffer = vec![0; metadata.len() as usize];
f.read_exact(&mut buffer).expect("buffer overflow");

let instructions = {
let buffer = Box::leak(buffer.into_boxed_slice()); // Leak the buffer to extend its lifetime
fuel_asm::from_bytes(buffer.iter().cloned()).zip(buffer.chunks(fuel_asm::Instruction::SIZE))
};
let instructions = fuel_asm::from_bytes(buffer.clone())
.zip(buffer.chunks(fuel_asm::Instruction::SIZE).into_iter().map(|chunk: &[u8]| chunk.to_vec()));

Ok(instructions)
Ok(instructions.collect())
}

/// Gets the bytecode ID from a bytecode file. The bytecode ID is the hash of the bytecode after removing the
Expand All @@ -39,20 +40,20 @@ pub fn get_bytecode_id<P>(path: P) -> anyhow::Result<String>
where
P: AsRef<Path> + Clone,
{
let mut instructions = parse_bytecode_to_instructions(path.clone())?;
let mut instructions = parse_bytecode_to_instructions(path.clone())?.into_iter();

// Collect the first six instructions into a temporary vector
let mut first_six_instructions = Vec::with_capacity(6);
for _ in 0..6 {
for _ in 0..CONFIGURABLES_OFFSET_INSTR_HI + 1 {
if let Some(instruction) = instructions.next() {
first_six_instructions.push(instruction);
} else {
return Err(anyhow!("Incomplete bytecode"));
}
}

let (lo_instr, low_raw) = &first_six_instructions[4];
let (hi_instr, hi_raw) = &first_six_instructions[5];
let (lo_instr, low_raw) = &first_six_instructions[CONFIGURABLES_OFFSET_INSTR_LO];
let (hi_instr, hi_raw) = &first_six_instructions[CONFIGURABLES_OFFSET_INSTR_HI];

if let Err(fuel_asm::InvalidOpcode) = lo_instr {
if let Err(fuel_asm::InvalidOpcode) = hi_instr {
Expand Down
2 changes: 1 addition & 1 deletion forc/src/cli/commands/parse_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) struct Command {
}

pub(crate) fn exec(command: Command) -> ForcResult<()> {
let instructions = parse_bytecode_to_instructions(&command.file_path)?;
let instructions = parse_bytecode_to_instructions(&command.file_path)?.into_iter();

let mut table = term_table::Table::new();
table.separate_rows = false;
Expand Down

0 comments on commit 87257e9

Please sign in to comment.