From 55e3c9109ec27d26228957472ebfe8f45b0ab262 Mon Sep 17 00:00:00 2001 From: segfault-magnet Date: Mon, 11 Nov 2024 03:09:24 +0100 Subject: [PATCH] decode fixed --- .../src/asm_instructions/contract_call.rs | 31 +++++-------------- .../src/asm_instructions/cursor.rs | 10 ++++++ packages/fuels-programs/src/debug.rs | 5 +-- wasm-tests/src/lib.rs | 1 + 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/packages/fuels-programs/src/asm_instructions/contract_call.rs b/packages/fuels-programs/src/asm_instructions/contract_call.rs index 2646b2689..361d37b9b 100644 --- a/packages/fuels-programs/src/asm_instructions/contract_call.rs +++ b/packages/fuels-programs/src/asm_instructions/contract_call.rs @@ -218,23 +218,11 @@ impl ContractCallData { pub fn decode(data: &[u8], gas_fwd: bool) -> Result { 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")?; @@ -242,8 +230,8 @@ impl ContractCallData { 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() }; @@ -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 { diff --git a/packages/fuels-programs/src/asm_instructions/cursor.rs b/packages/fuels-programs/src/asm_instructions/cursor.rs index 35005041b..a0077ebc3 100644 --- a/packages/fuels-programs/src/asm_instructions/cursor.rs +++ b/packages/fuels-programs/src/asm_instructions/cursor.rs @@ -24,6 +24,16 @@ impl<'a> WasmFriendlyCursor<'a> { } } + pub fn consume_fixed( + &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 } diff --git a/packages/fuels-programs/src/debug.rs b/packages/fuels-programs/src/debug.rs index 02d84c4b1..fca122d6f 100644 --- a/packages/fuels-programs/src/debug.rs +++ b/packages/fuels-programs/src/debug.rs @@ -170,10 +170,7 @@ fn parse_loader_script(script: &[u8], data: &[u8]) -> Result