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

Fix codehash for EOA's and FixedBytes in sol_interface! #66

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stylus-proc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn solidity_type_info(ty: &Type) -> (Cow<'static, str>, Cow<'static, str>) {
Type::String(_) => simple!(String),
Type::Bytes(_) => simple!(Bytes),
Type::FixedBytes(_, size) => (
"stylus_sdk::abi::FixedBytesSolType<{size}>".into(),
format!("stylus_sdk::abi::FixedBytesSolType<{size}>").into(),
abi!("bytes[{size}]"),
),
Type::Uint(_, size) => {
Expand Down
21 changes: 16 additions & 5 deletions stylus-sdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
//! ```

use crate::hostio;
use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{b256, Address, B256, U256};

/// Trait that allows the [`Address`] type to inspect the corresponding account's balance and codehash.
pub trait AddressVM {
/// The balance in wei of the account.
fn balance(&self) -> U256;

/// The codehash of the contract at the given address, or `None` when an [`EOA`].
/// The codehash of the contract or [`EOA`] at the given address.
///
/// [`EOA`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
fn codehash(&self) -> Option<B256>;
fn codehash(&self) -> B256;

/// Determines if an account is an [`EOA`].
///
/// [`EOA`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
fn is_eoa(&self) -> bool;
}

impl AddressVM for Address {
Expand All @@ -34,9 +39,15 @@ impl AddressVM for Address {
U256::from_be_bytes(data)
}

fn codehash(&self) -> Option<B256> {
fn codehash(&self) -> B256 {
let mut data = [0; 32];
unsafe { hostio::account_codehash(self.0.as_ptr(), data.as_mut_ptr()) };
(data != [0; 32]).then_some(data.into())
data.into()
}

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