Skip to content

Commit bf75913

Browse files
committed
tests are passing now
1 parent 9d812ce commit bf75913

File tree

5 files changed

+146
-181
lines changed

5 files changed

+146
-181
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ tokio = { version = "1.36.0", features = ["full", "macros", "rt-multi-thread"] }
6161

6262
async-trait = "0.1.80"
6363
oauth2 = "4.4.2"
64+
tracing-subscriber = "0.3.19"

bin/builder.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use builder::{
66
submit::SubmitTask, tx_poller,
77
},
88
};
9+
use signet_sim::SimCache;
910
use tokio::select;
1011

1112
#[tokio::main]
@@ -47,10 +48,12 @@ async fn main() -> eyre::Result<()> {
4748

4849
let authenticator_jh = authenticator.spawn();
4950

50-
let (submit_channel, submit_jh) = submit.spawn();
51+
let (_submit_channel, submit_jh) = submit.spawn();
5152

52-
let build_jh =
53-
builder.spawn(constants, ru_provider, tx_receiver, bundle_receiver, submit_channel);
53+
let sim_items = SimCache::new();
54+
let sim_cache_jh = builder.spawn_cache_task(tx_receiver, bundle_receiver, sim_items.clone());
55+
56+
let build_jh = builder.handle_build(constants, ru_provider, sim_items.clone());
5457

5558
let port = config.builder_port;
5659
let server = serve_builder_with_span(([0, 0, 0, 0], port), span);
@@ -62,6 +65,9 @@ async fn main() -> eyre::Result<()> {
6265
_ = bundle_poller_jh => {
6366
tracing::info!("bundle_poller finished");
6467
},
68+
_ = sim_cache_jh => {
69+
tracing::info!("sim cache task finished");
70+
}
6571
_ = submit_jh => {
6672
tracing::info!("submit finished");
6773
},

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ pub mod utils;
2929

3030
// Anonymous import suppresses warnings about unused imports.
3131
use openssl as _;
32+
use tracing_subscriber as _;

src/tasks/block.rs

Lines changed: 92 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@ use crate::{
99
use alloy::{consensus::TxEnvelope, eips::BlockId, providers::Provider};
1010
use signet_sim::{BlockBuild, BuiltBlock, SimCache, SimItem};
1111
use signet_types::config::SignetSystemConstants;
12-
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
1312
use tokio::{
1413
select,
15-
sync::mpsc::{self, UnboundedReceiver},
16-
task::JoinHandle,
14+
sync::mpsc::{self},
1715
};
18-
use tracing::{Instrument, info};
1916
use trevm::{
20-
NoopBlock, NoopCfg,
17+
NoopBlock,
2118
revm::{
19+
context::CfgEnv,
2220
database::{AlloyDB, WrapDatabaseAsync},
2321
inspector::NoOpInspector,
22+
primitives::hardfork::SpecId,
2423
},
2524
};
2625

2726
/// Ethereum's slot time in seconds.
2827
pub const ETHEREUM_SLOT_TIME: u64 = 12;
2928

29+
/// Pecorino Chain ID
30+
pub const PECORINO_CHAIN_ID: u64 = 14174;
31+
3032
/// BlockBuilder is a task that periodically builds a block then sends it for
3133
/// signing and submission.
3234
#[derive(Debug)]
@@ -56,155 +58,65 @@ impl BlockBuilder {
5658
}
5759
}
5860

59-
// calculate the duration in seconds until the beginning of the next block slot.
60-
fn secs_to_next_slot(&self) -> u64 {
61-
let curr_timestamp: u64 = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
62-
let current_slot_time = (curr_timestamp - self.config.chain_offset) % ETHEREUM_SLOT_TIME;
63-
(ETHEREUM_SLOT_TIME - current_slot_time) % ETHEREUM_SLOT_TIME
64-
}
65-
66-
// add a buffer to the beginning of the block slot.
67-
fn secs_to_next_target(&self) -> u64 {
68-
self.secs_to_next_slot() + self.config.target_slot_time
69-
}
70-
7161
/// Spawn the block builder task, returning the inbound channel to it, and
7262
/// a handle to the running task.
73-
pub fn spawn(
74-
self,
63+
pub async fn handle_build(
64+
&self,
7565
constants: SignetSystemConstants,
7666
ru_provider: WalletlessProvider,
77-
mut tx_receiver: UnboundedReceiver<TxEnvelope>,
67+
sim_items: SimCache,
68+
) -> Result<BuiltBlock, mpsc::error::SendError<BuiltBlock>> {
69+
let db = create_db(ru_provider).await.unwrap();
70+
71+
// TODO: add real slot calculator
72+
let finish_by = std::time::Instant::now() + std::time::Duration::from_millis(200);
73+
74+
dbg!(sim_items.read_best(2));
75+
dbg!(sim_items.len());
76+
77+
let block_build: BlockBuild<_, NoOpInspector> = BlockBuild::new(
78+
db,
79+
constants,
80+
PecorinoCfg {},
81+
NoopBlock,
82+
finish_by,
83+
self.config.concurrency_limit,
84+
sim_items,
85+
self.config.rollup_block_gas_limit,
86+
);
87+
88+
let block = block_build.build().await;
89+
90+
Ok(block)
91+
}
92+
93+
/// Spawns a task that receives transactions and bundles from the pollers and
94+
/// adds them to the shared cache.
95+
pub async fn spawn_cache_task(
96+
&self,
97+
mut tx_receiver: mpsc::UnboundedReceiver<TxEnvelope>,
7898
mut bundle_receiver: mpsc::UnboundedReceiver<Bundle>,
79-
block_sender: mpsc::UnboundedSender<BuiltBlock>,
80-
) -> JoinHandle<()> {
81-
println!("GOT HERE 0");
82-
tokio::spawn(
83-
async move {
84-
// Create a sim item handler
85-
let sim_items = SimCache::new();
86-
println!("got here 1");
87-
88-
tokio::spawn({
89-
let sim_items = sim_items.clone();
90-
async move {
91-
println!("starting up the receiver");
92-
loop {
93-
select! {
94-
tx = tx_receiver.recv() => {
95-
if let Some(tx) = tx {
96-
println!("received transaction {}", tx.hash());
97-
sim_items.add_item(signet_sim::SimItem::Tx(tx.into()));
98-
}
99-
}
100-
bundle = bundle_receiver.recv() => {
101-
if let Some(bundle) = bundle {
102-
println!("received bundle {}", bundle.id);
103-
sim_items.add_item(SimItem::Bundle(bundle.bundle));
104-
}
105-
}
106-
}
107-
}
99+
cache: SimCache,
100+
) {
101+
loop {
102+
select! {
103+
maybe_tx = tx_receiver.recv() => {
104+
if let Some(tx) = maybe_tx {
105+
cache.add_item(SimItem::Tx(tx));
106+
}
107+
}
108+
maybe_bundle = bundle_receiver.recv() => {
109+
if let Some(bundle) = maybe_bundle {
110+
cache.add_item(SimItem::Bundle(bundle.bundle));
108111
}
109-
});
110-
111-
println!("starting the block builder loop");
112-
113-
loop {
114-
println!("STARTING 1");
115-
// Calculate the next wake up
116-
let buffer = self.secs_to_next_target();
117-
let deadline = Instant::now().checked_add(Duration::from_secs(buffer)).unwrap();
118-
println!("DEADLINE {:?}", deadline.clone());
119-
120-
tokio::time::sleep(Duration::from_secs(buffer)).await;
121-
122-
// Fetch latest block number from the rollup
123-
let db = match create_db(&ru_provider).await {
124-
Some(value) => value,
125-
None => {
126-
println!("failed to get a database - check runtime type");
127-
continue;
128-
}
129-
};
130-
131-
println!("SIM ITEMS LEN {}", sim_items.len());
132-
133-
tokio::spawn({
134-
let outbound = block_sender.clone();
135-
let sim_items = sim_items.clone();
136-
137-
async move {
138-
let block_builder: BlockBuild<_, NoOpInspector> = BlockBuild::new(
139-
db,
140-
constants,
141-
NoopCfg,
142-
NoopBlock,
143-
deadline,
144-
self.config.concurrency_limit.clone(),
145-
sim_items.clone(),
146-
self.config.rollup_block_gas_limit.clone(),
147-
);
148-
149-
let block = block_builder.build().await;
150-
println!("GOT BLOCK {}", block.contents_hash());
151-
152-
if let Err(e) = outbound.send(block) {
153-
println!("failed to send built block: {}", e);
154-
tracing::error!(error = %e, "failed to send built block");
155-
} else {
156-
info!("block build cycle complete");
157-
}
158-
}
159-
});
160112
}
161113
}
162-
.in_current_span(),
163-
)
114+
}
164115
}
165116
}
166117

167118
/// Creates an AlloyDB from a rollup provider
168-
async fn create_db(
169-
ru_provider: &alloy::providers::fillers::FillProvider<
170-
alloy::providers::fillers::JoinFill<
171-
alloy::providers::Identity,
172-
alloy::providers::fillers::JoinFill<
173-
alloy::providers::fillers::GasFiller,
174-
alloy::providers::fillers::JoinFill<
175-
alloy::providers::fillers::BlobGasFiller,
176-
alloy::providers::fillers::JoinFill<
177-
alloy::providers::fillers::NonceFiller,
178-
alloy::providers::fillers::ChainIdFiller,
179-
>,
180-
>,
181-
>,
182-
>,
183-
alloy::providers::RootProvider,
184-
>,
185-
) -> Option<
186-
WrapDatabaseAsync<
187-
AlloyDB<
188-
alloy::network::Ethereum,
189-
alloy::providers::fillers::FillProvider<
190-
alloy::providers::fillers::JoinFill<
191-
alloy::providers::Identity,
192-
alloy::providers::fillers::JoinFill<
193-
alloy::providers::fillers::GasFiller,
194-
alloy::providers::fillers::JoinFill<
195-
alloy::providers::fillers::BlobGasFiller,
196-
alloy::providers::fillers::JoinFill<
197-
alloy::providers::fillers::NonceFiller,
198-
alloy::providers::fillers::ChainIdFiller,
199-
>,
200-
>,
201-
>,
202-
>,
203-
alloy::providers::RootProvider,
204-
>,
205-
>,
206-
>,
207-
> {
119+
async fn create_db(ru_provider: WalletlessProvider) -> Option<WrapAlloyDatabaseAsync> {
208120
let latest = match ru_provider.get_block_number().await {
209121
Ok(block_number) => block_number,
210122
Err(e) => {
@@ -220,3 +132,41 @@ async fn create_db(
220132
});
221133
Some(wrapped_db)
222134
}
135+
136+
/// The wrapped alloy database type that is compatible with Db + DatabaseRef
137+
type WrapAlloyDatabaseAsync = WrapDatabaseAsync<
138+
AlloyDB<
139+
alloy::network::Ethereum,
140+
alloy::providers::fillers::FillProvider<
141+
alloy::providers::fillers::JoinFill<
142+
alloy::providers::Identity,
143+
alloy::providers::fillers::JoinFill<
144+
alloy::providers::fillers::GasFiller,
145+
alloy::providers::fillers::JoinFill<
146+
alloy::providers::fillers::BlobGasFiller,
147+
alloy::providers::fillers::JoinFill<
148+
alloy::providers::fillers::NonceFiller,
149+
alloy::providers::fillers::ChainIdFiller,
150+
>,
151+
>,
152+
>,
153+
>,
154+
alloy::providers::RootProvider,
155+
>,
156+
>,
157+
>;
158+
159+
/// Configuration struct for Pecorino network values
160+
#[derive(Debug, Clone)]
161+
pub struct PecorinoCfg {}
162+
163+
impl Copy for PecorinoCfg {}
164+
165+
impl trevm::Cfg for PecorinoCfg {
166+
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
167+
let CfgEnv { chain_id, spec, .. } = cfg_env;
168+
169+
*chain_id = PECORINO_CHAIN_ID;
170+
*spec = SpecId::default();
171+
}
172+
}

0 commit comments

Comments
 (0)