-
Notifications
You must be signed in to change notification settings - Fork 0
fix: move block env creation for host to env task #169
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ use crate::{ | |
| config::{BuilderConfig, HostProvider, RuProvider}, | ||
| tasks::env::SimEnv, | ||
| }; | ||
| use alloy::{eips::BlockId, network::Ethereum, primitives::BlockNumber}; | ||
| use alloy::{consensus::Header, eips::BlockId, network::Ethereum}; | ||
| use init4_bin_base::{ | ||
| deps::metrics::{counter, histogram}, | ||
| utils::calc::SlotCalculator, | ||
|
|
@@ -21,13 +21,9 @@ use tokio::{ | |
| task::JoinHandle, | ||
| }; | ||
| use tracing::{Instrument, Span, debug, instrument}; | ||
| use trevm::{ | ||
| Block, | ||
| revm::{ | ||
| context::BlockEnv, | ||
| database::{AlloyDB, WrapDatabaseAsync}, | ||
| inspector::NoOpInspector, | ||
| }, | ||
| use trevm::revm::{ | ||
| database::{AlloyDB, WrapDatabaseAsync}, | ||
| inspector::NoOpInspector, | ||
| }; | ||
|
|
||
| type HostAlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, HostProvider>>; | ||
|
|
@@ -58,6 +54,16 @@ pub struct SimResult { | |
| } | ||
|
|
||
| impl SimResult { | ||
| /// Get a reference to the previous host header. | ||
| pub const fn prev_host(&self) -> &Header { | ||
| self.sim_env.prev_host() | ||
| } | ||
|
|
||
| /// Get a reference to the previous signet header. | ||
| pub const fn prev_header(&self) -> &Header { | ||
| self.sim_env.prev_header() | ||
| } | ||
|
|
||
| /// Returns the block number of the built block. | ||
| pub const fn block_number(&self) -> u64 { | ||
| self.block.block_number() | ||
|
|
@@ -134,7 +140,7 @@ impl Simulator { | |
| sim_env: SimEnv, | ||
| ) -> eyre::Result<BuiltBlock> { | ||
| let concurrency_limit = self.config.concurrency_limit(); | ||
| let max_host_gas = self.config.max_host_gas(sim_env.prev_host.gas_limit); | ||
| let max_host_gas = self.config.max_host_gas(sim_env.prev_host().gas_limit); | ||
|
|
||
| let (rollup_env, host_env) = self.create_envs(constants, &sim_env).await; | ||
|
|
||
|
|
@@ -170,29 +176,25 @@ impl Simulator { | |
| HostEnv<HostAlloyDatabaseProvider, NoOpInspector>, | ||
| ) { | ||
| // Host DB and Env | ||
| let host_block_number = BlockNumber::from(sim_env.prev_host.number); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is where the bug was. need to add 1 to the prev host header's block number |
||
| let host_block_number = sim_env.host_block_number(); | ||
| let host_db = self.create_host_db(host_block_number).await; | ||
| let mut host_block_env = BlockEnv::default(); | ||
| sim_env.prev_host.fill_block_env(&mut host_block_env); | ||
|
|
||
| let host_env = HostEnv::<HostAlloyDatabaseProvider, NoOpInspector>::new( | ||
| host_db, | ||
| constants.clone(), | ||
| &self.config.host_cfg_env(), | ||
| &host_block_env, | ||
| sim_env.host_env(), | ||
| ); | ||
|
|
||
| // Rollup DB and Env | ||
| let rollup_block_number = BlockNumber::from(sim_env.prev_header.number); | ||
| let rollup_block_number = sim_env.block_number(); | ||
| let rollup_db = self.create_rollup_db(rollup_block_number); | ||
| let mut rollup_block_env = BlockEnv::default(); | ||
| sim_env.prev_header.fill_block_env(&mut rollup_block_env); | ||
|
|
||
| let rollup_env = RollupEnv::<RollupAlloyDatabaseProvider, NoOpInspector>::new( | ||
| rollup_db, | ||
| constants, | ||
| &self.config.ru_cfg_env(), | ||
| &rollup_block_env, | ||
| sim_env.rollup_env(), | ||
| ); | ||
|
|
||
| (rollup_env, host_env) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,31 +25,82 @@ pub struct EnvTask { | |
| ru_provider: RuProvider, | ||
| } | ||
|
|
||
| /// An environment for simulating a signet block. | ||
anna-carroll marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #[derive(Debug, Clone)] | ||
| pub struct Environment { | ||
| block_env: BlockEnv, | ||
| prev_header: Header, | ||
| } | ||
|
|
||
| impl Environment { | ||
| /// Create a new `Environment` with the given block environment and | ||
| /// previous header. | ||
| pub const fn new(block_env: BlockEnv, prev_header: Header) -> Self { | ||
| Self { block_env, prev_header } | ||
| } | ||
|
|
||
| /// Get a reference to the block environment. | ||
| pub const fn block_env(&self) -> &BlockEnv { | ||
| &self.block_env | ||
| } | ||
|
|
||
| /// Get a reference to the previous block header. | ||
| pub const fn prev_header(&self) -> &Header { | ||
| &self.prev_header | ||
| } | ||
|
|
||
| /// Create a new empty `Environment` for testing purposes. | ||
| #[doc(hidden)] | ||
| pub fn for_testing() -> Self { | ||
| Self { block_env: Default::default(), prev_header: Header::default() } | ||
| } | ||
| } | ||
|
|
||
| /// Contains a signet BlockEnv and its corresponding host Header. | ||
| #[derive(Debug, Clone)] | ||
| pub struct SimEnv { | ||
| /// The signet block environment, for rollup block simulation. | ||
| pub block_env: BlockEnv, | ||
| /// The header of the previous rollup block. | ||
| pub prev_header: Header, | ||
| /// The header of the previous host block. | ||
| pub prev_host: Header, | ||
| /// The host environment, for host block simulation. | ||
| pub host: Environment, | ||
|
|
||
| /// The signet environment, for rollup block simulation. | ||
anna-carroll marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub rollup: Environment, | ||
|
|
||
| /// A tracing span associated with this block | ||
| pub span: Span, | ||
| } | ||
|
|
||
| impl SimEnv { | ||
| /// Returns the block number of the signet block environment. | ||
| /// Get a reference to previous Signet header. | ||
| pub const fn prev_header(&self) -> &Header { | ||
| &self.rollup.prev_header | ||
| } | ||
|
|
||
| /// Get a reference to the previous host header | ||
| pub const fn prev_host(&self) -> &Header { | ||
| &self.host.prev_header | ||
| } | ||
|
|
||
| /// Get the block number of the signet block environment. | ||
| pub const fn block_number(&self) -> u64 { | ||
| self.prev_header.number.saturating_add(1) | ||
| self.prev_header().number.saturating_add(1) | ||
| } | ||
|
|
||
| /// Returns the host block number for the signet block environment. | ||
| /// Get the host block number for the signet block environment. | ||
| pub const fn host_block_number(&self) -> u64 { | ||
| self.prev_host.number.saturating_add(1) | ||
| self.prev_host().number.saturating_add(1) | ||
| } | ||
|
|
||
| /// Returns a reference to the tracing span associated with this block env. | ||
| /// Get a reference to the rollup block environment. | ||
| pub const fn rollup_env(&self) -> &BlockEnv { | ||
| &self.rollup.block_env | ||
| } | ||
|
|
||
| /// Get a reference to the host block environment. | ||
| pub const fn host_env(&self) -> &BlockEnv { | ||
| &self.host.block_env | ||
| } | ||
|
|
||
| /// Get a reference to the tracing span associated with this block env. | ||
| pub const fn span(&self) -> &Span { | ||
| &self.span | ||
| } | ||
|
|
@@ -71,8 +122,8 @@ impl EnvTask { | |
| } | ||
|
|
||
| /// Construct a [`BlockEnv`] by from the previous block header. | ||
| fn construct_block_env(&self, previous: &Header) -> BlockEnv { | ||
| BlockEnv { | ||
| fn construct_block_env(&self, previous: Header) -> Environment { | ||
| let env = BlockEnv { | ||
| number: U256::from(previous.number + 1), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can see here, the rollup block env was being created by adding 1 to the previous header's number. by refactoring to move the host block env creation to use the same function, we fix the bug |
||
| beneficiary: self.config.builder_rewards_address, | ||
| // NB: EXACTLY the same as the previous block | ||
|
|
@@ -87,7 +138,8 @@ impl EnvTask { | |
| excess_blob_gas: 0, | ||
| blob_gasprice: 0, | ||
| }), | ||
| } | ||
| }; | ||
| Environment::new(env, previous) | ||
| } | ||
|
|
||
| /// Returns a sender that sends [`SimEnv`] for communicating the next block environment. | ||
|
|
@@ -127,23 +179,17 @@ impl EnvTask { | |
| .inner; | ||
|
|
||
| // Construct the block env using the previous block header | ||
| let signet_env = self.construct_block_env(&rollup_header); | ||
| let signet_env = self.construct_block_env(rollup_header.into()); | ||
| let host_env = self.construct_block_env(prev_host); | ||
|
|
||
| span_debug!( | ||
| span, | ||
| signet_env_number = signet_env.number.to::<u64>(), | ||
| signet_env_basefee = signet_env.basefee, | ||
| signet_env_number = signet_env.block_env.number.to::<u64>(), | ||
| signet_env_basefee = signet_env.block_env.basefee, | ||
| "constructed signet block env" | ||
| ); | ||
|
|
||
| if sender | ||
| .send(Some(SimEnv { | ||
| span, | ||
| block_env: signet_env, | ||
| prev_header: rollup_header.inner, | ||
| prev_host, | ||
| })) | ||
| .is_err() | ||
| { | ||
| if sender.send(Some(SimEnv { span, rollup: signet_env, host: host_env })).is_err() { | ||
| // The receiver has been dropped, so we can stop the task. | ||
| tracing::debug!("receiver dropped, stopping task"); | ||
| break; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.