Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dispatcher #668

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions components/chainhook-cli/src/scan/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use chainhook_sdk::chainhooks::bitcoin::{
BitcoinChainhookOccurrence, BitcoinTriggerChainhook,
};
use chainhook_sdk::chainhooks::bitcoin::BitcoinChainhookInstance;
use chainhook_sdk::dispatcher::{ChainhookOccurrencePayload, Dispatcher};
use chainhook_sdk::indexer;
use chainhook_sdk::indexer::bitcoin::{
build_http_client, download_and_parse_block_with_retry, retrieve_block_hash_with_retry,
Expand All @@ -29,6 +30,7 @@ use super::common::PredicateScanResult;
pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
predicate_spec: &BitcoinChainhookInstance,
unfinished_scan_data: Option<ScanningData>,
dispatcher: Dispatcher<ChainhookOccurrencePayload>,
config: &Config,
kill_signal: Option<Arc<RwLock<bool>>>,
ctx: &Context,
Expand Down Expand Up @@ -188,6 +190,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
block,
&vec![&predicate_spec],
&event_observer_config,
&dispatcher,
ctx,
)
.await
Expand Down Expand Up @@ -264,6 +267,7 @@ pub async fn process_block_with_predicates(
block: BitcoinBlockData,
predicates: &Vec<&BitcoinChainhookInstance>,
event_observer_config: &EventObserverConfig,
dispatcher: &Dispatcher<ChainhookOccurrencePayload>,
ctx: &Context,
) -> Result<u32, String> {
let chain_event =
Expand All @@ -275,11 +279,12 @@ pub async fn process_block_with_predicates(
let (predicates_triggered, _predicates_evaluated, _predicates_expired) =
evaluate_bitcoin_chainhooks_on_chain_event(&chain_event, predicates, ctx);

execute_predicates_action(predicates_triggered, event_observer_config, ctx).await
execute_predicates_action(predicates_triggered, dispatcher, event_observer_config, ctx).await
}

pub async fn execute_predicates_action<'a>(
hits: Vec<BitcoinTriggerChainhook<'a>>,
dispatcher: &Dispatcher<ChainhookOccurrencePayload>,
config: &EventObserverConfig,
ctx: &Context,
) -> Result<u32, String> {
Expand All @@ -300,8 +305,9 @@ pub async fn execute_predicates_action<'a>(
Ok(action) => {
actions_triggered += 1;
match action {
BitcoinChainhookOccurrence::Http(request, _) => {
send_request(request, 10, 3, ctx).await?
BitcoinChainhookOccurrence::Http(request, data) => {
dispatcher.send(request, ChainhookOccurrencePayload::Bitcoin(data));
//send_request(request, 10, 3, ctx).await?
}
BitcoinChainhookOccurrence::File(path, bytes) => {
file_append(path, bytes, ctx)?
Expand Down
9 changes: 6 additions & 3 deletions components/chainhook-cli/src/scan/stacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
open_readonly_stacks_db_conn_with_retry, open_readwrite_stacks_db_conn,
},
};
use chainhook_sdk::types::{BlockIdentifier, Chain};
use chainhook_sdk::{dispatcher::{ChainhookOccurrencePayload, Dispatcher}, types::{BlockIdentifier, Chain}};
use chainhook_sdk::{
chainhooks::stacks::evaluate_stacks_chainhook_on_blocks,
indexer::{self, stacks::standardize_stacks_serialized_block_header, Indexer},
Expand Down Expand Up @@ -181,6 +181,7 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
predicate_spec: &StacksChainhookInstance,
unfinished_scan_data: Option<ScanningData>,
stacks_db_conn: &DB,
dispatcher: Dispatcher<ChainhookOccurrencePayload>,
config: &Config,
kill_signal: Option<Arc<RwLock<bool>>>,
ctx: &Context,
Expand Down Expand Up @@ -357,8 +358,10 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
number_of_times_triggered += 1;
loop_did_trigger = true;
let res = match action {
StacksChainhookOccurrence::Http(request, _) => {
send_request(request, 3, 1, ctx).await
StacksChainhookOccurrence::Http(request, data) => {
dispatcher.send(request, ChainhookOccurrencePayload::Stacks(data));
Ok(())
//send_request(request, 3, 1, ctx).await
}
StacksChainhookOccurrence::File(path, bytes) => file_append(path, bytes, ctx),
StacksChainhookOccurrence::Data(_payload) => Ok(()),
Expand Down
3 changes: 3 additions & 0 deletions components/chainhook-cli/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl Service {
&config,
stacks_scan_op_rx,
observer_command_tx_moved.clone(),
None,
&ctx,
);
// the scan runloop should loop forever; if it finishes, something is wrong
Expand All @@ -195,6 +196,7 @@ impl Service {
&config,
bitcoin_scan_op_rx,
observer_command_tx_moved.clone(),
None,
&ctx,
);
// the scan runloop should loop forever; if it finishes, something is wrong
Expand Down Expand Up @@ -284,6 +286,7 @@ impl Service {
observer_command_rx,
Some(observer_event_tx_moved),
None,
None,
Some(stacks_startup_context),
self.ctx.clone(),
);
Expand Down
32 changes: 29 additions & 3 deletions components/chainhook-cli/src/service/runloops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use chainhook_sdk::{
chainhooks::{
bitcoin::BitcoinChainhookInstance, stacks::StacksChainhookInstance,
types::ChainhookInstance,
},
observer::ObserverCommand,
utils::Context,
}, dispatcher::{ChainhookOccurrencePayload, Dispatcher}, observer::ObserverCommand, utils::Context
};
use threadpool::ThreadPool;

Expand Down Expand Up @@ -37,11 +35,21 @@ pub fn start_stacks_scan_runloop(
config: &Config,
stacks_scan_op_rx: crossbeam_channel::Receiver<StacksScanOp>,
observer_command_tx: Sender<ObserverCommand>,
dispatcher: Option<Dispatcher<ChainhookOccurrencePayload>>,
ctx: &Context,
) {
let stacks_scan_pool = ThreadPool::new(config.limits.max_number_of_concurrent_stacks_scans);
let mut kill_signals = HashMap::new();

let mut dispatcher = match dispatcher {
Some(instance) => instance,
None => {
let mut dispatcher = Dispatcher::new_single_threaded(ctx);
dispatcher.start();
dispatcher
},
};

while let Ok(op) = stacks_scan_op_rx.recv() {
match op {
StacksScanOp::StartScan {
Expand All @@ -51,6 +59,7 @@ pub fn start_stacks_scan_runloop(
let moved_ctx = ctx.clone();
let moved_config = config.clone();
let observer_command_tx = observer_command_tx.clone();
let moved_dispatcher = dispatcher.clone();
let kill_signal = Arc::new(RwLock::new(false));
kill_signals.insert(predicate_spec.uuid.clone(), kill_signal.clone());
stacks_scan_pool.execute(move || {
Expand All @@ -76,6 +85,7 @@ pub fn start_stacks_scan_runloop(
&predicate_spec,
unfinished_scan_data,
&stacks_db_conn,
moved_dispatcher,
&moved_config,
Some(kill_signal),
&moved_ctx,
Expand Down Expand Up @@ -125,6 +135,8 @@ pub fn start_stacks_scan_runloop(
}
}
stacks_scan_pool.join();

dispatcher.graceful_shutdown();
}

pub enum BitcoinScanOp {
Expand All @@ -139,11 +151,21 @@ pub fn start_bitcoin_scan_runloop(
config: &Config,
bitcoin_scan_op_rx: crossbeam_channel::Receiver<BitcoinScanOp>,
observer_command_tx: Sender<ObserverCommand>,
dispatcher: Option<Dispatcher<ChainhookOccurrencePayload>>,
ctx: &Context,
) {
let bitcoin_scan_pool = ThreadPool::new(config.limits.max_number_of_concurrent_bitcoin_scans);
let mut kill_signals = HashMap::new();

let mut dispatcher = match dispatcher {
Some(instance) => instance,
None => {
let mut dispatcher = Dispatcher::new_single_threaded(ctx);
dispatcher.start();
dispatcher
},
};

while let Ok(op) = bitcoin_scan_op_rx.recv() {
match op {
BitcoinScanOp::StartScan {
Expand All @@ -155,11 +177,13 @@ pub fn start_bitcoin_scan_runloop(
let observer_command_tx = observer_command_tx.clone();
let kill_signal = Arc::new(RwLock::new(false));
kill_signals.insert(predicate_spec.uuid.clone(), kill_signal.clone());
let moved_dispatcher = dispatcher.clone();

bitcoin_scan_pool.execute(move || {
let op = scan_bitcoin_chainstate_via_rpc_using_predicate(
&predicate_spec,
unfinished_scan_data,
moved_dispatcher,
&moved_config,
Some(kill_signal),
&moved_ctx,
Expand Down Expand Up @@ -209,4 +233,6 @@ pub fn start_bitcoin_scan_runloop(
}
}
bitcoin_scan_pool.join();

dispatcher.graceful_shutdown();
}
Loading