Skip to content

Commit 4cb9e8b

Browse files
committed
Check in using local ledger nonce
1 parent 2e967da commit 4cb9e8b

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ed25519-dalek = { version = "2", default-features = false, features = [
3636
] }
3737
env_logger = "0.11"
3838
flate2 = "1.0"
39+
filetime = "0.2"
3940
fs-err = "3.0.0"
4041
hex = "0.4"
4142
hmac = "0.12"

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dcc-common = { path = "../common" }
2727
decent_cloud_canister = { path = "../ic-canister" }
2828
dirs.workspace = true
2929
ed25519-dalek.workspace = true
30+
filetime.workspace = true
3031
fs-err.workspace = true
3132
hex.workspace = true
3233
hmac.workspace = true

cli/src/main.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use icrc_ledger_types::{
2424
};
2525
use ledger_map::{platform_specific::persistent_storage_read, LedgerMap};
2626
use log::{info, Level, LevelFilter, Metadata, Record};
27+
use std::time::SystemTime;
2728
use std::{
2829
collections::HashMap,
2930
io::{self, BufReader, Seek, Write},
@@ -188,7 +189,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
188189
panic!("You must specify an identity to register");
189190
}
190191
} else if arg_matches.get_flag("check-in-nonce") {
191-
let nonce_bytes = ledger_canister(None).await?.get_np_check_in_nonce().await;
192+
let nonce_bytes = ledger_canister(None).await?.get_check_in_nonce().await;
192193
let nonce_string = hex::encode(&nonce_bytes);
193194

194195
println!("0x{}", nonce_string);
@@ -197,10 +198,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
197198
let dcc_ident = DccIdentity::load_from_dir(&PathBuf::from(np_desc))?;
198199
let ic_auth = dcc_to_ic_auth(&dcc_ident);
199200

200-
let nonce_bytes = ledger_canister(ic_auth)
201-
.await?
202-
.get_np_check_in_nonce()
203-
.await;
201+
// Check the local ledger timestamp
202+
let local_ledger_path = ledger_local
203+
.get_file_path()
204+
.expect("Failed to get local ledger path");
205+
let local_ledger_file_mtime = local_ledger_path.metadata()?.modified()?;
206+
207+
// If the local ledger is older than 1 minute, refresh it automatically before proceeding
208+
// If needed, the local ledger can also be refreshed manually from the command line
209+
if local_ledger_file_mtime
210+
< SystemTime::now() - std::time::Duration::from_secs(60)
211+
{
212+
info!("Local ledger is older than 1 minute, refreshing...");
213+
let canister = ledger_canister(None).await?;
214+
ledger_data_fetch(&canister, local_ledger_path).await?;
215+
216+
refresh_caches_from_ledger(&ledger_local)
217+
.expect("Loading balances from ledger failed");
218+
}
219+
// The local ledger needs to be refreshed to get the latest nonce
220+
// This provides the incentive to clone and frequently re-fetch the ledger
221+
let nonce_bytes = ledger_local.get_latest_block_hash();
204222
let nonce_string = hex::encode(&nonce_bytes);
205223

206224
info!(
@@ -210,7 +228,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
210228
nonce_string,
211229
nonce_bytes.len()
212230
);
213-
let ic_auth = dcc_to_ic_auth(&dcc_ident);
214231
let result = ledger_canister(ic_auth)
215232
.await?
216233
.node_provider_check_in(
@@ -421,8 +438,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
421438
}
422439
print!("{}", table);
423440
}
424-
"get_np_check_in_nonce" => {
425-
let nonce_bytes = ledger_canister(None).await?.get_np_check_in_nonce().await;
441+
"get_check_in_nonce" => {
442+
let nonce_bytes = ledger_canister(None).await?.get_check_in_nonce().await;
426443
println!("{}", hex::encode(nonce_bytes));
427444
}
428445
"get_logs_debug" => {
@@ -625,6 +642,9 @@ async fn ledger_data_fetch(
625642
local_ledger_path.display()
626643
);
627644
}
645+
// Set the modified time to the current time, to mark that the data is up-to-date
646+
filetime::set_file_mtime(local_ledger_path, std::time::SystemTime::now().into())?;
647+
628648
Ok(())
629649
}
630650

0 commit comments

Comments
 (0)