|
1 | 1 | mod http_api;
|
| 2 | +mod runloops; |
2 | 3 |
|
3 | 4 | use crate::config::{Config, PredicatesApi};
|
4 |
| -use crate::scan::bitcoin::scan_bitcoin_chainstate_via_http_using_predicate; |
| 5 | +use crate::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate; |
5 | 6 | use crate::scan::stacks::{
|
6 | 7 | consolidate_local_stacks_chainstate_using_csv,
|
7 | 8 | scan_stacks_chainstate_via_rocksdb_using_predicate,
|
8 | 9 | };
|
9 | 10 | use crate::service::http_api::{load_predicates_from_redis, start_predicate_api_server};
|
| 11 | +use crate::service::runloops::{start_bitcoin_scan_runloop, start_stacks_scan_runloop}; |
10 | 12 | use crate::storage::{
|
11 |
| - insert_entries_in_stacks_blocks, open_readonly_stacks_db_conn, open_readwrite_stacks_db_conn, |
| 13 | + confirm_entries_in_stacks_blocks, draft_entries_in_stacks_blocks, |
| 14 | + insert_unconfirmed_entry_in_stacks_blocks, open_readonly_stacks_db_conn, |
| 15 | + open_readwrite_stacks_db_conn, |
12 | 16 | };
|
13 | 17 |
|
14 | 18 | use chainhook_event_observer::chainhooks::types::{ChainhookConfig, ChainhookFullSpecification};
|
@@ -135,102 +139,33 @@ impl Service {
|
135 | 139 |
|
136 | 140 | // Stacks scan operation threadpool
|
137 | 141 | let (stacks_scan_op_tx, stacks_scan_op_rx) = crossbeam_channel::unbounded();
|
138 |
| - let stacks_scan_pool = |
139 |
| - ThreadPool::new(self.config.limits.max_number_of_concurrent_stacks_scans); |
140 | 142 | let ctx = self.ctx.clone();
|
141 | 143 | let config = self.config.clone();
|
142 | 144 | let observer_command_tx_moved = observer_command_tx.clone();
|
143 | 145 | let _ = hiro_system_kit::thread_named("Stacks scan runloop")
|
144 | 146 | .spawn(move || {
|
145 |
| - while let Ok(mut predicate_spec) = stacks_scan_op_rx.recv() { |
146 |
| - let moved_ctx = ctx.clone(); |
147 |
| - let moved_config = config.clone(); |
148 |
| - let observer_command_tx = observer_command_tx_moved.clone(); |
149 |
| - stacks_scan_pool.execute(move || { |
150 |
| - let stacks_db_conn = match open_readonly_stacks_db_conn( |
151 |
| - &moved_config.expected_cache_path(), |
152 |
| - &moved_ctx, |
153 |
| - ) { |
154 |
| - Ok(db_conn) => db_conn, |
155 |
| - Err(e) => { |
156 |
| - error!( |
157 |
| - moved_ctx.expect_logger(), |
158 |
| - "unable to store stacks block: {}", |
159 |
| - e.to_string() |
160 |
| - ); |
161 |
| - unimplemented!() |
162 |
| - } |
163 |
| - }; |
164 |
| - |
165 |
| - let op = scan_stacks_chainstate_via_rocksdb_using_predicate( |
166 |
| - &predicate_spec, |
167 |
| - &stacks_db_conn, |
168 |
| - &moved_config, |
169 |
| - &moved_ctx, |
170 |
| - ); |
171 |
| - let last_block_scanned = match hiro_system_kit::nestable_block_on(op) { |
172 |
| - Ok(last_block_scanned) => last_block_scanned, |
173 |
| - Err(e) => { |
174 |
| - error!( |
175 |
| - moved_ctx.expect_logger(), |
176 |
| - "Unable to evaluate predicate on Stacks chainstate: {e}", |
177 |
| - ); |
178 |
| - return; |
179 |
| - } |
180 |
| - }; |
181 |
| - info!( |
182 |
| - moved_ctx.expect_logger(), |
183 |
| - "Stacks chainstate scan completed up to block: {}", |
184 |
| - last_block_scanned.index |
185 |
| - ); |
186 |
| - predicate_spec.end_block = Some(last_block_scanned.index); |
187 |
| - let _ = observer_command_tx.send(ObserverCommand::EnablePredicate( |
188 |
| - ChainhookSpecification::Stacks(predicate_spec), |
189 |
| - )); |
190 |
| - }); |
191 |
| - } |
192 |
| - let res = stacks_scan_pool.join(); |
193 |
| - res |
| 147 | + start_stacks_scan_runloop( |
| 148 | + &config, |
| 149 | + stacks_scan_op_rx, |
| 150 | + observer_command_tx_moved, |
| 151 | + &ctx, |
| 152 | + ); |
194 | 153 | })
|
195 | 154 | .expect("unable to spawn thread");
|
196 | 155 |
|
197 | 156 | // Bitcoin scan operation threadpool
|
198 | 157 | let (bitcoin_scan_op_tx, bitcoin_scan_op_rx) = crossbeam_channel::unbounded();
|
199 |
| - let bitcoin_scan_pool = |
200 |
| - ThreadPool::new(self.config.limits.max_number_of_concurrent_bitcoin_scans); |
201 | 158 | let ctx = self.ctx.clone();
|
202 | 159 | let config = self.config.clone();
|
203 |
| - let moved_observer_command_tx = observer_command_tx.clone(); |
| 160 | + let observer_command_tx_moved = observer_command_tx.clone(); |
204 | 161 | let _ = hiro_system_kit::thread_named("Bitcoin scan runloop")
|
205 | 162 | .spawn(move || {
|
206 |
| - while let Ok(predicate_spec) = bitcoin_scan_op_rx.recv() { |
207 |
| - let moved_ctx = ctx.clone(); |
208 |
| - let moved_config = config.clone(); |
209 |
| - let observer_command_tx = moved_observer_command_tx.clone(); |
210 |
| - bitcoin_scan_pool.execute(move || { |
211 |
| - let op = scan_bitcoin_chainstate_via_http_using_predicate( |
212 |
| - &predicate_spec, |
213 |
| - &moved_config, |
214 |
| - &moved_ctx, |
215 |
| - ); |
216 |
| - |
217 |
| - match hiro_system_kit::nestable_block_on(op) { |
218 |
| - Ok(_) => {} |
219 |
| - Err(e) => { |
220 |
| - error!( |
221 |
| - moved_ctx.expect_logger(), |
222 |
| - "Unable to evaluate predicate on Bitcoin chainstate: {e}", |
223 |
| - ); |
224 |
| - return; |
225 |
| - } |
226 |
| - }; |
227 |
| - let _ = observer_command_tx.send(ObserverCommand::EnablePredicate( |
228 |
| - ChainhookSpecification::Bitcoin(predicate_spec), |
229 |
| - )); |
230 |
| - }); |
231 |
| - } |
232 |
| - let res = bitcoin_scan_pool.join(); |
233 |
| - res |
| 163 | + start_bitcoin_scan_runloop( |
| 164 | + &config, |
| 165 | + bitcoin_scan_op_rx, |
| 166 | + observer_command_tx_moved, |
| 167 | + &ctx, |
| 168 | + ); |
234 | 169 | })
|
235 | 170 | .expect("unable to spawn thread");
|
236 | 171 |
|
@@ -358,18 +293,28 @@ impl Service {
|
358 | 293 | match &chain_event {
|
359 | 294 | StacksChainEvent::ChainUpdatedWithBlocks(data) => {
|
360 | 295 | stacks_event += 1;
|
361 |
| - insert_entries_in_stacks_blocks( |
| 296 | + confirm_entries_in_stacks_blocks( |
362 | 297 | &data.confirmed_blocks,
|
363 | 298 | &stacks_db_conn_rw,
|
364 | 299 | &self.ctx,
|
365 | 300 | );
|
| 301 | + draft_entries_in_stacks_blocks( |
| 302 | + &data.new_blocks, |
| 303 | + &stacks_db_conn_rw, |
| 304 | + &self.ctx, |
| 305 | + ) |
366 | 306 | }
|
367 | 307 | StacksChainEvent::ChainUpdatedWithReorg(data) => {
|
368 |
| - insert_entries_in_stacks_blocks( |
| 308 | + confirm_entries_in_stacks_blocks( |
369 | 309 | &data.confirmed_blocks,
|
370 | 310 | &stacks_db_conn_rw,
|
371 | 311 | &self.ctx,
|
372 | 312 | );
|
| 313 | + draft_entries_in_stacks_blocks( |
| 314 | + &data.blocks_to_apply, |
| 315 | + &stacks_db_conn_rw, |
| 316 | + &self.ctx, |
| 317 | + ) |
373 | 318 | }
|
374 | 319 | StacksChainEvent::ChainUpdatedWithMicroblocks(_)
|
375 | 320 | | StacksChainEvent::ChainUpdatedWithMicroblocksReorg(_) => {}
|
|
0 commit comments