Skip to content

Commit

Permalink
refactor: implement code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
iqdecay committed Mar 13, 2023
1 parent f19cefb commit 11a2430
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 deletions.
19 changes: 3 additions & 16 deletions packages/fuels-core/src/abi_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fuels_types::{
enum_variants::EnumVariants,
errors::{error, Error, Result},
param_types::ParamType,
unzip_param_types, Byte, StringToken, Token,
unzip_param_types, StringToken, Token,
};

use crate::Tokenizable;
Expand Down Expand Up @@ -78,22 +78,9 @@ impl ABIDecoder {
}

fn decode_bytes(bytes: &[u8]) -> Result<DecodeResult> {
let num_of_elements = ParamType::Bytes.calculate_num_of_elements(bytes)?;
let (tokens, bytes_read) =
Self::decode_multiple(&vec![ParamType::Byte; num_of_elements], bytes)?;

let u8_vec = tokens
.into_iter()
.map(Byte::from_token)
.collect::<Result<Vec<Byte>>>()
.map_err(|e| error!(InvalidData, "{e}"))?
.into_iter()
.map(u8::from)
.collect();

Ok(DecodeResult {
token: Token::Bytes(u8_vec),
bytes_read,
token: Token::Bytes(bytes.to_vec()),
bytes_read: bytes.len(),
})
}
fn decode_vector(param_type: &ParamType, bytes: &[u8]) -> Result<DecodeResult> {
Expand Down
15 changes: 1 addition & 14 deletions packages/fuels-programs/src/call_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use fuels_signers::{provider::Provider, Signer, WalletUnlocked};
use fuels_types::{
bech32::Bech32Address,
constants::{BASE_ASSET_ID, WORD_SIZE},
error,
errors::{Error, Result},
param_types::ParamType,
parameters::TxParameters,
Expand Down Expand Up @@ -264,19 +263,7 @@ pub(crate) fn get_single_call_instructions(
]
.to_vec();
// The instructions are different if you want to return data that was on the heap
if output_param_type.is_vm_heap_type() {
let inner_type_byte_size = match output_param_type {
ParamType::Vector(inner_param_type) => {
Ok((inner_param_type.compute_encoding_width() * WORD_SIZE) as u16)
}
// `Bytes` type is byte-packed in the VM, so don't multiply by WORD_SIZE
ParamType::Bytes => Ok(ParamType::Byte.compute_encoding_width() as u16),
_ => Err(error!(
InvalidData,
"If `.uses_heap_data` is true then the type is either `Bytes` or `Vector`"
)),
}
.unwrap();
if let Some(inner_type_byte_size) = output_param_type.heap_inner_element_size() {
instructions.extend([
// The RET register contains the pointer address of the `CALL` return (a stack
// address).
Expand Down
13 changes: 11 additions & 2 deletions packages/fuels-types/src/param_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ impl ParamType {

pub fn calculate_num_of_elements(&self, bytes: &[u8]) -> Result<usize> {
let memory_size = match self {
// The `Bytes` type in the VM uses byte-packing, don't multiply by WORD_SIZE
ParamType::Bytes => Ok(ParamType::U8.compute_encoding_width()),
ParamType::Bytes => Ok(std::mem::size_of::<u8>()),
ParamType::Vector(param_type) => Ok(param_type.compute_encoding_width() * WORD_SIZE),
ParamType::RawSlice => Ok(ParamType::U64.compute_encoding_width() * WORD_SIZE),
_ => Err(error!(
Expand Down Expand Up @@ -129,6 +128,16 @@ impl ParamType {
pub fn is_vm_heap_type(&self) -> bool {
matches!(self, ParamType::Vector(..)) || matches!(self, ParamType::Bytes)
}
pub fn heap_inner_element_size(&self) -> Option<u16> {
match &self {
ParamType::Vector(inner_param_type) => {
Some((inner_param_type.compute_encoding_width() * WORD_SIZE) as u16)
}
// `Bytes` type is byte-packed in the VM, so it's the size of an u8
ParamType::Bytes => Some(std::mem::size_of::<u8>() as u16),
_ => None,
}
}

/// Calculates the number of `WORD`s the VM expects this parameter to be encoded in.
pub fn compute_encoding_width(&self) -> usize {
Expand Down

0 comments on commit 11a2430

Please sign in to comment.