From f05885672cf519915fe3eac2b5730131e0d5fa11 Mon Sep 17 00:00:00 2001 From: simonr0204 Date: Thu, 30 Jun 2022 11:06:58 -0400 Subject: [PATCH 1/9] add input/output type constants to std-lib --- sway-lib-std/src/constants.sw | 12 ++++++++++++ sway-lib-std/src/token.sw | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sway-lib-std/src/constants.sw b/sway-lib-std/src/constants.sw index d676d20d7c6..1a6807fa3c2 100644 --- a/sway-lib-std/src/constants.sw +++ b/sway-lib-std/src/constants.sw @@ -1,3 +1,15 @@ library constants; const NATIVE_ASSET_ID = 0x0000000000000000000000000000000000000000000000000000000000000000; + +// Input types +const INPUT_COIN = 0u8; +const INPUT_CONTRACT = 1u8; +const INPUT_MESSAGE = 2u8; + +// Output types +const OUTPUT_CONTRACT = 1u8; +const OUTPUT_MESSAGE = 2u8; +const OUTPUT_CHANGE = 3u8; +const OUTPUT_VARIABLE = 4u8; +const OUTPUT_CONTRACT_CREATED = 5u8; diff --git a/sway-lib-std/src/token.sw b/sway-lib-std/src/token.sw index 2543090fb8b..70c82e99737 100644 --- a/sway-lib-std/src/token.sw +++ b/sway-lib-std/src/token.sw @@ -2,6 +2,7 @@ library token; //! Functionality for performing common operations on tokens. use ::address::Address; +use ::constants::*; use ::contract_id::ContractId; use ::revert::revert; use ::tx::*; @@ -46,7 +47,6 @@ pub fn force_transfer(amount: u64, asset_id: ContractId, destination: ContractId /// Transfer `amount` coins of tof type `asset_id` and send them to the address `recipient`. pub fn transfer_to_output(amount: u64, asset_id: ContractId, recipient: Address) { - const OUTPUT_VARIABLE_TYPE: u8 = 4; // maintain a manual index as we only have `while` loops in sway atm: let mut index = 0; @@ -59,7 +59,7 @@ pub fn transfer_to_output(amount: u64, asset_id: ContractId, recipient: Address) let outputs_count = tx_outputs_count(); while index < outputs_count { let output_pointer = tx_output_pointer(index); - if tx_output_type(output_pointer) == OUTPUT_VARIABLE_TYPE && tx_output_amount(output_pointer) == 0 { + if tx_output_type(output_pointer) == OUTPUT_VARIABLE && tx_output_amount(output_pointer) == 0 { output_index = index; output_found = true; index = outputs_count; // break early and use the output we found From 7c34d743f90d06402f9f9532fc075615bcd6dda0 Mon Sep 17 00:00:00 2001 From: simonr0204 Date: Thu, 30 Jun 2022 11:31:32 -0400 Subject: [PATCH 2/9] changes receipts root test as per TODO instruction --- test/src/sdk-harness/test_projects/tx_fields/mod.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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 9c3b6cad763..07f9b978469 100644 --- a/test/src/sdk-harness/test_projects/tx_fields/mod.rs +++ b/test/src/sdk-harness/test_projects/tx_fields/mod.rs @@ -168,10 +168,7 @@ async fn can_get_receipts_root() { .await .unwrap(); - // TODO: `tx_receipts_root()` currently always returns zero because the receipts root is only - // updated in post script execution and its initial value is zero. Change the `assert_eq` below - // to `assert_ne` once this behavior is fixed: https://github.com/FuelLabs/fuel-vm/issues/125 - assert_eq!(Bytes32::from(result.value), zero_receipts_root); + assert_ne!(Bytes32::from(result.value), zero_receipts_root); } #[tokio::test] From a34e3aa59b264b580f52728c1512940862f609c2 Mon Sep 17 00:00:00 2001 From: simonr0204 Date: Thu, 30 Jun 2022 11:36:14 -0400 Subject: [PATCH 3/9] Revert "changes receipts root test as per TODO instruction" This reverts commit 7c34d743f90d06402f9f9532fc075615bcd6dda0. --- test/src/sdk-harness/test_projects/tx_fields/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 07f9b978469..9c3b6cad763 100644 --- a/test/src/sdk-harness/test_projects/tx_fields/mod.rs +++ b/test/src/sdk-harness/test_projects/tx_fields/mod.rs @@ -168,7 +168,10 @@ async fn can_get_receipts_root() { .await .unwrap(); - assert_ne!(Bytes32::from(result.value), zero_receipts_root); + // TODO: `tx_receipts_root()` currently always returns zero because the receipts root is only + // updated in post script execution and its initial value is zero. Change the `assert_eq` below + // to `assert_ne` once this behavior is fixed: https://github.com/FuelLabs/fuel-vm/issues/125 + assert_eq!(Bytes32::from(result.value), zero_receipts_root); } #[tokio::test] From 5bf3eeb172e634e22867638f96176e917ecb66fd Mon Sep 17 00:00:00 2001 From: simonr0204 Date: Thu, 30 Jun 2022 12:41:35 -0400 Subject: [PATCH 4/9] move input/output types to std::tx --- sway-lib-std/src/constants.sw | 12 ------------ sway-lib-std/src/token.sw | 1 - sway-lib-std/src/tx.sw | 13 +++++++++++++ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/sway-lib-std/src/constants.sw b/sway-lib-std/src/constants.sw index 1728be38e22..de58e645c1e 100644 --- a/sway-lib-std/src/constants.sw +++ b/sway-lib-std/src/constants.sw @@ -4,15 +4,3 @@ use ::contract_id::ContractId; // TODO: use ZERO_B256 const in BASE_ASSET_ID initialization when https://github.com/FuelLabs/sway/issues/2151 will be resolved const ZERO_B256 = 0x0000000000000000000000000000000000000000000000000000000000000000; const BASE_ASSET_ID = ~ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - -// Input types -const INPUT_COIN = 0u8; -const INPUT_CONTRACT = 1u8; -const INPUT_MESSAGE = 2u8; - -// Output types -const OUTPUT_CONTRACT = 1u8; -const OUTPUT_MESSAGE = 2u8; -const OUTPUT_CHANGE = 3u8; -const OUTPUT_VARIABLE = 4u8; -const OUTPUT_CONTRACT_CREATED = 5u8; diff --git a/sway-lib-std/src/token.sw b/sway-lib-std/src/token.sw index e7707336b66..f55ad4e107f 100644 --- a/sway-lib-std/src/token.sw +++ b/sway-lib-std/src/token.sw @@ -2,7 +2,6 @@ library token; use ::address::Address; -use ::constants::*; use ::contract_id::ContractId; use ::revert::revert; use ::tx::*; diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index e14b6af375b..303d641c642 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -42,6 +42,19 @@ const TX_WITNESSES_COUNT_OFFSET = 10312; const TX_RECEIPTS_ROOT_OFFSET = 10320; const TX_SCRIPT_START_OFFSET = 10352; +// Input types +const INPUT_COIN = 0u8; +const INPUT_CONTRACT = 1u8; +const INPUT_MESSAGE = 2u8; + +// Output types +const OUTPUT_CONTRACT = 1u8; +const OUTPUT_MESSAGE = 2u8; +const OUTPUT_CHANGE = 3u8; +const OUTPUT_VARIABLE = 4u8; +const OUTPUT_CONTRACT_CREATED = 5u8; + + /// Get the transaction type. pub fn tx_type() -> u8 { asm(r1, r2: TX_TYPE_OFFSET) { From 3316825b7003cf675e50e71960e9d2343b3bf876 Mon Sep 17 00:00:00 2001 From: simonr0204 Date: Thu, 30 Jun 2022 12:58:52 -0400 Subject: [PATCH 5/9] revert changes to constants.sw --- sway-lib-std/src/constants.sw | 3 ++- sway-lib-std/src/tx.sw | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sway-lib-std/src/constants.sw b/sway-lib-std/src/constants.sw index de58e645c1e..ec5aff82170 100644 --- a/sway-lib-std/src/constants.sw +++ b/sway-lib-std/src/constants.sw @@ -1,6 +1,7 @@ library constants; + use ::contract_id::ContractId; // TODO: use ZERO_B256 const in BASE_ASSET_ID initialization when https://github.com/FuelLabs/sway/issues/2151 will be resolved -const ZERO_B256 = 0x0000000000000000000000000000000000000000000000000000000000000000; const BASE_ASSET_ID = ~ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); +const ZERO_B256 = 0x0000000000000000000000000000000000000000000000000000000000000000; diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index 303d641c642..f8dcb8f67bf 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -54,7 +54,6 @@ const OUTPUT_CHANGE = 3u8; const OUTPUT_VARIABLE = 4u8; const OUTPUT_CONTRACT_CREATED = 5u8; - /// Get the transaction type. pub fn tx_type() -> u8 { asm(r1, r2: TX_TYPE_OFFSET) { From 2812ac022b478045e825643b340cdb3533fb7342 Mon Sep 17 00:00:00 2001 From: Simon Roberts Date: Sun, 3 Jul 2022 19:11:33 -0400 Subject: [PATCH 6/9] revert weird formatting --- sway-lib-std/src/u128.sw | 2 +- sway-lib-std/src/u256.sw | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 0e57b707d15..f7740d2f236 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -28,7 +28,7 @@ pub trait From { impl From for U128 { pub fn from(upper: u64, lower: u64) -> U128 { U128 { - upper, lower, + upper, lower, } } } diff --git a/sway-lib-std/src/u256.sw b/sway-lib-std/src/u256.sw index 683c8d37ec3..5d9547c3deb 100644 --- a/sway-lib-std/src/u256.sw +++ b/sway-lib-std/src/u256.sw @@ -29,7 +29,7 @@ pub trait From { impl From for U256 { pub fn from(a: u64, b: u64, c: u64, d: u64) -> U256 { U256 { - a, b, c, d, + a, b, c, d, } } } From d6860d057e91804f9310e0152f765e471fdee3fd Mon Sep 17 00:00:00 2001 From: Simon Roberts <46566889+simonr0204@users.noreply.github.com> Date: Sun, 3 Jul 2022 22:27:36 -0400 Subject: [PATCH 7/9] Update sway-lib-std/src/u128.sw Co-authored-by: Toby Hutton --- sway-lib-std/src/u128.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index f7740d2f236..82a7ce2dfaf 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -250,7 +250,7 @@ impl core::ops::Subtract for U128 { } U128 { - upper, lower, + upper, lower, } } } From 4ca8e97f7268d5a28bc4c3486c9d35310d0987fd Mon Sep 17 00:00:00 2001 From: Simon Roberts Date: Sun, 3 Jul 2022 22:41:12 -0400 Subject: [PATCH 8/9] output coin --- 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 f8dcb8f67bf..6594028a589 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -48,6 +48,7 @@ const INPUT_CONTRACT = 1u8; const INPUT_MESSAGE = 2u8; // Output types +const OUTPUT_COIN = 0u8; const OUTPUT_CONTRACT = 1u8; const OUTPUT_MESSAGE = 2u8; const OUTPUT_CHANGE = 3u8; From 1914855575178274003a2ff7848660fa887c6ebb Mon Sep 17 00:00:00 2001 From: Simon Roberts Date: Mon, 4 Jul 2022 09:33:18 -0400 Subject: [PATCH 9/9] fix tx lib use from incorrect merge --- sway-lib-std/src/token.sw | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sway-lib-std/src/token.sw b/sway-lib-std/src/token.sw index 889887acb09..3298860cff0 100644 --- a/sway-lib-std/src/token.sw +++ b/sway-lib-std/src/token.sw @@ -97,8 +97,7 @@ pub fn transfer_to_output(amount: u64, asset_id: ContractId, to: Address) { // variable output with a value of zero is by definition unused. let outputs_count = tx_outputs_count(); while index < outputs_count { - let output_pointer = tx_output_pointer(index); - if tx_output_type(output_pointer) == OUTPUT_VARIABLE && tx_output_amount(output_pointer) == 0 { + if tx_output_type(index) == OUTPUT_VARIABLE && tx_output_amount(index) == 0 { output_index = index; output_found = true; break; // break early and use the output we found