diff --git a/CHANGELOG.md b/CHANGELOG.md index 10be1f07..2a19ede7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +33,7 @@ Description of the upcoming release here. ### Added v0.24.0 -- [#285](https://github.com/FuelLabs/sway-libs/pull/285) Adds the `BytecodeRoot` and `ContractConfigurables` types to the Bytecode Library. +- [#293](https://github.com/FuelLabs/sway-libs/pull/293) Adds the `BytecodeRoot` and `ContractConfigurables` types to the Bytecode Library. - [#286](https://github.com/FuelLabs/sway-libs/pull/286) Adds the `_metadata()` function to the Asset Library. ### Changed v0.24.0 @@ -48,50 +48,6 @@ Description of the upcoming release here. ### Breaking v0.24.0 -- [#285](https://github.com/FuelLabs/sway-libs/pull/285) Removes `_with_configurables()` functions from Bytecode Library in favor of using an `Option`. - -The following demonstrates the breaking change. - -Before: - -```sway -// Compute bytecode root -let root_no_configurables: BytecodeRoot = compute_bytecode_root(my_bytecode); -let root_with_configurables: BytecodeRoot = compute_bytecode_root_with_configurables(my_bytecode, my_configurables); - -// Compute predicate address -let address_no_configurables: Address = compute_predicate_address(my_bytecode); -let address_with_configurables: Address = compute_predicate_address_with_configurables(my_bytecode, my_configurables); - -// Verify contract bytecode -verify_contract_bytecode(my_contract_id, my_bytecode); // No configurables -verify_contract_bytecode_with_configurables(my_contract_id, my_bytecode, my_configurables); // With configurables - -// Verify predicate address -verify_predicate_address(my_predicate_address, my_bytecode); // No configurables -verify_predicate_address_with_configurables(my_predicate_address, my_bytecode, my_configurables); // With configurables -``` - -After: - -```sway -// Compute bytecode root -let root_no_configurables: BytecodeRoot = compute_bytecode_root(my_bytecode, None); -let root_with_configurables: BytecodeRoot = compute_bytecode_root(my_bytecode, Some(my_configurables)); - -// Compute predicate address -let address_no_configurables: Address = compute_predicate_address(my_bytecode, None); -let address_with_configurables: Address = compute_predicate_address(my_bytecode, Some(my_configurables)); - -// Verify contract bytecode -verify_contract_bytecode(my_contract_id, my_bytecode, None); // No configurables -verify_contract_bytecode(my_contract_id, my_bytecode, Some(my_configurables)); // With configurables - -// Verify predicate address -verify_predicate_address(my_predicate_address, my_bytecode, None); // No configurables -verify_predicate_address(my_predicate_address, my_bytecode, Some(my_configurables)); // With configurables -``` - - [#286](https://github.com/FuelLabs/sway-libs/pull/286) The support functions for `Metadata` have been removed. They have been moved to sway-standards. Before: diff --git a/docs/book/src/bytecode/index.md b/docs/book/src/bytecode/index.md index 6fcfac2c..0bebfc3c 100644 --- a/docs/book/src/bytecode/index.md +++ b/docs/book/src/bytecode/index.md @@ -21,11 +21,15 @@ To import the Bytecode Library to your Sway Smart Contract, add the following to Once imported, using the Bytecode Library is as simple as calling the desired function. Here is a list of function definitions that you may use. - `compute_bytecode_root()` +- `compute_bytecode_root_with_configurables()` - `compute_predicate_address()` +- `compute_predicate_address_with_configurables()` - `predicate_address_from_root()` - `swap_configurables()` - `verify_contract_bytecode()` +- `verify_contract_bytecode_with_configurables()` - `verify_predicate_address()` +- `verify_predicate_address_with_configurables()` ## Known Issues @@ -51,7 +55,7 @@ Given some bytecode, you may swap the configurables of both Contracts and Predic ### Computing the Bytecode Root -To compute a contract's bytecode root you may call the `compute_bytecode_root()` function. +To compute a contract's bytecode root you may call the `compute_bytecode_root()` or `compute_bytecode_root_with_configurables()` functions. ```sway {{#include ../../../../examples/bytecode/src/main.sw:compute_bytecode_root}} @@ -59,7 +63,7 @@ To compute a contract's bytecode root you may call the `compute_bytecode_root()` ### Verifying a Contract's Bytecode Root -To verify a contract's bytecode root you may call `verify_bytecode_root()` function. +To verify a contract's bytecode root you may call `verify_bytecode_root()` or `verify_contract_bytecode_with_configurables()` functions. ```sway {{#include ../../../../examples/bytecode/src/main.sw:verify_contract_bytecode}} @@ -69,7 +73,7 @@ To verify a contract's bytecode root you may call `verify_bytecode_root()` funct ### Computing the Address from Bytecode -To compute a predicates's address you may call the `compute_predicate_address()` function. +To compute a predicates's address you may call the `compute_predicate_address()` or `compute_predicate_address_with_configurables()` functions. ```sway {{#include ../../../../examples/bytecode/src/main.sw:compute_predicate_address}} @@ -85,7 +89,7 @@ If you have the root of a predicate, you may compute it's corresponding predicat ### Verifying the Address -To verify a predicates's address you may call `verify_predicate_address()` function. +To verify a predicates's address you may call `verify_predicate_address()` or `verify_predicate_address_with_configurables()` functions. ```sway {{#include ../../../../examples/bytecode/src/main.sw:verify_predicate_address}} diff --git a/examples/bytecode/src/main.sw b/examples/bytecode/src/main.sw index 9eb417a4..d12a481e 100644 --- a/examples/bytecode/src/main.sw +++ b/examples/bytecode/src/main.sw @@ -23,41 +23,57 @@ fn make_mutable(not_mutable_bytecode: Vec) { // ANCHOR_END: known_issue // ANCHOR: swap_configurables -fn swap(my_bytecode: Vec, my_configurables: Vec<(u64, Vec)>) { +fn swap( + my_bytecode: Vec, + my_configurables: ContractConfigurables, +) { let mut my_bytecode = my_bytecode; let resulting_bytecode: Vec = swap_configurables(my_bytecode, my_configurables); } // ANCHOR_END: swap_configurables // ANCHOR: compute_bytecode_root -fn compute_bytecode( +fn compute_bytecode(my_bytecode: Vec) { + let root: BytecodeRoot = compute_bytecode_root(my_bytecode); +} + +fn compute_bytecode_configurables( my_bytecode: Vec, - my_configurables: Option, + my_configurables: ContractConfigurables, ) { let mut my_bytecode = my_bytecode; - let root: BytecodeRoot = compute_bytecode_root(my_bytecode, my_configurables); + let root: BytecodeRoot = compute_bytecode_root_with_configurables(my_bytecode, my_configurables); } // ANCHOR_END: compute_bytecode_root // ANCHOR: verify_contract_bytecode -fn verify_contract( +fn verify_contract(my_contract: ContractId, my_bytecode: Vec) { + verify_contract_bytecode(my_contract, my_bytecode); + // By reaching this line the contract has been verified to match the bytecode provided. +} + +fn verify_contract_configurables( my_contract: ContractId, my_bytecode: Vec, - my_configurables: Option, + my_configurables: ContractConfigurables, ) { let mut my_bytecode = my_bytecode; - verify_contract_bytecode(my_contract, my_bytecode, my_configurables); + verify_contract_bytecode_with_configurables(my_contract, my_bytecode, my_configurables); // By reaching this line the contract has been verified to match the bytecode provided. } // ANCHOR_END: verify_contract_bytecode // ANCHOR: compute_predicate_address -fn compute_predicate( +fn compute_predicate(my_bytecode: Vec) { + let address: Address = compute_predicate_address(my_bytecode); +} + +fn compute_predicate_configurables( my_bytecode: Vec, - my_configurables: Option, + my_configurables: ContractConfigurables, ) { let mut my_bytecode = my_bytecode; - let address: Address = compute_predicate_address(my_bytecode, my_configurables); + let address: Address = compute_predicate_address_with_configurables(my_bytecode, my_configurables); } // ANCHOR_END: compute_predicate_address @@ -68,13 +84,18 @@ fn predicate_address(my_root: BytecodeRoot) { // ANCHOR_END: predicate_address_from_root // ANCHOR: verify_predicate_address -fn verify_predicate( +fn verify_predicate(my_predicate: Address, my_bytecode: Vec) { + verify_predicate_address(my_predicate, my_bytecode); + // By reaching this line the predicate bytecode matches the address provided. +} + +fn verify_predicate_configurables( my_predicate: Address, my_bytecode: Vec, - my_configurables: Option, + my_configurables: ContractConfigurables, ) { let mut my_bytecode = my_bytecode; - verify_predicate_address(my_predicate, my_bytecode, my_configurables); + verify_predicate_address_with_configurables(my_predicate, my_bytecode, my_configurables); // By reaching this line the predicate bytecode matches the address provided. } // ANCHOR_END: verify_predicate_address diff --git a/libs/src/bytecode.sw b/libs/src/bytecode.sw index 74521748..04a1b723 100644 --- a/libs/src/bytecode.sw +++ b/libs/src/bytecode.sw @@ -9,12 +9,40 @@ use ::bytecode::utils::{_compute_bytecode_root, _predicate_address_from_root, _s pub type BytecodeRoot = b256; pub type ContractConfigurables = Vec<(u64, Vec)>; +/// Takes the bytecode of a contract or predicate and computes the bytecode root. +/// +/// # Arguments +/// +/// * `bytecode`: [Vec] - The bytecode of a contract or predicate. +/// +/// # Returns +/// +/// * [b256] - The bytecode root of the contract or predicate. +/// +/// # Reverts +/// +/// * When the bytecode is empty. +/// +/// # Examples +/// +/// ```sway +/// use sway_libs::bytecode::{compute_bytecode_root, BytecodeRoot}; +/// +/// fn foo(my_bytecode: Vec) { +/// let bytecode_root: BytecodeRoot = compute_bytecode_root(my_bytecode); +/// assert(bytecode_root != b256::zero()); +/// } +/// ``` +pub fn compute_bytecode_root(bytecode: Vec) -> BytecodeRoot { + _compute_bytecode_root(bytecode.as_raw_slice()) +} + /// Takes the bytecode of a contract or predicate and configurables and computes the bytecode root. /// /// # Arguments /// /// * `bytecode`: [Vec] - The bytecode of a contract or predicate. -/// * `configurables`: [Option] - The configurable values to swap. +/// * `configurables`: [ContractConfigurables] - The configurable values to swap. /// /// # Returns /// @@ -27,28 +55,51 @@ pub type ContractConfigurables = Vec<(u64, Vec)>; /// # Examples /// /// ```sway -/// use sway_libs::bytecode::{compute_bytecode_root, ContractConfigurables, BytecodeRoot}; +/// use sway_libs::bytecode::{compute_bytecode_root_with_configurables, BytecodeRoot, ContractConfigurables}; /// -/// fn foo(my_bytecode: Vec, my_configurables: Option) { +/// fn foo(my_bytecode: Vec, my_configurables: ContractConfigurables) { /// let mut my_bytecode = my_bytecode; -/// let bytecode_root: BytecodeRoot = compute_bytecode_root(my_bytecode, my_configurables); +/// let bytecode_root: BytecodeRoot = compute_bytecode_root_with_configurables(my_bytecode, my_configurables); /// assert(bytecode_root != b256::zero()); /// } /// ``` -pub fn compute_bytecode_root( +pub fn compute_bytecode_root_with_configurables( ref mut bytecode: Vec, - configurables: Option, + configurables: ContractConfigurables, ) -> BytecodeRoot { - match configurables { - Some(configurables) => { - let mut bytecode_slice = bytecode.as_raw_slice(); - _swap_configurables(bytecode_slice, configurables); - _compute_bytecode_root(bytecode_slice) - }, - None => { - _compute_bytecode_root(bytecode.as_raw_slice()) - } - } + let mut bytecode_slice = bytecode.as_raw_slice(); + _swap_configurables(bytecode_slice, configurables); + _compute_bytecode_root(bytecode_slice) +} + +/// Takes the bytecode of a predicate and computes the address of a predicate. +/// +/// # Arguments +/// +/// * `bytecode`: [Vec] - The bytecode of a predicate. +/// +/// # Returns +/// +/// * [Address] - The address of the predicate. +/// +/// +/// # Reverts +/// +/// * When the bytecode is empty. +/// +/// # Examples +/// +/// ```sway +/// use sway_libs::bytecode::compute_predicate_address; +/// +/// fn foo(my_bytecode: Vec) { +/// let predicate_address: Address = compute_predicate_address(my_bytecode); +/// assert(predicate_address != Address::zero()); +/// } +/// ``` +pub fn compute_predicate_address(bytecode: Vec) -> Address { + let bytecode_root = _compute_bytecode_root(bytecode.as_raw_slice()); + _predicate_address_from_root(bytecode_root) } /// Takes the bytecode of a predicate and configurables and computes the address of a predicate. @@ -56,7 +107,7 @@ pub fn compute_bytecode_root( /// # Arguments /// /// * `bytecode`: [Vec] - The bytecode of a predicate. -/// * `configurables`: [Option] - The configurable values to swap. +/// * `configurables`: [ContractConfigurables] - The configurable values to swap. /// /// # Returns /// @@ -71,28 +122,20 @@ pub fn compute_bytecode_root( /// ```sway /// use sway_libs::bytecode::{compute_predicate_address, ContractConfigurables}; /// -/// fn foo(my_bytecode: Vec, my_configurables: Option) { +/// fn foo(my_bytecode: Vec, my_configurables: ContractConfigurables) { /// let mut my_bytecode = my_bytecode; /// let predicate_address: Address = compute_predicate_address(my_bytecode, my_configurables); /// assert(predicate_address != Address::zero()); /// } /// ``` -pub fn compute_predicate_address( +pub fn compute_predicate_address_with_configurables( ref mut bytecode: Vec, - configurables: Option, + configurables: ContractConfigurables, ) -> Address { - match configurables { - Some(configurables) => { - let mut bytecode_slice = bytecode.as_raw_slice(); - _swap_configurables(bytecode_slice, configurables); - let bytecode_root = _compute_bytecode_root(bytecode_slice); - _predicate_address_from_root(bytecode_root) - }, - None => { - let bytecode_root = _compute_bytecode_root(bytecode.as_raw_slice()); - _predicate_address_from_root(bytecode_root) - } - } + let mut bytecode_slice = bytecode.as_raw_slice(); + _swap_configurables(bytecode_slice, configurables); + let bytecode_root = _compute_bytecode_root(bytecode_slice); + _predicate_address_from_root(bytecode_root) } /// Takes the bytecode root of a predicate and generates the address of the predicate. @@ -111,7 +154,7 @@ pub fn compute_predicate_address( /// use sway_libs::bytecode::{predicate_address_from_root, BytecodeRoot}; /// /// fn foo(predicate_bytecode_root: BytecodeRoot) { -/// let predicate_address = predicate_address_from_root(predicate_bytecode_root); +/// let predicate_address: Address = predicate_address_from_root(predicate_bytecode_root); /// assert(predicate_address != Address::zero()); /// } /// ``` @@ -133,7 +176,7 @@ pub fn predicate_address_from_root(bytecode_root: BytecodeRoot) -> Address { /// # Examples /// /// ```sway -/// use sway_libs::bytecode::{sway_configurables ContractConfigurables}; +/// use sway_libs::bytecode::{sway_configurables, ContractConfigurables}; /// /// fn foo(my_bytecode: Vec, my_configurables: ContractConfigurables) { /// let mut my_bytecode = my_bytecode; @@ -150,13 +193,42 @@ pub fn swap_configurables( bytecode } +/// Asserts that a contract's bytecode and the given bytecode match. +/// +/// # Arguments +/// +/// * `contract_id`: [ContractId] - The contract that the bytecode should match. +/// * `bytecode`: [Vec] - The bytecode of the contract. +/// +/// # Reverts +/// +/// * When the bytecode is empty. +/// * When the contract's bytecode root does not match the passed bytecode. +/// +/// # Examples +/// +/// ```sway +/// use sway_libs::bytecode::verify_contract_bytecode; +/// +/// fn foo(my_contract_id: ContractId, my_bytecode: Vec) { +/// verify_contract_bytecode(my_contract_id, my_bytecode); +/// // This line will only be reached if the contract's bytecode root and the computed bytecode root match. +/// } +/// ``` +pub fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) { + let root = bytecode_root(contract_id); + let computed_root = _compute_bytecode_root(bytecode.as_raw_slice()); + + assert(root == computed_root); +} + /// Asserts that a contract's bytecode and the given bytecode and configurable values match. /// /// # Arguments /// /// * `contract_id`: [ContractId] - The contract that the bytecode should match. /// * `bytecode`: [Vec] - The bytecode of the contract. -/// * `configurables`: [Option] - The configurable values to swap. +/// * `configurables`: [ContractConfigurables] - The configurable values to swap. /// /// # Reverts /// @@ -166,35 +238,54 @@ pub fn swap_configurables( /// # Examples /// /// ```sway -/// use sway_libs::bytecode::{verify_contract_bytecode, ContractConfigurables}; +/// use sway_libs::bytecode::{verify_contract_bytecode_with_configurables, ContractConfigurables}; /// -/// fn foo(my_contract_id: ContractId, my_bytecode: Vec, my_configurables: Option) { +/// fn foo(my_contract_id: ContractId, my_bytecode: Vec, my_configurables: ContractConfigurables) { /// let mut my_bytecode = my_bytecode; -/// verify_contract_bytecode(my_contract_id, my_bytecode, my_configurables); +/// verify_contract_bytecode_with_configurables(my_contract_id, my_bytecode, my_configurables); /// // This line will only be reached if the contract's bytecode root and the computed bytecode root match. /// } /// ``` -pub fn verify_contract_bytecode( +pub fn verify_contract_bytecode_with_configurables( contract_id: ContractId, ref mut bytecode: Vec, - configurables: Option, + configurables: ContractConfigurables, ) { - match configurables { - Some(configurables) => { - let root = bytecode_root(contract_id); - let mut bytecode_slice = bytecode.as_raw_slice(); - _swap_configurables(bytecode_slice, configurables); - let computed_root = _compute_bytecode_root(bytecode_slice); - - assert(root == computed_root); - }, - None => { - let root = bytecode_root(contract_id); - let computed_root = _compute_bytecode_root(bytecode.as_raw_slice()); - - assert(root == computed_root); - } - } + let root = bytecode_root(contract_id); + let mut bytecode_slice = bytecode.as_raw_slice(); + _swap_configurables(bytecode_slice, configurables); + let computed_root = _compute_bytecode_root(bytecode_slice); + + assert(root == computed_root); +} + +/// Asserts that a predicates's address from some bytecode and the given address match. +/// +/// # Arguments +/// +/// * `predicate_id`: [Address] - The predicate address that the bytecode should match. +/// * `bytecode`: [Vec] - The bytecode of the predicate. +/// +/// # Reverts +/// +/// * When the bytecode is empty. +/// * When the predicate's address does not match the passed address. +/// +/// # Examples +/// +/// ```sway +/// use sway_libs::bytecode::verify_predicate_address; +/// +/// fn foo(my_predicate_id: Address, my_bytecode: Vec) { +/// verify_predicate_address(my_predicate_id, my_bytecode); +/// // This line will only be reached if the predicates's address and the computed address match. +/// } +/// ``` +pub fn verify_predicate_address(predicate_id: Address, bytecode: Vec) { + let bytecode_root = _compute_bytecode_root(bytecode.as_raw_slice()); + let generated_address = _predicate_address_from_root(bytecode_root); + + assert(generated_address == predicate_id); } /// Asserts that a predicates's address from some bytecode and configurables and the given address match. @@ -203,7 +294,7 @@ pub fn verify_contract_bytecode( /// /// * `predicate_id`: [Address] - The predicate address that the bytecode should match. /// * `bytecode`: [Vec] - The bytecode of the predicate. -/// * `configurables`: [Option] - The configurable values to swap. +/// * `configurables`: [ContractConfigurables] - The configurable values to swap. /// /// # Reverts /// @@ -213,33 +304,23 @@ pub fn verify_contract_bytecode( /// # Examples /// /// ```sway -/// use sway_libs::bytecode::{verify_predicate_address, ContractConfigurables}; +/// use sway_libs::bytecode::verify_predicate_address_with_configurables; /// -/// fn foo(my_predicate_id: Address, my_bytecode: Vec, my_configurables: Option) { +/// fn foo(my_predicate_id: Address, my_bytecode: Vec, my_configurables: ContractConfigurables) { /// let mut my_bytecode = my_bytecode; -/// verify_predicate_address(my_predicate_id, my_bytecode, my_configurables); +/// verify_predicate_address_with_configurables(my_predicate_id, my_bytecode, my_configurables); /// // This line will only be reached if the predicates's address and the computed address match. /// } /// ``` -pub fn verify_predicate_address( +pub fn verify_predicate_address_with_configurables( predicate_id: Address, ref mut bytecode: Vec, - configurables: Option, + configurables: ContractConfigurables, ) { - match configurables { - Some(configurables) => { - let mut bytecode_slice = bytecode.as_raw_slice(); - _swap_configurables(bytecode_slice, configurables); - let bytecode_root = _compute_bytecode_root(bytecode_slice); - let generated_address = _predicate_address_from_root(bytecode_root); - - assert(generated_address == predicate_id); - }, - None => { - let bytecode_root = _compute_bytecode_root(bytecode.as_raw_slice()); - let generated_address = _predicate_address_from_root(bytecode_root); - - assert(generated_address == predicate_id); - } - } + let mut bytecode_slice = bytecode.as_raw_slice(); + _swap_configurables(bytecode_slice, configurables); + let bytecode_root = _compute_bytecode_root(bytecode_slice); + let generated_address = _predicate_address_from_root(bytecode_root); + + assert(generated_address == predicate_id); } diff --git a/tests/src/bytecode/test_contract/src/main.sw b/tests/src/bytecode/test_contract/src/main.sw index 6687f177..847ed675 100644 --- a/tests/src/bytecode/test_contract/src/main.sw +++ b/tests/src/bytecode/test_contract/src/main.sw @@ -2,35 +2,43 @@ contract; use sway_libs::bytecode::{ compute_bytecode_root, + compute_bytecode_root_with_configurables, compute_predicate_address, + compute_predicate_address_with_configurables, predicate_address_from_root, swap_configurables, verify_contract_bytecode, + verify_contract_bytecode_with_configurables, verify_predicate_address, + verify_predicate_address_with_configurables, }; use std::alloc::alloc_bytes; abi TestBytecodeSolver { fn predicate_address_from_root(bytecode_root: b256) -> Address; - fn compute_predicate_address( + fn compute_predicate_address(bytecode: Vec) -> Address; + fn compute_predicate_address_with_configurables( bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ) -> Address; - fn compute_bytecode_root( + fn compute_bytecode_root(bytecode: Vec) -> b256; + fn compute_bytecode_root_with_configurables( bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ) -> b256; fn swap_configurables(bytecode: Vec, configurables: Vec<(u64, Vec)>) -> Vec; - fn verify_contract_bytecode( + fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec); + fn verify_contract_bytecode_with_configurables( contract_id: ContractId, bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ); - fn verify_predicate_address( + fn verify_predicate_address(predicate_id: Address, bytecode: Vec); + fn verify_predicate_address_with_configurables( predicate_id: Address, bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ); } @@ -38,10 +46,13 @@ impl TestBytecodeSolver for Contract { fn predicate_address_from_root(bytecode_root: b256) -> Address { predicate_address_from_root(bytecode_root) } + fn compute_predicate_address(bytecode: Vec) -> Address { + compute_predicate_address(bytecode) + } - fn compute_predicate_address( + fn compute_predicate_address_with_configurables( bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ) -> Address { // Copy the bytecode to a newly allocated memory to avoid memory ownership error. let mut bytecode_slice = raw_slice::from_parts::(alloc_bytes(bytecode.len()), bytecode.len()); @@ -49,12 +60,16 @@ impl TestBytecodeSolver for Contract { .ptr() .copy_bytes_to(bytecode_slice.ptr(), bytecode.len()); let mut bytecode_vec = Vec::from(bytecode_slice); - compute_predicate_address(bytecode_vec, configurables) + compute_predicate_address_with_configurables(bytecode_vec, configurables) + } + + fn compute_bytecode_root(bytecode: Vec) -> b256 { + compute_bytecode_root(bytecode) } - fn compute_bytecode_root( + fn compute_bytecode_root_with_configurables( bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ) -> b256 { // Copy the bytecode to a newly allocated memory to avoid memory ownership error. let mut bytecode_slice = raw_slice::from_parts::(alloc_bytes(bytecode.len()), bytecode.len()); @@ -62,7 +77,7 @@ impl TestBytecodeSolver for Contract { .ptr() .copy_bytes_to(bytecode_slice.ptr(), bytecode.len()); let mut bytecode_vec = Vec::from(bytecode_slice); - compute_bytecode_root(bytecode_vec, configurables) + compute_bytecode_root_with_configurables(bytecode_vec, configurables) } fn swap_configurables(bytecode: Vec, configurables: Vec<(u64, Vec)>) -> Vec { @@ -75,10 +90,14 @@ impl TestBytecodeSolver for Contract { swap_configurables(bytecode_vec, configurables) } - fn verify_contract_bytecode( + fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) { + verify_contract_bytecode(contract_id, bytecode); + } + + fn verify_contract_bytecode_with_configurables( contract_id: ContractId, bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ) { // Copy the bytecode to a newly allocated memory to avoid memory ownership error. let mut bytecode_slice = raw_slice::from_parts::(alloc_bytes(bytecode.len()), bytecode.len()); @@ -86,13 +105,17 @@ impl TestBytecodeSolver for Contract { .ptr() .copy_bytes_to(bytecode_slice.ptr(), bytecode.len()); let mut bytecode_vec = Vec::from(bytecode_slice); - verify_contract_bytecode(contract_id, bytecode_vec, configurables); + verify_contract_bytecode_with_configurables(contract_id, bytecode_vec, configurables); + } + + fn verify_predicate_address(predicate_id: Address, bytecode: Vec) { + verify_predicate_address(predicate_id, bytecode); } - fn verify_predicate_address( + fn verify_predicate_address_with_configurables( predicate_id: Address, bytecode: Vec, - configurables: Option)>>, + configurables: Vec<(u64, Vec)>, ) { // Copy the bytecode to a newly allocated memory to avoid memory ownership error. let mut bytecode_slice = raw_slice::from_parts::(alloc_bytes(bytecode.len()), bytecode.len()); @@ -100,6 +123,6 @@ impl TestBytecodeSolver for Contract { .ptr() .copy_bytes_to(bytecode_slice.ptr(), bytecode.len()); let mut bytecode_vec = Vec::from(bytecode_slice); - verify_predicate_address(predicate_id, bytecode_vec, configurables); + verify_predicate_address_with_configurables(predicate_id, bytecode_vec, configurables); } } diff --git a/tests/src/bytecode/tests/functions/compute_bytecode_root.rs b/tests/src/bytecode/tests/functions/compute_bytecode_root.rs index 9fc3b7aa..f11ed029 100644 --- a/tests/src/bytecode/tests/functions/compute_bytecode_root.rs +++ b/tests/src/bytecode/tests/functions/compute_bytecode_root.rs @@ -1,13 +1,9 @@ use crate::bytecode::tests::utils::{ abi_calls::compute_bytecode_root, test_helpers::{ - build_complex_configurables, build_simple_configurables, complex_contract_bytecode, - complex_contract_bytecode_root_from_file, - complex_contract_bytecode_root_with_configurables_from_file, complex_defaults, defaults, - predicate_bytecode, simple_contract_bytecode, simple_contract_bytecode_root_from_file, - simple_contract_bytecode_root_with_configurables_from_file, - simple_predicate_bytecode_root_from_file, - simple_predicate_bytecode_root_with_configurables_from_file, test_contract_instance, + complex_contract_bytecode, complex_contract_bytecode_root_from_file, predicate_bytecode, + simple_contract_bytecode, simple_contract_bytecode_root_from_file, + simple_predicate_bytecode_root_from_file, test_contract_instance, }, }; @@ -27,33 +23,7 @@ mod success { // Call the contract and compute the bytecode root let result_bytecode_root = - compute_bytecode_root(&test_contract_instance, file_bytecode, None).await; - - assert_eq!(result_bytecode_root, file_bytecode_root); - } - - #[tokio::test] - async fn compute_bytecode_root_with_configurables_of_simple_contract() { - let (test_contract_instance, _wallet) = test_contract_instance().await; - let (contract_offset, _predicate_offset, config_value) = defaults(); - - // Get the bytecode for the contract - let file_bytecode = simple_contract_bytecode(); - - // Build the configurable changes - let my_configurables = build_simple_configurables(contract_offset, config_value); - - // Get the bytecode root from the file - let file_bytecode_root = - simple_contract_bytecode_root_with_configurables_from_file(config_value as u64).await; - - // Call the contract and compute the bytecode root - let result_bytecode_root = compute_bytecode_root( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - ) - .await; + compute_bytecode_root(&test_contract_instance, file_bytecode).await; assert_eq!(result_bytecode_root, file_bytecode_root); } @@ -70,39 +40,7 @@ mod success { // Call the contract and compute the bytecode root let result_bytecode_root = - compute_bytecode_root(&test_contract_instance, file_bytecode, None).await; - - assert_eq!(result_bytecode_root, file_bytecode_root); - } - - #[tokio::test] - async fn compute_bytecode_root_with_configurables_of_complex_contract() { - let (test_contract_instance, _wallet) = test_contract_instance().await; - let (_contract_offset, _predicate_offset, config_value) = defaults(); - let (config_struct, config_enum) = complex_defaults(); - - // Get the bytecode for the contract - let file_bytecode = complex_contract_bytecode(); - - // Build the configurable changes - let my_configurables = - build_complex_configurables(config_value, config_struct.clone(), config_enum.clone()); - - // Get the bytecode root from the file - let file_bytecode_root = complex_contract_bytecode_root_with_configurables_from_file( - config_value as u64, - config_struct, - config_enum, - ) - .await; - - // Call the contract and compute the bytecode root - let result_bytecode_root = compute_bytecode_root( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - ) - .await; + compute_bytecode_root(&test_contract_instance, file_bytecode).await; assert_eq!(result_bytecode_root, file_bytecode_root); } @@ -116,7 +54,7 @@ mod success { // Call the contract and compute the bytecode root let result_bytecode_root = - compute_bytecode_root(&test_contract_instance, file_bytecode, None).await; + compute_bytecode_root(&test_contract_instance, file_bytecode).await; // Get the bytecode root of the predicate from the file. let predicate_bytecode_root = @@ -125,68 +63,21 @@ mod success { // Assert that the roots are the same. assert_eq!(result_bytecode_root, predicate_bytecode_root); } - - #[tokio::test] - async fn compute_bytecode_root_with_configurables_of_predicate() { - let (test_contract_instance, wallet) = test_contract_instance().await; - let (_contract_offset, predicate_offset, config_value) = defaults(); - - // Get the bytecode for the predicate - let file_bytecode = predicate_bytecode(); - - // Build the configurable changes - let my_configurables = build_simple_configurables(predicate_offset, config_value); - - // Call the contract and compute the bytecode root - let result_bytecode_root = compute_bytecode_root( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - ) - .await; - - // Get the bytecode root of the predicate from the file. - let predicate_bytecode_root = simple_predicate_bytecode_root_with_configurables_from_file( - wallet.clone(), - config_value as u64, - ) - .await; - - // Assert that the roots are the same. - assert_eq!(result_bytecode_root, predicate_bytecode_root); - } } mod revert { use super::*; - #[tokio::test] - #[should_panic] - async fn when_bytecode_is_empty_no_config() { - let (test_contract_instance, _wallet) = test_contract_instance().await; - - let empty_bytecode: Vec = Vec::new(); - - // Call the contract and compute the bytecode root - let _result_bytecode_root = - compute_bytecode_root(&test_contract_instance, empty_bytecode, None).await; - } - #[tokio::test] #[should_panic] async fn when_bytecode_is_empty() { let (test_contract_instance, _wallet) = test_contract_instance().await; let empty_bytecode: Vec = Vec::new(); - let my_configurables: Vec<(u64, Vec)> = Vec::new(); // Call the contract and compute the bytecode root - let _result_bytecode_root = compute_bytecode_root( - &test_contract_instance, - empty_bytecode, - Some(my_configurables), - ) - .await; + let _result_bytecode_root = + compute_bytecode_root(&test_contract_instance, empty_bytecode).await; } } diff --git a/tests/src/bytecode/tests/functions/compute_bytecode_root_with_configurables.rs b/tests/src/bytecode/tests/functions/compute_bytecode_root_with_configurables.rs new file mode 100644 index 00000000..98ad1d24 --- /dev/null +++ b/tests/src/bytecode/tests/functions/compute_bytecode_root_with_configurables.rs @@ -0,0 +1,125 @@ +use crate::bytecode::tests::utils::{ + abi_calls::compute_bytecode_root_with_configurables, + test_helpers::{ + build_complex_configurables, build_simple_configurables, complex_contract_bytecode, + complex_contract_bytecode_root_with_configurables_from_file, complex_defaults, defaults, + predicate_bytecode, simple_contract_bytecode, + simple_contract_bytecode_root_with_configurables_from_file, + simple_predicate_bytecode_root_with_configurables_from_file, test_contract_instance, + }, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn compute_bytecode_root_with_configurables_of_simple_contract() { + let (test_contract_instance, _wallet) = test_contract_instance().await; + let (contract_offset, _predicate_offset, config_value) = defaults(); + + // Get the bytecode for the contract + let file_bytecode = simple_contract_bytecode(); + + // Build the configurable changes + let my_configurables = build_simple_configurables(contract_offset, config_value); + + // Get the bytecode root from the file + let file_bytecode_root = + simple_contract_bytecode_root_with_configurables_from_file(config_value as u64).await; + + // Call the contract and compute the bytecode root + let result_bytecode_root = compute_bytecode_root_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + ) + .await; + + assert_eq!(result_bytecode_root, file_bytecode_root); + } + + #[tokio::test] + async fn compute_bytecode_root_with_configurables_of_complex_contract() { + let (test_contract_instance, _wallet) = test_contract_instance().await; + let (_contract_offset, _predicate_offset, config_value) = defaults(); + let (config_struct, config_enum) = complex_defaults(); + + // Get the bytecode for the contract + let file_bytecode = complex_contract_bytecode(); + + // Build the configurable changes + let my_configurables = + build_complex_configurables(config_value, config_struct.clone(), config_enum.clone()); + + // Get the bytecode root from the file + let file_bytecode_root = complex_contract_bytecode_root_with_configurables_from_file( + config_value as u64, + config_struct, + config_enum, + ) + .await; + + // Call the contract and compute the bytecode root + let result_bytecode_root = compute_bytecode_root_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + ) + .await; + + assert_eq!(result_bytecode_root, file_bytecode_root); + } + + #[tokio::test] + async fn compute_bytecode_root_with_configurables_of_predicate() { + let (test_contract_instance, wallet) = test_contract_instance().await; + let (_contract_offset, predicate_offset, config_value) = defaults(); + + // Get the bytecode for the predicate + let file_bytecode = predicate_bytecode(); + + // Build the configurable changes + let my_configurables = build_simple_configurables(predicate_offset, config_value); + + // Call the contract and compute the bytecode root + let result_bytecode_root = compute_bytecode_root_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + ) + .await; + + // Get the bytecode root of the predicate from the file. + let predicate_bytecode_root = simple_predicate_bytecode_root_with_configurables_from_file( + wallet.clone(), + config_value as u64, + ) + .await; + + // Assert that the roots are the same. + assert_eq!(result_bytecode_root, predicate_bytecode_root); + } +} + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic] + async fn when_bytecode_is_empty() { + let (test_contract_instance, _wallet) = test_contract_instance().await; + + let empty_bytecode: Vec = Vec::new(); + let my_configurables: Vec<(u64, Vec)> = Vec::new(); + + // Call the contract and compute the bytecode root + let _result_bytecode_root = compute_bytecode_root_with_configurables( + &test_contract_instance, + empty_bytecode, + my_configurables, + ) + .await; + } +} diff --git a/tests/src/bytecode/tests/functions/compute_predicate_address.rs b/tests/src/bytecode/tests/functions/compute_predicate_address.rs index 93a2a3da..b1090bd7 100644 --- a/tests/src/bytecode/tests/functions/compute_predicate_address.rs +++ b/tests/src/bytecode/tests/functions/compute_predicate_address.rs @@ -1,9 +1,6 @@ use crate::bytecode::tests::utils::{ abi_calls::compute_predicate_address, - test_helpers::{ - build_simple_configurables, defaults, predicate_bytecode, setup_predicate_from_file, - setup_predicate_from_file_with_configurable, test_contract_instance, - }, + test_helpers::{predicate_bytecode, setup_predicate_from_file, test_contract_instance}, }; mod success { @@ -19,7 +16,7 @@ mod success { // Call the contract and compute the address let result_address = - compute_predicate_address(&test_contract_instance, file_bytecode, None).await; + compute_predicate_address(&test_contract_instance, file_bytecode).await; // Create an instance of the predicate let predicate_instance = setup_predicate_from_file(wallet.clone()).await; @@ -27,56 +24,12 @@ mod success { // Assert that the roots are the same. assert_eq!(result_address, predicate_instance.address().into()); } - - #[tokio::test] - async fn compute_predicate_address_with_configurables_from_bytecode() { - let (test_contract_instance, wallet) = test_contract_instance().await; - let (_contract_offset, predicate_offset, config_value) = defaults(); - - // Get the bytecode for the predicate - let file_bytecode = predicate_bytecode(); - - // Build the configurable changes - let my_configurables = build_simple_configurables(predicate_offset, config_value); - - // Call the contract and compute the address - let result_address = compute_predicate_address( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - ) - .await; - - // Create an instance of the predicate - let predicate_instance = - setup_predicate_from_file_with_configurable(wallet.clone(), config_value as u64).await; - - // Assert that the roots are the same. - assert_eq!(result_address, predicate_instance.address().into()); - } } mod revert { use super::*; - #[tokio::test] - #[should_panic] - async fn when_bytecode_is_empty_with_configurables() { - let (test_contract_instance, _wallet) = test_contract_instance().await; - - let empty_bytecode: Vec = Vec::new(); - let my_configurables: Vec<(u64, Vec)> = Vec::new(); - - // Call the contract and compute the address - let _result_address = compute_predicate_address( - &test_contract_instance, - empty_bytecode, - Some(my_configurables), - ) - .await; - } - #[tokio::test] #[should_panic] async fn when_bytecode_is_empty() { @@ -86,6 +39,6 @@ mod revert { // Call the contract and compute the address let _result_address = - compute_predicate_address(&test_contract_instance, empty_bytecode, None).await; + compute_predicate_address(&test_contract_instance, empty_bytecode).await; } } diff --git a/tests/src/bytecode/tests/functions/compute_predicate_address_with_configurables.rs b/tests/src/bytecode/tests/functions/compute_predicate_address_with_configurables.rs new file mode 100644 index 00000000..dda74d1d --- /dev/null +++ b/tests/src/bytecode/tests/functions/compute_predicate_address_with_configurables.rs @@ -0,0 +1,61 @@ +use crate::bytecode::tests::utils::{ + abi_calls::compute_predicate_address_with_configurables, + test_helpers::{ + build_simple_configurables, defaults, predicate_bytecode, + setup_predicate_from_file_with_configurable, test_contract_instance, + }, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn compute_predicate_address_with_configurables_from_bytecode() { + let (test_contract_instance, wallet) = test_contract_instance().await; + let (_contract_offset, predicate_offset, config_value) = defaults(); + + // Get the bytecode for the predicate + let file_bytecode = predicate_bytecode(); + + // Build the configurable changes + let my_configurables = build_simple_configurables(predicate_offset, config_value); + + // Call the contract and compute the address + let result_address = compute_predicate_address_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + ) + .await; + + // Create an instance of the predicate + let predicate_instance = + setup_predicate_from_file_with_configurable(wallet.clone(), config_value as u64).await; + + // Assert that the roots are the same. + assert_eq!(result_address, predicate_instance.address().into()); + } +} + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic] + async fn when_bytecode_is_empty() { + let (test_contract_instance, _wallet) = test_contract_instance().await; + + let empty_bytecode: Vec = Vec::new(); + let my_configurables: Vec<(u64, Vec)> = Vec::new(); + + // Call the contract and compute the address + let _result_address = compute_predicate_address_with_configurables( + &test_contract_instance, + empty_bytecode, + my_configurables, + ) + .await; + } +} diff --git a/tests/src/bytecode/tests/functions/mod.rs b/tests/src/bytecode/tests/functions/mod.rs index 2a366099..bdbd25a8 100644 --- a/tests/src/bytecode/tests/functions/mod.rs +++ b/tests/src/bytecode/tests/functions/mod.rs @@ -1,6 +1,10 @@ mod compute_bytecode_root; +mod compute_bytecode_root_with_configurables; mod compute_predicate_address; +mod compute_predicate_address_with_configurables; mod predicate_address_from_root; mod swap_configurables; mod verify_contract_bytecode; +mod verify_contract_bytecode_with_configurables; mod verify_predicate_address; +mod verify_predicate_address_with_configurables; diff --git a/tests/src/bytecode/tests/functions/verify_contract_bytecode.rs b/tests/src/bytecode/tests/functions/verify_contract_bytecode.rs index ffae0611..79a208e4 100644 --- a/tests/src/bytecode/tests/functions/verify_contract_bytecode.rs +++ b/tests/src/bytecode/tests/functions/verify_contract_bytecode.rs @@ -1,11 +1,9 @@ use crate::bytecode::tests::utils::{ abi_calls::{verify_complex_contract_bytecode, verify_simple_contract_bytecode}, test_helpers::{ - build_complex_configurables, build_simple_configurables, complex_contract_bytecode, - complex_defaults, defaults, deploy_complex_contract_from_file, - deploy_complex_contract_with_configurables_from_file, deploy_simple_contract_from_file, - deploy_simple_contract_with_configurables_from_file, predicate_bytecode, - simple_contract_bytecode, test_contract_instance, + complex_contract_bytecode, deploy_complex_contract_from_file, + deploy_simple_contract_from_file, predicate_bytecode, simple_contract_bytecode, + test_contract_instance, }, }; @@ -13,66 +11,6 @@ mod success { use super::*; - #[tokio::test] - async fn verify_bytecode_root_of_simple_contract_with_configurables() { - let (test_contract_instance, wallet) = test_contract_instance().await; - let (contract_offset, _predicate_offset, config_value) = defaults(); - - // Get the bytecode for the contract - let file_bytecode = simple_contract_bytecode(); - - // Build the configurable changes - let my_configurables = build_simple_configurables(contract_offset, config_value); - - // Deploy the new simple contract with the bytecode that contains the changes - let (simple_contract_instance, id) = deploy_simple_contract_with_configurables_from_file( - wallet.clone(), - config_value as u64, - ) - .await; - - verify_simple_contract_bytecode( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - id, - simple_contract_instance, - ) - .await; - } - - #[tokio::test] - async fn verify_bytecode_root_of_complex_contract_with_configurables() { - let (test_contract_instance, wallet) = test_contract_instance().await; - let (_contract_offset, _predicate_offset, config_value) = defaults(); - let (config_struct, config_enum) = complex_defaults(); - - // Get the bytecode for the contract - let file_bytecode = complex_contract_bytecode(); - - // Build the configurable changes - let my_configurables = - build_complex_configurables(config_value, config_struct.clone(), config_enum.clone()); - - // Deploy the new contract with the bytecode that contains the changes - let (complex_contract_instance, id) = deploy_complex_contract_with_configurables_from_file( - wallet.clone(), - config_value as u64, - config_struct, - config_enum, - ) - .await; - - verify_complex_contract_bytecode( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - id, - complex_contract_instance, - ) - .await; - } - #[tokio::test] async fn verify_bytecode_root_of_simple_contract() { let (test_contract_instance, wallet) = test_contract_instance().await; @@ -86,7 +24,6 @@ mod success { verify_simple_contract_bytecode( &test_contract_instance, file_bytecode, - None, id, simple_contract_instance, ) @@ -107,7 +44,6 @@ mod success { verify_complex_contract_bytecode( &test_contract_instance, file_bytecode, - None, id, complex_contract_instance, ) @@ -119,58 +55,6 @@ mod revert { use super::*; - #[tokio::test] - #[should_panic] - async fn when_configurables_do_not_match() { - let (test_contract_instance, wallet) = test_contract_instance().await; - let (contract_offset, _predicate_offset, config_value) = defaults(); - - // Get the bytecode for the contract - let file_bytecode = simple_contract_bytecode(); - - // Build the configurable changes - let my_configurables = build_simple_configurables(contract_offset, config_value); - - // Deploy the new simple contract with the bytecode that contains the changes - let (simple_contract_instance, id) = deploy_simple_contract_with_configurables_from_file( - wallet.clone(), - (config_value as u64) + 1, - ) - .await; - - verify_simple_contract_bytecode( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - id, - simple_contract_instance, - ) - .await; - } - - #[tokio::test] - #[should_panic] - async fn when_bytecode_is_empty_with_configurables() { - let (test_contract_instance, wallet) = test_contract_instance().await; - - let empty_bytecode: Vec = Vec::new(); - let my_configurables: Vec<(u64, Vec)> = Vec::new(); - - // Deploy the new simple contract with the bytecode that contains the changes - let (simple_contract_instance, id) = - deploy_simple_contract_with_configurables_from_file(wallet.clone(), 0).await; - - // Call the contract and compute the bytecode root - verify_simple_contract_bytecode( - &test_contract_instance, - empty_bytecode, - Some(my_configurables), - id, - simple_contract_instance, - ) - .await; - } - #[tokio::test] #[should_panic] async fn when_bytecode_does_not_match() { @@ -185,7 +69,6 @@ mod revert { verify_simple_contract_bytecode( &test_contract_instance, file_bytecode, - None, id, simple_contract_instance, ) @@ -205,7 +88,6 @@ mod revert { verify_simple_contract_bytecode( &test_contract_instance, empty_bytecode, - None, id, simple_contract_instance, ) diff --git a/tests/src/bytecode/tests/functions/verify_contract_bytecode_with_configurables.rs b/tests/src/bytecode/tests/functions/verify_contract_bytecode_with_configurables.rs new file mode 100644 index 00000000..2c88b4d1 --- /dev/null +++ b/tests/src/bytecode/tests/functions/verify_contract_bytecode_with_configurables.rs @@ -0,0 +1,134 @@ +use crate::bytecode::tests::utils::{ + abi_calls::{ + verify_complex_contract_bytecode_with_configurables, + verify_simple_contract_bytecode_with_configurables, + }, + test_helpers::{ + build_complex_configurables, build_simple_configurables, complex_contract_bytecode, + complex_defaults, defaults, deploy_complex_contract_with_configurables_from_file, + deploy_simple_contract_with_configurables_from_file, simple_contract_bytecode, + test_contract_instance, + }, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn verify_bytecode_root_of_simple_contract() { + let (test_contract_instance, wallet) = test_contract_instance().await; + let (contract_offset, _predicate_offset, config_value) = defaults(); + + // Get the bytecode for the contract + let file_bytecode = simple_contract_bytecode(); + + // Build the configurable changes + let my_configurables = build_simple_configurables(contract_offset, config_value); + + // Deploy the new simple contract with the bytecode that contains the changes + let (simple_contract_instance, id) = deploy_simple_contract_with_configurables_from_file( + wallet.clone(), + config_value as u64, + ) + .await; + + verify_simple_contract_bytecode_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + id, + simple_contract_instance, + ) + .await; + } + + #[tokio::test] + async fn verify_bytecode_root_of_complex_contract() { + let (test_contract_instance, wallet) = test_contract_instance().await; + let (_contract_offset, _predicate_offset, config_value) = defaults(); + let (config_struct, config_enum) = complex_defaults(); + + // Get the bytecode for the contract + let file_bytecode = complex_contract_bytecode(); + + // Build the configurable changes + let my_configurables = + build_complex_configurables(config_value, config_struct.clone(), config_enum.clone()); + + // Deploy the new contract with the bytecode that contains the changes + let (complex_contract_instance, id) = deploy_complex_contract_with_configurables_from_file( + wallet.clone(), + config_value as u64, + config_struct, + config_enum, + ) + .await; + + verify_complex_contract_bytecode_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + id, + complex_contract_instance, + ) + .await; + } +} + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic] + async fn when_configurables_do_not_match() { + let (test_contract_instance, wallet) = test_contract_instance().await; + let (contract_offset, _predicate_offset, config_value) = defaults(); + + // Get the bytecode for the contract + let file_bytecode = simple_contract_bytecode(); + + // Build the configurable changes + let my_configurables = build_simple_configurables(contract_offset, config_value); + + // Deploy the new simple contract with the bytecode that contains the changes + let (simple_contract_instance, id) = deploy_simple_contract_with_configurables_from_file( + wallet.clone(), + (config_value as u64) + 1, + ) + .await; + + verify_simple_contract_bytecode_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + id, + simple_contract_instance, + ) + .await; + } + + #[tokio::test] + #[should_panic] + async fn when_bytecode_is_empty() { + let (test_contract_instance, wallet) = test_contract_instance().await; + + let empty_bytecode: Vec = Vec::new(); + let my_configurables: Vec<(u64, Vec)> = Vec::new(); + + // Deploy the new simple contract with the bytecode that contains the changes + let (simple_contract_instance, id) = + deploy_simple_contract_with_configurables_from_file(wallet.clone(), 0).await; + + // Call the contract and compute the bytecode root + verify_simple_contract_bytecode_with_configurables( + &test_contract_instance, + empty_bytecode, + my_configurables, + id, + simple_contract_instance, + ) + .await; + } +} diff --git a/tests/src/bytecode/tests/functions/verify_predicate_address.rs b/tests/src/bytecode/tests/functions/verify_predicate_address.rs index f33162d6..23b9ccea 100644 --- a/tests/src/bytecode/tests/functions/verify_predicate_address.rs +++ b/tests/src/bytecode/tests/functions/verify_predicate_address.rs @@ -1,8 +1,7 @@ use crate::bytecode::tests::utils::{ abi_calls::verify_predicate_address, test_helpers::{ - build_simple_configurables, defaults, predicate_bytecode, setup_predicate_from_file, - setup_predicate_from_file_with_configurable, simple_contract_bytecode, + predicate_bytecode, setup_predicate_from_file, simple_contract_bytecode, test_contract_instance, }, }; @@ -11,30 +10,6 @@ mod success { use super::*; - #[tokio::test] - async fn verify_predicate_address_with_configurables_from_bytecode() { - let (test_contract_instance, wallet) = test_contract_instance().await; - let (_contract_offset, predicate_offset, config_value) = defaults(); - - // Get the bytecode for the predicate - let file_bytecode = predicate_bytecode(); - - // Build the configurable changes - let my_configurables = build_simple_configurables(predicate_offset, config_value); - - // Create an instance of the predicate - let predicate_instance = - setup_predicate_from_file_with_configurable(wallet.clone(), config_value as u64).await; - - verify_predicate_address( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - predicate_instance.address().into(), - ) - .await; - } - #[tokio::test] async fn verify_predicate_address_from_bytecode() { let (test_contract_instance, wallet) = test_contract_instance().await; @@ -48,7 +23,6 @@ mod success { verify_predicate_address( &test_contract_instance, file_bytecode, - None, predicate_instance.address().into(), ) .await; @@ -59,53 +33,6 @@ mod revert { use super::*; - #[tokio::test] - #[should_panic] - async fn when_configurables_do_not_match() { - let (test_contract_instance, wallet) = test_contract_instance().await; - let (_contract_offset, predicate_offset, config_value) = defaults(); - - // Get the bytecode for the predicate - let file_bytecode = predicate_bytecode(); - - // Build the configurable changes - let my_configurables = build_simple_configurables(predicate_offset, config_value); - - // Create an instance of the predicate - let predicate_instance = - setup_predicate_from_file_with_configurable(wallet.clone(), (config_value as u64) + 1) - .await; - - verify_predicate_address( - &test_contract_instance, - file_bytecode, - Some(my_configurables), - predicate_instance.address().into(), - ) - .await; - } - - #[tokio::test] - #[should_panic] - async fn when_bytecode_is_empty_with_configurables() { - let (test_contract_instance, wallet) = test_contract_instance().await; - - let empty_bytecode: Vec = Vec::new(); - let my_configurables: Vec<(u64, Vec)> = Vec::new(); - - // Create an instance of the predicate - let predicate_instance = - setup_predicate_from_file_with_configurable(wallet.clone(), 0).await; - - verify_predicate_address( - &test_contract_instance, - empty_bytecode, - Some(my_configurables), - predicate_instance.address().into(), - ) - .await; - } - #[tokio::test] #[should_panic] async fn when_bytecode_does_not_match() { @@ -120,7 +47,6 @@ mod revert { verify_predicate_address( &test_contract_instance, file_bytecode, - None, predicate_instance.address().into(), ) .await; @@ -139,7 +65,6 @@ mod revert { verify_predicate_address( &test_contract_instance, empty_bytecode, - None, predicate_instance.address().into(), ) .await; diff --git a/tests/src/bytecode/tests/functions/verify_predicate_address_with_configurables.rs b/tests/src/bytecode/tests/functions/verify_predicate_address_with_configurables.rs new file mode 100644 index 00000000..d8930d2d --- /dev/null +++ b/tests/src/bytecode/tests/functions/verify_predicate_address_with_configurables.rs @@ -0,0 +1,88 @@ +use crate::bytecode::tests::utils::{ + abi_calls::verify_predicate_address_with_configurables, + test_helpers::{ + build_simple_configurables, defaults, predicate_bytecode, + setup_predicate_from_file_with_configurable, test_contract_instance, + }, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn verify_predicate_address_with_configurables_from_bytecode() { + let (test_contract_instance, wallet) = test_contract_instance().await; + let (_contract_offset, predicate_offset, config_value) = defaults(); + + // Get the bytecode for the predicate + let file_bytecode = predicate_bytecode(); + + // Build the configurable changes + let my_configurables = build_simple_configurables(predicate_offset, config_value); + + // Create an instance of the predicate + let predicate_instance = + setup_predicate_from_file_with_configurable(wallet.clone(), config_value as u64).await; + + verify_predicate_address_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + predicate_instance.address().into(), + ) + .await; + } +} + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic] + async fn when_configurables_do_not_match() { + let (test_contract_instance, wallet) = test_contract_instance().await; + let (_contract_offset, predicate_offset, config_value) = defaults(); + + // Get the bytecode for the predicate + let file_bytecode = predicate_bytecode(); + + // Build the configurable changes + let my_configurables = build_simple_configurables(predicate_offset, config_value); + + // Create an instance of the predicate + let predicate_instance = + setup_predicate_from_file_with_configurable(wallet.clone(), (config_value as u64) + 1) + .await; + + verify_predicate_address_with_configurables( + &test_contract_instance, + file_bytecode, + my_configurables, + predicate_instance.address().into(), + ) + .await; + } + + #[tokio::test] + #[should_panic] + async fn when_bytecode_is_empty() { + let (test_contract_instance, wallet) = test_contract_instance().await; + + let empty_bytecode: Vec = Vec::new(); + let my_configurables: Vec<(u64, Vec)> = Vec::new(); + + // Create an instance of the predicate + let predicate_instance = + setup_predicate_from_file_with_configurable(wallet.clone(), 0).await; + + verify_predicate_address_with_configurables( + &test_contract_instance, + empty_bytecode, + my_configurables, + predicate_instance.address().into(), + ) + .await; + } +} diff --git a/tests/src/bytecode/tests/utils/mod.rs b/tests/src/bytecode/tests/utils/mod.rs index 4299c46f..1cce15ba 100644 --- a/tests/src/bytecode/tests/utils/mod.rs +++ b/tests/src/bytecode/tests/utils/mod.rs @@ -66,11 +66,24 @@ pub mod abi_calls { pub async fn compute_predicate_address( contract: &BytecodeTestContract, bytecode: Vec, - configurables: Option)>>, ) -> Address { contract .methods() - .compute_predicate_address(bytecode, configurables) + .compute_predicate_address(bytecode) + .call() + .await + .unwrap() + .value + } + + pub async fn compute_predicate_address_with_configurables( + contract: &BytecodeTestContract, + bytecode: Vec, + configurables: Vec<(u64, Vec)>, + ) -> Address { + contract + .methods() + .compute_predicate_address_with_configurables(bytecode, configurables) .call() .await .unwrap() @@ -80,7 +93,6 @@ pub mod abi_calls { pub async fn compute_bytecode_root( contract: &BytecodeTestContract, bytecode: Vec, - configurables: Option)>>, ) -> Bits256 { contract .clone() @@ -89,7 +101,26 @@ pub mod abi_calls { max_tokens: 100_000, }) .methods() - .compute_bytecode_root(bytecode, configurables) + .compute_bytecode_root(bytecode) + .call() + .await + .unwrap() + .value + } + + pub async fn compute_bytecode_root_with_configurables( + contract: &BytecodeTestContract, + bytecode: Vec, + configurables: Vec<(u64, Vec)>, + ) -> Bits256 { + contract + .clone() + .with_encoder_config(EncoderConfig { + max_depth: 10, + max_tokens: 100_000, + }) + .methods() + .compute_bytecode_root_with_configurables(bytecode, configurables) .call() .await .unwrap() @@ -144,13 +175,12 @@ pub mod abi_calls { pub async fn verify_simple_contract_bytecode( contract: &BytecodeTestContract, bytecode: Vec, - configurables: Option)>>, contract_id: ContractId, simple_contract_instance: SimpleContract, ) -> CallResponse<()> { contract .methods() - .verify_contract_bytecode(contract_id, bytecode, configurables) + .verify_contract_bytecode(contract_id, bytecode) .with_contracts(&[&simple_contract_instance]) .call() .await @@ -160,7 +190,6 @@ pub mod abi_calls { pub async fn verify_complex_contract_bytecode( contract: &BytecodeTestContract, bytecode: Vec, - configurables: Option)>>, contract_id: ContractId, complex_contract_instance: ComplexContract, ) -> CallResponse<()> { @@ -171,7 +200,44 @@ pub mod abi_calls { max_tokens: 100_000, }) .methods() - .verify_contract_bytecode(contract_id, bytecode, configurables) + .verify_contract_bytecode(contract_id, bytecode) + .with_contracts(&[&complex_contract_instance]) + .call() + .await + .unwrap() + } + + pub async fn verify_simple_contract_bytecode_with_configurables( + contract: &BytecodeTestContract, + bytecode: Vec, + configurables: Vec<(u64, Vec)>, + contract_id: ContractId, + simple_contract_instance: SimpleContract, + ) -> CallResponse<()> { + contract + .methods() + .verify_contract_bytecode_with_configurables(contract_id, bytecode, configurables) + .with_contracts(&[&simple_contract_instance]) + .call() + .await + .unwrap() + } + + pub async fn verify_complex_contract_bytecode_with_configurables( + contract: &BytecodeTestContract, + bytecode: Vec, + configurables: Vec<(u64, Vec)>, + contract_id: ContractId, + complex_contract_instance: ComplexContract, + ) -> CallResponse<()> { + contract + .clone() + .with_encoder_config(EncoderConfig { + max_depth: 10, + max_tokens: 100_000, + }) + .methods() + .verify_contract_bytecode_with_configurables(contract_id, bytecode, configurables) .with_contracts(&[&complex_contract_instance]) .call() .await @@ -181,12 +247,25 @@ pub mod abi_calls { pub async fn verify_predicate_address( contract: &BytecodeTestContract, bytecode: Vec, - configurables: Option)>>, predicate_id: Address, ) -> CallResponse<()> { contract .methods() - .verify_predicate_address(predicate_id, bytecode, configurables) + .verify_predicate_address(predicate_id, bytecode) + .call() + .await + .unwrap() + } + + pub async fn verify_predicate_address_with_configurables( + contract: &BytecodeTestContract, + bytecode: Vec, + configurables: Vec<(u64, Vec)>, + predicate_id: Address, + ) -> CallResponse<()> { + contract + .methods() + .verify_predicate_address_with_configurables(predicate_id, bytecode, configurables) .call() .await .unwrap()