Skip to content
Open
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
15 changes: 11 additions & 4 deletions bin/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use builder::{
config::BuilderConfig,
service::serve_builder,
tasks::{block::sim::Simulator, cache::CacheTasks, metrics::MetricsTask, submit::SubmitTask},
tasks::{
block::sim::Simulator, cache::CacheTasks, env::EnvTask, metrics::MetricsTask,
submit::SubmitTask,
},
};
use init4_bin_base::{
deps::tracing::{info, info_span},
Expand All @@ -26,9 +29,13 @@ async fn main() -> eyre::Result<()> {
let ru_provider = config.connect_ru_provider().await?;

// Spawn the EnvTask
let env_task = config.env_task();
let (block_env, env_jh) =
env_task.await.expect("ws validity checked in connect_ru_provider above").spawn();
let env_task = EnvTask::new(
config.clone(),
constants.clone(),
config.connect_host_provider().await?,
ru_provider.clone(),
);
let (block_env, env_jh) = env_task.spawn();

// Spawn the cache system
let cache_tasks = CacheTasks::new(config.clone(), block_env.clone());
Expand Down
11 changes: 1 addition & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
quincey::Quincey,
tasks::{block::cfg::SignetCfgEnv, env::EnvTask},
};
use crate::{quincey::Quincey, tasks::block::cfg::SignetCfgEnv};
use alloy::{
network::{Ethereum, EthereumWallet},
primitives::Address,
Expand Down Expand Up @@ -248,12 +245,6 @@ impl BuilderConfig {
Ok(Quincey::new_remote(client, url, token))
}

/// Create an [`EnvTask`] using this config.
pub async fn env_task(&self) -> eyre::Result<EnvTask> {
let ru_provider = self.connect_ru_provider().await?;
Ok(EnvTask::new(self.clone(), ru_provider))
}

/// Create a [`SignetCfgEnv`] using this config.
pub const fn cfg_env(&self) -> SignetCfgEnv {
SignetCfgEnv { chain_id: self.ru_chain_id }
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[macro_use]
mod macros;

/// Configuration for the Builder binary.
pub mod config;

Expand Down
34 changes: 34 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// Helper macro to log an event within a span that is not currently entered.
macro_rules! span_scoped {
($span:expr, $level:ident!($($arg:tt)*)) => {
$span.in_scope(|| {
$level!($($arg)*);
});
};
}

/// Helper macro to unwrap a result or continue the loop with a tracing event.
macro_rules! res_unwrap_or_continue {
($result:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
match $result {
Ok(value) => value,
Err(err) => {
span_scoped!($span, $level!(%err, $($arg)*));
continue;
}
}
};
}

/// Helper macro to unwrap an option or continue the loop with a tracing event.
macro_rules! opt_unwrap_or_continue {
($option:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
match $option {
Some(value) => value,
None => {
span_scoped!($span, $level!($($arg)*));
continue;
}
}
};
}
51 changes: 43 additions & 8 deletions src/tasks/env.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
use crate::config::{BuilderConfig, RuProvider};
use crate::config::{BuilderConfig, HostProvider, RuProvider};
use alloy::{
consensus::Header,
eips::eip1559::BaseFeeParams,
primitives::{B256, U256},
providers::Provider,
};
use init4_bin_base::deps::tracing::{Instrument, debug, error, info_span};
use signet_constants::SignetSystemConstants;
use tokio::{sync::watch, task::JoinHandle};
use tokio_stream::StreamExt;
use tracing::warn;
use trevm::revm::{context::BlockEnv, context_interface::block::BlobExcessGasAndPrice};

/// A task that constructs a BlockEnv for the next block in the rollup chain.
#[derive(Debug, Clone)]
pub struct EnvTask {
/// Builder configuration values.
config: BuilderConfig,
/// Rollup provider is used to get the latest rollup block header for simulation.

/// Signet system constants.
constants: SignetSystemConstants,

/// Host provider is used to get the latest host block header for
/// constructing the next block environment.
host_provider: HostProvider,

/// Rollup provider is used to get the latest rollup block header for
/// simulation.
ru_provider: RuProvider,
}

Expand All @@ -26,12 +37,19 @@ pub struct SimEnv {
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,
}

impl EnvTask {
/// Create a new [`EnvTask`] with the given config and providers.
pub const fn new(config: BuilderConfig, ru_provider: RuProvider) -> Self {
Self { config, ru_provider }
pub const fn new(
config: BuilderConfig,
constants: SignetSystemConstants,
host_provider: HostProvider,
ru_provider: RuProvider,
) -> Self {
Self { config, constants, host_provider, ru_provider }
}

/// Construct a [`BlockEnv`] by from the previous block header.
Expand Down Expand Up @@ -74,10 +92,23 @@ impl EnvTask {
while let Some(rollup_header) =
headers.next().instrument(info_span!("EnvTask::task_fut::stream")).await
{
let span =
info_span!("EnvTask::task_fut::loop", %rollup_header.hash, %rollup_header.number);
let host_block_number =
self.constants.rollup_block_to_host_block_num(rollup_header.number);

span.record("rollup_block_number", rollup_header.number);
let span = info_span!("EnvTask::task_fut::loop", %host_block_number, %rollup_header.hash, %rollup_header.number);

let host_block_opt = res_unwrap_or_continue!(
self.host_provider.get_block_by_number(host_block_number.into()).await,
span,
error!("error fetching previous host block - skipping block submission")
);
let prev_host = opt_unwrap_or_continue!(
host_block_opt,
span,
warn!("previous host block not found - skipping block submission")
)
.header
.inner;

// Construct the block env using the previous block header
let signet_env = self.construct_block_env(&rollup_header);
Expand All @@ -88,7 +119,11 @@ impl EnvTask {
);

if sender
.send(Some(SimEnv { block_env: signet_env, prev_header: rollup_header.inner }))
.send(Some(SimEnv {
block_env: signet_env,
prev_header: rollup_header.inner,
prev_host,
}))
.is_err()
{
// The receiver has been dropped, so we can stop the task.
Expand Down
54 changes: 3 additions & 51 deletions src/tasks/submit/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,6 @@ use signet_constants::SignetSystemConstants;
use std::{ops::Range, time::Instant};
use tokio::{sync::mpsc, task::JoinHandle};

/// Helper macro to log an event within a span that is not currently entered.
macro_rules! span_scoped {
($span:expr, $level:ident!($($arg:tt)*)) => {
$span.in_scope(|| {
$level!($($arg)*);
});
};
}

/// Helper macro to unwrap a result or continue the loop with a tracing event.
macro_rules! res_unwrap_or_continue {
($result:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
match $result {
Ok(value) => value,
Err(err) => {
span_scoped!($span, $level!(%err, $($arg)*));
continue;
}
}
};
}

/// Helper macro to unwrap an option or continue the loop with a tracing event.
macro_rules! opt_unwrap_or_continue {
($option:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
match $option {
Some(value) => value,
None => {
span_scoped!($span, $level!($($arg)*));
continue;
}
}
};
}

/// Helper macro to spawn a tokio task that broadcasts a tx.
macro_rules! spawn_provider_send {
($provider:expr, $tx:expr) => {
Expand Down Expand Up @@ -299,21 +264,6 @@ impl SubmitTask {
// drop guard before await
drop(guard);

// Fetch the previous host block, not the current host block which is currently being built
let prev_host_block = host_block_number - 1;

// If we encounter a provider error, log it and skip.
let prev_host_resp_opt = res_unwrap_or_continue!(
self.provider().get_block_by_number(prev_host_block.into()).await,
span,
error!("error fetching previous host block - skipping block submission")
);
let prev_host = opt_unwrap_or_continue!(
prev_host_resp_opt,
span,
warn!(prev_host_block, "previous host block not found - skipping block submission")
);

// Prep the span we'll use for the transaction submission
let submission_span = debug_span!(
parent: &span,
Expand All @@ -332,7 +282,9 @@ impl SubmitTask {
self.constants.clone(),
);
let bumpable = res_unwrap_or_continue!(
prep.prep_transaction(&prev_host.header).instrument(submission_span.clone()).await,
prep.prep_transaction(&sim_result.env.prev_host)
.instrument(submission_span.clone())
.await,
submission_span,
error!("failed to prepare transaction for submission - skipping block submission")
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn populate_initial_gas(req: &mut TransactionRequest, prev_header: &Header)
.expect("signet deployed after 1559 active") as u128;
let blob_basefee = prev_header
.next_block_blob_fee(BlobParams::prague())
.expect("signet deployed after 4844 active");
.expect("signet deployed after 7840 active");

req.max_priority_fee_per_gas = Some(STARTING_MPFPG);
req.max_fee_per_gas = Some((base_fee_per_gas * 1025 / 1024) + STARTING_MPFPG);
Expand Down
11 changes: 9 additions & 2 deletions tests/block_builder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloy::{
signers::local::PrivateKeySigner,
};
use builder::{
tasks::block::sim::Simulator,
tasks::{block::sim::Simulator, env::EnvTask},
test_utils::{new_signed_tx, setup_logging, setup_test_config, test_block_env},
};
use signet_sim::SimCache;
Expand Down Expand Up @@ -42,7 +42,14 @@ async fn test_handle_build() {
// Create a rollup provider
let ru_provider = RootProvider::<Ethereum>::new_http(anvil_instance.endpoint_url());

let block_env = config.env_task().await.unwrap().spawn().0;
let block_env = EnvTask::new(
config.clone(),
constants.clone(),
config.connect_host_provider().await.unwrap(),
ru_provider.clone(),
)
.spawn()
.0;

let block_builder = Simulator::new(&config, ru_provider.clone(), block_env);

Expand Down
11 changes: 9 additions & 2 deletions tests/cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use builder::{
tasks::cache::CacheTasks,
tasks::{cache::CacheTasks, env::EnvTask},
test_utils::{setup_logging, setup_test_config},
};
use init4_bin_base::deps::tracing::warn;
use signet_constants::SignetSystemConstants;
use std::time::Duration;

#[ignore = "integration test. This test will take >12 seconds to run, and requires Authz configuration env vars."]
Expand All @@ -12,7 +13,13 @@ async fn test_bundle_poller_roundtrip() -> eyre::Result<()> {

let config = setup_test_config().unwrap();

let (block_env, _jh) = config.env_task().await.unwrap().spawn();
let (block_env, _jh) = EnvTask::new(
config.clone(),
SignetSystemConstants::pecorino(),
config.connect_host_provider().await?,
config.connect_ru_provider().await?,
)
.spawn();
let cache_tasks = CacheTasks::new(config.clone(), block_env);
let cache_system = cache_tasks.spawn();

Expand Down
19 changes: 15 additions & 4 deletions tests/env.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use builder::test_utils::{setup_logging, setup_test_config};
use builder::{
tasks::env::EnvTask,
test_utils::{setup_logging, setup_test_config},
};
use signet_constants::SignetSystemConstants;

#[ignore = "integration test. This test will take between 0 and 12 seconds to run."]
#[tokio::test]
async fn test_bundle_poller_roundtrip() {
async fn test_bundle_poller_roundtrip() -> eyre::Result<()> {
setup_logging();

let config = setup_test_config().unwrap();
let env_task = config.env_task();
let (mut env_watcher, _jh) = env_task.await.unwrap().spawn();
let (mut env_watcher, _jh) = EnvTask::new(
config.clone(),
SignetSystemConstants::pecorino(),
config.connect_host_provider().await?,
config.connect_ru_provider().await?,
)
.spawn();

env_watcher.changed().await.unwrap();
let env = env_watcher.borrow_and_update();
assert!(env.as_ref().is_some(), "Env should be Some");

Ok(())
}