Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement transient get/set bytes32 hostios #92

Open
wants to merge 14 commits into
base: testnet-2
Choose a base branch
from
1 change: 1 addition & 0 deletions .github/workflows/smoke-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion ci/smoke_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 --private-key $PRIV_KEY
6 changes: 4 additions & 2 deletions examples/erc20/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/erc20/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions stylus-sdk/src/hostio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ 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 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.
///
/// [`TLOAD`]: https://www.evm.codes/#5c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's link the following for now since these opcodes don't yet exist
https://eips.ethereum.org/EIPS/eip-1153

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

#[allow(dead_code)]
pub fn storage_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
/// 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
#[allow(dead_code)]
pub fn storage_transient_store_bytes32(key: *const u8, value: *const u8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed on slack, let's rename these

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


/// Gets the basefee of the current block. The semantics are equivalent to that of the EVM's
/// [`BASEFEE`] opcode.
///
Expand Down
20 changes: 20 additions & 0 deletions stylus-sdk/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say May alias transient storage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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<T>(cell: &mut OnceCell<T>, value: T) {
Expand Down
4 changes: 2 additions & 2 deletions stylus-sdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl AddressVM for Address {

fn has_code(&self) -> bool {
let hash = self.codehash();
hash.is_zero()
|| hash == b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
!hash.is_zero()
&& hash != b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
}
}
Loading