Skip to content

Commit

Permalink
add code_size, comments, and slight optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
rachel-bousfield committed Jan 30, 2024
1 parent 44ab101 commit c8951ea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
18 changes: 1 addition & 17 deletions stylus-sdk/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use crate::hostio::{self, wrap_hostio};
use alloc::{vec, vec::Vec};
use alloy_primitives::{Address, B256};
use alloy_primitives::B256;
use alloy_sol_types::{token::WordToken, SolEvent, TopicList};

/// Emits an evm log from combined topics and data.
Expand Down Expand Up @@ -57,22 +57,6 @@ pub fn pay_for_memory_grow(pages: u16) {
unsafe { hostio::pay_for_memory_grow(pages) }
}

/// Get the code associated with the given `address`.
///
/// Start at `offset` into the code and write at most `size` bytes.
pub fn ext_code(address: &Address, offset: usize, size: Option<usize>) -> Vec<u8> {
let size = if let Some(sz) = size {
sz
} else {
unsafe { hostio::account_code_size(address.0.as_ptr()) }
};
let mut data = vec![0; size];
let size_written =
unsafe { hostio::account_code(address.0.as_ptr(), offset, size, data.as_mut_ptr()) };
data.truncate(size_written);
data
}

wrap_hostio!(
/// Gets the amount of gas remaining. See [`Ink and Gas`] for more information on Stylus's compute pricing.
///
Expand Down
2 changes: 1 addition & 1 deletion stylus-sdk/src/storage/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl StorageKey for String {

impl StorageKey for Address {
fn to_slot(&self, root: B256) -> U256 {
let int: U160 = self.0.try_into().unwrap();
let int: U160 = self.0.into();
int.to_slot(root)
}
}
Expand Down
32 changes: 23 additions & 9 deletions stylus-sdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@
//! let balance = account.balance();
//! ```
use alloc::vec;
use alloc::vec::Vec;

use crate::hostio;
use alloc::vec::Vec;
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 code of the contract at the given address.
/// The account's code.
///
/// Returns an empty [`vec`] for [`EOAs`].
///
/// [`EOAs`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
fn code(&self) -> Vec<u8>;

/// The length of the account's code in bytes.
///
/// Returns `0` for [`EOAs`].
///
/// [`EOAs`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
fn code_size(&self) -> usize;

/// The codehash of the contract or [`EOA`] at the given address.
///
/// [`EOA`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
Expand All @@ -43,22 +52,27 @@ pub trait AddressVM {
impl AddressVM for Address {
fn balance(&self) -> U256 {
let mut data = [0; 32];
unsafe { hostio::account_balance(self.0.as_ptr(), data.as_mut_ptr()) };
unsafe { hostio::account_balance(self.as_ptr(), data.as_mut_ptr()) };
U256::from_be_bytes(data)
}

fn code(&self) -> Vec<u8> {
let size = unsafe { hostio::account_code_size(self.0.as_ptr()) };
let mut data = vec![0; size];
let size = self.code_size();
let mut data = Vec::with_capacity(size);
unsafe {
hostio::account_code(self.0.as_ptr(), 0, size, data.as_mut_ptr());
hostio::account_code(self.as_ptr(), 0, size, data.as_mut_ptr());
data.set_len(size);
}
data
}

fn code_size(&self) -> usize {
unsafe { hostio::account_code_size(self.as_ptr()) }
}

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

Expand Down

0 comments on commit c8951ea

Please sign in to comment.