Skip to content

Commit 7487589

Browse files
author
Ludo Galabru
committed
feat: plumbing for ordhook-sdk-js
1 parent dc4cdce commit 7487589

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

components/ordhook-cli/src/cli/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
618618
// .collect::<Result<Vec<ChainhookFullSpecification>, _>>()?;
619619

620620
let mut service = Service::new(config, ctx.clone());
621-
return service.run(predicates).await;
621+
return service.run(predicates, None).await;
622622
}
623623
},
624624
Command::Config(subcmd) => match subcmd {

components/ordhook-core/src/service/mod.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::service::predicates::{
2626
};
2727
use crate::service::runloops::start_bitcoin_scan_runloop;
2828

29+
use chainhook_sdk::chainhooks::bitcoin::BitcoinChainhookOccurrencePayload;
2930
use chainhook_sdk::chainhooks::types::{
3031
BitcoinChainhookSpecification, ChainhookFullSpecification, ChainhookSpecification,
3132
};
@@ -36,7 +37,7 @@ use chainhook_sdk::observer::{
3637
use chainhook_sdk::types::{BitcoinBlockData, BlockIdentifier};
3738
use chainhook_sdk::utils::Context;
3839
use crossbeam_channel::unbounded;
39-
use crossbeam_channel::{select, Sender};
40+
use crossbeam_channel::select;
4041
use dashmap::DashMap;
4142
use fxhash::FxHasher;
4243
use redis::Commands;
@@ -56,13 +57,21 @@ impl Service {
5657
Self { config, ctx }
5758
}
5859

59-
pub async fn run(&mut self, predicates: Vec<ChainhookFullSpecification>) -> Result<(), String> {
60+
pub async fn run(
61+
&mut self,
62+
predicates: Vec<ChainhookFullSpecification>,
63+
predicate_activity_relayer: Option<
64+
crossbeam_channel::Sender<BitcoinChainhookOccurrencePayload>,
65+
>,
66+
) -> Result<(), String> {
6067
let mut event_observer_config = self.config.get_event_observer_config();
6168
let chainhook_config = create_and_consolidate_chainhook_config_with_predicates(
6269
predicates,
70+
predicate_activity_relayer.is_some(),
6371
&self.config,
6472
&self.ctx,
6573
);
74+
6675
event_observer_config.chainhook_config = Some(chainhook_config);
6776

6877
let ordhook_config = self.config.get_ordhook_config();
@@ -106,9 +115,14 @@ impl Service {
106115
self.start_main_runloop_with_dynamic_predicates(
107116
&observer_command_tx,
108117
observer_event_rx,
118+
predicate_activity_relayer,
109119
)?;
110120
} else {
111-
self.start_main_runloop(&observer_command_tx, observer_event_rx)?;
121+
self.start_main_runloop(
122+
&observer_command_tx,
123+
observer_event_rx,
124+
predicate_activity_relayer,
125+
)?;
112126
}
113127
Ok(())
114128
}
@@ -117,6 +131,9 @@ impl Service {
117131
&self,
118132
_observer_command_tx: &std::sync::mpsc::Sender<ObserverCommand>,
119133
observer_event_rx: crossbeam_channel::Receiver<ObserverEvent>,
134+
predicate_activity_relayer: Option<
135+
crossbeam_channel::Sender<BitcoinChainhookOccurrencePayload>,
136+
>,
120137
) -> Result<(), String> {
121138
loop {
122139
let event = match observer_event_rx.recv() {
@@ -131,6 +148,11 @@ impl Service {
131148
}
132149
};
133150
match event {
151+
ObserverEvent::BitcoinPredicateTriggered(data) => {
152+
if let Some(ref tx) = predicate_activity_relayer {
153+
let _ = tx.send(data);
154+
}
155+
}
134156
ObserverEvent::Terminate => {
135157
info!(self.ctx.expect_logger(), "Terminating runloop");
136158
break;
@@ -145,6 +167,9 @@ impl Service {
145167
&self,
146168
observer_command_tx: &std::sync::mpsc::Sender<ObserverCommand>,
147169
observer_event_rx: crossbeam_channel::Receiver<ObserverEvent>,
170+
predicate_activity_relayer: Option<
171+
crossbeam_channel::Sender<BitcoinChainhookOccurrencePayload>,
172+
>,
148173
) -> Result<(), String> {
149174
let PredicatesApi::On(ref api_config) = self.config.http_api else {
150175
return Ok(())
@@ -282,6 +307,11 @@ impl Service {
282307
}
283308
}
284309
}
310+
ObserverEvent::BitcoinPredicateTriggered(data) => {
311+
if let Some(ref tx) = predicate_activity_relayer {
312+
let _ = tx.send(data);
313+
}
314+
}
285315
ObserverEvent::Terminate => {
286316
info!(self.ctx.expect_logger(), "Terminating runloop");
287317
break;

components/ordhook-core/src/service/predicates.rs

+25
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ pub fn start_predicate_processor(
171171

172172
pub fn create_and_consolidate_chainhook_config_with_predicates(
173173
predicates: Vec<ChainhookFullSpecification>,
174+
enable_internal_trigger: bool,
174175
config: &Config,
175176
ctx: &Context,
176177
) -> ChainhookConfig {
@@ -235,5 +236,29 @@ pub fn create_and_consolidate_chainhook_config_with_predicates(
235236
}
236237
}
237238

239+
if enable_internal_trigger {
240+
let _ = chainhook_config.register_specification(ChainhookSpecification::Bitcoin(
241+
BitcoinChainhookSpecification {
242+
uuid: format!("ordhook"),
243+
owner_uuid: None,
244+
name: format!("ordhook"),
245+
network: chainhook_sdk::types::BitcoinNetwork::Mainnet,
246+
version: 1,
247+
blocks: None,
248+
start_block: None,
249+
end_block: None,
250+
expire_after_occurrence: None,
251+
predicate: chainhook_sdk::chainhooks::types::BitcoinPredicateType::OrdinalsProtocol(
252+
chainhook_sdk::chainhooks::types::OrdinalOperations::InscriptionFeed,
253+
),
254+
action: chainhook_sdk::chainhooks::types::HookAction::Noop,
255+
include_proof: false,
256+
include_inputs: true,
257+
include_outputs: false,
258+
include_witness: false,
259+
enabled: true,
260+
},
261+
));
262+
}
238263
chainhook_config
239264
}

0 commit comments

Comments
 (0)