Skip to content

Commit

Permalink
decode fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet committed Nov 11, 2024
1 parent 6a3e950 commit 55e3c91
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
31 changes: 8 additions & 23 deletions packages/fuels-programs/src/asm_instructions/contract_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,32 +218,20 @@ impl ContractCallData {
pub fn decode(data: &[u8], gas_fwd: bool) -> Result<Self> {
let mut data = WasmFriendlyCursor::new(data);

let amount = u64::from_be_bytes(
data.consume(8, "amount")?
.try_into()
.expect("will have exactly 8 bytes"),
);

let asset_id = AssetId::new(
data.consume(32, "asset id")?
.try_into()
.expect("will have exactly 32 bytes"),
);

let contract_id = ContractId::new(
data.consume(32, "contract id")?
.try_into()
.expect("will have exactly 32 bytes"),
);
let amount = u64::from_be_bytes(data.consume_fixed("amount")?);

let asset_id = AssetId::new(data.consume_fixed("asset id")?);

let contract_id = ContractId::new(data.consume_fixed("contract id")?);

let _ = data.consume(8, "function selector offset")?;

let _ = data.consume(8, "encoded args offset")?;

let fn_selector = {
let fn_selector_len = {
let bytes = data.consume(8, "function selector lenght")?;
u64::from_be_bytes(bytes.try_into().expect("will have exactly 8 bytes")) as usize
let bytes = data.consume_fixed("function selector lenght")?;
u64::from_be_bytes(bytes) as usize
};
data.consume(fn_selector_len, "function selector")?.to_vec()
};
Expand All @@ -253,10 +241,7 @@ impl ContractCallData {
.consume(data.unconsumed().saturating_sub(WORD_SIZE), "encoded_args")?
.to_vec();

let gas_fwd = {
let gas_fwd_bytes = data.consume(WORD_SIZE, "forwarded gas")?;
u64::from_be_bytes(gas_fwd_bytes.try_into().expect("exactly 8 bytes"))
};
let gas_fwd = { u64::from_be_bytes(data.consume_fixed("forwarded gas")?) };

(encoded_args, Some(gas_fwd))
} else {
Expand Down
10 changes: 10 additions & 0 deletions packages/fuels-programs/src/asm_instructions/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ impl<'a> WasmFriendlyCursor<'a> {
}
}

pub fn consume_fixed<const AMOUNT: usize>(
&mut self,
ctx: &'static str,
) -> Result<[u8; AMOUNT]> {
Ok(self
.consume(AMOUNT, ctx)?
.try_into()
.expect("should have failed if not enough data"))
}

pub fn consume_all(&self) -> &'a [u8] {
self.data
}
Expand Down
5 changes: 1 addition & 4 deletions packages/fuels-programs/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ fn parse_loader_script(script: &[u8], data: &[u8]) -> Result<Option<(ScriptCallD
return Ok(None);
}

let blob_id = script_cursor
.consume(32, "blob id")?
.try_into()
.expect("will have exactly 32 bytes");
let blob_id = script_cursor.consume_fixed("blob id")?;

let _data_section_len = script_cursor.consume(WORD_SIZE, "data section len")?;

Expand Down
1 change: 1 addition & 0 deletions wasm-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate alloc;

// #[cfg(all(test, target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use std::{default::Default, str::FromStr};

Expand Down

0 comments on commit 55e3c91

Please sign in to comment.