1
1
use crate :: config:: Config ;
2
- use crate :: scan:: bitcoin:: scan_bitcoin_chain_with_predicate_via_http;
3
-
4
-
5
- use chainhook_event_observer:: chainhooks:: types:: {
6
- ChainhookConfig , ChainhookFullSpecification ,
7
- } ;
2
+ use crate :: scan:: bitcoin:: scan_bitcoin_chainstate_via_http_using_predicate;
3
+ use crate :: scan:: stacks:: scan_stacks_chainstate_via_csv_using_predicate;
8
4
5
+ use chainhook_event_observer:: chainhooks:: types:: { ChainhookConfig , ChainhookFullSpecification } ;
9
6
10
7
use chainhook_event_observer:: observer:: { start_event_observer, ApiKey , ObserverEvent } ;
11
- use chainhook_event_observer:: utils:: { Context } ;
8
+ use chainhook_event_observer:: utils:: Context ;
12
9
use chainhook_event_observer:: {
13
- chainhooks:: stacks:: {
14
- evaluate_stacks_predicate_on_transaction, handle_stacks_hook_action,
15
- StacksChainhookOccurrence , StacksTriggerChainhook ,
16
- } ,
17
10
chainhooks:: types:: ChainhookSpecification ,
18
11
} ;
19
12
use chainhook_types:: {
20
- BitcoinBlockSignaling , BlockIdentifier , StacksBlockData , StacksBlockMetadata , StacksChainEvent ,
21
- StacksTransactionData ,
13
+ BitcoinBlockSignaling , StacksBlockData , StacksChainEvent ,
22
14
} ;
23
15
use redis:: { Commands , Connection } ;
24
16
25
- use std:: collections:: { HashMap } ;
26
- use std:: sync:: mpsc:: channel;
27
17
18
+ use std:: sync:: mpsc:: channel;
28
19
29
20
pub const DEFAULT_INGESTION_PORT : u16 = 20455 ;
30
21
pub const DEFAULT_CONTROL_PORT : u16 = 20456 ;
@@ -191,129 +182,30 @@ impl Service {
191
182
) ;
192
183
}
193
184
match chainhook {
194
- ChainhookSpecification :: Stacks ( stacks_hook) => {
195
- // Retrieve highest block height stored
196
- let tip_height: u64 = redis_con. get ( & format ! ( "stx:tip" ) ) . unwrap_or ( 1 ) ;
197
-
198
- let start_block = stacks_hook. start_block . unwrap_or ( 1 ) ; // TODO(lgalabru): handle STX hooks and genesis block :s
199
- let end_block = stacks_hook. end_block . unwrap_or ( tip_height) ; // TODO(lgalabru): handle STX hooks and genesis block :s
200
-
185
+ ChainhookSpecification :: Stacks ( predicate_spec) => {
186
+ let end_block = match scan_stacks_chainstate_via_csv_using_predicate (
187
+ predicate_spec,
188
+ & mut self . config ,
189
+ & self . ctx ,
190
+ )
191
+ . await
192
+ {
193
+ Ok ( end_block) => end_block,
194
+ Err ( e) => {
195
+ error ! (
196
+ self . ctx. expect_logger( ) ,
197
+ "Unable to evaluate predicate on Bitcoin chainstate: {e}" ,
198
+ ) ;
199
+ continue ;
200
+ }
201
+ } ;
201
202
info ! (
202
203
self . ctx. expect_logger( ) ,
203
- "Processing Stacks chainhook {}, will scan blocks [{}; {}]" ,
204
- stacks_hook. uuid,
205
- start_block,
206
- end_block
204
+ "Stacks chainstate scan completed up to block: {}" , end_block. index
207
205
) ;
208
- let mut total_hits = 0 ;
209
- for cursor in start_block..=end_block {
210
- debug ! (
211
- self . ctx. expect_logger( ) ,
212
- "Evaluating predicate #{} on block #{}" ,
213
- stacks_hook. uuid,
214
- cursor
215
- ) ;
216
- let (
217
- block_identifier,
218
- parent_block_identifier,
219
- timestamp,
220
- transactions,
221
- metadata,
222
- ) = {
223
- let payload: Vec < String > = redis_con
224
- . hget (
225
- & format ! ( "stx:{}" , cursor) ,
226
- & [
227
- "block_identifier" ,
228
- "parent_block_identifier" ,
229
- "timestamp" ,
230
- "transactions" ,
231
- "metadata" ,
232
- ] ,
233
- )
234
- . expect ( "unable to retrieve tip height" ) ;
235
- if payload. len ( ) != 5 {
236
- warn ! ( self . ctx. expect_logger( ) , "Chain still being processed, please retry in a few minutes" ) ;
237
- continue ;
238
- }
239
- (
240
- serde_json:: from_str :: < BlockIdentifier > ( & payload[ 0 ] )
241
- . unwrap ( ) ,
242
- serde_json:: from_str :: < BlockIdentifier > ( & payload[ 1 ] )
243
- . unwrap ( ) ,
244
- serde_json:: from_str :: < i64 > ( & payload[ 2 ] ) . unwrap ( ) ,
245
- serde_json:: from_str :: < Vec < StacksTransactionData > > (
246
- & payload[ 3 ] ,
247
- )
248
- . unwrap ( ) ,
249
- serde_json:: from_str :: < StacksBlockMetadata > ( & payload[ 4 ] )
250
- . unwrap ( ) ,
251
- )
252
- } ;
253
- let mut hits = vec ! [ ] ;
254
- for tx in transactions. iter ( ) {
255
- if evaluate_stacks_predicate_on_transaction (
256
- & tx,
257
- & stacks_hook,
258
- & self . ctx ,
259
- ) {
260
- debug ! (
261
- self . ctx. expect_logger( ) ,
262
- "Action #{} triggered by transaction {} (block #{})" ,
263
- stacks_hook. uuid,
264
- tx. transaction_identifier. hash,
265
- cursor
266
- ) ;
267
- hits. push ( tx) ;
268
- total_hits += 1 ;
269
- }
270
- }
271
-
272
- if hits. len ( ) > 0 {
273
- let block = StacksBlockData {
274
- block_identifier,
275
- parent_block_identifier,
276
- timestamp,
277
- transactions : vec ! [ ] ,
278
- metadata,
279
- } ;
280
- let trigger = StacksTriggerChainhook {
281
- chainhook : & stacks_hook,
282
- apply : vec ! [ ( hits, & block) ] ,
283
- rollback : vec ! [ ] ,
284
- } ;
285
-
286
- let proofs = HashMap :: new ( ) ;
287
- match handle_stacks_hook_action ( trigger, & proofs, & self . ctx ) {
288
- Err ( e) => {
289
- info ! (
290
- self . ctx. expect_logger( ) ,
291
- "unable to handle action {}" , e
292
- ) ;
293
- }
294
- Ok ( StacksChainhookOccurrence :: Http ( request) ) => {
295
- if let Err ( e) =
296
- hiro_system_kit:: nestable_block_on ( request. send ( ) )
297
- {
298
- error ! (
299
- self . ctx. expect_logger( ) ,
300
- "unable to perform action {}" , e
301
- ) ;
302
- }
303
- }
304
- Ok ( _) => {
305
- error ! (
306
- self . ctx. expect_logger( ) ,
307
- "action not supported"
308
- ) ;
309
- }
310
- }
311
- }
312
- }
313
- info ! ( self . ctx. expect_logger( ) , "Stacks chainhook {} scan completed: action triggered by {} transactions" , stacks_hook. uuid, total_hits) ;
314
206
}
315
207
ChainhookSpecification :: Bitcoin ( predicate_spec) => {
316
- match scan_bitcoin_chain_with_predicate_via_http (
208
+ match scan_bitcoin_chainstate_via_http_using_predicate (
317
209
predicate_spec,
318
210
& self . config ,
319
211
& self . ctx ,
@@ -322,7 +214,7 @@ impl Service {
322
214
{
323
215
Ok ( _) => { }
324
216
Err ( e) => {
325
- info ! (
217
+ error ! (
326
218
self . ctx. expect_logger( ) ,
327
219
"Unable to evaluate predicate on Bitcoin chainstate: {e}" ,
328
220
) ;
0 commit comments