Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experimental encoding for contracts, scripts and predicates #1303

Merged
merged 38 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
0f08f80
feat!: new encoding
hal3e Feb 19, 2024
b04d73d
Work in progress
hal3e Feb 26, 2024
9d0e71a
Merge branch 'master' into hal3e/exp-encoding
hal3e Feb 26, 2024
1a40dff
Merge branch 'master' into hal3e/exp-encoding
hal3e Mar 7, 2024
bc3f94e
update sway
hal3e Mar 7, 2024
0ef792d
encoder - scripts
hal3e Mar 9, 2024
b01d622
move token to its module
hal3e Mar 11, 2024
991f641
add correct u32 encoding
hal3e Mar 11, 2024
012a42f
Work in progress
hal3e Mar 11, 2024
ba21451
fix contract call
hal3e Mar 12, 2024
21f37b4
wip
hal3e Mar 12, 2024
f88f1b2
wip
hal3e Mar 12, 2024
2a58270
merge master
hal3e Mar 14, 2024
c3d362e
fix or exclude tests
hal3e Mar 14, 2024
db35990
merge master
hal3e Mar 18, 2024
3f23133
fix tests
hal3e Mar 19, 2024
558ee65
add script heap_types test
hal3e Mar 21, 2024
c0f401b
add contract heap_types test
hal3e Mar 21, 2024
92ac876
refactor
hal3e Mar 21, 2024
ea58803
refactor
hal3e Mar 21, 2024
4a4e2d3
forc-fmt
hal3e Mar 21, 2024
42390be
invert experimental flag
hal3e Mar 21, 2024
50eaad6
Merge branch 'master' into hal3e/exp-encoding
hal3e Mar 21, 2024
214a60e
update ci
hal3e Mar 21, 2024
504cfed
Merge branch 'master' into hal3e/exp-encoding
hal3e Mar 21, 2024
c03e803
temporarily disable the `generics` forc project
hal3e Mar 22, 2024
3cdc204
temporarily disable the `generics` forc project
hal3e Mar 22, 2024
7e0c130
fix warning
hal3e Mar 22, 2024
520d917
Merge branch 'master' into hal3e/exp-encoding
MujkicA Mar 26, 2024
db7de20
pr comments
hal3e Mar 27, 2024
65135c6
use feature flag
hal3e Mar 27, 2024
0fd5c78
add wasm experimental test
hal3e Mar 27, 2024
b1fa647
update CI
hal3e Mar 27, 2024
0bb6ec0
fix imports
hal3e Mar 27, 2024
4eda217
use forc 0.52.0
hal3e Mar 27, 2024
396ac2f
fix test with forc 0.52.0
hal3e Mar 27, 2024
06fa970
pr comments
hal3e Mar 29, 2024
a847c03
Merge branch 'master' into hal3e/exp-encoding
hal3e Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ env:
FUEL_CORE_VERSION: 0.23.0
FUEL_CORE_PATCH_BRANCH:
RUST_VERSION: 1.76.0
FORC_VERSION: 0.51.1
FORC_VERSION: 0.52.0
FORC_PATCH_BRANCH: ""
FORC_PATCH_REVISION: ""

Expand Down Expand Up @@ -98,14 +98,14 @@ jobs:
!packages/fuels/tests/.gitignore

# TODO: To be removed once experimental encoding is the default
- name: Build Sway test projects w experimental logs
- name: Build Sway test projects w experimental encoding
run: forc build --terse --error-on-warnings --json-abi-with-callpaths --experimental-new-encoding
working-directory: packages/fuels

- uses: actions/upload-artifact@v2
with:
retention-days: 2
name: sway-examples-w-experimental-logs
name: sway-examples-w-experimental-encoding
# cache only the sway build artifacts, skip all src files
path: |
packages/fuels/tests
Expand Down Expand Up @@ -202,9 +202,9 @@ jobs:
- command: check_doc_unresolved_links
args:
# TODO: To be removed once experimental encoding is the default
- command: test_experimental_logs
args:
download_sway_artifacts: sway-examples-w-experimental-logs
- cargo_command: nextest
args: run --all-targets --features "experimental" --workspace
download_sway_artifacts: sway-examples-w-experimental-encoding
install_fuel_core: true
steps:
- name: Checkout repository
Expand Down Expand Up @@ -242,9 +242,8 @@ jobs:
name: ${{ matrix.download_sway_artifacts }}
path: packages/fuels/tests/

# TODO: `test_experimental_logs` to be removed once experimental encoding is the default.
- name: Install nextest
if: ${{ matrix.cargo_command == 'nextest' || matrix.command == 'test_experimental_logs' }}
if: ${{ matrix.cargo_command == 'nextest' }}
uses: taiki-e/install-action@nextest

- name: Install cargo-machete
Expand Down Expand Up @@ -272,6 +271,7 @@ jobs:
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
cd packages/wasm-tests
wasm-pack test --node
wasm-pack test --node --features experimental

- name: Check for invalid documentation anchors
if: ${{ matrix.command == 'check_doc_anchors_valid' }}
Expand All @@ -282,11 +282,6 @@ jobs:
run: |
! cargo doc --document-private-items |& grep -A 6 "warning: unresolved link to"

# TODO: To be removed once experimental encoding is the default.
- name: Test experimental logs
if: ${{ matrix.command == 'test_experimental_logs' }}
run: RUSTFLAGS='--cfg experimental' cargo nextest run --test logs

publish:
needs:
- cargo-verifications
Expand Down
1 change: 1 addition & 0 deletions examples/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tokio = { workspace = true, features = ["full"] }
[features]
fuel-core-lib = ["fuels/fuel-core-lib"]
rocksdb = ["fuels/rocksdb"]
experimental = ["fuels/experimental"]
15 changes: 13 additions & 2 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ mod tests {
.await?;
// ANCHOR_END: contract_call_cost_estimation

assert_eq!(transaction_cost.gas_used, 791);
#[cfg(not(feature = "experimental"))]
let expected_gas = 796;
#[cfg(feature = "experimental")]
let expected_gas = 2087;

assert_eq!(transaction_cost.gas_used, expected_gas);

Ok(())
}
Expand Down Expand Up @@ -602,7 +607,12 @@ mod tests {
.await?;
// ANCHOR_END: multi_call_cost_estimation

assert_eq!(transaction_cost.gas_used, 1162);
#[cfg(not(feature = "experimental"))]
let expected_gas = 1172;
#[cfg(feature = "experimental")]
let expected_gas = 3513;

assert_eq!(transaction_cost.gas_used, expected_gas);

Ok(())
}
Expand Down Expand Up @@ -677,6 +687,7 @@ mod tests {
}

#[tokio::test]
#[cfg(not(feature = "experimental"))]
async fn low_level_call_example() -> Result<()> {
use fuels::{
core::codec::{calldata, fn_selector},
Expand Down
3 changes: 3 additions & 0 deletions examples/debugging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ fuels = { workspace = true }
rand = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["full"] }

[features]
experimental = ["fuels/experimental"]
220 changes: 117 additions & 103 deletions examples/debugging/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#[cfg(test)]
mod tests {
#[cfg(not(feature = "experimental"))]
use std::collections::HashMap;

#[cfg(not(feature = "experimental"))]
use fuel_abi_types::abi::program::ProgramABI;
#[cfg(not(feature = "experimental"))]
use fuels::core::codec::{calldata, fn_selector};
#[cfg(not(feature = "experimental"))]
use fuels::{
core::{
codec::{calldata, fn_selector, resolve_fn_selector, ABIDecoder},
traits::Parameterize,
},
macros::abigen,
//core::codec::ABIDecoder, // TODO: @hal3e temporarily disabled
//macros::abigen, // TODO: @hal3e temporarily disabled
types::{errors::Result, param_types::ParamType, SizedAsciiString},
};

#[cfg(not(feature = "experimental"))]
#[test]
fn get_a_fn_selector() {
use fuels::core::{codec::resolve_fn_selector, traits::Parameterize};

// ANCHOR: example_fn_selector
// fn some_fn_name(arg1: Vec<str[3]>, arg2: u8)
let fn_name = "some_fn_name";
Expand All @@ -25,8 +30,11 @@ mod tests {
// ANCHOR_END: example_fn_selector
}

#[cfg(not(feature = "experimental"))]
#[test]
fn a_fn_selector_from_json_abi() -> Result<()> {
use fuels::core::codec::resolve_fn_selector;

let json_abi_file =
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json";
let abi_file_contents = std::fs::read_to_string(json_abi_file)?;
Expand Down Expand Up @@ -60,6 +68,7 @@ mod tests {
Ok(())
}

#[cfg(not(feature = "experimental"))]
#[test]
fn test_macros() -> Result<()> {
let function_selector = fn_selector!(initialize_counter(u64));
Expand All @@ -71,102 +80,107 @@ mod tests {
Ok(())
}

#[test]
fn decoded_debug_matches_rust_debug() -> Result<()> {
abigen!(Contract(
name = "MyContract",
abi = "packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json"
));

let json_abi_file =
"../../packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json";
let abi_file_contents = std::fs::read_to_string(json_abi_file)?;

let parsed_abi: ProgramABI = serde_json::from_str(&abi_file_contents)?;

let type_lookup = parsed_abi
.types
.into_iter()
.map(|decl| (decl.type_id, decl))
.collect::<HashMap<_, _>>();

let get_first_fn_argument = |fn_name: &str| {
parsed_abi
.functions
.iter()
.find(|abi_fun| abi_fun.name == fn_name)
.expect("should be there")
.inputs
.first()
.expect("should be there")
};
let decoder = ABIDecoder::default();

{
// simple struct with a single generic parameter
let type_application = get_first_fn_argument("struct_w_generic");
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

let expected_struct = SimpleGeneric {
single_generic_param: 123u64,
};

assert_eq!(
format!("{expected_struct:?}"),
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 123])?
);
}
{
// struct that delegates the generic param internally
let type_application = get_first_fn_argument("struct_delegating_generic");
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

let expected_struct = PassTheGenericOn {
one: SimpleGeneric {
single_generic_param: SizedAsciiString::<3>::try_from("abc")?,
},
};

assert_eq!(
format!("{expected_struct:?}"),
decoder.decode_as_debug_str(&param_type, &[97, 98, 99])?
);
}
{
// enum with generic in variant
let type_application = get_first_fn_argument("enum_w_generic");
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

let expected_enum = EnumWGeneric::B(10u64);

assert_eq!(
format!("{expected_enum:?}"),
decoder.decode_as_debug_str(
&param_type,
&[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 10]
)?
);
}
{
// logged type
let logged_type = parsed_abi
.logged_types
.as_ref()
.expect("has logs")
.first()
.expect("has log");

let param_type =
ParamType::try_from_type_application(&logged_type.application, &type_lookup)?;

let expected_u8 = 1;

assert_eq!(
format!("{expected_u8}"),
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 1])?
);
}

Ok(())
}
// #[test] // TODO: @hal3e temporarily disabled the test
// fn decoded_debug_matches_rust_debug() -> Result<()> {
// abigen!(Contract(
// name = "MyContract",
// abi = "packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json"
// ));

// let json_abi_file =
// "../../packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json";
// let abi_file_contents = std::fs::read_to_string(json_abi_file)?;

// let parsed_abi: ProgramABI = serde_json::from_str(&abi_file_contents)?;

// let type_lookup = parsed_abi
// .types
// .into_iter()
// .map(|decl| (decl.type_id, decl))
// .collect::<HashMap<_, _>>();

// let get_first_fn_argument = |fn_name: &str| {
// parsed_abi
// .functions
// .iter()
// .find(|abi_fun| abi_fun.name == fn_name)
// .expect("should be there")
// .inputs
// .first()
// .expect("should be there")
// };
// let decoder = ABIDecoder::default();

// {
// // simple struct with a single generic parameter
// let type_application = get_first_fn_argument("struct_w_generic");
// let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

// let expected_struct = SimpleGeneric {
// single_generic_param: 123u64,
// };

// assert_eq!(
// format!("{expected_struct:?}"),
// decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 123])?
// );
// }
// {
// // struct that delegates the generic param internally
// let type_application = get_first_fn_argument("struct_delegating_generic");
// let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

// let expected_struct = PassTheGenericOn {
// one: SimpleGeneric {
// single_generic_param: SizedAsciiString::<3>::try_from("abc")?,
// },
// };

// assert_eq!(
// format!("{expected_struct:?}"),
// decoder.decode_as_debug_str(&param_type, &[97, 98, 99])?
// );
// }
// {
// // enum with generic in variant
// let type_application = get_first_fn_argument("enum_w_generic");
// let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

// let expected_enum = EnumWGeneric::B(10u64);

// assert_eq!(
// format!("{expected_enum:?}"),
// decoder.decode_as_debug_str(
// &param_type,
// &[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 10]
// )?
// );
// }
// {
// // logged type
// let logged_type = parsed_abi
// .logged_types
// .as_ref()
// .expect("has logs")
// .first()
// .expect("has log");

// let param_type =
// ParamType::try_from_type_application(&logged_type.application, &type_lookup)?;

// let expected_u8 = 1;

// #[cfg(not(feature = "experimental"))]
// let data = [0, 0, 0, 0, 0, 0, 0, 1];
// #[cfg(feature = "experimental")]
// let data = [1];

// assert_eq!(
// format!("{expected_u8}"),
// decoder.decode_as_debug_str(&param_type, &data)?
// );
// }

// Ok(())
// }
}
6 changes: 4 additions & 2 deletions examples/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ mod tests {

#[cfg(feature = "fuel-core-lib")]
use fuels::prelude::Config;
use fuels::prelude::Result;
use fuels::types::{Bits256, EvmAddress, Identity};
use fuels::{
prelude::Result,
types::{Bits256, EvmAddress, Identity},
};

#[tokio::test]
async fn bytes32() -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ tokio = { workspace = true, features = ["test-util", "macros"] }
[features]
default = ["std"]
std = ["dep:fuel-core-client"]
experimental = []
Loading
Loading