Skip to content

Commit

Permalink
refactor: remove the Byte type
Browse files Browse the repository at this point in the history
  • Loading branch information
iqdecay committed Mar 13, 2023
1 parent 11a2430 commit 3450199
Show file tree
Hide file tree
Showing 13 changed files with 8 additions and 184 deletions.
22 changes: 0 additions & 22 deletions packages/fuels-code-gen/src/program_bindings/resolved_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ pub(crate) fn resolve_type(

[
to_simple_type,
to_byte,
to_bits256,
to_generic,
to_array,
Expand Down Expand Up @@ -216,22 +215,6 @@ fn to_simple_type(
}
}

fn to_byte(
type_field: &str,
_: impl Fn() -> Vec<ResolvedType>,
_: impl Fn() -> Vec<ResolvedType>,
_: bool,
) -> Option<ResolvedType> {
if type_field == "byte" {
Some(ResolvedType {
type_name: quote! {::fuels::types::Byte},
generic_params: vec![],
})
} else {
None
}
}

fn to_bits256(
type_field: &str,
_: impl Fn() -> Vec<ResolvedType>,
Expand Down Expand Up @@ -359,11 +342,6 @@ mod tests {
test_resolve_primitive_type("bool", "bool")
}

#[test]
fn test_resolve_byte() -> Result<()> {
test_resolve_primitive_type("byte", ":: fuels :: types :: Byte")
}

#[test]
fn test_resolve_b256() -> Result<()> {
test_resolve_primitive_type("b256", ":: fuels :: types :: Bits256")
Expand Down
1 change: 0 additions & 1 deletion packages/fuels-code-gen/src/program_bindings/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ pub(crate) fn get_sdk_provided_types() -> Vec<TypePath> {
"::fuels::types::B512",
"::fuels::types::RawSlice",
"::fuels::types::Bytes",
"::fuels::types::Byte",
"::std::vec::Vec",
"::core::result::Result",
"::core::option::Option",
Expand Down
10 changes: 1 addition & 9 deletions packages/fuels-core/src/abi_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ impl ABIDecoder {
ParamType::U32 => Self::decode_u32(bytes),
ParamType::U64 => Self::decode_u64(bytes),
ParamType::Bool => Self::decode_bool(bytes),
ParamType::Byte => Self::decode_byte(bytes),
ParamType::B256 => Self::decode_b256(bytes),
ParamType::RawSlice => Self::decode_raw_slice(bytes),
ParamType::String(length) => Self::decode_string(bytes, *length),
Expand All @@ -83,6 +82,7 @@ impl ABIDecoder {
bytes_read: bytes.len(),
})
}

fn decode_vector(param_type: &ParamType, bytes: &[u8]) -> Result<DecodeResult> {
let num_of_elements =
ParamType::Vector(Box::from(param_type.clone())).calculate_num_of_elements(bytes)?;
Expand Down Expand Up @@ -209,14 +209,6 @@ impl ABIDecoder {
})
}

fn decode_byte(bytes: &[u8]) -> Result<DecodeResult> {
let bytes_array: [u8; 1] = <[u8; 1]>::try_from(peek(bytes, 1)?)?;
Ok(DecodeResult {
token: Token::Byte(u8::from_be_bytes(bytes_array)),
bytes_read: 1,
})
}

fn decode_u8(bytes: &[u8]) -> Result<DecodeResult> {
Ok(DecodeResult {
token: Token::U8(peek_u8(bytes)?),
Expand Down
39 changes: 0 additions & 39 deletions packages/fuels-core/src/abi_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ impl ABIEncoder {
Token::U16(arg_u16) => vec![Self::encode_u16(*arg_u16)],
Token::U32(arg_u32) => vec![Self::encode_u32(*arg_u32)],
Token::U64(arg_u64) => vec![Self::encode_u64(*arg_u64)],
Token::Byte(arg_byte) => vec![Self::encode_byte(*arg_byte)],
Token::Bool(arg_bool) => vec![Self::encode_bool(*arg_bool)],
Token::B256(arg_bits256) => vec![Self::encode_b256(arg_bits256)],
Token::Array(arg_array) => Self::encode_array(arg_array)?,
Expand Down Expand Up @@ -165,10 +164,6 @@ impl ABIEncoder {
Data::Inline(pad_u8(u8::from(arg_bool)).to_vec())
}

fn encode_byte(arg_byte: u8) -> Data {
Data::Inline(pad_u8(arg_byte).to_vec())
}

fn encode_u64(arg_u64: u64) -> Data {
Data::Inline(arg_u64.to_be_bytes().to_vec())
}
Expand Down Expand Up @@ -429,40 +424,6 @@ mod tests {
Ok(())
}

#[test]
fn encode_function_with_byte_type() -> Result<()> {
// let json_abi =
// r#"
// [
// {
// "type":"function",
// "inputs": [{"name":"arg","type":"byte"}],
// "name":"takes_one_byte",
// "outputs": []
// }
// ]
// "#;

let fn_signature = "takes_one_byte(byte)";
let arg = Token::Byte(u8::MAX);

let args: Vec<Token> = vec![arg];

let expected_encoded_abi = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff];

let expected_function_selector = [0x0, 0x0, 0x0, 0x0, 0x2e, 0xe3, 0xce, 0x1f];

let encoded_function_selector = first_four_bytes_of_sha256_hash(fn_signature);

let encoded = ABIEncoder::encode(&args)?.resolve(0);

println!("Encoded ABI for ({fn_signature}): {encoded:#0x?}");

assert_eq!(hex::encode(expected_encoded_abi), hex::encode(encoded));
assert_eq!(encoded_function_selector, expected_function_selector);
Ok(())
}

#[test]
fn encode_function_with_bits256_type() -> Result<()> {
// let json_abi =
Expand Down
2 changes: 0 additions & 2 deletions packages/fuels-core/src/function_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ fn resolve_arg(arg: &ParamType) -> String {
ParamType::U32 => "u32".to_owned(),
ParamType::U64 => "u64".to_owned(),
ParamType::Bool => "bool".to_owned(),
ParamType::Byte => "byte".to_owned(),
ParamType::B256 => "b256".to_owned(),
ParamType::Unit => "()".to_owned(),
ParamType::String(len) => {
Expand Down Expand Up @@ -99,7 +98,6 @@ mod tests {
(ParamType::U32, "u32"),
(ParamType::U64, "u64"),
(ParamType::Bool, "bool"),
(ParamType::Byte, "byte"),
(ParamType::B256, "b256"),
(ParamType::Unit, "()"),
(ParamType::String(15), "str[15]"),
Expand Down
1 change: 0 additions & 1 deletion packages/fuels-core/src/traits/decodable_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ fn paramtype_decode_log(param_type: &ParamType, token: &Token) -> Result<String>
(ParamType::U32, Token::U32(val)) => val.to_string(),
(ParamType::U64, Token::U64(val)) => val.to_string(),
(ParamType::Bool, Token::Bool(val)) => val.to_string(),
(ParamType::Byte, Token::Byte(val)) => val.to_string(),
(ParamType::B256, Token::B256(val)) => {
format!("Bits256({val:?})")
}
Expand Down
1 change: 0 additions & 1 deletion packages/fuels-programs/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ impl Contract {
| Token::B256(_)
| Token::Tuple(_)
| Token::Array(_)
| Token::Byte(_)
| Token::Vector(_)
)
})
Expand Down
7 changes: 1 addition & 6 deletions packages/fuels-types/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ mod raw_slice;
mod sized_ascii_string;

pub use crate::core::{
bits::*,
byte::{Byte, Bytes},
native::*,
raw_slice::RawSlice,
sized_ascii_string::*,
bits::*, byte::Bytes, native::*, raw_slice::RawSlice, sized_ascii_string::*,
};

pub type ByteArray = [u8; 8];
Expand Down Expand Up @@ -83,7 +79,6 @@ pub enum Token {
U32(u32),
U64(u64),
Bool(bool),
Byte(u8),
B256([u8; 32]),
Array(Vec<Token>),
Vector(Vec<Token>),
Expand Down
8 changes: 0 additions & 8 deletions packages/fuels-types/src/core/byte.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
#[derive(Debug, PartialEq, Clone, Eq)]
pub struct Byte(pub u8);
impl From<Byte> for u8 {
fn from(byte: Byte) -> u8 {
byte.0
}
}

#[derive(Debug, PartialEq, Clone, Eq)]
pub struct Bytes(pub Vec<u8>);

Expand Down
11 changes: 3 additions & 8 deletions packages/fuels-types/src/param_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub enum ParamType {
U32,
U64,
Bool,
Byte,
B256,
// The Unit paramtype is used for unit variants in Enums. The corresponding type field is `()`,
// similar to Rust.
Expand Down Expand Up @@ -156,8 +155,7 @@ impl ParamType {
| ParamType::U16
| ParamType::U32
| ParamType::U64
| ParamType::Bool
| ParamType::Byte => 1,
| ParamType::Bool => 1,
ParamType::Vector(_) => 3,
ParamType::B256 => 4,
ParamType::Array(param, count) => param.compute_encoding_width() * count,
Expand Down Expand Up @@ -473,7 +471,6 @@ fn try_array(the_type: &Type) -> Result<Option<ParamType>> {

fn try_primitive(the_type: &Type) -> Result<Option<ParamType>> {
let result = match the_type.type_field.as_str() {
"byte" => Some(ParamType::Byte),
"bool" => Some(ParamType::Bool),
"u8" => Some(ParamType::U8),
"u16" => Some(ParamType::U16),
Expand Down Expand Up @@ -614,7 +611,6 @@ mod tests {
assert_eq!(parse_param_type("u32")?, ParamType::U32);
assert_eq!(parse_param_type("u64")?, ParamType::U64);
assert_eq!(parse_param_type("bool")?, ParamType::Bool);
assert_eq!(parse_param_type("byte")?, ParamType::Byte);
assert_eq!(parse_param_type("b256")?, ParamType::B256);
assert_eq!(parse_param_type("()")?, ParamType::Unit);
assert_eq!(parse_param_type("str[21]")?, ParamType::String(21));
Expand Down Expand Up @@ -1314,7 +1310,6 @@ mod tests {
assert!(!ParamType::U32.contains_nested_heap_types());
assert!(!ParamType::U64.contains_nested_heap_types());
assert!(!ParamType::Bool.contains_nested_heap_types());
assert!(!ParamType::Byte.contains_nested_heap_types());
assert!(!ParamType::B256.contains_nested_heap_types());
assert!(!ParamType::String(10).contains_nested_heap_types());
assert!(!ParamType::RawSlice.contains_nested_heap_types());
Expand All @@ -1335,7 +1330,7 @@ mod tests {
("Boum".to_string(), base_vector.clone()),
];
let param_types_no_nested_vec = vec![ParamType::U64, ParamType::U32];
let param_types_nested_vec = vec![ParamType::Unit, ParamType::Byte, base_vector.clone()];
let param_types_nested_vec = vec![ParamType::Unit, ParamType::Bool, base_vector.clone()];

assert!(!base_vector.contains_nested_heap_types());
assert!(ParamType::Vector(Box::from(base_vector.clone())).contains_nested_heap_types());
Expand Down Expand Up @@ -1399,7 +1394,7 @@ mod tests {
("Boum".to_string(), base_bytes.clone()),
];
let param_types_no_nested_bytes = vec![ParamType::U64, ParamType::U32];
let param_types_nested_bytes = vec![ParamType::Unit, ParamType::Byte, base_bytes.clone()];
let param_types_nested_bytes = vec![ParamType::Unit, ParamType::Bool, base_bytes.clone()];

assert!(!base_bytes.contains_nested_heap_types());
assert!(ParamType::Vector(Box::from(base_bytes.clone())).contains_nested_heap_types());
Expand Down
8 changes: 1 addition & 7 deletions packages/fuels-types/src/traits/parameterize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::iter::zip;
use fuel_types::{Address, AssetId, ContractId};

use crate::{
core::{Bits256, Byte, RawSlice, SizedAsciiString},
core::{Bits256, RawSlice, SizedAsciiString},
enum_variants::EnumVariants,
param_types::ParamType,
Bytes,
Expand All @@ -27,12 +27,6 @@ impl Parameterize for RawSlice {
}
}

impl Parameterize for Byte {
fn param_type() -> ParamType {
ParamType::Byte
}
}

impl<const SIZE: usize, T: Parameterize> Parameterize for [T; SIZE] {
fn param_type() -> ParamType {
ParamType::Array(Box::new(T::param_type()), SIZE)
Expand Down
21 changes: 1 addition & 20 deletions packages/fuels-types/src/traits/tokenizable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fuel_types::{Address, AssetId, ContractId};

use crate::{
core::{Bits256, Byte, RawSlice, SizedAsciiString, StringToken, Token},
core::{Bits256, RawSlice, SizedAsciiString, StringToken, Token},
errors::{error, Error, Result},
param_types::ParamType,
traits::Parameterize,
Expand Down Expand Up @@ -45,25 +45,6 @@ impl Tokenizable for Bits256 {
}
}

impl Tokenizable for Byte {
fn from_token(token: Token) -> Result<Self>
where
Self: Sized,
{
match token {
Token::Byte(value) => Ok(Byte(value)),
_ => Err(error!(
InvalidData,
"Byte::from_token failed! Can only handle Token::Byte, got {token:?}"
)),
}
}

fn into_token(self) -> Token {
Token::Byte(self.0)
}
}

impl<T: Tokenizable> Tokenizable for Vec<T> {
fn from_token(token: Token) -> Result<Self>
where
Expand Down
61 changes: 1 addition & 60 deletions packages/fuels/tests/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{slice, str::FromStr};
use fuels::{
core::abi_encoder::ABIEncoder,
prelude::*,
types::{traits::Tokenizable, Bits256, Byte, EvmAddress},
types::{traits::Tokenizable, Bits256, EvmAddress},
};
use sha2::{Digest, Sha256};

Expand Down Expand Up @@ -254,65 +254,6 @@ async fn compile_bindings_bool_array_input() {
);
}

#[tokio::test]
async fn compile_bindings_byte_input() {
// Generates the bindings from the an ABI definition inline.
// The generated bindings can be accessed through `SimpleContract`.
abigen!(Contract(
name = "SimpleContract",
abi = r#"
{
"types": [
{
"typeId": 0,
"type": "()",
"components": [],
"typeParameters": null
},
{
"typeId": 1,
"type": "byte",
"components": null,
"typeParameters": null
}
],
"functions": [
{
"inputs": [
{
"name": "arg",
"type": 1,
"typeArguments": null
}
],
"name": "takes_byte",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
]
}
"#,
));

let wallet = launch_provider_and_get_wallet().await;

let contract_instance = SimpleContract::new(null_contract_id(), wallet);

let call_handler = contract_instance.methods().takes_byte(Byte(10u8));

let encoded_args = call_handler.contract_call.encoded_args.resolve(0);
let encoded = format!(
"{}{}",
hex::encode(call_handler.contract_call.encoded_selector),
hex::encode(encoded_args)
);

assert_eq!("00000000a4bd3861000000000000000a", encoded);
}

#[tokio::test]
async fn compile_bindings_string_input() {
// Generates the bindings from the an ABI definition inline.
Expand Down

0 comments on commit 3450199

Please sign in to comment.