Skip to content

Commit

Permalink
chore: improve b256 tests (#1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Apr 23, 2024
1 parent cf0c200 commit 6838d53
Show file tree
Hide file tree
Showing 17 changed files with 167 additions and 100 deletions.
2 changes: 1 addition & 1 deletion docs/src/types/bits256.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ In Fuel, a type called `b256` represents hashes and holds a 256-bit value. The R
Here's an example:

```rust,ignore
{{#include ../../../packages/fuels/tests/bindings.rs:256_arg}}
{{#include ../../../packages/fuels/tests/types_contracts.rs:256_arg}}
```

If you have a hexadecimal value as a string and wish to convert it to `Bits256`, you may do so with `from_hex_str`:
Expand Down
4 changes: 3 additions & 1 deletion packages/fuels/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ members = [
'tests/scripts/require_from_contract',
'tests/scripts/script_array',
'tests/scripts/script_asserts',
'tests/scripts/script_b256',
'tests/scripts/script_configurables',
'tests/scripts/script_enum',
'tests/scripts/script_needs_custom_decoder',
'tests/scripts/script_require',
'tests/scripts/script_struct',
'tests/scripts/transfer_script',
'tests/types/contracts/b256',
'tests/types/contracts/b512',
'tests/types/contracts/bytes',
'tests/types/contracts/call_empty_return',
Expand Down Expand Up @@ -81,6 +81,7 @@ members = [
'tests/types/contracts/vectors',
'tests/types/predicates/address',
'tests/types/predicates/enums',
'tests/types/predicates/predicate_b256',
'tests/types/predicates/predicate_bytes',
'tests/types/predicates/predicate_bytes_hash',
'tests/types/predicates/predicate_generics',
Expand All @@ -94,6 +95,7 @@ members = [
'tests/types/predicates/structs',
'tests/types/predicates/u64',
'tests/types/scripts/options_results',
'tests/types/scripts/script_b256',
'tests/types/scripts/script_bytes',
'tests/types/scripts/script_generics',
'tests/types/scripts/script_heap_types',
Expand Down
72 changes: 0 additions & 72 deletions packages/fuels/tests/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,78 +334,6 @@ async fn compile_bindings_string_input() -> Result<()> {
Ok(())
}

#[cfg(feature = "legacy_encoding")]
#[tokio::test]
async fn compile_bindings_b256_input() -> Result<()> {
// 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": "b256",
"components": null,
"typeParameters": null
}
],
"functions": [
{
"inputs": [
{
"name": "arg",
"type": 1,
"typeArguments": null
}
],
"name": "takes_b256",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
]
}
"#,
));

let wallet = launch_provider_and_get_wallet().await?;

let contract_instance = SimpleContract::new(null_contract_id(), wallet);

let mut hasher = Sha256::new();
hasher.update("test string".as_bytes());

// ANCHOR: 256_arg
let arg: [u8; 32] = hasher.finalize().into();

let call_handler = contract_instance.methods().takes_b256(Bits256(arg));
// ANCHOR_END: 256_arg

let encoded_args = call_handler.contract_call.encoded_args.unwrap().resolve(0);
let encoded = format!(
"{}{}",
hex::encode(call_handler.contract_call.encoded_selector),
hex::encode(encoded_args)
);

assert_eq!(
"0000000054992852d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b",
encoded
);

Ok(())
}

#[cfg(feature = "legacy_encoding")]
#[tokio::test]
async fn compile_bindings_evm_address_input() -> Result<()> {
Expand Down
23 changes: 0 additions & 23 deletions packages/fuels/tests/scripts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use fuels::{
core::codec::{DecoderConfig, EncoderConfig},
prelude::*,
types::Bits256,
};

#[tokio::test]
Expand Down Expand Up @@ -255,28 +254,6 @@ async fn test_script_array() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn test_script_b256() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Script(
name = "MyScript",
project = "packages/fuels/tests/scripts/script_b256"
)),
LoadScript(
name = "script_instance",
script = "MyScript",
wallet = "wallet"
)
);

let my_b256 = Bits256([1; 32]);
let response = script_instance.main(my_b256).call().await?;

assert!(response.value);
Ok(())
}

#[tokio::test]
async fn can_configure_decoder_on_script_call() -> Result<()> {
setup_program_test!(
Expand Down
5 changes: 5 additions & 0 deletions packages/fuels/tests/types/contracts/b256/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "b256"
16 changes: 16 additions & 0 deletions packages/fuels/tests/types/contracts/b256/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
contract;

abi MyContract {
fn b256_as_output() -> b256;
fn b256_as_input(foo: b256) -> bool;
}

impl MyContract for Contract {
fn b256_as_output() -> b256 {
0x0202020202020202020202020202020202020202020202020202020202020202
}

fn b256_as_input(foo: b256) -> bool {
foo == 0x0101010101010101010101010101010101010101010101010101010101010101
}
}
11 changes: 11 additions & 0 deletions packages/fuels/tests/types/contracts/vector_output/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abi VectorsOutputContract {
fn u32_in_vec(len: u32) -> Vec<u32>;
fn u64_in_vec(len: u64) -> Vec<u64>;
fn u8_in_vec(len: u8) -> Vec<u8>;
fn b256_in_vec(len: u8) -> Vec<b256>;
}

impl VectorsOutputContract for Contract {
Expand Down Expand Up @@ -145,4 +146,14 @@ impl VectorsOutputContract for Contract {
}
vec
}

fn b256_in_vec(len: u8) -> Vec<b256> {
let mut vec: Vec<b256> = Vec::new();
let mut i = 0u8;
while i < len {
vec.push(0x0202020202020202020202020202020202020202020202020202020202020202);
i += 1;
}
vec
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "predicate_b256"
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
script;
predicate;

fn main(foo: b256) -> bool {
// [1; 32]
foo == 0x0101010101010101010101010101010101010101010101010101010101010101
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main(
vec_in_array: [Vec<u32>; 2],
vec_in_enum: SomeEnum<Vec<u32>>,
enum_in_vec: Vec<SomeEnum<u32>>,
b256_in_vec: Vec<b256>,
tuple_in_vec: Vec<(u32, u32)>,
vec_in_tuple: (Vec<u32>, Vec<u32>),
vec_in_a_vec_in_a_struct_in_a_vec: Vec<SomeStruct<Vec<Vec<u32>>>>,
Expand Down Expand Up @@ -50,6 +51,11 @@ fn main(
result = false;
}

result = result
&& (b256_in_vec
.get(1)
.unwrap() == 0x0202020202020202020202020202020202020202020202020202020202020202);

result = result && (tuple_in_vec.get(1).unwrap().0 == 128u32);

let (tuple_a, _) = vec_in_tuple;
Expand Down
10 changes: 10 additions & 0 deletions packages/fuels/tests/types/scripts/script_b256/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
script;

fn main(foo: b256) -> b256 {
assert_eq(
foo,
0x0101010101010101010101010101010101010101010101010101010101010101,
);

0x0202020202020202020202020202020202020202020202020202020202020202
}
16 changes: 16 additions & 0 deletions packages/fuels/tests/types/scripts/script_vectors/src/eq_impls.sw
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ impl Eq for Vec<u32> {
}
}

impl Eq for Vec<b256> {
fn eq(self, other: Self) -> bool {
if self.len() != other.len() {
return false;
}
let mut i = 0;
while i < self.len() {
if self.get(i).unwrap() != other.get(i).unwrap() {
return false;
}
i += 1;
}
true
}
}

impl Eq for (Vec<u32>, Vec<u32>) {
fn eq(self, other: Self) -> bool {
self.0 == other.0 && self.1 == other.1
Expand Down
8 changes: 8 additions & 0 deletions packages/fuels/tests/types/scripts/script_vectors/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn main(
vec_in_array: [Vec<u32>; 2],
vec_in_enum: SomeEnum<Vec<u32>>,
enum_in_vec: Vec<SomeEnum<u32>>,
b256_in_vec: Vec<b256>,
tuple_in_vec: Vec<(u32, u32)>,
vec_in_tuple: (Vec<u32>, Vec<u32>),
vec_in_a_vec_in_a_struct_in_a_vec: Vec<SomeStruct<Vec<Vec<u32>>>>,
Expand Down Expand Up @@ -72,6 +73,13 @@ fn main(

require(enum_in_vec == exp_enum_in_vec, "enum_in_vec err");
}
{
let mut exp_b256_in_vec = Vec::new();
exp_b256_in_vec.push(0x0202020202020202020202020202020202020202020202020202020202020202);
exp_b256_in_vec.push(0x0202020202020202020202020202020202020202020202020202020202020202);

require(b256_in_vec == exp_b256_in_vec, "b256_in_vec err");
}
{
let mut exp_tuple_in_vec = Vec::new();
exp_tuple_in_vec.push((0u32, 0u32));
Expand Down
41 changes: 41 additions & 0 deletions packages/fuels/tests/types_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,44 @@ async fn test_vector() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn test_b256() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
name = "TypesContract",
project = "packages/fuels/tests/types/contracts/b256"
)),
Deploy(
name = "contract_instance",
contract = "TypesContract",
wallet = "wallet"
),
);

assert_eq!(
Bits256([2; 32]),
contract_instance
.methods()
.b256_as_output()
.call()
.await?
.value
);

{
// ANCHOR: 256_arg
let b256 = Bits256([1; 32]);

let call_handler = contract_instance.methods().b256_as_input(b256);
// ANCHOR_END: 256_arg

assert!(call_handler.call().await?.value);
}

Ok(())
}

#[tokio::test]
async fn test_b512() -> Result<()> {
setup_program_test!(
Expand Down Expand Up @@ -1800,6 +1838,9 @@ async fn test_base_type_in_vec_output() -> Result<()> {
let response = contract_methods.bool_in_vec().call().await?;
assert_eq!(response.value, [true, false, true, false].to_vec());

let response = contract_methods.b256_in_vec(13).call().await?;
assert_eq!(response.value, vec![Bits256([2; 32]); 13]);

Ok(())
}

Expand Down
15 changes: 15 additions & 0 deletions packages/fuels/tests/types_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ async fn spend_predicate_coins_messages_vectors() -> Result<()> {
let array_in_vec = vec![[0u64, 1u64], [32u64, 1u64]];
let vec_in_enum = SomeEnum::A(vec![0, 1, 128]);
let enum_in_vec = vec![SomeEnum::A(0), SomeEnum::A(16)];
let b256_in_vec = vec![Bits256([2; 32]), Bits256([2; 32])];
let tuple_in_vec = vec![(0, 0), (128, 1)];
let vec_in_tuple = (vec![0, 64, 2], vec![0, 1, 2]);
let vec_in_a_vec_in_a_struct_in_a_vec = vec![
Expand All @@ -256,6 +257,7 @@ async fn spend_predicate_coins_messages_vectors() -> Result<()> {
vec_in_array,
vec_in_enum,
enum_in_vec,
b256_in_vec,
tuple_in_vec,
vec_in_tuple,
vec_in_a_vec_in_a_struct_in_a_vec,
Expand Down Expand Up @@ -364,6 +366,19 @@ async fn predicate_handles_u128() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn predicate_handles_b256() -> Result<()> {
abigen!(Predicate(
name = "MyPredicate",
abi = "packages/fuels/tests/types/predicates/predicate_b256/out/debug/predicate_b256-abi.json"
));

let data = MyPredicateEncoder::default().encode_data(Bits256([1; 32]))?;
assert_predicate_spendable(data, "tests/types/predicates/predicate_b256").await?;

Ok(())
}

fn u256_from(parts: (u64, u64, u64, u64)) -> U256 {
let bytes: [u8; 32] = [
parts.0.to_be_bytes(),
Expand Down
Loading

0 comments on commit 6838d53

Please sign in to comment.