Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bin/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ async fn main() -> eyre::Result<()> {

// Set up the simulator
let sim = Simulator::new(&config, host_provider, ru_provider, block_env);
let build_jh =
sim.spawn_simulator_task(config.constants.clone(), cache_system.sim_cache, submit_channel);
let build_jh = sim.spawn_simulator_task(cache_system.sim_cache, submit_channel);

// Start the healthcheck server
let server = serve_builder(([0, 0, 0, 0], config.builder_port));
Expand Down
40 changes: 4 additions & 36 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
quincey::Quincey,
tasks::{
block::{cfg::SignetCfgEnv, sim::SimResult},
submit::FlashbotsTask,
},
tasks::{block::sim::SimResult, submit::FlashbotsTask},
};
use alloy::{
network::{Ethereum, EthereumWallet},
Expand Down Expand Up @@ -73,14 +70,6 @@ pub const DEFAULT_CONCURRENCY_LIMIT: usize = 8;
/// chain.
#[derive(Debug, Clone, FromEnv)]
pub struct BuilderConfig {
/// The chain ID of the host chain.
#[from_env(var = "HOST_CHAIN_ID", desc = "The chain ID of the host chain")]
pub host_chain_id: u64,

/// The chain ID of the rollup chain.
#[from_env(var = "RU_CHAIN_ID", desc = "The chain ID of the rollup chain")]
pub ru_chain_id: u64,

/// URL for Host RPC node.
#[from_env(
var = "HOST_RPC_URL",
Expand Down Expand Up @@ -121,17 +110,6 @@ pub struct BuilderConfig {
)]
pub flashbots_endpoint: Option<url::Url>,

/// Address of the Zenith contract on Host.
#[from_env(var = "ZENITH_ADDRESS", desc = "address of the Zenith contract on Host")]
pub zenith_address: Address,

/// Address of the Builder Helper contract on Host.
#[from_env(
var = "BUILDER_HELPER_ADDRESS",
desc = "address of the Builder Helper contract on Host"
)]
pub builder_helper_address: Address,

/// URL for remote Quincey Sequencer server to sign blocks.
/// NB: Disregarded if a sequencer_signer is configured.
#[from_env(
Expand Down Expand Up @@ -207,7 +185,7 @@ impl BuilderConfig {
static ONCE: tokio::sync::OnceCell<LocalOrAws> = tokio::sync::OnceCell::const_new();

ONCE.get_or_try_init(|| async {
LocalOrAws::load(&self.builder_key, Some(self.host_chain_id)).await
LocalOrAws::load(&self.builder_key, Some(self.constants.host_chain_id())).await
})
.await
.cloned()
Expand All @@ -217,7 +195,7 @@ impl BuilderConfig {
/// Connect to the Sequencer signer.
pub async fn connect_sequencer_signer(&self) -> eyre::Result<Option<LocalOrAws>> {
if let Some(sequencer_key) = &self.sequencer_key {
LocalOrAws::load(sequencer_key, Some(self.host_chain_id))
LocalOrAws::load(sequencer_key, Some(self.constants.host_chain_id()))
.await
.map_err(Into::into)
.map(Some)
Expand Down Expand Up @@ -279,7 +257,7 @@ impl BuilderConfig {

/// Connect to the Zenith instance, using the specified provider.
pub const fn connect_zenith(&self, provider: HostProvider) -> ZenithInstance {
Zenith::new(self.zenith_address, provider)
Zenith::new(self.constants.host_zenith(), provider)
}

/// Get an oauth2 token for the builder, starting the authenticator if it
Expand Down Expand Up @@ -309,16 +287,6 @@ impl BuilderConfig {
Ok(Quincey::new_remote(client, url, token))
}

/// Create a rollup [`SignetCfgEnv`] using this config.
pub const fn ru_cfg_env(&self) -> SignetCfgEnv {
SignetCfgEnv { chain_id: self.ru_chain_id }
}

/// Create a host [`SignetCfgEnv`] using this config.
pub const fn host_cfg_env(&self) -> SignetCfgEnv {
SignetCfgEnv { chain_id: self.host_chain_id }
}

/// Memoizes the concurrency limit for the current system. Uses [`std::thread::available_parallelism`] if no
/// value is set. If that for some reason fails, it returns the default concurrency limit.
pub fn concurrency_limit(&self) -> usize {
Expand Down
98 changes: 13 additions & 85 deletions src/tasks/block/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::{
config::{BuilderConfig, HostProvider, RuProvider},
tasks::env::SimEnv,
};
use alloy::{consensus::Header, eips::BlockId, network::Ethereum, providers::Provider};
use alloy::consensus::Header;
use init4_bin_base::{
deps::metrics::{counter, histogram},
utils::calc::SlotCalculator,
};
use signet_sim::{BlockBuild, BuiltBlock, HostEnv, RollupEnv, SimCache};
use signet_sim::{BlockBuild, BuiltBlock, SimCache};
use signet_types::constants::SignetSystemConstants;
use std::time::{Duration, Instant};
use tokio::{
Expand All @@ -21,13 +21,6 @@ use tokio::{
task::JoinHandle,
};
use tracing::{Instrument, Span, debug, instrument};
use trevm::revm::{
database::{AlloyDB, WrapDatabaseAsync},
inspector::NoOpInspector,
};

type HostAlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, HostProvider>>;
type RollupAlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, RuProvider>>;

/// `Simulator` is responsible for periodically building blocks and submitting them for
/// signing and inclusion in the blockchain. It wraps a rollup provider and a slot
Expand Down Expand Up @@ -112,6 +105,11 @@ impl Simulator {
&self.config.slot_calculator
}

/// Get the system constants.
pub const fn constants(&self) -> &SignetSystemConstants {
&self.config.constants
}

/// Handles building a single block.
///
/// Builds a block in the block environment with items from the simulation cache
Expand All @@ -134,16 +132,16 @@ impl Simulator {
))]
pub async fn handle_build(
&self,
constants: SignetSystemConstants,
sim_items: SimCache,
finish_by: Instant,
sim_env: SimEnv,
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 (rollup_env, host_env) = self.create_envs(constants, &sim_env).await;
debug!(rollup_block_height = ?rollup_env.block().number, host_block_height = ?host_env.block().number, "building with block environments");
let rollup_env = sim_env.sim_rollup_env(self.constants(), self.ru_provider.clone());

let host_env = sim_env.sim_host_env(self.constants(), self.host_provider.clone());

let block_build = BlockBuild::new(
rollup_env,
Expand All @@ -167,45 +165,6 @@ impl Simulator {
Ok(built_block)
}

// Helper to create rollup + host envs from the sim env.
async fn create_envs(
&self,
constants: SignetSystemConstants,
sim_env: &SimEnv,
) -> (
RollupEnv<RollupAlloyDatabaseProvider, NoOpInspector>,
HostEnv<HostAlloyDatabaseProvider, NoOpInspector>,
) {
// Host DB and Env
let host_db = self.create_host_db().await;

let host_block_number = sim_env.host_block_number();
let host_env_block_number = sim_env.host_env().number;
debug!(%host_block_number, %host_env_block_number, " host env comparisons");

let host_env = HostEnv::<HostAlloyDatabaseProvider, NoOpInspector>::new(
host_db,
constants.clone(),
&self.config.host_cfg_env(),
sim_env.host_env(),
);
debug!(?host_env, "created host env");

// Rollup DB and Env
let rollup_block_number = sim_env.rollup_block_number();
let rollup_db = self.create_rollup_db(rollup_block_number);

let rollup_env = RollupEnv::<RollupAlloyDatabaseProvider, NoOpInspector>::new(
rollup_db,
constants,
&self.config.ru_cfg_env(),
sim_env.rollup_env(),
);
debug!(?rollup_env, "created rollup env");

(rollup_env, host_env)
}

/// Spawns the simulator task, which ticks along the simulation loop
/// as it receives block environments.
///
Expand All @@ -220,13 +179,12 @@ impl Simulator {
/// A `JoinHandle` for the spawned task.
pub fn spawn_simulator_task(
self,
constants: SignetSystemConstants,
cache: SimCache,
submit_sender: mpsc::UnboundedSender<SimResult>,
) -> JoinHandle<()> {
debug!("starting simulator task");

tokio::spawn(async move { self.run_simulator(constants, cache, submit_sender).await })
tokio::spawn(async move { self.run_simulator(cache, submit_sender).await })
}

/// This function runs indefinitely, waiting for the block environment to be set and checking
Expand All @@ -247,7 +205,6 @@ impl Simulator {
/// - `submit_sender`: A channel sender used to submit built blocks.
async fn run_simulator(
mut self,
constants: SignetSystemConstants,
cache: SimCache,
submit_sender: mpsc::UnboundedSender<SimResult>,
) {
Expand All @@ -269,7 +226,7 @@ impl Simulator {
let sim_cache = cache.clone();

let Ok(block) = self
.handle_build(constants.clone(), sim_cache, finish_by, sim_env.clone())
.handle_build(sim_cache, finish_by, &sim_env)
.instrument(span.clone())
.await
.inspect_err(|err| span_error!(span, %err, "error during block build"))
Expand Down Expand Up @@ -302,33 +259,4 @@ impl Simulator {
let deadline = Instant::now() + Duration::from_secs(remaining);
deadline.max(Instant::now())
}

/// Creates an `AlloyDB` instance from the host provider.
async fn create_host_db(&self) -> HostAlloyDatabaseProvider {
let block_height = self.host_provider.get_block_number().await.unwrap();
debug!(%block_height, "creating host alloyDB at block height");

let alloy_db = AlloyDB::new(self.host_provider.clone(), BlockId::latest());

// Wrap the AlloyDB instance in a WrapDatabaseAsync and return it.
// This is safe to unwrap because the main function sets the proper runtime settings.
//
// See: https://docs.rs/tokio/latest/tokio/attr.main.html
WrapDatabaseAsync::new(alloy_db).unwrap()
}

/// Creates an `AlloyDB` instance from the rollup provider.
fn create_rollup_db(&self, latest_block_number: u64) -> RollupAlloyDatabaseProvider {
debug!(%latest_block_number, "creating rollup alloyDB at block height");

// Make an AlloyDB instance from the rollup provider with that latest block number
let alloy_db: AlloyDB<Ethereum, RuProvider> =
AlloyDB::new(self.ru_provider.clone(), BlockId::from(latest_block_number));

// Wrap the AlloyDB instance in a WrapDatabaseAsync and return it.
// This is safe to unwrap because the main function sets the proper runtime settings.
//
// See: https://docs.rs/tokio/latest/tokio/attr.main.html
WrapDatabaseAsync::new(alloy_db).unwrap()
}
}
Loading
Loading