Skip to content

Commit 3bd308f

Browse files
author
Ludo Galabru
committed
fix: loading predicates from redis
1 parent 5264d59 commit 3bd308f

File tree

3 files changed

+41
-13
lines changed
  • components

3 files changed

+41
-13
lines changed

components/chainhook-cli/src/service/mod.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,33 @@ impl Service {
3939
Self { config, ctx }
4040
}
4141

42-
pub async fn run(
43-
&mut self,
44-
mut predicates: Vec<ChainhookFullSpecification>,
45-
) -> Result<(), String> {
42+
pub async fn run(&mut self, predicates: Vec<ChainhookFullSpecification>) -> Result<(), String> {
4643
let mut chainhook_config = ChainhookConfig::new();
4744

4845
if predicates.is_empty() {
49-
let mut registered_predicates = load_predicates_from_redis(&self.config, &self.ctx)?;
50-
predicates.append(&mut registered_predicates);
46+
let registered_predicates = load_predicates_from_redis(&self.config, &self.ctx)?;
47+
for predicate in registered_predicates.into_iter() {
48+
let predicate_uuid = predicate.uuid().to_string();
49+
match chainhook_config.register_specification(predicate) {
50+
Ok(_) => {
51+
info!(
52+
self.ctx.expect_logger(),
53+
"Predicate {} retrieved from storage and loaded", predicate_uuid,
54+
);
55+
}
56+
Err(e) => {
57+
error!(
58+
self.ctx.expect_logger(),
59+
"Failed loading predicate from storage: {}",
60+
e.to_string()
61+
);
62+
}
63+
}
64+
}
5165
}
5266

5367
for predicate in predicates.into_iter() {
54-
match chainhook_config.register_hook(
68+
match chainhook_config.register_full_specification(
5569
(
5670
&self.config.network.bitcoin_network,
5771
&self.config.network.stacks_network,
@@ -62,14 +76,14 @@ impl Service {
6276
Ok(spec) => {
6377
info!(
6478
self.ctx.expect_logger(),
65-
"Predicate {} retrieved from storage and loaded",
79+
"Predicate {} retrieved from config and loaded",
6680
spec.uuid(),
6781
);
6882
}
6983
Err(e) => {
7084
error!(
7185
self.ctx.expect_logger(),
72-
"Failed loading predicate from storage: {}",
86+
"Failed loading predicate from config: {}",
7387
e.to_string()
7488
);
7589
}
@@ -593,7 +607,7 @@ fn update_storage_with_confirmed_stacks_blocks(
593607
fn load_predicates_from_redis(
594608
config: &Config,
595609
ctx: &Context,
596-
) -> Result<Vec<ChainhookFullSpecification>, String> {
610+
) -> Result<Vec<ChainhookSpecification>, String> {
597611
let redis_config = config.expected_redis_config();
598612
let client = redis::Client::open(redis_config.uri.clone()).unwrap();
599613
let mut redis_con = match client.get_connection() {
@@ -619,7 +633,7 @@ fn load_predicates_from_redis(
619633
for key in chainhooks_to_load.iter() {
620634
let chainhook = match redis_con.hget::<_, _, String>(key, "specification") {
621635
Ok(spec) => {
622-
ChainhookFullSpecification::deserialize_specification(&spec, key).unwrap()
636+
ChainhookSpecification::deserialize_specification(&spec, key).unwrap()
623637
// todo
624638
}
625639
Err(e) => {

components/chainhook-event-observer/src/chainhooks/types.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl ChainhookConfig {
4545
bitcoin
4646
}
4747

48-
pub fn register_hook(
48+
pub fn register_full_specification(
4949
&mut self,
5050
networks: (&BitcoinNetwork, &StacksNetwork),
5151
hook: ChainhookFullSpecification,
@@ -68,6 +68,18 @@ impl ChainhookConfig {
6868
Ok(spec)
6969
}
7070

71+
pub fn register_specification(&mut self, spec: ChainhookSpecification) -> Result<(), String> {
72+
match spec {
73+
ChainhookSpecification::Stacks(spec) => {
74+
self.stacks_chainhooks.push(spec.clone());
75+
}
76+
ChainhookSpecification::Bitcoin(spec) => {
77+
self.bitcoin_chainhooks.push(spec.clone());
78+
}
79+
};
80+
Ok(())
81+
}
82+
7183
pub fn deregister_stacks_hook(
7284
&mut self,
7385
hook_uuid: String,

components/chainhook-event-observer/src/observer/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,9 @@ pub async fn start_observer_commands_handler(
12071207
}
12081208
};
12091209

1210-
let spec = match hook_formation.register_hook(networks, hook, &api_key) {
1210+
let spec = match hook_formation
1211+
.register_full_specification(networks, hook, &api_key)
1212+
{
12111213
Ok(uuid) => uuid,
12121214
Err(e) => {
12131215
ctx.try_log(|logger| {

0 commit comments

Comments
 (0)