Skip to content

Commit

Permalink
feat: finalize support for raw_slice and string_slice (#1448)
Browse files Browse the repository at this point in the history
closes: #1152,
#1158
  • Loading branch information
hal3e authored Jul 2, 2024
1 parent 169bf16 commit a14870e
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 81 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ env:
FUEL_CORE_VERSION: 0.30.0
FUEL_CORE_PATCH_BRANCH:
RUST_VERSION: 1.76.0
FORC_VERSION: 0.60.0
FORC_PATCH_BRANCH: ""
FORC_VERSION: 0.61.0
FORC_PATCH_BRANCH: "xunilrj/fix-vec-memoverflow"
FORC_PATCH_REVISION: ""
NEXTEST_HIDE_PROGRESS_BAR: "true"
NEXTEST_STATUS_LEVEL: "fail"
Expand Down
6 changes: 3 additions & 3 deletions e2e/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ members = [
'sway/types/predicates/predicate_generics',
'sway/types/predicates/predicate_raw_slice',
'sway/types/predicates/predicate_std_lib_string',
'sway/types/predicates/predicate_string_slice',
'sway/types/predicates/predicate_tuples',
'sway/types/predicates/predicate_u128',
'sway/types/predicates/predicate_u256',
Expand All @@ -101,10 +102,9 @@ members = [
'sway/types/scripts/script_bytes',
'sway/types/scripts/script_generics',
'sway/types/scripts/script_heap_types',
#TODO: Decide what to do with this test project once
# https://github.com/FuelLabs/sway/issues/5727 is resolved
# 'sway/types/scripts/script_raw_slice',
'sway/types/scripts/script_raw_slice',
'sway/types/scripts/script_std_lib_string',
'sway/types/scripts/script_string_slice',
'sway/types/scripts/script_tuples',
'sway/types/scripts/script_u128',
'sway/types/scripts/script_u256',
Expand Down
6 changes: 4 additions & 2 deletions e2e/sway/types/contracts/string_slice/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
contract;

abi RawSliceContract {
fn return_str() -> str;
fn handles_str(input: str) -> str;
}

impl RawSliceContract for Contract {
fn return_str() -> str {
fn handles_str(input: str) -> str {
assert_eq(input, "contract-input");

"contract-return"
}
}
5 changes: 5 additions & 0 deletions e2e/sway/types/predicates/predicate_string_slice/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "predicate_string_slice"
5 changes: 5 additions & 0 deletions e2e/sway/types/predicates/predicate_string_slice/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
predicate;

fn main(input: str) -> bool {
input == "predicate-input"
}
17 changes: 9 additions & 8 deletions e2e/sway/types/scripts/script_raw_slice/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct Wrapper<T> {
}

fn validate_raw_slice(input: raw_slice) {
let vec: Vec<u64> = Vec::from(input);
let vec: Vec<u8> = Vec::from(input);
require(vec.len() == 3, "raw slice len is not 3");
require(
vec
Expand Down Expand Up @@ -40,7 +40,7 @@ fn validate_vec(vec: Vec<raw_slice>) {
validate_raw_slice(vec.get(1).unwrap());
}

fn main(_arg: u64, wrapper: Wrapper<Vec<raw_slice>>) -> raw_slice {
fn main(length: u8, wrapper: Wrapper<Vec<raw_slice>>) -> raw_slice {
if let SomeEnum::Second(enum_raw_slice) = wrapper.inner_enum
{
validate_raw_slice(enum_raw_slice);
Expand All @@ -50,10 +50,11 @@ fn main(_arg: u64, wrapper: Wrapper<Vec<raw_slice>>) -> raw_slice {

validate_vec(wrapper.inner);

let mut rtn: Vec<u64> = Vec::new();
rtn.push(1);
rtn.push(2);
rtn.push(3);

rtn.as_raw_slice()
let mut vec = Vec::new();
let mut counter = 0u8;
while counter < length {
vec.push(counter);
counter = counter + 1;
}
vec.as_raw_slice()
}
6 changes: 4 additions & 2 deletions e2e/sway/types/scripts/script_std_lib_string/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ script;

use std::string::String;

fn main(string: String) {
assert_eq(string, String::from_ascii_str("Hello World"));
fn main(string: String) -> String {
assert_eq(string, String::from_ascii_str("script-input"));

String::from_ascii_str("script-return")
}
5 changes: 5 additions & 0 deletions e2e/sway/types/scripts/script_string_slice/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "script_string_slice"
7 changes: 7 additions & 0 deletions e2e/sway/types/scripts/script_string_slice/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
script;

fn main(input: str) -> str {
assert_eq(input, "script-input");

"script-return"
}
2 changes: 1 addition & 1 deletion e2e/tests/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ async fn test_gas_forwarded_defaults_to_tx_limit() -> Result<()> {
);

// The gas used by the script to call a contract and forward remaining gas limit.
let gas_used_by_script = 255;
let gas_used_by_script = 249;
let gas_limit = 225_883;
let response = contract_instance
.methods()
Expand Down
44 changes: 23 additions & 21 deletions e2e/tests/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use fuels::{
prelude::*,
tx::StorageSlot,
Expand Down Expand Up @@ -57,28 +55,32 @@ async fn test_init_storage_automatically() -> Result<()> {
.deploy(&wallet, TxPolicies::default())
.await?;

let key1 =
Bytes32::from_str("de9090cb50e71c2588c773487d1da7066d0c719849a7e58dc8b6397a25c567c0")
.unwrap();
let key2 =
Bytes32::from_str("f383b0ce51358be57daa3b725fe44acdb2d880604e367199080b4379c41bb6ed")
.unwrap();

let contract_methods = MyContract::new(contract_id, wallet.clone()).methods();
{
let key: Bytes32 =
"d95f4c8d717d52323d34c1118b3f0598a5ec3cabae386887507cabd6dd546a43".parse()?;

let value = contract_methods
.get_value_b256(Bits256(*key1))
.call()
.await?
.value;
assert_eq!(value.0, [1u8; 32]);
let value = contract_methods
.get_value_b256(Bits256(*key))
.call()
.await?
.value;

assert_eq!(value.0, [1u8; 32]);
}
{
let key: Bytes32 =
"c979570128d5f52725e9a343a7f4992d8ed386d7c8cfd25f1c646c51c2ac6b4b".parse()?;

let value = contract_methods
.get_value_u64(Bits256(*key))
.call()
.await?
.value;

assert_eq!(value, 64);
}

let value = contract_methods
.get_value_u64(Bits256(*key2))
.call()
.await?
.value;
assert_eq!(value, 64);
Ok(())
}

Expand Down
18 changes: 9 additions & 9 deletions e2e/tests/types_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,15 +1539,14 @@ async fn generics_test() -> Result<()> {
10u32,
)],
};

contract_methods.complex_test(arg1.clone()).call().await?;
}

Ok(())
}

#[tokio::test]
async fn test_vector() -> Result<()> {
async fn contract_vectors() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
Expand Down Expand Up @@ -1976,7 +1975,7 @@ async fn test_bytes_as_input() -> Result<()> {
}

#[tokio::test]
async fn test_contract_raw_slice() -> Result<()> {
async fn contract_raw_slice() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
Expand Down Expand Up @@ -2021,7 +2020,7 @@ async fn test_contract_raw_slice() -> Result<()> {
}

#[tokio::test]
async fn test_contract_returning_string_slice() -> Result<()> {
async fn contract_string_slice() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
Expand All @@ -2037,16 +2036,17 @@ async fn test_contract_returning_string_slice() -> Result<()> {

let contract_methods = contract_instance.methods();

{
let response = contract_methods.return_str().call().await?;
assert_eq!(response.value, "contract-return");
}
let response = contract_methods
.handles_str("contract-input".try_into()?)
.call()
.await?;
assert_eq!(response.value, "contract-return");

Ok(())
}

#[tokio::test]
async fn test_contract_std_lib_string() -> Result<()> {
async fn contract_std_lib_string() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
Expand Down
13 changes: 13 additions & 0 deletions e2e/tests/types_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,16 @@ async fn predicate_handles_std_string() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn predicate_string_slice() -> Result<()> {
abigen!(Predicate(
name = "MyPredicate",
abi = "e2e/sway/types/predicates/predicate_string_slice/out/release/predicate_string_slice-abi.json"
));

let data = MyPredicateEncoder::default().encode_data("predicate-input".try_into()?)?;
assert_predicate_spendable(data, "sway/types/predicates/predicate_string_slice").await?;

Ok(())
}
87 changes: 56 additions & 31 deletions e2e/tests/types_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,34 +206,32 @@ async fn main_function_vector_arguments() -> Result<()> {
Ok(())
}

// TODO: Decide what to do with this test once
// https://github.com/FuelLabs/sway/issues/5727 is resolved
// #[tokio::test]
// async fn test_script_raw_slice() -> Result<()> {
// setup_program_test!(
// Wallets("wallet"),
// Abigen(Script(
// name = "BimBamScript",
// project = "e2e/sway/types/scripts/script_raw_slice",
// )),
// LoadScript(
// name = "script_instance",
// script = "BimBamScript",
// wallet = "wallet"
// )
// );

// let raw_slice = RawSlice(vec![40, 41, 42]);
// let wrapper = Wrapper {
// inner: vec![raw_slice.clone(), raw_slice.clone()],
// inner_enum: SomeEnum::Second(raw_slice),
// };

// let rtn = script_instance.main(10, wrapper).call().await?.value;
// assert_eq!(rtn, RawSlice(vec![1, 2, 3]));

// Ok(())
// }
#[tokio::test]
async fn script_raw_slice() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Script(
name = "BimBamScript",
project = "e2e/sway/types/scripts/script_raw_slice",
)),
LoadScript(
name = "script_instance",
script = "BimBamScript",
wallet = "wallet"
)
);

let raw_slice = RawSlice(vec![40, 41, 42]);
let wrapper = Wrapper {
inner: vec![raw_slice.clone(), raw_slice.clone()],
inner_enum: SomeEnum::Second(raw_slice),
};

let rtn = script_instance.main(6, wrapper).call().await?.value;
assert_eq!(rtn, RawSlice(vec![0, 1, 2, 3, 4, 5]));

Ok(())
}

#[tokio::test]
async fn main_function_bytes_arguments() -> Result<()> {
Expand Down Expand Up @@ -331,7 +329,7 @@ async fn script_handles_u256() -> Result<()> {
}

#[tokio::test]
async fn script_handles_std_string() -> Result<()> {
async fn script_std_string() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Script(
Expand All @@ -345,8 +343,35 @@ async fn script_handles_std_string() -> Result<()> {
)
);

let arg = String::from("Hello World");
script_instance.main(arg).call().await?;
let response = script_instance
.main("script-input".to_string())
.call()
.await?;
assert_eq!(response.value, "script-return".to_string());

Ok(())
}

#[tokio::test]
async fn script_string_slice() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Script(
name = "MyScript",
project = "e2e/sway/types/scripts/script_string_slice",
)),
LoadScript(
name = "script_instance",
script = "MyScript",
wallet = "wallet"
)
);

let response = script_instance
.main("script-input".try_into()?)
.call()
.await?;
assert_eq!(response.value, "script-return");

Ok(())
}
Expand Down
4 changes: 2 additions & 2 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 = 2692;
let expected_gas = 2609;

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 = 4191;
let expected_gas = 4076;

assert_eq!(transaction_cost.gas_used, expected_gas);

Expand Down

0 comments on commit a14870e

Please sign in to comment.