@@ -24,6 +24,7 @@ use icrc_ledger_types::{
24
24
} ;
25
25
use ledger_map:: { platform_specific:: persistent_storage_read, LedgerMap } ;
26
26
use log:: { info, Level , LevelFilter , Metadata , Record } ;
27
+ use std:: time:: SystemTime ;
27
28
use std:: {
28
29
collections:: HashMap ,
29
30
io:: { self , BufReader , Seek , Write } ,
@@ -188,7 +189,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
188
189
panic ! ( "You must specify an identity to register" ) ;
189
190
}
190
191
} 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 ;
192
193
let nonce_string = hex:: encode ( & nonce_bytes) ;
193
194
194
195
println ! ( "0x{}" , nonce_string) ;
@@ -197,10 +198,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
197
198
let dcc_ident = DccIdentity :: load_from_dir ( & PathBuf :: from ( np_desc) ) ?;
198
199
let ic_auth = dcc_to_ic_auth ( & dcc_ident) ;
199
200
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 ( ) ;
204
222
let nonce_string = hex:: encode ( & nonce_bytes) ;
205
223
206
224
info ! (
@@ -210,7 +228,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
210
228
nonce_string,
211
229
nonce_bytes. len( )
212
230
) ;
213
- let ic_auth = dcc_to_ic_auth ( & dcc_ident) ;
214
231
let result = ledger_canister ( ic_auth)
215
232
. await ?
216
233
. node_provider_check_in (
@@ -421,8 +438,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
421
438
}
422
439
print ! ( "{}" , table) ;
423
440
}
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 ;
426
443
println ! ( "{}" , hex:: encode( nonce_bytes) ) ;
427
444
}
428
445
"get_logs_debug" => {
@@ -625,6 +642,9 @@ async fn ledger_data_fetch(
625
642
local_ledger_path. display( )
626
643
) ;
627
644
}
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
+
628
648
Ok ( ( ) )
629
649
}
630
650
0 commit comments