Skip to content

Commit

Permalink
feat!: remove legacy encoding (#1338)
Browse files Browse the repository at this point in the history
closes: #1318

This PR removes all code related to the legacy encoding.
  • Loading branch information
hal3e committed May 7, 2024
1 parent fef8e2e commit 8df71c3
Show file tree
Hide file tree
Showing 46 changed files with 318 additions and 1,510 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:
FUEL_CORE_PATCH_BRANCH:
RUST_VERSION: 1.76.0
FORC_VERSION: 0.56.0
FORC_PATCH_BRANCH: ""
FORC_PATCH_BRANCH: "xunilrj/new-encoding-configurables"
FORC_PATCH_REVISION: ""
NEXTEST_HIDE_PROGRESS_BAR: "true"
NEXTEST_STATUS_LEVEL: "fail"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/calling-contracts/low-level-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Your caller contract should call `std::low_level_call::call_with_function_select
{{#include ../../../packages/fuels/tests/contracts/low_level_caller/src/main.sw:low_level_call_contract}}
```

On the SDK side, you can construct an encoded function selector using the `fuels::core::fn_selector!` macro, and encoded calldata using the `fuels::core::calldata!` macro.
On the SDK side, you can construct an encoded function selector using `fuels::core::encode_fn_selector`, and encoded calldata using the `fuels::core::calldata!` macro.

E.g. to call the following function on the target contract:

Expand Down
5 changes: 0 additions & 5 deletions docs/src/codec/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ Encoding is done via the [`ABIEncoder`](https://docs.rs/fuels/latest/fuels/core/
{{#include ../../../examples/codec/src/lib.rs:encoding_example}}
```

Note that the return type of `encode` is `UnresolvedBytes`. The encoding cannot be finished until we know at which memory address this data is to be loaded. If you don't use heap types (`::std::vec::Vec`, `::fuels::types::Bytes`, `::std::string::String`), then you can safely `.resolve(0)` to get the encoded bytes.

There is also a shortcut-macro that can encode multiple types which implement [`Tokenizable`](https://docs.rs/fuels/latest/fuels/core/traits/trait.Tokenizable.html):

```rust,ignore
{{#include ../../../examples/codec/src/lib.rs:encoding_example_w_macro}}
```

> Note:
> The above example will call `.resolve(0)`. Don't use it if you're encoding heap types.
## Configuring the encoder

The encoder can be configured to limit its resource expenditure:
Expand Down
6 changes: 1 addition & 5 deletions examples/codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod tests {
use fuels::{
core::{codec::ABIEncoder, traits::Tokenizable},
macros::Tokenizable,
types::unresolved_bytes::UnresolvedBytes,
};

#[derive(Tokenizable)]
Expand All @@ -20,9 +19,7 @@ mod tests {
}

let instance = MyStruct { field: 101 };
let encoded: UnresolvedBytes = ABIEncoder::default().encode(&[instance.into_token()])?;
let load_memory_address: u64 = 0x100;
let _: Vec<u8> = encoded.resolve(load_memory_address);
let _encoded: Vec<u8> = ABIEncoder::default().encode(&[instance.into_token()])?;
//ANCHOR_END: encoding_example

Ok(())
Expand Down Expand Up @@ -110,7 +107,6 @@ mod tests {
ABIEncoder::new(EncoderConfig {
max_depth: 5,
max_tokens: 100,
max_total_enum_width: 10_000,
});
// ANCHOR_END: configuring_the_encoder

Expand Down
5 changes: 2 additions & 3 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ mod tests {
.await?;
// ANCHOR_END: contract_call_cost_estimation

let expected_gas = 2206;
let expected_gas = 2229;

assert_eq!(transaction_cost.gas_used, expected_gas);

Expand Down Expand Up @@ -610,7 +610,7 @@ mod tests {
.await?;
// ANCHOR_END: multi_call_cost_estimation

let expected_gas = 3378;
let expected_gas = 3413;

assert_eq!(transaction_cost.gas_used, expected_gas);

Expand Down Expand Up @@ -898,7 +898,6 @@ mod tests {
.with_encoder_config(EncoderConfig {
max_depth: 10,
max_tokens: 2_000,
max_total_enum_width: 10_000,
})
.methods()
.initialize_counter(42)
Expand Down
10 changes: 5 additions & 5 deletions packages/fuels-accounts/src/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt::Debug, fs};
#[cfg(feature = "std")]
use fuels_core::types::{input::Input, AssetId};
use fuels_core::{
types::{bech32::Bech32Address, errors::Result, unresolved_bytes::UnresolvedBytes},
types::{bech32::Bech32Address, errors::Result},
Configurables,
};

Expand All @@ -16,7 +16,7 @@ use crate::{provider::Provider, Account, ViewOnlyAccount};
pub struct Predicate {
address: Bech32Address,
code: Vec<u8>,
data: UnresolvedBytes,
data: Vec<u8>,
#[cfg(feature = "std")]
provider: Option<Provider>,
}
Expand All @@ -26,11 +26,11 @@ impl Predicate {
&self.address
}

pub fn code(&self) -> &Vec<u8> {
pub fn code(&self) -> &[u8] {
&self.code
}

pub fn data(&self) -> &UnresolvedBytes {
pub fn data(&self) -> &[u8] {
&self.data
}

Expand All @@ -53,7 +53,7 @@ impl Predicate {
}
}

pub fn with_data(mut self, data: UnresolvedBytes) -> Self {
pub fn with_data(mut self, data: Vec<u8>) -> Self {
self.data = data;
self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn expand_fn(abi: &FullProgramABI) -> Result<TokenStream> {
self.encoder.encode(&#arg_tokens)
};
let output_type = quote! {
::fuels::types::errors::Result<::fuels::types::unresolved_bytes::UnresolvedBytes>
::fuels::types::errors::Result<::std::vec::Vec<u8>>
};

generator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn generate_struct_decl(configurable_struct_name: &Ident) -> TokenStream {
#[derive(Clone, Debug, Default)]
pub struct #configurable_struct_name {
offsets_with_data: ::std::vec::Vec<(u64, ::std::vec::Vec<u8>)>,
encoder: ::fuels::core::codec::ConfigurablesEncoder,
encoder: ::fuels::core::codec::ABIEncoder,
}
}
}
Expand All @@ -66,7 +66,7 @@ fn generate_struct_impl(
impl #configurable_struct_name {
pub fn new(encoder_config: ::fuels::core::codec::EncoderConfig) -> Self {
Self {
encoder: ::fuels::core::codec::ConfigurablesEncoder::new(encoder_config),
encoder: ::fuels::core::codec::ABIEncoder::new(encoder_config),
..::std::default::Default::default()
}
}
Expand All @@ -88,7 +88,7 @@ fn generate_builder_methods(resolved_configurables: &[ResolvedConfigurable]) ->
#[allow(non_snake_case)]
// Generate the `with_XXX` methods for setting the configurables
pub fn #name(mut self, value: #ttype) -> ::fuels::prelude::Result<Self> {
let encoded = #encoder_code?.resolve(0);
let encoded = #encoder_code?;
self.offsets_with_data.push((#offset, encoded));
::fuels::prelude::Result::Ok(self)
}
Expand Down
1 change: 0 additions & 1 deletion packages/fuels-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ thiserror = { workspace = true, default-features = false }
uint = { workspace = true, default-features = false }

[dev-dependencies]
fuels-macros = { workspace = true }
tokio = { workspace = true, features = ["test-util", "macros"] }

[features]
Expand Down
31 changes: 16 additions & 15 deletions packages/fuels-core/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,8 @@ mod tests {
};

#[test]
fn can_convert_bytes_into_tuple() -> Result<()> {
let tuple_in_bytes: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2];

let the_tuple: (u64, u32) = try_from_bytes(&tuple_in_bytes, DecoderConfig::default())?;

assert_eq!(the_tuple, (1, 2));

Ok(())
}

#[test]
fn can_convert_all_from_bool_to_u64() -> Result<()> {
let bytes: Vec<u8> = vec![0xFF; WORD_SIZE];
fn convert_all_from_bool_to_u64() -> Result<()> {
let bytes = [255; WORD_SIZE];

macro_rules! test_decode {
($($for_type: ident),*) => {
Expand All @@ -57,14 +46,26 @@ mod tests {
}

assert!(try_from_bytes::<bool>(&bytes, DecoderConfig::default())?);

test_decode!(u8, u16, u32, u64);

Ok(())
}

#[test]
fn can_convert_native_types() -> Result<()> {
let bytes = [0xFF; 32];
fn convert_bytes_into_tuple() -> Result<()> {
let tuple_in_bytes = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2];

let the_tuple: (u64, u32) = try_from_bytes(&tuple_in_bytes, DecoderConfig::default())?;

assert_eq!(the_tuple, (1, 2));

Ok(())
}

#[test]
fn convert_native_types() -> Result<()> {
let bytes = [255; 32];

macro_rules! test_decode {
($($for_type: ident),*) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/fuels-core/src/codec/abi_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,10 @@ mod tests {
generics: vec![],
}];

// "0" discriminant and 42 enum value
let data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42];
let data = [
0, 0, 0, 0, 0, 0, 0, 0, // discriminant
0, 0, 0, 42, // u32
];

let decoded = ABIDecoder::default().decode_multiple(&types, &data)?;

Expand Down

0 comments on commit 8df71c3

Please sign in to comment.