@@ -6,16 +6,20 @@ use crate::{
6
6
tx_poller:: TxPoller ,
7
7
} ,
8
8
} ;
9
- use alloy:: { consensus:: TxEnvelope , eips:: BlockId , genesis :: Genesis } ;
10
- use signet_sim:: { BlockBuild , BuiltBlock , SimCache } ;
9
+ use alloy:: { consensus:: TxEnvelope , eips:: BlockId , providers :: Provider } ;
10
+ use signet_sim:: { BlockBuild , BuiltBlock , SimCache , SimItem } ;
11
11
use signet_types:: config:: SignetSystemConstants ;
12
12
use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
13
- use tokio:: { select, sync:: mpsc, task:: JoinHandle } ;
13
+ use tokio:: {
14
+ select,
15
+ sync:: mpsc:: { self , UnboundedReceiver } ,
16
+ task:: JoinHandle ,
17
+ } ;
14
18
use tracing:: { Instrument , info} ;
15
19
use trevm:: {
16
20
NoopBlock , NoopCfg ,
17
21
revm:: {
18
- database:: { AlloyDB , CacheDB , WrapDatabaseAsync } ,
22
+ database:: { AlloyDB , WrapDatabaseAsync } ,
19
23
inspector:: NoOpInspector ,
20
24
} ,
21
25
} ;
@@ -54,7 +58,6 @@ impl BlockBuilder {
54
58
55
59
// calculate the duration in seconds until the beginning of the next block slot.
56
60
fn secs_to_next_slot ( & self ) -> u64 {
57
- // TODO: Account for multiple builders and return the time to our next _assigned_ slot here.
58
61
let curr_timestamp: u64 = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_secs ( ) ;
59
62
let current_slot_time = ( curr_timestamp - self . config . chain_offset ) % ETHEREUM_SLOT_TIME ;
60
63
( ETHEREUM_SLOT_TIME - current_slot_time) % ETHEREUM_SLOT_TIME
@@ -69,49 +72,83 @@ impl BlockBuilder {
69
72
/// a handle to the running task.
70
73
pub fn spawn (
71
74
self ,
75
+ constants : SignetSystemConstants ,
72
76
ru_provider : WalletlessProvider ,
73
- mut tx_receiver : mpsc :: UnboundedReceiver < TxEnvelope > ,
77
+ mut tx_receiver : UnboundedReceiver < TxEnvelope > ,
74
78
mut bundle_receiver : mpsc:: UnboundedReceiver < Bundle > ,
75
- outbound : mpsc:: UnboundedSender < BuiltBlock > ,
79
+ block_sender : mpsc:: UnboundedSender < BuiltBlock > ,
76
80
) -> JoinHandle < ( ) > {
77
- let genesis = Genesis :: default ( ) ;
78
- let constants = SignetSystemConstants :: try_from_genesis ( & genesis) . unwrap ( ) ; // TODO: get from actual genesis file.
79
- let concurrency_limit = 1000 ;
80
- let max_gas = 30_000_000 ;
81
-
81
+ println ! ( "GOT HERE 0" ) ;
82
82
tokio:: spawn (
83
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
+ }
108
+ }
109
+ } ) ;
110
+
111
+ println ! ( "starting the block builder loop" ) ;
112
+
84
113
loop {
85
- // setup during slot time dead zone
86
- let alloy_db = AlloyDB :: new ( ru_provider. clone ( ) , BlockId :: from ( 1 ) ) ;
87
- let wrapped_db = WrapDatabaseAsync :: new ( alloy_db) . unwrap ( ) ;
88
- let cached_db = CacheDB :: new ( wrapped_db) ;
89
-
90
- let sim_items = SimCache :: new ( ) ;
91
-
92
- let deadline = Instant :: now ( )
93
- . checked_add ( Duration :: from_secs ( self . secs_to_next_target ( ) ) )
94
- . unwrap ( ) ;
95
-
96
- let block_builder: BlockBuild < _ , NoOpInspector > = BlockBuild :: new (
97
- cached_db,
98
- constants,
99
- NoopCfg ,
100
- NoopBlock ,
101
- deadline,
102
- concurrency_limit,
103
- sim_items. clone ( ) ,
104
- max_gas,
105
- ) ;
106
-
107
- // sleep until next buffer time
108
- tokio:: time:: sleep ( Duration :: from_secs ( self . secs_to_next_target ( ) ) ) . await ;
109
- info ! ( "beginning block build cycle" ) ;
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( ) ) ;
110
132
111
133
tokio:: spawn ( {
112
- let outbound = outbound. clone ( ) ;
134
+ let outbound = block_sender. clone ( ) ;
135
+ let sim_items = sim_items. clone ( ) ;
136
+
113
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
+
114
149
let block = block_builder. build ( ) . await ;
150
+ println ! ( "GOT BLOCK {}" , block. contents_hash( ) ) ;
151
+
115
152
if let Err ( e) = outbound. send ( block) {
116
153
println ! ( "failed to send built block: {}" , e) ;
117
154
tracing:: error!( error = %e, "failed to send built block" ) ;
@@ -120,28 +157,66 @@ impl BlockBuilder {
120
157
}
121
158
}
122
159
} ) ;
123
-
124
- // Feed it transactions and bundles until deadline
125
- loop {
126
- select ! {
127
- tx = tx_receiver. recv( ) => {
128
- if let Some ( tx) = tx {
129
- sim_items. add_item( tx) ;
130
- }
131
- }
132
- bundle = bundle_receiver. recv( ) => {
133
- if let Some ( bundle) = bundle {
134
- sim_items. add_item( bundle. bundle) ;
135
- }
136
- }
137
- _ = tokio:: time:: sleep_until( deadline. into( ) ) => {
138
- break ;
139
- }
140
- }
141
- }
142
160
}
143
161
}
144
162
. in_current_span ( ) ,
145
163
)
146
164
}
147
165
}
166
+
167
+ /// 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
+ > {
208
+ let latest = match ru_provider. get_block_number ( ) . await {
209
+ Ok ( block_number) => block_number,
210
+ Err ( e) => {
211
+ tracing:: error!( error = %e, "failed to get latest block number" ) ;
212
+ println ! ( "failed to get latest block number" ) ;
213
+ // Should this do anything else?
214
+ return None ;
215
+ }
216
+ } ;
217
+ let alloy_db = AlloyDB :: new ( ru_provider. clone ( ) , BlockId :: from ( latest) ) ;
218
+ let wrapped_db = WrapDatabaseAsync :: new ( alloy_db) . unwrap_or_else ( || {
219
+ panic ! ( "failed to acquire async alloy_db; check which runtime you're using" )
220
+ } ) ;
221
+ Some ( wrapped_db)
222
+ }
0 commit comments