Skip to content

Commit c8951ea

Browse files
add code_size, comments, and slight optimization
1 parent 44ab101 commit c8951ea

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

stylus-sdk/src/evm.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use crate::hostio::{self, wrap_hostio};
1616
use alloc::{vec, vec::Vec};
17-
use alloy_primitives::{Address, B256};
17+
use alloy_primitives::B256;
1818
use alloy_sol_types::{token::WordToken, SolEvent, TopicList};
1919

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

60-
/// Get the code associated with the given `address`.
61-
///
62-
/// Start at `offset` into the code and write at most `size` bytes.
63-
pub fn ext_code(address: &Address, offset: usize, size: Option<usize>) -> Vec<u8> {
64-
let size = if let Some(sz) = size {
65-
sz
66-
} else {
67-
unsafe { hostio::account_code_size(address.0.as_ptr()) }
68-
};
69-
let mut data = vec![0; size];
70-
let size_written =
71-
unsafe { hostio::account_code(address.0.as_ptr(), offset, size, data.as_mut_ptr()) };
72-
data.truncate(size_written);
73-
data
74-
}
75-
7660
wrap_hostio!(
7761
/// Gets the amount of gas remaining. See [`Ink and Gas`] for more information on Stylus's compute pricing.
7862
///

stylus-sdk/src/storage/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl StorageKey for String {
186186

187187
impl StorageKey for Address {
188188
fn to_slot(&self, root: B256) -> U256 {
189-
let int: U160 = self.0.try_into().unwrap();
189+
let int: U160 = self.0.into();
190190
int.to_slot(root)
191191
}
192192
}

stylus-sdk/src/types.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,29 @@
1313
//! let balance = account.balance();
1414
//! ```
1515
16-
use alloc::vec;
17-
use alloc::vec::Vec;
18-
1916
use crate::hostio;
17+
use alloc::vec::Vec;
2018
use alloy_primitives::{b256, Address, B256, U256};
2119

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

27-
/// The code of the contract at the given address.
25+
/// The account's code.
26+
///
27+
/// Returns an empty [`vec`] for [`EOAs`].
28+
///
29+
/// [`EOAs`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
2830
fn code(&self) -> Vec<u8>;
2931

32+
/// The length of the account's code in bytes.
33+
///
34+
/// Returns `0` for [`EOAs`].
35+
///
36+
/// [`EOAs`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
37+
fn code_size(&self) -> usize;
38+
3039
/// The codehash of the contract or [`EOA`] at the given address.
3140
///
3241
/// [`EOA`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account
@@ -43,22 +52,27 @@ pub trait AddressVM {
4352
impl AddressVM for Address {
4453
fn balance(&self) -> U256 {
4554
let mut data = [0; 32];
46-
unsafe { hostio::account_balance(self.0.as_ptr(), data.as_mut_ptr()) };
55+
unsafe { hostio::account_balance(self.as_ptr(), data.as_mut_ptr()) };
4756
U256::from_be_bytes(data)
4857
}
4958

5059
fn code(&self) -> Vec<u8> {
51-
let size = unsafe { hostio::account_code_size(self.0.as_ptr()) };
52-
let mut data = vec![0; size];
60+
let size = self.code_size();
61+
let mut data = Vec::with_capacity(size);
5362
unsafe {
54-
hostio::account_code(self.0.as_ptr(), 0, size, data.as_mut_ptr());
63+
hostio::account_code(self.as_ptr(), 0, size, data.as_mut_ptr());
64+
data.set_len(size);
5565
}
5666
data
5767
}
5868

69+
fn code_size(&self) -> usize {
70+
unsafe { hostio::account_code_size(self.as_ptr()) }
71+
}
72+
5973
fn codehash(&self) -> B256 {
6074
let mut data = [0; 32];
61-
unsafe { hostio::account_codehash(self.0.as_ptr(), data.as_mut_ptr()) };
75+
unsafe { hostio::account_codehash(self.as_ptr(), data.as_mut_ptr()) };
6276
data.into()
6377
}
6478

0 commit comments

Comments
 (0)