Skip to content

Commit 3ea9f59

Browse files
author
Ludo Galabru
committed
feat: ability to update stacks db from cli + fix caching logic
1 parent 93d3597 commit 3ea9f59

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ pub async fn download_stacks_dataset_if_required(config: &mut Config, ctx: &Cont
197197
};
198198
let should_download = match (local_sha_file, remote_sha_file) {
199199
(Ok(local), Ok(remote_response)) => {
200-
let cache_invalidated = remote_response.starts_with(&local[0..32]) == false;
201-
if cache_invalidated {
200+
let cache_not_expired = remote_response.starts_with(&local[0..32]) == false;
201+
if cache_not_expired {
202202
info!(
203203
ctx.expect_logger(),
204204
"More recent Stacks archive file detected"
205205
);
206206
}
207-
cache_invalidated
207+
cache_not_expired == false
208208
}
209209
(_, _) => {
210210
info!(

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::archive::download_stacks_dataset_if_required;
12
use crate::block::DigestingCommand;
23
use crate::config::generator::generate_config;
34
use crate::config::{Config, PredicatesApi};
@@ -238,6 +239,9 @@ enum StacksDbCommand {
238239
/// Check integrity
239240
#[clap(name = "check", bin_name = "check")]
240241
Check(CheckDbCommand),
242+
/// Update database using latest Stacks archive file
243+
#[clap(name = "update", bin_name = "update")]
244+
Update(UpdateDbCommand),
241245
}
242246

243247
#[derive(Subcommand, PartialEq, Clone, Debug)]
@@ -374,6 +378,13 @@ struct CheckDbCommand {
374378
pub config_path: Option<String>,
375379
}
376380

381+
#[derive(Parser, PartialEq, Clone, Debug)]
382+
struct UpdateDbCommand {
383+
/// Load config file path
384+
#[clap(long = "config-path")]
385+
pub config_path: Option<String>,
386+
}
387+
377388
#[derive(Parser, PartialEq, Clone, Debug)]
378389
struct InitHordDbCommand {
379390
/// Load config file path
@@ -869,6 +880,10 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
869880
unimplemented!()
870881
}
871882
},
883+
Command::Stacks(StacksCommand::Db(StacksDbCommand::Update(cmd))) => {
884+
let mut config = Config::default(false, false, false, &cmd.config_path)?;
885+
download_stacks_dataset_if_required(&mut config, &ctx).await;
886+
}
872887
Command::Stacks(StacksCommand::Db(StacksDbCommand::Check(cmd))) => {
873888
let config = Config::default(false, false, false, &cmd.config_path)?;
874889
// Delete data, if any
@@ -891,7 +906,10 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
891906
}
892907
}
893908
if missing_blocks.is_empty() {
894-
info!(ctx.expect_logger(), "Stacks db successfully checked ({min}, {max})");
909+
info!(
910+
ctx.expect_logger(),
911+
"Stacks db successfully checked ({min}, {max})"
912+
);
895913
} else {
896914
warn!(
897915
ctx.expect_logger(),

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,20 @@ pub fn update_predicate_status(
370370
predicates_db_conn: &mut Connection,
371371
ctx: &Context,
372372
) {
373+
let serialized_status = json!(status).to_string();
373374
if let Err(e) =
374-
predicates_db_conn.hset::<_, _, _, ()>(&predicate_key, "status", json!(status).to_string())
375+
predicates_db_conn.hset::<_, _, _, ()>(&predicate_key, "status", &serialized_status)
375376
{
376377
error!(
377378
ctx.expect_logger(),
378379
"Error updating status: {}",
379380
e.to_string()
380381
);
382+
} else {
383+
info!(
384+
ctx.expect_logger(),
385+
"Updating predicate {predicate_key} status: {serialized_status}"
386+
);
381387
}
382388
}
383389

@@ -387,16 +393,20 @@ pub fn update_predicate_spec(
387393
predicates_db_conn: &mut Connection,
388394
ctx: &Context,
389395
) {
390-
if let Err(e) = predicates_db_conn.hset::<_, _, _, ()>(
391-
&predicate_key,
392-
"specification",
393-
json!(spec).to_string(),
394-
) {
396+
let serialized_spec = json!(spec).to_string();
397+
if let Err(e) =
398+
predicates_db_conn.hset::<_, _, _, ()>(&predicate_key, "specification", &serialized_spec)
399+
{
395400
error!(
396401
ctx.expect_logger(),
397402
"Error updating status: {}",
398403
e.to_string()
399404
);
405+
} else {
406+
info!(
407+
ctx.expect_logger(),
408+
"Updating predicate {predicate_key} with spec: {serialized_spec}"
409+
);
400410
}
401411
}
402412

0 commit comments

Comments
 (0)