From be5e5c82fa2f016fa40b21d97a06d07a9da81d93 Mon Sep 17 00:00:00 2001 From: Michael Benfield Date: Wed, 29 Nov 2023 14:40:30 -0700 Subject: [PATCH 01/11] examples/erc20 bugfix --- examples/erc20/Cargo.lock | 6 ++++-- examples/erc20/src/main.rs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/erc20/Cargo.lock b/examples/erc20/Cargo.lock index 52f131b8..578ef6e0 100644 --- a/examples/erc20/Cargo.lock +++ b/examples/erc20/Cargo.lock @@ -624,10 +624,11 @@ dependencies = [ [[package]] name = "stylus-proc" -version = "0.1.0" +version = "0.4.2" dependencies = [ "alloy-primitives", "alloy-sol-types", + "cfg-if 1.0.0", "convert_case 0.6.0", "lazy_static", "proc-macro2", @@ -640,10 +641,11 @@ dependencies = [ [[package]] name = "stylus-sdk" -version = "0.1.0" +version = "0.4.2" dependencies = [ "alloy-primitives", "alloy-sol-types", + "cfg-if 1.0.0", "derivative", "fnv", "hex", diff --git a/examples/erc20/src/main.rs b/examples/erc20/src/main.rs index e12bf2fb..6778a35b 100644 --- a/examples/erc20/src/main.rs +++ b/examples/erc20/src/main.rs @@ -48,7 +48,7 @@ impl Weth { self.erc20.burn(msg::sender(), amount)?; // send the user their funds - call::transfer_eth(self, msg::sender(), amount) + call::transfer_eth(msg::sender(), amount) } // sums numbers From e00002948201d0d386a2a4069b230b9a3dd5471e Mon Sep 17 00:00:00 2001 From: cygaar Date: Sun, 3 Dec 2023 20:13:02 -0500 Subject: [PATCH 02/11] Fix Address has_code check --- stylus-sdk/src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stylus-sdk/src/types.rs b/stylus-sdk/src/types.rs index 95692201..e0ada5b5 100644 --- a/stylus-sdk/src/types.rs +++ b/stylus-sdk/src/types.rs @@ -49,7 +49,7 @@ impl AddressVM for Address { fn has_code(&self) -> bool { let hash = self.codehash(); - hash.is_zero() + !hash.is_zero() || hash == b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") } } From d01b3130420c8b422cdb481430b63d4da7f1cc35 Mon Sep 17 00:00:00 2001 From: cygaar Date: Sun, 3 Dec 2023 21:10:58 -0500 Subject: [PATCH 03/11] Update --- stylus-sdk/src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stylus-sdk/src/types.rs b/stylus-sdk/src/types.rs index e0ada5b5..eb2594e7 100644 --- a/stylus-sdk/src/types.rs +++ b/stylus-sdk/src/types.rs @@ -50,6 +50,6 @@ impl AddressVM for Address { fn has_code(&self) -> bool { let hash = self.codehash(); !hash.is_zero() - || hash == b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") + && hash != b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") } } From bf46b3acd49fc7beeb72463ae45f5659775a1aef Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Tue, 5 Dec 2023 13:38:56 -0600 Subject: [PATCH 04/11] prevent leaking a secret key in CI --- .github/workflows/smoke-test.yml | 1 + ci/smoke_test.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/smoke-test.yml b/.github/workflows/smoke-test.yml index fbb5b1d5..419f86be 100644 --- a/.github/workflows/smoke-test.yml +++ b/.github/workflows/smoke-test.yml @@ -14,6 +14,7 @@ jobs: name: 'Test (Smoke)(${{ matrix.cfg_release_channel }})' env: CFG_RELEASE_CHANNEL: ${{ matrix.cfg_release_channel }} + PRIV_KEY: ${{ secrets.SEPOLIA_PRIVATE_KEY }} strategy: matrix: target: [wasm32-unknown-unknown] diff --git a/ci/smoke_test.sh b/ci/smoke_test.sh index 6c8b56c4..3f51078b 100755 --- a/ci/smoke_test.sh +++ b/ci/smoke_test.sh @@ -11,4 +11,4 @@ cargo stylus new counter cd counter echo "[workspace]" >> Cargo.toml -cargo stylus deploy -e http://localhost:8547 --private-key 0xb6b15c8cb491557369f3c7d2c287b053eb229daa9c22138887752191c9520659 +cargo stylus deploy -e http://localhost:8547 --private-key $PRIV_KEY From bdbdf688bb36212d1b0c73ca15f6f9c69f48544a Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 6 Dec 2023 19:45:36 -0700 Subject: [PATCH 05/11] Implement transient get/set bytes32 hostios --- stylus-sdk/src/hostio.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/stylus-sdk/src/hostio.rs b/stylus-sdk/src/hostio.rs index 7d2d861a..f8c2636c 100644 --- a/stylus-sdk/src/hostio.rs +++ b/stylus-sdk/src/hostio.rs @@ -52,6 +52,22 @@ extern "C" { /// [`SSTORE`]: https://www.evm.codes/#55 pub fn storage_store_bytes32(key: *const u8, value: *const u8); + /// Reads a 32-byte value from permanent storage. Stylus's storage format is identical to + /// that of the EVM. This means that, under the hood, this hostio is accessing the 32-byte + /// value stored in the EVM transient storage at offset `key`, which will be `0` when not + /// previously set. The semantics, then, are equivalent to that of the EVM's [`TLOAD`] opcode. + /// + /// [`TLOAD`]: https://www.evm.codes/#5c + pub fn storage_transient_load_bytes32(key: *const u8, dest: *mut u8); + + /// Stores a 32-byte value to permanent storage. Stylus's storage format is identical to that + /// of the EVM. This means that, under the hood, this hostio is storing a 32-byte value into + /// the EVM transient storage at offset `key`. Furthermore, refunds are tabulated exactly as in + /// the EVM. The semantics, then, are equivalent to that of the EVM's [`TSTORE`] opcode. + /// + /// [`TSTORE`]: https://www.evm.codes/#5d + pub fn storage_transient_store_bytes32(key: *const u8, value: *const u8); + /// Gets the basefee of the current block. The semantics are equivalent to that of the EVM's /// [`BASEFEE`] opcode. /// From 298c5310c9e78ca18cfed6094622b0e46c23b454 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 6 Dec 2023 21:00:48 -0700 Subject: [PATCH 06/11] make linter happy --- stylus-sdk/src/hostio.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stylus-sdk/src/hostio.rs b/stylus-sdk/src/hostio.rs index f8c2636c..b7a9728a 100644 --- a/stylus-sdk/src/hostio.rs +++ b/stylus-sdk/src/hostio.rs @@ -58,6 +58,7 @@ extern "C" { /// previously set. The semantics, then, are equivalent to that of the EVM's [`TLOAD`] opcode. /// /// [`TLOAD`]: https://www.evm.codes/#5c + #[allow(dead_code)] pub fn storage_transient_load_bytes32(key: *const u8, dest: *mut u8); /// Stores a 32-byte value to permanent storage. Stylus's storage format is identical to that @@ -66,6 +67,7 @@ extern "C" { /// the EVM. The semantics, then, are equivalent to that of the EVM's [`TSTORE`] opcode. /// /// [`TSTORE`]: https://www.evm.codes/#5d + #[allow(dead_code)] pub fn storage_transient_store_bytes32(key: *const u8, value: *const u8); /// Gets the basefee of the current block. The semantics are equivalent to that of the EVM's From a07656b7d10f0f2812dbb58fa4eed4de9b5a7af4 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Thu, 7 Dec 2023 15:33:54 -0600 Subject: [PATCH 07/11] edits --- ci/smoke_test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/smoke_test.sh b/ci/smoke_test.sh index 3f51078b..fa23fd4b 100755 --- a/ci/smoke_test.sh +++ b/ci/smoke_test.sh @@ -11,4 +11,4 @@ cargo stylus new counter cd counter echo "[workspace]" >> Cargo.toml -cargo stylus deploy -e http://localhost:8547 --private-key $PRIV_KEY +cargo stylus deploy --private-key $PRIV_KEY From c5b120fe184507fd54a3601f4af58dd4de328e3b Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Fri, 8 Dec 2023 13:48:24 -0700 Subject: [PATCH 08/11] add unsafe transient functions to storage --- stylus-sdk/src/storage/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/stylus-sdk/src/storage/mod.rs b/stylus-sdk/src/storage/mod.rs index 80853ba8..af005ac4 100644 --- a/stylus-sdk/src/storage/mod.rs +++ b/stylus-sdk/src/storage/mod.rs @@ -82,6 +82,26 @@ pub unsafe fn store_bytes32(key: U256, data: B256) { unsafe { hostio::storage_store_bytes32(B256::from(key).as_ptr(), data.as_ptr()) }; } +/// Retrieves a 32-byte EVM word from transient storage directly, bypassing all caches. +/// +/// # Safety +/// +/// May alias storage. +pub unsafe fn transient_load_bytes32(key: U256) -> B256 { + let mut data = B256::ZERO; + unsafe { hostio::storage_transient_load_bytes32(B256::from(key).as_ptr(), data.as_mut_ptr()) }; + data +} + +/// Stores a 32-byte EVM word to transient storage directly, bypassing all caches. +/// +/// # Safety +/// +/// May alias storage. +pub unsafe fn transient_store_bytes32(key: U256, data: B256) { + unsafe { hostio::storage_transient_store_bytes32(B256::from(key).as_ptr(), data.as_ptr()) }; +} + /// Overwrites the value in a cell. #[inline] fn overwrite_cell(cell: &mut OnceCell, value: T) { From 8f1e9220ec4ada124a994de50b249372099181b6 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 13 Dec 2023 18:51:18 -0700 Subject: [PATCH 09/11] Fix comment --- stylus-sdk/src/hostio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stylus-sdk/src/hostio.rs b/stylus-sdk/src/hostio.rs index b7a9728a..8c99dfb7 100644 --- a/stylus-sdk/src/hostio.rs +++ b/stylus-sdk/src/hostio.rs @@ -52,7 +52,7 @@ extern "C" { /// [`SSTORE`]: https://www.evm.codes/#55 pub fn storage_store_bytes32(key: *const u8, value: *const u8); - /// Reads a 32-byte value from permanent storage. Stylus's storage format is identical to + /// Reads a 32-byte value from transient storage. Stylus's storage format is identical to /// that of the EVM. This means that, under the hood, this hostio is accessing the 32-byte /// value stored in the EVM transient storage at offset `key`, which will be `0` when not /// previously set. The semantics, then, are equivalent to that of the EVM's [`TLOAD`] opcode. @@ -61,7 +61,7 @@ extern "C" { #[allow(dead_code)] pub fn storage_transient_load_bytes32(key: *const u8, dest: *mut u8); - /// Stores a 32-byte value to permanent storage. Stylus's storage format is identical to that + /// Stores a 32-byte value to transient storage. Stylus's storage format is identical to that /// of the EVM. This means that, under the hood, this hostio is storing a 32-byte value into /// the EVM transient storage at offset `key`. Furthermore, refunds are tabulated exactly as in /// the EVM. The semantics, then, are equivalent to that of the EVM's [`TSTORE`] opcode. From 7f87b7cfd21dfbe540b73b51ed5ee190ccea3d94 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 13 Dec 2023 20:05:19 -0700 Subject: [PATCH 10/11] Change transient hostio name --- stylus-sdk/src/hostio.rs | 4 ++-- stylus-sdk/src/storage/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stylus-sdk/src/hostio.rs b/stylus-sdk/src/hostio.rs index 8c99dfb7..94e24ee4 100644 --- a/stylus-sdk/src/hostio.rs +++ b/stylus-sdk/src/hostio.rs @@ -59,7 +59,7 @@ extern "C" { /// /// [`TLOAD`]: https://www.evm.codes/#5c #[allow(dead_code)] - pub fn storage_transient_load_bytes32(key: *const u8, dest: *mut u8); + pub fn transient_load_bytes32(key: *const u8, dest: *mut u8); /// Stores a 32-byte value to transient storage. Stylus's storage format is identical to that /// of the EVM. This means that, under the hood, this hostio is storing a 32-byte value into @@ -68,7 +68,7 @@ extern "C" { /// /// [`TSTORE`]: https://www.evm.codes/#5d #[allow(dead_code)] - pub fn storage_transient_store_bytes32(key: *const u8, value: *const u8); + pub fn transient_store_bytes32(key: *const u8, value: *const u8); /// Gets the basefee of the current block. The semantics are equivalent to that of the EVM's /// [`BASEFEE`] opcode. diff --git a/stylus-sdk/src/storage/mod.rs b/stylus-sdk/src/storage/mod.rs index af005ac4..46b1fde3 100644 --- a/stylus-sdk/src/storage/mod.rs +++ b/stylus-sdk/src/storage/mod.rs @@ -89,7 +89,7 @@ pub unsafe fn store_bytes32(key: U256, data: B256) { /// May alias storage. pub unsafe fn transient_load_bytes32(key: U256) -> B256 { let mut data = B256::ZERO; - unsafe { hostio::storage_transient_load_bytes32(B256::from(key).as_ptr(), data.as_mut_ptr()) }; + unsafe { hostio::transient_load_bytes32(B256::from(key).as_ptr(), data.as_mut_ptr()) }; data } @@ -99,7 +99,7 @@ pub unsafe fn transient_load_bytes32(key: U256) -> B256 { /// /// May alias storage. pub unsafe fn transient_store_bytes32(key: U256, data: B256) { - unsafe { hostio::storage_transient_store_bytes32(B256::from(key).as_ptr(), data.as_ptr()) }; + unsafe { hostio::transient_store_bytes32(B256::from(key).as_ptr(), data.as_ptr()) }; } /// Overwrites the value in a cell. From 910b386bae0140c23ea04b50b5d7790845dfd28f Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 13 Dec 2023 21:00:51 -0700 Subject: [PATCH 11/11] Address minor code review comments --- stylus-sdk/src/hostio.rs | 4 ++-- stylus-sdk/src/storage/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stylus-sdk/src/hostio.rs b/stylus-sdk/src/hostio.rs index 94e24ee4..5e3535b0 100644 --- a/stylus-sdk/src/hostio.rs +++ b/stylus-sdk/src/hostio.rs @@ -57,7 +57,7 @@ extern "C" { /// value stored in the EVM transient storage at offset `key`, which will be `0` when not /// previously set. The semantics, then, are equivalent to that of the EVM's [`TLOAD`] opcode. /// - /// [`TLOAD`]: https://www.evm.codes/#5c + /// [`TLOAD`]: https://eips.ethereum.org/EIPS/eip-1153 #[allow(dead_code)] pub fn transient_load_bytes32(key: *const u8, dest: *mut u8); @@ -66,7 +66,7 @@ extern "C" { /// the EVM transient storage at offset `key`. Furthermore, refunds are tabulated exactly as in /// the EVM. The semantics, then, are equivalent to that of the EVM's [`TSTORE`] opcode. /// - /// [`TSTORE`]: https://www.evm.codes/#5d + /// [`TSTORE`]: https://eips.ethereum.org/EIPS/eip-1153 #[allow(dead_code)] pub fn transient_store_bytes32(key: *const u8, value: *const u8); diff --git a/stylus-sdk/src/storage/mod.rs b/stylus-sdk/src/storage/mod.rs index 46b1fde3..99bf9239 100644 --- a/stylus-sdk/src/storage/mod.rs +++ b/stylus-sdk/src/storage/mod.rs @@ -86,7 +86,7 @@ pub unsafe fn store_bytes32(key: U256, data: B256) { /// /// # Safety /// -/// May alias storage. +/// May alias transient storage. pub unsafe fn transient_load_bytes32(key: U256) -> B256 { let mut data = B256::ZERO; unsafe { hostio::transient_load_bytes32(B256::from(key).as_ptr(), data.as_mut_ptr()) }; @@ -97,7 +97,7 @@ pub unsafe fn transient_load_bytes32(key: U256) -> B256 { /// /// # Safety /// -/// May alias storage. +/// May alias transient storage. pub unsafe fn transient_store_bytes32(key: U256, data: B256) { unsafe { hostio::transient_store_bytes32(B256::from(key).as_ptr(), data.as_ptr()) }; }