-
Notifications
You must be signed in to change notification settings - Fork 40
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
WEB3-276: feat: Add Account query functionality to Steel #414
Open
Wollac
wants to merge
2
commits into
main
Choose a base branch
from
feat/account-info
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Copyright 2025 RISC Zero, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Types related to account queries. | ||
pub use revm::primitives::{AccountInfo, Bytecode}; | ||
|
||
use crate::{state::WrapStateDb, EvmBlockHeader, GuestEvmEnv}; | ||
use alloy_primitives::Address; | ||
use anyhow::Result; | ||
use revm::Database as RevmDatabase; | ||
|
||
/// Represents an EVM account query. | ||
/// | ||
/// ### Usage | ||
/// - **Preflight calls on the Host:** To prepare the account query on the host environment and | ||
/// build the necessary proof, use [Account::preflight]. | ||
/// - **Calls in the Guest:** To initialize the account query in the guest, use [Account::new]. | ||
/// | ||
/// ### Examples | ||
/// ```rust,no_run | ||
/// # use risc0_steel::{Account, ethereum::EthEvmEnv}; | ||
/// # use alloy_primitives::address; | ||
/// | ||
/// # #[tokio::main(flavor = "current_thread")] | ||
/// # async fn main() -> anyhow::Result<()> { | ||
/// let account_address = address!("F977814e90dA44bFA03b6295A0616a897441aceC"); | ||
/// | ||
/// // Host: | ||
/// let url = "https://ethereum-rpc.publicnode.com".parse()?; | ||
/// let mut env = EthEvmEnv::builder().rpc(url).build().await?; | ||
/// let account = Account::preflight(account_address, &mut env); | ||
/// let info = account.bytecode(true).info().await?; | ||
/// | ||
/// let evm_input = env.into_input().await?; | ||
/// | ||
/// // Guest: | ||
/// let env = evm_input.into_env(); | ||
/// let account = Account::new(account_address, &env); | ||
/// let info = account.bytecode(true).info(); | ||
/// | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub struct Account<E> { | ||
address: Address, | ||
env: E, | ||
code: bool, | ||
} | ||
|
||
impl<E> Account<E> { | ||
/// Sets whether to fetch the bytecode for this account. | ||
/// | ||
/// If set to `true`, the bytecode will be fetched when calling [Account::info]. | ||
pub fn bytecode(mut self, code: bool) -> Self { | ||
self.code = code; | ||
self | ||
} | ||
} | ||
|
||
impl<'a, H: EvmBlockHeader> Account<&'a GuestEvmEnv<H>> { | ||
/// Constructor for querying an Ethereum account in the guest. | ||
pub fn new(address: Address, env: &'a GuestEvmEnv<H>) -> Self { | ||
Self { | ||
address, | ||
env, | ||
code: false, | ||
} | ||
} | ||
|
||
/// Attempts to get the [AccountInfo] for the corresponding account and returns an error if the | ||
/// query fails. | ||
/// | ||
/// In general, it's recommended to use [Account::info] unless explicit error handling is | ||
/// required. | ||
pub fn try_info(self) -> Result<AccountInfo> { | ||
let mut db = WrapStateDb::new(self.env.db()); | ||
let mut info = db.basic(self.address)?.unwrap_or_default(); | ||
if self.code && info.code.is_none() { | ||
let code = db.code_by_hash(info.code_hash)?; | ||
info.code = Some(code); | ||
} | ||
|
||
Ok(info) | ||
} | ||
|
||
/// Gets the [AccountInfo] for the corresponding account and panics on failure. | ||
/// | ||
/// A convenience wrapper for [Account::try_info], panicking if the query fails. Useful when | ||
/// success is expected. | ||
pub fn info(self) -> AccountInfo { | ||
self.try_info().unwrap() | ||
} | ||
} | ||
|
||
#[cfg(feature = "host")] | ||
mod host { | ||
use super::*; | ||
use crate::host::HostEvmEnv; | ||
use anyhow::Context; | ||
use std::error::Error as StdError; | ||
|
||
impl<'a, D, H, C> Account<&'a mut HostEvmEnv<D, H, C>> | ||
where | ||
D: RevmDatabase + Send + 'static, | ||
<D as RevmDatabase>::Error: StdError + Send + Sync + 'static, | ||
{ | ||
/// Constructor for preflighting queries to an Ethereum account on the host. | ||
/// | ||
/// Initializes the environment for querying account information, fetching necessary data | ||
/// via the [Provider], and generating a storage proof for any accessed elements using | ||
/// [EvmEnv::into_input]. | ||
/// | ||
/// [EvmEnv::into_input]: crate::EvmEnv::into_input | ||
/// [EvmEnv]: crate::EvmEnv | ||
/// [Provider]: alloy::providers::Provider | ||
pub fn preflight(address: Address, env: &'a mut HostEvmEnv<D, H, C>) -> Self { | ||
Self { | ||
address, | ||
env, | ||
code: false, | ||
} | ||
} | ||
|
||
/// Gets the [AccountInfo] for the corresponding account using an [EvmEnv] constructed with | ||
/// [Account::preflight]. | ||
/// | ||
/// [EvmEnv]: crate::EvmEnv | ||
pub async fn info(self) -> Result<AccountInfo> { | ||
log::info!("Executing preflight querying account {}", &self.address); | ||
|
||
let mut info = self | ||
.env | ||
.spawn_with_db(move |db| db.basic(self.address)) | ||
.await | ||
.context("failed to get basic account information")? | ||
.unwrap_or_default(); | ||
if self.code && info.code.is_none() { | ||
let code = self | ||
.env | ||
.spawn_with_db(move |db| db.code_by_hash(info.code_hash)) | ||
.await | ||
.context("failed to get account code by its hash")?; | ||
info.code = Some(code); | ||
} | ||
|
||
Ok(info) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Steel account query returns the corresponding
revm::primitives::AccountInfo
. This is a bit closer to the actual (revm) EVM execution, but it does not return the storage root (which is also inaccessible from inside the EVM) and the code is returned asrevm::primitives::Bytecode
instead of justBytes
. Alternatively, it may be preferable to define and return our own new type:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I did a little research on this: The problem is the storage_root. Unfortunately, this is not accessible in EVM, so there is no easy way to get it from RPC or the revm DB. The only way is to call an
eth_getProof
just to get this value. This is possible, but would require a bit more code.So do we think the storage_root is necessary? Otherwise the existing alternative would be sufficient and easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move on without the storage root then. If there becomes a use case for this, we can revisit, possibly adding a helper function of some sort to specifically get the storage root.