diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index 08e42bd259b..3ee245f806c 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -85,8 +85,8 @@ pub fn tx_type() -> Transaction { } const TIP_POLICY: u32 = 1u32 << 0; -const MATURITY_POLICY: u32 = 1u32 << 1; -const WITNESS_LIMIT_POLICY: u32 = 1u32 << 2; +const WITNESS_LIMIT_POLICY: u32 = 1u32 << 1; +const MATURITY_POLICY: u32 = 1u32 << 2; const MAX_FEE_POLICY: u32 = 1u32 << 3; /// Returns policies bits. It can be used to identify which policies are set. @@ -151,14 +151,14 @@ pub fn script_gas_limit() -> u64 { /// use std::tx::tx_maturity; /// /// fn foo() { -/// let maturity = tx_maturity(); +/// let maturity = tx_maturity().unwrap(); /// log(maturity); /// } /// ``` -pub fn tx_maturity() -> Option { +pub fn tx_maturity() -> Option { let bits = policies(); if bits & MATURITY_POLICY > 0 { - Some(__gtf::(0, GTF_POLICY_MATURITY)) + Some(__gtf::(0, GTF_POLICY_MATURITY)) } else { None } @@ -218,11 +218,7 @@ pub fn tx_max_fee() -> Option { /// /// # Returns /// -/// * [u64] - The script length for the transaction. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The script length for the transaction. /// /// # Examples /// @@ -230,14 +226,14 @@ pub fn tx_max_fee() -> Option { /// use std::tx::tx_script_length; /// /// fn foo() { -/// let script_length = tx_script_length(); +/// let script_length = tx_script_length().unwrap(); /// assert(script_length > 0); /// } /// ``` -pub fn tx_script_length() -> u64 { +pub fn tx_script_length() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT_LENGTH), - Transaction::Create => revert(0), + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT_LENGTH)), + Transaction::Create => None, } } @@ -247,24 +243,20 @@ pub fn tx_script_length() -> u64 { /// /// * [u64] - The script data length for the transaction. /// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. -/// /// # Examples /// /// ```sway /// use std::tx::tx_script_data_length; /// /// fn foo() { -/// let script_data_length = tx_script_data_length(); +/// let script_data_length = tx_script_data_length().unwrap(); /// assert(script_data_length > 0); /// } /// ``` -pub fn tx_script_data_length() -> u64 { +pub fn tx_script_data_length() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT_DATA_LENGTH), - Transaction::Create => revert(0), + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT_DATA_LENGTH)), + Transaction::Create => None, } } @@ -299,7 +291,7 @@ pub fn tx_witnesses_count() -> u64 { /// /// # Returns /// -/// * [u64] - The pointer to the witness at index `index`. +/// * [Option] - The pointer to the witness at index `index`. /// /// # Examples /// @@ -307,14 +299,18 @@ pub fn tx_witnesses_count() -> u64 { /// use std::tx::tx_witness_pointer; /// /// fn foo() { -/// let witness_pointer = tx_witness_pointer(0); -/// log(witness_pointer); +/// let witness_pointer = tx_witness_pointer(0).unwrap(); /// } /// ``` -pub fn tx_witness_pointer(index: u64) -> u64 { +#[allow(dead_code)] +fn tx_witness_pointer(index: u64) -> Option { + if index >= tx_witnesses_count() { + return None + } + match tx_type() { - Transaction::Script => __gtf::(index, GTF_SCRIPT_WITNESS_AT_INDEX), - Transaction::Create => __gtf::(index, GTF_CREATE_WITNESS_AT_INDEX), + Transaction::Script => Some(__gtf::(index, GTF_SCRIPT_WITNESS_AT_INDEX)), + Transaction::Create => Some(__gtf::(index, GTF_CREATE_WITNESS_AT_INDEX)), } } @@ -326,7 +322,7 @@ pub fn tx_witness_pointer(index: u64) -> u64 { /// /// # Returns /// -/// * [u64] - The length of the witness data at `index`. +/// * [Option<64>] - The length of the witness data at `index`. /// /// # Examples /// @@ -334,12 +330,16 @@ pub fn tx_witness_pointer(index: u64) -> u64 { /// use std::tx::tx_witness_data_length; /// /// fn foo() { -/// let witness_data_length = tx_witness_data_length(0); +/// let witness_data_length = tx_witness_data_length(0).unwrap(); /// log(witness_data_length); /// } /// ``` -pub fn tx_witness_data_length(index: u64) -> u64 { - __gtf::(index, GTF_WITNESS_DATA_LENGTH) +pub fn tx_witness_data_length(index: u64) -> Option { + if index >= tx_witnesses_count() { + return None + } + + Some(__gtf::(index, GTF_WITNESS_DATA_LENGTH)) } /// Get the witness data at `index`. @@ -350,7 +350,7 @@ pub fn tx_witness_data_length(index: u64) -> u64 { /// /// # Returns /// -/// * [T] - The witness data at `index`. +/// * [Option] - The witness data at `index`. /// /// # Examples /// @@ -358,15 +358,19 @@ pub fn tx_witness_data_length(index: u64) -> u64 { /// use std::tx::tx_witness_data; /// /// fn foo() { -/// let witness_data: u64 = tx_witness_data(0); +/// let witness_data: u64 = tx_witness_data(0).unwrap(); /// log(witness_data); /// } /// ``` -pub fn tx_witness_data(index: u64) -> T { +pub fn tx_witness_data(index: u64) -> Option { + if index >= tx_witnesses_count() { + return None + } + if __size_of::() == 1 { - __gtf::(index, GTF_WITNESS_DATA).add::(7).read::() + Some(__gtf::(index, GTF_WITNESS_DATA).add::(7).read::()) } else { - __gtf::(index, GTF_WITNESS_DATA).read::() + Some(__gtf::(index, GTF_WITNESS_DATA).read::()) } } @@ -374,11 +378,7 @@ pub fn tx_witness_data(index: u64) -> T { /// /// # Returns /// -/// * [raw_ptr] - The transaction script start pointer. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The transaction script start pointer. /// /// # Examples /// @@ -386,14 +386,14 @@ pub fn tx_witness_data(index: u64) -> T { /// use std::tx::tx_script_start_pointer; /// /// fn foo() { -/// let script_start_pointer = tx_script_start_pointer(); +/// let script_start_pointer = tx_script_start_pointer().unwrap(); /// log(script_start_pointer); /// } /// ``` -pub fn tx_script_start_pointer() -> raw_ptr { +fn tx_script_start_pointer() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT), - _ => revert(0), + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT)), + _ => None, } } @@ -401,11 +401,7 @@ pub fn tx_script_start_pointer() -> raw_ptr { /// /// # Returns /// -/// * [raw_ptr] - The transaction script data start pointer. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The transaction script data start pointer. /// /// # Examples /// @@ -413,17 +409,14 @@ pub fn tx_script_start_pointer() -> raw_ptr { /// use std::tx::tx_script_data_start_pointer; /// /// fn foo() { -/// let script_data_start_pointer = tx_script_data_start_pointer(); +/// let script_data_start_pointer = tx_script_data_start_pointer().unwrap(); /// log(script_data_start_pointer); /// } /// ``` -pub fn tx_script_data_start_pointer() -> raw_ptr { +fn tx_script_data_start_pointer() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT_DATA), - _ => { - // transaction-create has no script data length - revert(0); - } + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT_DATA)), + _ => None, } } @@ -436,11 +429,7 @@ pub fn tx_script_data_start_pointer() -> raw_ptr { /// /// # Returns /// -/// * [T] - The script data, typed. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The script data, typed. /// /// # Examples /// @@ -452,10 +441,14 @@ pub fn tx_script_data_start_pointer() -> raw_ptr { /// log(script_data); /// } /// ``` -pub fn tx_script_data() -> T { +pub fn tx_script_data() -> Option { let ptr = tx_script_data_start_pointer(); + if ptr.is_none() { + return None + } + // TODO some safety checks on the input data? We are going to assume it is the right type for now. - ptr.read::() + Some(ptr.unwrap().read::()) } /// Get the script bytecode. @@ -467,11 +460,7 @@ pub fn tx_script_data() -> T { /// /// # Returns /// -/// * [T] - The script bytecode. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The script bytecode. /// /// # Examples /// @@ -479,12 +468,15 @@ pub fn tx_script_data() -> T { /// use std::tx::tx_script_bytecode; /// /// fn foo() { -/// let script_bytecode: [u64; 64] = tx_script_bytecode(); +/// let script_bytecode: [u64; 64] = tx_script_bytecode().unwrap(); /// log(script_bytecode); /// } /// ``` -pub fn tx_script_bytecode() -> T { - tx_script_start_pointer().read::() +pub fn tx_script_bytecode() -> Option { + match tx_type() { + Transaction::Script => Some(tx_script_start_pointer().unwrap().read::()), + _ => None, + } } /// Get the hash of the script bytecode. @@ -492,11 +484,7 @@ pub fn tx_script_bytecode() -> T { /// /// # Returns /// -/// * [b256] - The hash of the script bytecode. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The hash of the script bytecode. /// /// # Examples /// @@ -504,25 +492,27 @@ pub fn tx_script_bytecode() -> T { /// use std::tx::tx_script_bytecode_hash; /// /// fn foo() { -/// let script_bytecode_hash: b256 = tx_script_bytecode_hash(); +/// let script_bytecode_hash: b256 = tx_script_bytecode_hash().unwrap(); /// assert(script_bytecode_hash == 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef); /// } /// ``` -pub fn tx_script_bytecode_hash() -> b256 { +pub fn tx_script_bytecode_hash() -> Option { match tx_type() { Transaction::Script => { // Get the script memory details let mut result_buffer = b256::zero(); - let script_length = tx_script_length(); - let script_ptr = tx_script_start_pointer(); + let script_length = tx_script_length().unwrap(); + let script_ptr = tx_script_start_pointer().unwrap(); // Run the hash opcode for the script in memory - asm(hash: result_buffer, ptr: script_ptr, len: script_length) { - s256 hash ptr len; - hash: b256 - } + Some( + asm(hash: result_buffer, ptr: script_ptr, len: script_length) { + s256 hash ptr len; + hash: b256 + }, + ) }, - _ => revert(0), + _ => None, } } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/gtf_intrinsic/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/gtf_intrinsic/src/main.sw index c02e50d0010..9f494313fee 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/gtf_intrinsic/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/gtf_intrinsic/src/main.sw @@ -4,7 +4,7 @@ use std::tx::*; fn main() -> u64 { assert(tx_witnesses_count() == 3); - assert(tx_witness_data::(1) == 1); - assert(tx_witness_data::(2) == 1234); + assert(tx_witness_data::(1).unwrap() == 1); + assert(tx_witness_data::(2).unwrap() == 1234); 0 } diff --git a/test/src/sdk-harness/test_artifacts/tx_contract/src/main.sw b/test/src/sdk-harness/test_artifacts/tx_contract/src/main.sw index 1be7402bf12..b19334dcfd7 100644 --- a/test/src/sdk-harness/test_artifacts/tx_contract/src/main.sw +++ b/test/src/sdk-harness/test_artifacts/tx_contract/src/main.sw @@ -16,23 +16,20 @@ use std::{ abi TxContractTest { fn get_tx_type() -> Transaction; - fn get_tx_tip() -> u64; + fn get_tx_tip() -> Option; fn get_script_gas_limit() -> u64; - fn get_tx_maturity() -> u64; - fn get_tx_witness_limit() -> u64; - fn get_tx_max_fee() -> u64; - fn get_tx_script_length() -> u64; - fn get_tx_script_data_length() -> u64; + fn get_tx_maturity() -> Option; + fn get_tx_witness_limit() -> Option; + fn get_tx_max_fee() -> Option; + fn get_tx_script_length() -> Option; + fn get_tx_script_data_length() -> Option; fn get_tx_inputs_count() -> u64; fn get_tx_outputs_count() -> u64; fn get_tx_witnesses_count() -> u64; - fn get_tx_witness_pointer(index: u64) -> u64; - fn get_tx_witness_data_length(index: u64) -> u64; - fn get_tx_witness_data(index: u64) -> B512; - fn get_tx_script_start_pointer() -> u64; - fn get_tx_script_data_start_pointer() -> u64; + fn get_tx_witness_data_length(index: u64) -> Option; + fn get_tx_witness_data(index: u64) -> Option; fn get_tx_id() -> b256; - fn get_tx_script_bytecode_hash() -> b256; + fn get_tx_script_bytecode_hash() -> Option; fn get_input_type(index: u64) -> Option; fn get_input_coin_owner(index: u64) -> Option
; @@ -56,25 +53,25 @@ impl TxContractTest for Contract { fn get_tx_type() -> Transaction { tx_type() } - fn get_tx_tip() -> u64 { - tx_tip().unwrap() + fn get_tx_tip() -> Option { + tx_tip() } fn get_script_gas_limit() -> u64 { script_gas_limit() } - fn get_tx_maturity() -> u64 { - tx_maturity().unwrap() + fn get_tx_maturity() -> Option { + tx_maturity() } - fn get_tx_witness_limit() -> u64 { - tx_witness_limit().unwrap() + fn get_tx_witness_limit() -> Option { + tx_witness_limit() } - fn get_tx_max_fee() -> u64 { - tx_max_fee().unwrap() + fn get_tx_max_fee() -> Option { + tx_max_fee() } - fn get_tx_script_length() -> u64 { + fn get_tx_script_length() -> Option { tx_script_length() } - fn get_tx_script_data_length() -> u64 { + fn get_tx_script_data_length() -> Option { tx_script_data_length() } fn get_tx_inputs_count() -> u64 { @@ -86,29 +83,16 @@ impl TxContractTest for Contract { fn get_tx_witnesses_count() -> u64 { tx_witnesses_count() } - fn get_tx_witness_pointer(index: u64) -> u64 { - tx_witness_pointer(index) - } - fn get_tx_witness_data_length(index: u64) -> u64 { + fn get_tx_witness_data_length(index: u64) -> Option { tx_witness_data_length(index) } - fn get_tx_witness_data(index: u64) -> B512 { + fn get_tx_witness_data(index: u64) -> Option { tx_witness_data(index) } - fn get_tx_script_start_pointer() -> u64 { - asm(ptr: tx_script_start_pointer()) { - ptr: u64 - } - } - fn get_tx_script_data_start_pointer() -> u64 { - asm(r1: tx_script_data_start_pointer()) { - r1: u64 - } - } fn get_tx_id() -> b256 { tx_id() } - fn get_tx_script_bytecode_hash() -> b256 { + fn get_tx_script_bytecode_hash() -> Option { tx_script_bytecode_hash() } fn get_input_type(index: u64) -> Option { diff --git a/test/src/sdk-harness/test_projects/script_bytecode/src/main.sw b/test/src/sdk-harness/test_projects/script_bytecode/src/main.sw index 68a45321117..2d9b577e1e9 100644 --- a/test/src/sdk-harness/test_projects/script_bytecode/src/main.sw +++ b/test/src/sdk-harness/test_projects/script_bytecode/src/main.sw @@ -15,7 +15,7 @@ impl Hash for [u64; 35] { fn main() -> b256 { // length of return array is script length padded to nearest full word - let script_bytecode: [u64; 35] = tx_script_bytecode(); + let script_bytecode: [u64; 35] = tx_script_bytecode().unwrap(); // Return the hash of the bytecode to compare sha256(script_bytecode) diff --git a/test/src/sdk-harness/test_projects/script_data/src/main.sw b/test/src/sdk-harness/test_projects/script_data/src/main.sw index e61c4f7a205..b31962c175c 100644 --- a/test/src/sdk-harness/test_projects/script_data/src/main.sw +++ b/test/src/sdk-harness/test_projects/script_data/src/main.sw @@ -4,13 +4,13 @@ use std::tx::tx_script_data; fn main() { // Reference type - let received: b256 = tx_script_data(); + let received: b256 = tx_script_data().unwrap(); let expected: b256 = 0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a; log(received); assert(received == expected); // Copy type - let received: u64 = tx_script_data(); + let received: u64 = tx_script_data().unwrap(); let expected: u64 = 17259675764097085660; log(received); assert(received == expected); diff --git a/test/src/sdk-harness/test_projects/tx_fields/mod.rs b/test/src/sdk-harness/test_projects/tx_fields/mod.rs index 12251d42da6..aaa56ec604b 100644 --- a/test/src/sdk-harness/test_projects/tx_fields/mod.rs +++ b/test/src/sdk-harness/test_projects/tx_fields/mod.rs @@ -1,5 +1,5 @@ use fuel_vm::fuel_crypto::Hasher; -use fuel_vm::fuel_tx::{Bytes32, ConsensusParameters, ContractId, Input as TxInput}; +use fuel_vm::fuel_tx::{ContractId, Input as TxInput}; use fuels::types::transaction_builders::TransactionBuilder; use fuels::{ accounts::{predicate::Predicate, wallet::WalletUnlocked, Account}, @@ -190,7 +190,6 @@ async fn setup_output_predicate() -> (WalletUnlocked, WalletUnlocked, Predicate, mod tx { use super::*; - use fuel_vm::fuel_tx::field::Script; use fuels::types::{coin_type::CoinType, transaction::Transaction}; #[tokio::test] @@ -220,7 +219,16 @@ mod tx { .await .unwrap(); - assert_eq!(result.value, tip); + assert_eq!(result.value, Some(tip)); + + let no_tip = contract_instance + .methods() + .get_tx_tip() + .call() + .await + .unwrap(); + + assert_eq!(no_tip.value, None); } #[tokio::test] @@ -251,7 +259,16 @@ mod tx { .call() .await .unwrap(); - assert_eq!(result.value, maturity); + assert_eq!(result.value, Some(maturity as u32)); + + // Assert none is returned with no maturity + let no_maturity = contract_instance + .methods() + .get_tx_maturity() + .call() + .await + .unwrap(); + assert_eq!(no_maturity.value, None); } #[tokio::test] @@ -266,7 +283,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(result.value, script_length); + assert_eq!(result.value, Some(script_length)); } #[tokio::test] @@ -281,7 +298,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(result.value, script_data_length); + assert_eq!(result.value, Some(script_data_length)); } #[tokio::test] @@ -357,20 +374,6 @@ mod tx { assert_eq!(result.value, witnesses_count); } - #[tokio::test] - async fn can_get_witness_pointer() { - let (contract_instance, _, _, _) = get_contracts(true).await; - - let response = contract_instance - .methods() - .get_tx_witness_pointer(0) - .call() - .await - .unwrap(); - - assert_eq!(response.value, 11024); - } - #[tokio::test] async fn can_get_witness_data_length() { let (contract_instance, _, _, _) = get_contracts(true).await; @@ -382,7 +385,7 @@ mod tx { .await .unwrap(); - assert_eq!(result.value, 64); + assert_eq!(result.value, Some(64)); } #[tokio::test] @@ -402,23 +405,7 @@ mod tx { .take_receipts_checked(None) .unwrap(); - assert_eq!(receipts[1].data().unwrap(), witnesses[0].as_vec()); - } - - #[tokio::test] - async fn can_get_script_start_offset() { - let (contract_instance, _, _, _) = get_contracts(true).await; - - let script_start_offset = ConsensusParameters::default().tx_params().tx_offset() - + fuel_vm::fuel_tx::Script::script_offset_static(); - - let result = contract_instance - .methods() - .get_tx_script_start_pointer() - .call() - .await - .unwrap(); - assert_eq!(result.value, script_start_offset as u64); + assert_eq!(receipts[1].data().unwrap()[8..72], *witnesses[0].as_vec()); } #[tokio::test] @@ -445,7 +432,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(Bytes32::from(result.value.0), hash); + assert_eq!(result.value.unwrap(), Bits256(*hash)); } #[tokio::test] @@ -468,18 +455,6 @@ mod tx { assert_eq!(receipts[1].data().unwrap(), byte_array); } - - #[tokio::test] - async fn can_get_get_tx_script_data_start_pointer() { - let (contract_instance, _, _, _) = get_contracts(true).await; - let result = contract_instance - .methods() - .get_tx_script_data_start_pointer() - .call() - .await - .unwrap(); - assert_eq!(result.value, 10392) - } } mod inputs {