@@ -3,7 +3,7 @@ use chainhook_event_observer::bitcoincore_rpc::RpcApi;
3
3
use chainhook_event_observer:: bitcoincore_rpc:: { Auth , Client } ;
4
4
use chainhook_event_observer:: chainhooks:: bitcoin:: {
5
5
evaluate_bitcoin_chainhooks_on_chain_event, handle_bitcoin_hook_action,
6
- BitcoinChainhookOccurrence ,
6
+ BitcoinChainhookOccurrence , BitcoinTriggerChainhook ,
7
7
} ;
8
8
use chainhook_event_observer:: chainhooks:: types:: {
9
9
BitcoinChainhookFullSpecification , BitcoinPredicateType , Protocols ,
@@ -133,16 +133,40 @@ pub async fn scan_bitcoin_chain_with_predicate(
133
133
let mut blocks_scanned = 0 ;
134
134
let mut actions_triggered = 0 ;
135
135
136
+ let event_observer_config = config. get_event_observer_config ( ) ;
137
+ let bitcoin_config = event_observer_config. get_bitcoin_config ( ) ;
138
+
136
139
if is_predicate_evaluating_ordinals {
137
- for ( _inscription_id, ( _inscription_number, _ordinal_number, block_number) ) in
138
- inscriptions_cache. into_iter ( )
139
- {
140
+ for ( cursor, _inscriptions) in inscriptions_cache. into_iter ( ) {
140
141
// Only consider inscriptions in the interval specified
141
- if block_number < start_block || block_number > end_block {
142
+ if cursor < start_block || cursor > end_block {
142
143
continue ;
143
144
}
144
145
145
- // Retrieve the transaction (including witness data) to get the inscription data
146
+ blocks_scanned += 1 ;
147
+
148
+ let block_hash = retrieve_block_hash_with_retry ( & cursor, & bitcoin_config, ctx) . await ?;
149
+ let block_breakdown =
150
+ retrieve_full_block_breakdown_with_retry ( & block_hash, & bitcoin_config, ctx) . await ?;
151
+ let block = indexer:: bitcoin:: standardize_bitcoin_block (
152
+ block_breakdown,
153
+ & event_observer_config. bitcoin_network ,
154
+ ctx,
155
+ ) ?;
156
+
157
+ let chain_event =
158
+ BitcoinChainEvent :: ChainUpdatedWithBlocks ( BitcoinChainUpdatedWithBlocksData {
159
+ new_blocks : vec ! [ block] ,
160
+ confirmed_blocks : vec ! [ ] ,
161
+ } ) ;
162
+
163
+ let hits = evaluate_bitcoin_chainhooks_on_chain_event (
164
+ & chain_event,
165
+ vec ! [ & predicate_spec] ,
166
+ ctx,
167
+ ) ;
168
+
169
+ actions_triggered += execute_predicates_action ( hits, & ctx) . await ;
146
170
}
147
171
} else {
148
172
let use_scan_to_seed_hord_db = true ;
@@ -151,9 +175,6 @@ pub async fn scan_bitcoin_chain_with_predicate(
151
175
// Start ingestion pipeline
152
176
}
153
177
154
- let event_observer_config = config. get_event_observer_config ( ) ;
155
- let bitcoin_config = event_observer_config. get_bitcoin_config ( ) ;
156
-
157
178
for cursor in start_block..=end_block {
158
179
blocks_scanned += 1 ;
159
180
let block_hash = retrieve_block_hash_with_retry ( & cursor, & bitcoin_config, ctx) . await ?;
@@ -185,26 +206,8 @@ pub async fn scan_bitcoin_chain_with_predicate(
185
206
vec ! [ & predicate_spec] ,
186
207
ctx,
187
208
) ;
188
- for hit in hits. into_iter ( ) {
189
- let proofs = HashMap :: new ( ) ;
190
- match handle_bitcoin_hook_action ( hit, & proofs) {
191
- Err ( e) => {
192
- error ! ( ctx. expect_logger( ) , "unable to handle action {}" , e) ;
193
- }
194
- Ok ( action) => {
195
- actions_triggered += 1 ;
196
- match action {
197
- BitcoinChainhookOccurrence :: Http ( request) => {
198
- send_request ( request, & ctx) . await
199
- }
200
- BitcoinChainhookOccurrence :: File ( path, bytes) => {
201
- file_append ( path, bytes, & ctx)
202
- }
203
- BitcoinChainhookOccurrence :: Data ( _payload) => unreachable ! ( ) ,
204
- }
205
- }
206
- }
207
- }
209
+
210
+ actions_triggered += execute_predicates_action ( hits, & ctx) . await ;
208
211
}
209
212
}
210
213
info ! (
@@ -214,3 +217,29 @@ pub async fn scan_bitcoin_chain_with_predicate(
214
217
215
218
Ok ( ( ) )
216
219
}
220
+
221
+ pub async fn execute_predicates_action < ' a > (
222
+ hits : Vec < BitcoinTriggerChainhook < ' a > > ,
223
+ ctx : & Context ,
224
+ ) -> u32 {
225
+ let mut actions_triggered = 0 ;
226
+
227
+ for hit in hits. into_iter ( ) {
228
+ let proofs = HashMap :: new ( ) ;
229
+ match handle_bitcoin_hook_action ( hit, & proofs) {
230
+ Err ( e) => {
231
+ error ! ( ctx. expect_logger( ) , "unable to handle action {}" , e) ;
232
+ }
233
+ Ok ( action) => {
234
+ actions_triggered += 1 ;
235
+ match action {
236
+ BitcoinChainhookOccurrence :: Http ( request) => send_request ( request, & ctx) . await ,
237
+ BitcoinChainhookOccurrence :: File ( path, bytes) => file_append ( path, bytes, & ctx) ,
238
+ BitcoinChainhookOccurrence :: Data ( _payload) => unreachable ! ( ) ,
239
+ }
240
+ }
241
+ }
242
+ }
243
+
244
+ actions_triggered
245
+ }
0 commit comments