Skip to content

Commit

Permalink
fix: rename to predicates config
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Oct 9, 2024
1 parent 3d1d9a8 commit 971ee38
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 57 deletions.
23 changes: 13 additions & 10 deletions components/chainhook-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod generator;

use chainhook_sdk::chainhooks::types::{ChainhookStore, PoxConfig};
pub use chainhook_sdk::indexer::IndexerConfig;
use chainhook_sdk::observer::EventObserverConfig;
use chainhook_sdk::observer::{EventObserverConfig, PredicatesConfig};
use chainhook_sdk::types::{
BitcoinBlockSignaling, BitcoinNetwork, StacksNetwork, StacksNodeConfig,
};
Expand Down Expand Up @@ -84,11 +84,6 @@ pub struct LimitsConfig {
pub max_caching_memory_size_mb: usize,
}

#[derive(Clone, Debug, PartialEq)]
pub struct PredicatesConfig {
pub payload_http_request_timeout_ms: Option<u64>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct MonitoringConfig {
pub prometheus_monitoring_port: Option<u16>,
Expand Down Expand Up @@ -123,7 +118,9 @@ impl Config {
EventObserverConfig {
bitcoin_rpc_proxy_enabled: true,
registered_chainhooks: ChainhookStore::new(),
predicate_payload_http_request_timeout_ms: self.predicates.payload_http_request_timeout_ms,
predicates_config: PredicatesConfig {
payload_http_request_timeout_ms: self.predicates.payload_http_request_timeout_ms,
},
bitcoind_rpc_username: self.network.bitcoind_rpc_username.clone(),
bitcoind_rpc_password: self.network.bitcoind_rpc_password.clone(),
bitcoind_rpc_url: self.network.bitcoind_rpc_url.clone(),
Expand Down Expand Up @@ -372,7 +369,9 @@ impl Config {
},
pox_config: PoxConfig::devnet_default(),
http_api: PredicatesApi::Off,
predicates: PredicatesConfig { payload_http_request_timeout_ms: None },
predicates: PredicatesConfig {
payload_http_request_timeout_ms: None,
},
event_sources: vec![],
limits: LimitsConfig {
max_number_of_bitcoin_predicates: BITCOIN_MAX_PREDICATE_REGISTRATION,
Expand Down Expand Up @@ -406,7 +405,9 @@ impl Config {
},
pox_config: PoxConfig::testnet_default(),
http_api: PredicatesApi::Off,
predicates: PredicatesConfig { payload_http_request_timeout_ms: None },
predicates: PredicatesConfig {
payload_http_request_timeout_ms: None,
},
event_sources: vec![EventSourceConfig::StacksTsvUrl(UrlConfig {
file_url: DEFAULT_TESTNET_STACKS_TSV_ARCHIVE.into(),
})],
Expand Down Expand Up @@ -442,7 +443,9 @@ impl Config {
},
pox_config: PoxConfig::mainnet_default(),
http_api: PredicatesApi::Off,
predicates: PredicatesConfig { payload_http_request_timeout_ms: None },
predicates: PredicatesConfig {
payload_http_request_timeout_ms: None,
},
event_sources: vec![EventSourceConfig::StacksTsvUrl(UrlConfig {
file_url: DEFAULT_MAINNET_STACKS_TSV_ARCHIVE.into(),
})],
Expand Down
2 changes: 1 addition & 1 deletion components/chainhook-cli/src/scan/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub async fn execute_predicates_action<'a>(
gather_proofs(&trigger, &mut proofs, config, ctx);
}
let predicate_uuid = &trigger.chainhook.uuid;
match handle_bitcoin_hook_action(trigger, &proofs, &config) {
match handle_bitcoin_hook_action(trigger, &proofs, &config.predicates_config) {
Err(e) => {
warn!(
ctx.expect_logger(),
Expand Down
30 changes: 23 additions & 7 deletions components/chainhook-cli/src/scan/stacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ pub async fn get_canonical_fork_from_tsv(
for result in reader_builder.deserialize() {
line += 1;
let record: Record = result.unwrap();
if let RecordKind::StacksBlockReceived = &record.kind { if let Err(_e) = record_tx.send(Some((record, line))) {
break;
} };
if let RecordKind::StacksBlockReceived = &record.kind {
if let Err(_e) = record_tx.send(Some((record, line))) {
break;
}
};
}
let _ = record_tx.send(None);
})
Expand Down Expand Up @@ -338,7 +340,12 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
apply: hits_per_blocks,
rollback: vec![],
};
let res = match handle_stacks_hook_action(trigger, &proofs, &config.get_event_observer_config(), ctx) {
let res = match handle_stacks_hook_action(
trigger,
&proofs,
&config.get_event_observer_config().predicates_config,
ctx,
) {
Err(e) => {
warn!(
ctx.expect_logger(),
Expand Down Expand Up @@ -487,7 +494,9 @@ pub async fn scan_stacks_chainstate_via_csv_using_predicate(
let mut tsv_line = String::new();
while tsv_current_line < tsv_line_number {
tsv_line.clear();
let bytes_read = tsv_reader.read_line(&mut tsv_line).map_err(|e| e.to_string())?;
let bytes_read = tsv_reader
.read_line(&mut tsv_line)
.map_err(|e| e.to_string())?;
if bytes_read == 0 {
return Err("Unexpected EOF when reading TSV".to_string());
}
Expand Down Expand Up @@ -525,7 +534,12 @@ pub async fn scan_stacks_chainstate_via_csv_using_predicate(
apply: hits_per_blocks,
rollback: vec![],
};
match handle_stacks_hook_action(trigger, &proofs, &config.get_event_observer_config(), ctx) {
match handle_stacks_hook_action(
trigger,
&proofs,
&config.get_event_observer_config().predicates_config,
ctx,
) {
Err(e) => {
error!(ctx.expect_logger(), "unable to handle action {}", e);
}
Expand Down Expand Up @@ -604,7 +618,9 @@ pub async fn consolidate_local_stacks_chainstate_using_csv(
let mut tsv_line = String::new();
while tsv_current_line < tsv_line_number {
tsv_line.clear();
let bytes_read = tsv_reader.read_line(&mut tsv_line).map_err(|e| e.to_string())?;
let bytes_read = tsv_reader
.read_line(&mut tsv_line)
.map_err(|e| e.to_string())?;
if bytes_read == 0 {
return Err("Unexpected EOF when reading TSV".to_string());
}
Expand Down
30 changes: 13 additions & 17 deletions components/chainhook-cli/src/service/tests/helpers/mock_service.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::config::{
Config, EventSourceConfig, LimitsConfig, MonitoringConfig, PathConfig, PredicatesApi, PredicatesApiConfig, PredicatesConfig, StorageConfig, DEFAULT_REDIS_URI
Config, EventSourceConfig, LimitsConfig, MonitoringConfig, PathConfig, PredicatesApi,
PredicatesApiConfig, StorageConfig, DEFAULT_REDIS_URI,
};
use crate::scan::stacks::consolidate_local_stacks_chainstate_using_csv;
use crate::service::{
http_api::start_predicate_api_server, update_predicate_spec, update_predicate_status,
PredicateStatus, Service,
};
use chainhook_sdk::chainhooks::types::PoxConfig;
use chainhook_sdk::observer::PredicatesConfig;
use chainhook_sdk::{
chainhooks::stacks::StacksChainhookSpecificationNetworkMap,
chainhooks::types::{ChainhookInstance, ChainhookSpecificationNetworkMap},
Expand Down Expand Up @@ -81,12 +83,12 @@ pub async fn filter_predicate_status_from_all_predicates(
match matching_predicate {
Some(predicate) => match predicate.get("status") {
Some(status) => {
return serde_json::from_value(status.clone()).map_err(|e| {
format!("failed to parse status {}", e)
});
return serde_json::from_value(status.clone())
.map_err(|e| format!("failed to parse status {}", e));
}
None => {
return Err("no status field on matching get predicates result".to_string())
return Err("no status field on matching get predicates result"
.to_string())
}
},
None => {
Expand All @@ -97,7 +99,9 @@ pub async fn filter_predicate_status_from_all_predicates(
}
}
None => {
return Err("failed to parse get predicate response's result field".to_string())
return Err(
"failed to parse get predicate response's result field".to_string()
)
}
},
None => {
Expand Down Expand Up @@ -266,10 +270,7 @@ pub fn flush_redis(port: u16) {
let client = redis::Client::open(format!("redis://localhost:{port}/"))
.expect("unable to connect to redis");
let mut predicate_db_conn = client.get_connection().expect("unable to connect to redis");
let db_keys: Vec<String> = predicate_db_conn
.scan_match("*")
.unwrap()
.collect();
let db_keys: Vec<String> = predicate_db_conn.scan_match("*").unwrap().collect();
for k in db_keys {
predicate_db_conn.del::<_, ()>(&k).unwrap();
}
Expand All @@ -292,7 +293,7 @@ pub fn get_chainhook_config(
};
Config {
http_api: PredicatesApi::On(api_config),
predicates: PredicatesConfig { payload_http_request_timeout_ms: None },
predicates: PredicatesConfig::default(),
pox_config: PoxConfig::devnet_default(),
storage: StorageConfig {
working_dir: working_dir.into(),
Expand Down Expand Up @@ -343,12 +344,7 @@ pub async fn start_chainhook_service(
);
let _ = hiro_system_kit::nestable_block_on(future);
})
.map_err(|e| {
format!(
"failed to start chainhook service thread, {}",
e
)
})?;
.map_err(|e| format!("failed to start chainhook service thread, {}", e))?;

// Loop to check if the server is ready
let mut attempts = 0;
Expand Down
4 changes: 2 additions & 2 deletions components/chainhook-cli/src/service/tests/observer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{sync::mpsc::channel, thread::sleep, time::Duration};

use chainhook_sdk::{
chainhooks::types::ChainhookStore,
observer::{start_event_observer, EventObserverConfig},
observer::{start_event_observer, EventObserverConfig, PredicatesConfig},
types::{BitcoinNetwork, StacksNodeConfig},
utils::Context,
};
Expand Down Expand Up @@ -190,7 +190,7 @@ async fn it_responds_200_for_unimplemented_endpoints(
});
let config = EventObserverConfig {
registered_chainhooks: ChainhookStore::new(),
predicate_payload_http_request_timeout_ms: None,
predicates_config: PredicatesConfig::default(),
bitcoin_rpc_proxy_enabled: false,
bitcoind_rpc_username: String::new(),
bitcoind_rpc_password: String::new(),
Expand Down
6 changes: 3 additions & 3 deletions components/chainhook-sdk/src/chainhooks/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::types::{
append_error_context, validate_txid, ChainhookInstance, ExactMatchingRule, HookAction,
MatchingRule, PoxConfig, TxinPredicate,
};
use crate::{observer::EventObserverConfig, utils::{Context, MAX_BLOCK_HEIGHTS_ENTRIES}};
use crate::{observer::PredicatesConfig, utils::{Context, MAX_BLOCK_HEIGHTS_ENTRIES}};

use bitcoincore_rpc_json::bitcoin::{address::Payload, Address};
use chainhook_types::{
Expand Down Expand Up @@ -760,12 +760,12 @@ pub fn serialize_bitcoin_transactions_to_json(
pub fn handle_bitcoin_hook_action<'a>(
trigger: BitcoinTriggerChainhook<'a>,
proofs: &HashMap<&'a TransactionIdentifier, String>,
config: &EventObserverConfig,
config: &PredicatesConfig,
) -> Result<BitcoinChainhookOccurrence, String> {
match &trigger.chainhook.action {
HookAction::HttpPost(http) => {
let mut client_builder = Client::builder();
if let Some(timeout) = config.predicate_payload_http_request_timeout_ms {
if let Some(timeout) = config.payload_http_request_timeout_ms {
client_builder = client_builder.timeout(Duration::from_millis(timeout));
}
let client = client_builder
Expand Down
6 changes: 3 additions & 3 deletions components/chainhook-sdk/src/chainhooks/stacks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::observer::EventObserverConfig;
use crate::observer::PredicatesConfig;
use crate::utils::{AbstractStacksBlock, Context, MAX_BLOCK_HEIGHTS_ENTRIES};

use super::types::{
Expand Down Expand Up @@ -1327,13 +1327,13 @@ pub fn serialize_stacks_payload_to_json<'a>(
pub fn handle_stacks_hook_action<'a>(
trigger: StacksTriggerChainhook<'a>,
proofs: &HashMap<&'a TransactionIdentifier, String>,
config: &EventObserverConfig,
config: &PredicatesConfig,
ctx: &Context,
) -> Result<StacksChainhookOccurrence, String> {
match &trigger.chainhook.action {
HookAction::HttpPost(http) => {
let mut client_builder = Client::builder();
if let Some(timeout) = config.predicate_payload_http_request_timeout_ms {
if let Some(timeout) = config.payload_http_request_timeout_ms {
client_builder = client_builder.timeout(Duration::from_millis(timeout));
}
let client = client_builder
Expand Down
11 changes: 8 additions & 3 deletions components/chainhook-sdk/src/chainhooks/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use super::{
},
types::{ExactMatchingRule, FileHook},
};
use crate::{chainhooks::stacks::serialize_stacks_payload_to_json, utils::Context};
use crate::{
chainhooks::stacks::serialize_stacks_payload_to_json, observer::PredicatesConfig,
utils::Context,
};
use crate::{
chainhooks::{
tests::fixtures::{get_expected_occurrence, get_test_event_payload_by_type},
Expand Down Expand Up @@ -735,7 +738,8 @@ fn test_stacks_hook_action_noop() {
logger: None,
tracer: false,
};
let occurrence = handle_stacks_hook_action(trigger, &proofs, &ctx).unwrap();
let occurrence =
handle_stacks_hook_action(trigger, &proofs, &PredicatesConfig::default(), &ctx).unwrap();
if let StacksChainhookOccurrence::Data(data) = occurrence {
assert_eq!(data.apply.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -812,7 +816,8 @@ fn test_stacks_hook_action_file_append() {
logger: None,
tracer: false,
};
let occurrence = handle_stacks_hook_action(trigger, &proofs, &ctx).unwrap();
let occurrence =
handle_stacks_hook_action(trigger, &proofs, &PredicatesConfig::default(), &ctx).unwrap();
if let StacksChainhookOccurrence::File(path, bytes) = occurrence {
assert_eq!(path, "./".to_string());
let json: JsonValue = serde_json::from_slice(&bytes).unwrap();
Expand Down
Loading

0 comments on commit 971ee38

Please sign in to comment.