Skip to content

Commit 19ec4f7

Browse files
committed
feat: adds host simulation to block build
- bumps bin-base to 0.15.1 - deprecates the BuilderHelper submission call in favor of the Zenith Block and Block Header - removes the BuilderHelper submit task - wires up the HostProvider into the Simulator for host simulation - cleans up unused code
1 parent 59afcac commit 19ec4f7

File tree

9 files changed

+515
-686
lines changed

9 files changed

+515
-686
lines changed

Cargo.lock

Lines changed: 428 additions & 641 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ name = "zenith-builder-example"
1818
path = "bin/builder.rs"
1919

2020
[dependencies]
21-
init4-bin-base = { version = "0.13.1", features = ["perms", "aws" ] }
21+
init4-bin-base = { version = "0.15.1", features = ["perms", "aws" ] }
2222

23-
signet-constants = { version = "0.11.2" }
24-
signet-sim = { version = "0.11.2" }
25-
signet-tx-cache = { version = "0.11.2" }
26-
signet-types = { version = "0.11.2" }
27-
signet-zenith = { version = "0.11.2" }
23+
signet-constants = { version = "0.13.0" }
24+
signet-sim = { version = "0.13.0" }
25+
signet-tx-cache = { version = "0.13.0" }
26+
signet-types = { version = "0.13.0" }
27+
signet-zenith = { version = "0.13.0" }
2828

29-
trevm = { version = "0.29.0", features = ["concurrent-db", "test-utils"] }
29+
trevm = { version = "0.30.1", features = ["concurrent-db", "test-utils"] }
3030

3131
alloy = { version = "1.0.37", features = [
3232
"full",
@@ -52,3 +52,15 @@ tracing = "0.1.41"
5252
tokio = { version = "1.36.0", features = ["full", "macros", "rt-multi-thread"] }
5353
tokio-stream = "0.1.17"
5454
url = "2.5.4"
55+
56+
[patch.crates-io]
57+
signet-constants = { path = "../signet-sdk/crates/constants" }
58+
signet-types = { path = "../signet-sdk/crates/types" }
59+
signet-zenith = { path = "../signet-sdk/crates/zenith" }
60+
signet-sim = { path = "../signet-sdk/crates/sim" }
61+
signet-evm = { path = "../signet-sdk/crates/evm" }
62+
signet-extract = { path = "../signet-sdk/crates/extract" }
63+
signet-journal = { path = "../signet-sdk/crates/journal" }
64+
signet-tx-cache = { path = "../signet-sdk/crates/tx-cache" }
65+
signet-bundle = { path = "../signet-sdk/crates/bundle" }
66+
init4-bin-base = { path = "../bin-base" }

bin/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn main() -> eyre::Result<()> {
3333
let cache_system = cache_tasks.spawn();
3434

3535
// Set up the metrics task
36-
let metrics = MetricsTask { host_provider };
36+
let metrics = MetricsTask { host_provider: host_provider.clone() };
3737
let (tx_channel, metrics_jh) = metrics.spawn();
3838

3939
// Set up the submit task. This will be either a Flashbots task or a
@@ -42,7 +42,7 @@ async fn main() -> eyre::Result<()> {
4242
let (submit_channel, submit_jh) = config.spawn_submit_task(tx_channel).await?;
4343

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

src/tasks/block/sim.rs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//! actor that handles the simulation of a stream of bundles and transactions
33
//! and turns them into valid Pecorino blocks for network submission.
44
use crate::{
5-
config::{BuilderConfig, RuProvider},
5+
config::{BuilderConfig, HostProvider, RuProvider},
66
tasks::env::SimEnv,
77
};
8-
use alloy::{eips::BlockId, network::Ethereum};
8+
use alloy::{eips::BlockId, network::Ethereum, primitives::BlockNumber};
99
use init4_bin_base::{
1010
deps::metrics::{counter, histogram},
1111
utils::calc::SlotCalculator,
1212
};
13-
use signet_sim::{BlockBuild, BuiltBlock, SimCache};
13+
use signet_sim::{BlockBuild, BuiltBlock, HostEnv, RollupEnv, SimCache};
1414
use signet_types::constants::SignetSystemConstants;
1515
use std::time::{Duration, Instant};
1616
use tokio::{
@@ -27,7 +27,8 @@ use trevm::revm::{
2727
inspector::NoOpInspector,
2828
};
2929

30-
type AlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, RuProvider>>;
30+
type HostAlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, HostProvider>>;
31+
type RollupAlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, RuProvider>>;
3132

3233
/// `Simulator` is responsible for periodically building blocks and submitting them for
3334
/// signing and inclusion in the blockchain. It wraps a rollup provider and a slot
@@ -36,6 +37,8 @@ type AlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, RuProvider>>;
3637
pub struct Simulator {
3738
/// Configuration for the builder.
3839
pub config: BuilderConfig,
40+
/// Host Provider to interact with the host chain.
41+
pub host_provider: HostProvider,
3942
/// A provider that cannot sign transactions, used for interacting with the rollup.
4043
pub ru_provider: RuProvider,
4144
/// The block configuration environment on which to simulate
@@ -88,10 +91,11 @@ impl Simulator {
8891
/// A new `Simulator` instance.
8992
pub fn new(
9093
config: &BuilderConfig,
94+
host_provider: HostProvider,
9195
ru_provider: RuProvider,
9296
sim_env: watch::Receiver<Option<SimEnv>>,
9397
) -> Self {
94-
Self { config: config.clone(), ru_provider, sim_env }
98+
Self { config: config.clone(), host_provider, ru_provider, sim_env }
9599
}
96100

97101
/// Get the slot calculator.
@@ -129,17 +133,33 @@ impl Simulator {
129133
let concurrency_limit = self.config.concurrency_limit();
130134

131135
// NB: Build AlloyDB from the previous block number's state, since block_env maps to the in-progress block
132-
let db = self.create_db(block_env.number.to::<u64>() - 1).unwrap();
136+
let latest_block_number = BlockNumber::from(block_env.number.to::<u64>() - 1);
133137

134-
let block_build: BlockBuild<_, NoOpInspector> = BlockBuild::new(
135-
db,
138+
let max_host_gas = 45_000_000u64;
139+
140+
let host_db = self.create_host_db(latest_block_number).await;
141+
let host_env = HostEnv::<_, NoOpInspector>::new(
142+
host_db,
143+
constants.clone(),
144+
&self.config.cfg_env(),
145+
&block_env,
146+
);
147+
let rollup_db = self.create_rollup_db(latest_block_number);
148+
let rollup_env = RollupEnv::<_, NoOpInspector>::new(
149+
rollup_db,
136150
constants,
137-
self.config.cfg_env(),
138-
block_env,
151+
&self.config.cfg_env(),
152+
&block_env,
153+
);
154+
155+
let block_build = BlockBuild::new(
156+
rollup_env,
157+
host_env,
139158
finish_by,
140159
concurrency_limit,
141160
sim_items,
142161
self.config.rollup_block_gas_limit,
162+
max_host_gas,
143163
);
144164

145165
let built_block = block_build.build().in_current_span().await;
@@ -149,7 +169,7 @@ impl Simulator {
149169
"block simulation completed",
150170
);
151171
counter!("signet.builder.built_blocks").increment(1);
152-
histogram!("signet.builder.built_blocks.tx_count").record(built_block.tx_count() as f64);
172+
histogram!("signet.builder.built_blocks.tx_count").record(built_block.tx_count() as u32);
153173

154174
Ok(built_block)
155175
}
@@ -251,12 +271,23 @@ impl Simulator {
251271
deadline.max(Instant::now())
252272
}
253273

274+
/// Creates an `AlloyDB` instnace from the host provider.
275+
async fn create_host_db(&self, latest_block_number: u64) -> HostAlloyDatabaseProvider {
276+
let alloy_db = AlloyDB::new(self.host_provider.clone(), BlockId::from(latest_block_number));
277+
278+
// Wrap the AlloyDB instance in a WrapDatabaseAsync and return it.
279+
// This is safe to unwrap because the main function sets the proper runtime settings.
280+
//
281+
// See: https://docs.rs/tokio/latest/tokio/attr.main.html
282+
WrapDatabaseAsync::new(alloy_db).unwrap()
283+
}
284+
254285
/// Creates an `AlloyDB` instance from the rollup provider.
255286
///
256287
/// # Returns
257288
///
258289
/// An `Option` containing the wrapped database or `None` if an error occurs.
259-
fn create_db(&self, latest_block_number: u64) -> Option<AlloyDatabaseProvider> {
290+
fn create_rollup_db(&self, latest_block_number: u64) -> RollupAlloyDatabaseProvider {
260291
// Make an AlloyDB instance from the rollup provider with that latest block number
261292
let alloy_db: AlloyDB<Ethereum, RuProvider> =
262293
AlloyDB::new(self.ru_provider.clone(), BlockId::from(latest_block_number));
@@ -265,7 +296,6 @@ impl Simulator {
265296
// This is safe to unwrap because the main function sets the proper runtime settings.
266297
//
267298
// See: https://docs.rs/tokio/latest/tokio/attr.main.html
268-
let wrapped_db: AlloyDatabaseProvider = WrapDatabaseAsync::new(alloy_db).unwrap();
269-
Some(wrapped_db)
299+
WrapDatabaseAsync::new(alloy_db).unwrap()
270300
}
271301
}

src/tasks/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl EnvTask {
6565
pub const fn new(
6666
config: BuilderConfig,
6767
host_provider: HostProvider,
68-
ru_provider: RuProvider,
68+
ru_provider: RuProvider,
6969
) -> Self {
7070
Self { config, host_provider, ru_provider }
7171
}

src/tasks/submit/flashbots.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl FlashbotsTask {
9191

9292
let bundle_body = sim_result
9393
.block
94-
.host_txns()
94+
.host_transactions()
9595
.iter()
9696
.cloned()
9797
.chain(std::iter::once(tx_bytes))

src/tasks/submit/prep.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use alloy::{
1414
use init4_bin_base::deps::metrics::counter;
1515
use signet_sim::BuiltBlock;
1616
use signet_types::{SignRequest, SignResponse};
17-
use signet_zenith::BundleHelper;
17+
use signet_zenith::Zenith;
1818
use tracing::{Instrument, debug};
1919

2020
/// Preparation logic for transactions issued to the host chain by the
@@ -92,26 +92,32 @@ impl<'a> SubmitPrep<'a> {
9292
self.quincey_resp().await.map(|resp| &resp.sig).map(utils::extract_signature_components)
9393
}
9494

95-
/// Converts the fills in the block to a vector of `FillPermit2`.
96-
fn fills(&self) -> Vec<BundleHelper::FillPermit2> {
97-
utils::convert_fills(self.block)
98-
}
99-
10095
/// Encodes the sidecar and then builds the 4844 blob transaction from the provided header and signature values.
10196
async fn build_blob_tx(&self) -> eyre::Result<TransactionRequest> {
10297
let (v, r, s) = self.quincey_signature().await?;
10398

104-
// Build the block header
105-
let header = BundleHelper::BlockHeader {
106-
hostBlockNumber: self.sig_request().host_block_number,
99+
let header = Zenith::BlockHeader {
107100
rollupChainId: U256::from(self.config.constants.ru_chain_id()),
101+
hostBlockNumber: self.sig_request().host_block_number,
108102
gasLimit: self.sig_request().gas_limit,
109103
rewardAddress: self.sig_request().ru_reward_address,
110104
blockDataHash: *self.block.contents_hash(),
111105
};
112-
debug!(?header.hostBlockNumber, "built rollup block header");
113-
114-
let data = BundleHelper::submitCall { fills: self.fills(), header, v, r, s }.abi_encode();
106+
debug!(?header.hostBlockNumber, "built zenith block header");
107+
108+
let data = Zenith::submitBlockCall {
109+
header,
110+
v,
111+
r,
112+
s,
113+
_4: self
114+
.block
115+
.host_transactions()
116+
.iter()
117+
.flat_map(|b| b.iter().cloned())
118+
.collect::<alloy::primitives::Bytes>(),
119+
}
120+
.abi_encode();
115121

116122
let sidecar = self.block.encode_blob::<SimpleCoder>().build()?;
117123

src/utils.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ use alloy::{
44
primitives::{B256, Signature},
55
rpc::types::TransactionRequest,
66
};
7-
use signet_sim::BuiltBlock;
8-
use signet_zenith::BundleHelper::FillPermit2;
9-
10-
// This function converts &[SignedFill] into [FillPermit2]
11-
pub(crate) fn convert_fills(block: &BuiltBlock) -> Vec<FillPermit2> {
12-
block.host_fills().iter().map(FillPermit2::from).collect()
13-
}
147

158
/// Extracts the components of a signature.
169
/// Currently alloy has no function for extracting the components of a signature.

tests/block_builder_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ async fn test_handle_build() {
4141

4242
// Create a rollup provider
4343
let ru_provider = RootProvider::<Ethereum>::new_http(anvil_instance.endpoint_url());
44+
let host_provider = config.connect_host_provider().await.unwrap();
4445

4546
let block_env = EnvTask::new(
4647
config.clone(),
@@ -50,7 +51,7 @@ async fn test_handle_build() {
5051
.spawn()
5152
.0;
5253

53-
let block_builder = Simulator::new(&config, ru_provider.clone(), block_env);
54+
let block_builder = Simulator::new(&config, host_provider, ru_provider.clone(), block_env);
5455

5556
// Setup a sim cache
5657
let sim_items = SimCache::new();

0 commit comments

Comments
 (0)