Skip to content

Commit

Permalink
refactor: remove the Byte type from the SDK (#883)
Browse files Browse the repository at this point in the history
This PR closes #880 by removing the Byte type, whose support was dropped
in Sway.
  • Loading branch information
iqdecay committed Mar 14, 2023
1 parent 05a55a0 commit 64364d7
Show file tree
Hide file tree
Showing 12 changed files with 6 additions and 171 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 @@ -198,22 +197,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 @@ -341,11 +324,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
8 changes: 0 additions & 8 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 Down Expand Up @@ -168,13 +167,6 @@ impl ABIDecoder {
})
}

fn decode_byte(bytes: &[u8]) -> Result<DecodeResult> {
Ok(DecodeResult {
token: Token::Byte(peek_u8(bytes)?),
bytes_read: 8,
})
}

fn decode_bool(bytes: &[u8]) -> Result<DecodeResult> {
// Grab last byte of the word and compare it to 0x00
let b = peek_u8(bytes)? != 0u8;
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 @@ -158,10 +157,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 @@ -422,40 +417,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 @@ -98,7 +97,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 @@ -278,7 +278,6 @@ impl Contract {
| Token::B256(_)
| Token::Tuple(_)
| Token::Array(_)
| Token::Byte(_)
| Token::Vector(_)
)
})
Expand Down
4 changes: 1 addition & 3 deletions packages/fuels-types/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use crate::{
};

mod bits;
mod byte;
mod native;
mod raw_slice;
mod sized_ascii_string;

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

pub type ByteArray = [u8; 8];
pub type Selector = ByteArray;
Expand Down Expand Up @@ -77,7 +76,6 @@ pub enum Token {
U32(u32),
U64(u64),
Bool(bool),
Byte(u8),
B256([u8; 32]),
Array(Vec<Token>),
Vector(Vec<Token>),
Expand Down
1 change: 0 additions & 1 deletion packages/fuels-types/src/core/byte.rs

This file was deleted.

9 changes: 2 additions & 7 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 @@ -123,8 +122,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 @@ -437,7 +435,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 @@ -578,7 +575,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 @@ -1278,7 +1274,6 @@ mod tests {
assert!(!ParamType::U32.contains_nested_vectors());
assert!(!ParamType::U64.contains_nested_vectors());
assert!(!ParamType::Bool.contains_nested_vectors());
assert!(!ParamType::Byte.contains_nested_vectors());
assert!(!ParamType::B256.contains_nested_vectors());
assert!(!ParamType::String(10).contains_nested_vectors());
assert!(!ParamType::RawSlice.contains_nested_vectors());
Expand All @@ -1298,7 +1293,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_vectors());
assert!(ParamType::Vector(Box::from(base_vector.clone())).contains_nested_vectors());
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,
};
Expand All @@ -26,12 +26,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 @@ -44,25 +44,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 64364d7

Please sign in to comment.