Skip to content

Commit

Permalink
add a comprehensive predicate+configurables+blobs test
Browse files Browse the repository at this point in the history
  • Loading branch information
Br1ght0ne committed Oct 5, 2024
1 parent 1f5827a commit b140e8c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
14 changes: 13 additions & 1 deletion e2e/sway/predicates/predicate_configurables/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct StructWithGeneric<D> {
configurable {
BOOL: bool = true,
U8: u8 = 8,
TUPLE: (u8, bool) = (8, true),
ARRAY: [u32; 3] = [253, 254, 255],
STRUCT: StructWithGeneric<u8> = StructWithGeneric {
field_1: 8,
field_2: 16,
Expand All @@ -41,9 +43,19 @@ configurable {
fn main(
switch: bool,
u_8: u8,
some_tuple: (u8, bool),
some_array: [u32; 3],
some_struct: StructWithGeneric<u8>,
some_enum: EnumWithGeneric<bool>,
) -> bool {
switch == BOOL && u_8 == U8 && some_struct == STRUCT && some_enum == ENUM
switch == BOOL &&
u_8 == U8 &&
some_tuple.0 == TUPLE.0 &&
some_tuple.1 == TUPLE.1 &&
some_array[0] == ARRAY[0] &&
some_array[1] == ARRAY[1] &&
some_array[2] == ARRAY[2] &&
some_struct == STRUCT &&
some_enum == ENUM
}
// ANCHOR_END: predicate_configurables
89 changes: 85 additions & 4 deletions e2e/tests/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,14 @@ async fn predicate_default_configurables() -> Result<()> {
};
let new_enum = EnumWithGeneric::VariantOne(true);

let predicate_data =
MyPredicateEncoder::default().encode_data(true, 8, new_struct, new_enum)?;
let predicate_data = MyPredicateEncoder::default().encode_data(
true,
8,
(8, true),
[253, 254, 255],
new_struct,
new_enum,
)?;

let mut predicate: Predicate = Predicate::load_from(
"sway/predicates/predicate_configurables/out/release/predicate_configurables.bin",
Expand Down Expand Up @@ -711,6 +717,8 @@ async fn predicate_configurables() -> Result<()> {
abi = "e2e/sway/predicates/predicate_configurables/out/release/predicate_configurables-abi.json"
));

let new_tuple = (16, false);
let new_array = [123, 124, 125];
let new_struct = StructWithGeneric {
field_1: 32u8,
field_2: 64,
Expand All @@ -719,11 +727,13 @@ async fn predicate_configurables() -> Result<()> {

let configurables = MyPredicateConfigurables::default()
.with_U8(8)?
.with_TUPLE(new_tuple)?
.with_ARRAY(new_array)?
.with_STRUCT(new_struct.clone())?
.with_ENUM(new_enum.clone())?;

let predicate_data =
MyPredicateEncoder::default().encode_data(true, 8u8, new_struct, new_enum)?;
let predicate_data = MyPredicateEncoder::default()
.encode_data(true, 8u8, new_tuple, new_array, new_struct, new_enum)?;

let mut predicate: Predicate = Predicate::load_from(
"sway/predicates/predicate_configurables/out/release/predicate_configurables.bin",
Expand Down Expand Up @@ -1155,3 +1165,74 @@ async fn predicate_blobs() -> Result<()> {

Ok(())
}

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

let new_tuple = (16, false);
let new_array = [123, 124, 125];
let new_struct = StructWithGeneric {
field_1: 32u8,
field_2: 64,
};
let new_enum = EnumWithGeneric::VariantTwo;

let configurables = MyPredicateConfigurables::default()
.with_U8(8)?
.with_TUPLE(new_tuple)?
.with_ARRAY(new_array)?
.with_STRUCT(new_struct.clone())?
.with_ENUM(new_enum.clone())?;

let predicate_data = MyPredicateEncoder::default()
.encode_data(true, 8u8, new_tuple, new_array, new_struct, new_enum)?;

let executable = Executable::load_from(
"sway/predicates/predicate_configurables/out/release/predicate_configurables.bin",
)?;

let loader = executable
.convert_to_loader()?
.with_configurables(configurables);

let mut predicate: Predicate = Predicate::from_code(loader.code()).with_data(predicate_data);

let num_coins = 4;
let num_messages = 8;
let amount = 16;
let (provider, predicate_balance, receiver, receiver_balance, asset_id, extra_wallet) =
setup_predicate_test(predicate.address(), num_coins, num_messages, amount).await?;

predicate.set_provider(provider.clone());

loader.upload_blob(extra_wallet).await?;

// TODO: https://github.com/FuelLabs/fuels-rs/issues/1394
let expected_fee = 1;
predicate
.transfer(
receiver.address(),
predicate_balance - expected_fee,
asset_id,
TxPolicies::default(),
)
.await?;

// The predicate has spent the funds
assert_address_balance(predicate.address(), &provider, asset_id, 0).await;

// Funds were transferred
assert_address_balance(
receiver.address(),
&provider,
asset_id,
receiver_balance + predicate_balance - expected_fee,
)
.await;

Ok(())
}

0 comments on commit b140e8c

Please sign in to comment.