@@ -9,24 +9,26 @@ use crate::{
9
9
use alloy:: { consensus:: TxEnvelope , eips:: BlockId , providers:: Provider } ;
10
10
use signet_sim:: { BlockBuild , BuiltBlock , SimCache , SimItem } ;
11
11
use signet_types:: config:: SignetSystemConstants ;
12
- use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
13
12
use tokio:: {
14
13
select,
15
- sync:: mpsc:: { self , UnboundedReceiver } ,
16
- task:: JoinHandle ,
14
+ sync:: mpsc:: { self } ,
17
15
} ;
18
- use tracing:: { Instrument , info} ;
19
16
use trevm:: {
20
- NoopBlock , NoopCfg ,
17
+ NoopBlock ,
21
18
revm:: {
19
+ context:: CfgEnv ,
22
20
database:: { AlloyDB , WrapDatabaseAsync } ,
23
21
inspector:: NoOpInspector ,
22
+ primitives:: hardfork:: SpecId ,
24
23
} ,
25
24
} ;
26
25
27
26
/// Ethereum's slot time in seconds.
28
27
pub const ETHEREUM_SLOT_TIME : u64 = 12 ;
29
28
29
+ /// Pecorino Chain ID
30
+ pub const PECORINO_CHAIN_ID : u64 = 14174 ;
31
+
30
32
/// BlockBuilder is a task that periodically builds a block then sends it for
31
33
/// signing and submission.
32
34
#[ derive( Debug ) ]
@@ -56,155 +58,65 @@ impl BlockBuilder {
56
58
}
57
59
}
58
60
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
-
71
61
/// Spawn the block builder task, returning the inbound channel to it, and
72
62
/// a handle to the running task.
73
- pub fn spawn (
74
- self ,
63
+ pub async fn handle_build (
64
+ & self ,
75
65
constants : SignetSystemConstants ,
76
66
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 > ,
78
98
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) ) ;
108
111
}
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
- } ) ;
160
112
}
161
113
}
162
- . in_current_span ( ) ,
163
- )
114
+ }
164
115
}
165
116
}
166
117
167
118
/// 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 > {
208
120
let latest = match ru_provider. get_block_number ( ) . await {
209
121
Ok ( block_number) => block_number,
210
122
Err ( e) => {
@@ -220,3 +132,41 @@ async fn create_db(
220
132
} ) ;
221
133
Some ( wrapped_db)
222
134
}
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