From 41435a2da22e3aa47d367cc3a35a13598c047d56 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 15 Aug 2024 16:32:09 +0700 Subject: [PATCH 1/7] Increase saftey of transaction introspection --- sway-lib-std/src/tx.sw | 157 +++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 85 deletions(-) diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index 08e42bd259b..f6fa6c74fce 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,17 @@ 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 { +pub 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 +321,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 +329,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 +349,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 +357,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 +377,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 +385,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 { +pub 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 +400,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 +408,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 { +pub 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 +428,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 +440,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 +459,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 +467,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 +483,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 +491,25 @@ 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) { + Some(asm(hash: result_buffer, ptr: script_ptr, len: script_length) { s256 hash ptr len; hash: b256 - } + }) }, - _ => revert(0), + _ => None, } } From a1c264a0cfef01f6adf5163f3d11c3409f355a20 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 15 Aug 2024 16:32:19 +0700 Subject: [PATCH 2/7] Update tests --- .../test_artifacts/tx_contract/src/main.sw | 77 +++++++++++-------- .../test_projects/script_bytecode/src/main.sw | 2 +- .../test_projects/script_data/src/main.sw | 4 +- .../test_projects/tx_fields/mod.rs | 40 +++++++--- 4 files changed, 76 insertions(+), 47 deletions(-) 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 5755298265c..44593f43781 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,23 @@ 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_pointer(index: u64) -> Option; + fn get_tx_witness_data_length(index: u64) -> Option; + fn get_tx_witness_data(index: u64) -> Option; + fn get_tx_script_start_pointer() -> Option; + fn get_tx_script_data_start_pointer() -> 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) -> Input; fn get_tx_input_pointer(index: u64) -> u64; @@ -58,25 +58,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 { @@ -88,29 +88,40 @@ 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_pointer(index: u64) -> Option { + let ptr = tx_witness_pointer(index); + if ptr.is_none() { + return None + } + + Some(asm(r1: ptr.unwrap()) { r1: u64 }) } - 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_start_pointer() -> Option { + let ptr = tx_script_start_pointer(); + if ptr.is_none() { + return None } + + Some(asm(r1: ptr.unwrap()) { r1: u64 }) } - fn get_tx_script_data_start_pointer() -> u64 { - asm(r1: tx_script_data_start_pointer()) { - r1: u64 + fn get_tx_script_data_start_pointer() -> Option { + let ptr = tx_script_data_start_pointer(); + if ptr.is_none() { + return None } + + Some(asm(r1: ptr.unwrap()) { 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_tx_input_pointer(index: u64) -> u64 { 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 1eb4dda1a61..19f98a9965b 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::{ConsensusParameters, ContractId, Input as TxInput}; use fuels::types::transaction_builders::TransactionBuilder; use fuels::{ accounts::{predicate::Predicate, wallet::WalletUnlocked, Account}, @@ -220,7 +220,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 +260,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 +284,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(result.value, script_length); + assert_eq!(result.value, Some(script_length)); } #[tokio::test] @@ -281,7 +299,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(result.value, script_data_length); + assert_eq!(result.value, Some(script_data_length)); } #[tokio::test] @@ -368,7 +386,7 @@ mod tx { .await .unwrap(); - assert_eq!(response.value, 11024); + assert_eq!(response.value, Some(11024)); } #[tokio::test] @@ -382,7 +400,7 @@ mod tx { .await .unwrap(); - assert_eq!(result.value, 64); + assert_eq!(result.value, Some(64)); } #[tokio::test] @@ -402,7 +420,7 @@ mod tx { .take_receipts_checked(None) .unwrap(); - assert_eq!(receipts[1].data().unwrap(), witnesses[0].as_vec()); + assert_eq!(receipts[1].data().unwrap()[8..72], *witnesses[0].as_vec()); } #[tokio::test] @@ -418,7 +436,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(result.value, script_start_offset as u64); + assert_eq!(result.value, Some(script_start_offset as u64)); } #[tokio::test] @@ -445,7 +463,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(Bytes32::from(result.value.0), hash); + assert_eq!(result.value.unwrap(), Bits256(*hash)); } #[tokio::test] @@ -478,7 +496,7 @@ mod tx { .call() .await .unwrap(); - assert_eq!(result.value, 10392) + assert_eq!(result.value, Some(10392)) } } From b6102e745da27074ff3b81756430b073fe652a20 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 15 Aug 2024 18:51:43 +0700 Subject: [PATCH 3/7] Run formatter --- sway-lib-std/src/tx.sw | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index f6fa6c74fce..5fa66bd0bda 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -504,10 +504,12 @@ pub fn tx_script_bytecode_hash() -> Option { let script_ptr = tx_script_start_pointer().unwrap(); // Run the hash opcode for the script in memory - Some(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 + }, + ) }, _ => None, } From 1e1066244339c2aa6d6a56691a35f6ce2797d70b Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 15 Aug 2024 19:08:56 +0700 Subject: [PATCH 4/7] update e2e test --- .../should_pass/language/gtf_intrinsic/src/main.sw | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..d1ddee0ffe0 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(0) == 1234); 0 } From 7212f0ae333966edae899848d19ff547454fdc36 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 15 Aug 2024 19:10:11 +0700 Subject: [PATCH 5/7] Fix typo --- .../should_pass/language/gtf_intrinsic/src/main.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d1ddee0ffe0..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 @@ -5,6 +5,6 @@ use std::tx::*; fn main() -> u64 { assert(tx_witnesses_count() == 3); assert(tx_witness_data::(1).unwrap() == 1); - assert(tx_witness_data::(2).unwrap(0) == 1234); + assert(tx_witness_data::(2).unwrap() == 1234); 0 } From 8f8f4b49e82af2bcca68c9a76217a034ac6673ca Mon Sep 17 00:00:00 2001 From: bitzoic Date: Fri, 16 Aug 2024 14:59:58 +0700 Subject: [PATCH 6/7] Make raw_ptr return type functions private --- sway-lib-std/src/tx.sw | 6 +-- .../test_artifacts/tx_contract/src/main.sw | 27 ----------- .../test_projects/tx_fields/mod.rs | 45 +------------------ 3 files changed, 4 insertions(+), 74 deletions(-) diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index 5fa66bd0bda..3c5e07279e6 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -302,7 +302,7 @@ pub fn tx_witnesses_count() -> u64 { /// let witness_pointer = tx_witness_pointer(0).unwrap(); /// } /// ``` -pub fn tx_witness_pointer(index: u64) -> Option { +fn tx_witness_pointer(index: u64) -> Option { if index >= tx_witnesses_count() { return None } @@ -389,7 +389,7 @@ pub fn tx_witness_data(index: u64) -> Option { /// log(script_start_pointer); /// } /// ``` -pub fn tx_script_start_pointer() -> Option { +fn tx_script_start_pointer() -> Option { match tx_type() { Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT)), _ => None, @@ -412,7 +412,7 @@ pub fn tx_script_start_pointer() -> Option { /// log(script_data_start_pointer); /// } /// ``` -pub fn tx_script_data_start_pointer() -> Option { +fn tx_script_data_start_pointer() -> Option { match tx_type() { Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT_DATA)), _ => None, 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 44593f43781..26e0d395147 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 @@ -26,11 +26,8 @@ abi TxContractTest { 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) -> Option; fn get_tx_witness_data_length(index: u64) -> Option; fn get_tx_witness_data(index: u64) -> Option; - fn get_tx_script_start_pointer() -> Option; - fn get_tx_script_data_start_pointer() -> Option; fn get_tx_id() -> b256; fn get_tx_script_bytecode_hash() -> Option; @@ -88,36 +85,12 @@ impl TxContractTest for Contract { fn get_tx_witnesses_count() -> u64 { tx_witnesses_count() } - fn get_tx_witness_pointer(index: u64) -> Option { - let ptr = tx_witness_pointer(index); - if ptr.is_none() { - return None - } - - Some(asm(r1: ptr.unwrap()) { r1: u64 }) - } fn get_tx_witness_data_length(index: u64) -> Option { tx_witness_data_length(index) } fn get_tx_witness_data(index: u64) -> Option { tx_witness_data(index) } - fn get_tx_script_start_pointer() -> Option { - let ptr = tx_script_start_pointer(); - if ptr.is_none() { - return None - } - - Some(asm(r1: ptr.unwrap()) { r1: u64 }) - } - fn get_tx_script_data_start_pointer() -> Option { - let ptr = tx_script_data_start_pointer(); - if ptr.is_none() { - return None - } - - Some(asm(r1: ptr.unwrap()) { r1: u64 }) - } fn get_tx_id() -> b256 { tx_id() } 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 2aa50f46b1b..058b0c05b06 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::{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] @@ -375,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, Some(11024)); - } - #[tokio::test] async fn can_get_witness_data_length() { let (contract_instance, _, _, _) = get_contracts(true).await; @@ -423,22 +408,6 @@ mod tx { assert_eq!(receipts[1].data().unwrap()[8..72], *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, Some(script_start_offset as u64)); - } - #[tokio::test] async fn can_get_script_bytecode_hash() { let (contract_instance, _, _, _) = get_contracts(true).await; @@ -486,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, Some(10392)) - } } mod inputs { From 86e3a83eaf932b0353109eb8f075c817ae016b83 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Fri, 16 Aug 2024 15:51:27 +0700 Subject: [PATCH 7/7] Fix e2e tests --- sway-lib-std/src/tx.sw | 1 + 1 file changed, 1 insertion(+) diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index 3c5e07279e6..3ee245f806c 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -302,6 +302,7 @@ pub fn tx_witnesses_count() -> u64 { /// let witness_pointer = tx_witness_pointer(0).unwrap(); /// } /// ``` +#[allow(dead_code)] fn tx_witness_pointer(index: u64) -> Option { if index >= tx_witnesses_count() { return None