Skip to content

Commit

Permalink
return commitment for Envs with BlockHeaderCommit
Browse files Browse the repository at this point in the history
  • Loading branch information
Wollac committed Dec 16, 2024
1 parent fcd494e commit 5599b10
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
8 changes: 2 additions & 6 deletions examples/token-stats/host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,15 @@ async fn main() -> Result<()> {
rate
);

let commitment = env.commitment();

// Construct the input from the environment.
let input1 = env.into_input().await?;

// Create another EVM environment for that provider defaulting to the latest block.
let mut env = EthEvmEnv::builder().provider(provider).build().await?;
env = env.with_chain_spec(&ETH_MAINNET_CHAIN_SPEC);

// TODO: Make it easier to get the commitment from a HostEvmEnv
let commitment = input1
.clone()
.into_env()
.with_chain_spec(&ETH_MAINNET_CHAIN_SPEC)
.into_commitment();
// Preflight the verification of the commitment of the previous input.
SteelVerifier::preflight(&mut env)
.verify(&commitment)
Expand Down
36 changes: 23 additions & 13 deletions steel/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
ethereum::{EthBlockHeader, EthEvmEnv},
history::HistoryCommit,
host::db::ProviderDb,
ComposeInput, EvmBlockHeader, EvmEnv, EvmInput,
BlockHeaderCommit, Commitment, ComposeInput, EvmBlockHeader, EvmEnv, EvmInput,
};
use alloy::eips::eip1898::{HexStringMissingPrefixError, ParseBlockNumberError};
use alloy::{
Expand Down Expand Up @@ -162,6 +162,21 @@ where
}
}

impl<D, H: EvmBlockHeader, C> HostEvmEnv<D, H, C> {
/// Sets the chain ID and specification ID from the given chain spec.
///
/// This will panic when there is no valid specification ID for the current block.
pub fn with_chain_spec(mut self, chain_spec: &ChainSpec) -> Self {
self.cfg_env.chain_id = chain_spec.chain_id();
self.cfg_env.handler_cfg.spec_id = chain_spec
.active_fork(self.header.number(), self.header.timestamp())
.unwrap();
self.commit.config_id = chain_spec.digest();

self
}
}

impl<T, N, P, H> HostEvmEnv<AlloyDb<T, N, P>, H, ()>
where
T: Transport + Clone,
Expand All @@ -178,18 +193,13 @@ where
}
}

impl<D, H: EvmBlockHeader, C> HostEvmEnv<D, H, C> {
/// Sets the chain ID and specification ID from the given chain spec.
///
/// This will panic when there is no valid specification ID for the current block.
pub fn with_chain_spec(mut self, chain_spec: &ChainSpec) -> Self {
self.cfg_env.chain_id = chain_spec.chain_id();
self.cfg_env.handler_cfg.spec_id = chain_spec
.active_fork(self.header.number(), self.header.timestamp())
.unwrap();
self.commit.config_id = chain_spec.digest();

self
impl<D, H: EvmBlockHeader, C: Clone + BlockHeaderCommit<H>> HostEvmEnv<D, H, C> {
/// Returns the [Commitment] used to validate the environment.
pub fn commitment(&self) -> Commitment {
self.commit
.inner
.clone()
.commit(&self.header, self.commit.config_id)
}
}

Expand Down

0 comments on commit 5599b10

Please sign in to comment.