From d8a86969129222e54ca71b68f8c062525ffdc90d Mon Sep 17 00:00:00 2001 From: Yan Liu Date: Sun, 2 Feb 2025 16:17:06 +0100 Subject: [PATCH] fix: Automatically refresh local ledger when pulling new data --- cli/src/argparse.rs | 8 ++++++++ cli/src/commands/account.rs | 7 +++++++ cli/src/commands/ledger.rs | 9 ++++++--- cli/src/commands/mod.rs | 9 ++++++++- cli/src/commands/np.rs | 4 ++-- cli/src/ledger/data_operations.rs | 6 +++++- 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/cli/src/argparse.rs b/cli/src/argparse.rs index 9ee2b58..00bf48a 100644 --- a/cli/src/argparse.rs +++ b/cli/src/argparse.rs @@ -76,6 +76,10 @@ pub struct AccountArgs { #[arg(long, requires = "identity")] pub balance: bool, + /// List all accounts in the local ledger + #[arg(long, visible_aliases = ["list-accounts"])] + pub list_all: bool, + /// Transfer funds to another account #[arg(long, requires = "identity")] pub transfer_to: Option, @@ -163,6 +167,10 @@ pub struct LedgerLocalArgs { /// List ledger entries #[arg(long)] pub list_entries: bool, + + /// List all accounts in the local ledger + #[arg(long)] + pub list_accounts: bool, } #[derive(Subcommand, PartialEq)] diff --git a/cli/src/commands/account.rs b/cli/src/commands/account.rs index d1cee87..6622959 100644 --- a/cli/src/commands/account.rs +++ b/cli/src/commands/account.rs @@ -1,5 +1,7 @@ use crate::argparse::AccountArgs; +use crate::identity::{list_identities, ListIdentityType}; use crate::ledger::handle_funds_transfer; +use crate::LedgerMap; use candid::Principal as IcPrincipal; use dcc_common::{ account_balance_get_as_string, DccIdentity, IcrcCompatibleAccount, TokenAmountE9s, @@ -12,7 +14,12 @@ pub async fn handle_account_command( network_url: &str, ledger_canister_id: IcPrincipal, identity: Option, + ledger_local: &LedgerMap, ) -> Result<(), Box> { + if account_args.list_all { + return list_identities(ledger_local, ListIdentityType::All, true); + } + let identity = identity.expect("Identity must be specified for this command, use --identity"); let dcc_id = DccIdentity::load_from_dir(&PathBuf::from(&identity))?; diff --git a/cli/src/commands/ledger.rs b/cli/src/commands/ledger.rs index 7d874da..f26e202 100644 --- a/cli/src/commands/ledger.rs +++ b/cli/src/commands/ledger.rs @@ -1,4 +1,5 @@ use crate::argparse::{LedgerLocalArgs, LedgerRemoteCommands}; +use crate::identity::{list_identities, ListIdentityType}; use base64::engine::general_purpose::STANDARD as BASE64; use base64::Engine; use candid::{Decode, Encode}; @@ -15,7 +16,9 @@ pub async fn handle_ledger_local_command( local_args: LedgerLocalArgs, ledger_local: LedgerMap, ) -> Result<(), Box> { - if local_args.list_entries { + if local_args.list_accounts { + return list_identities(&ledger_local, ListIdentityType::All, true); + } else if local_args.list_entries { println!("Entries:"); for entry in ledger_local.iter(None) { match entry.label() { @@ -44,7 +47,7 @@ pub async fn handle_ledger_remote_command( network_url: &str, ledger_canister_id: candid::Principal, identity: Option, - ledger_local: LedgerMap, + mut ledger_local: LedgerMap, ) -> Result<(), Box> { let local_ledger_path = ledger_local .get_file_path() @@ -54,7 +57,7 @@ pub async fn handle_ledger_remote_command( LedgerRemoteCommands::DataFetch => { let canister = LedgerCanister::new_without_identity(network_url, ledger_canister_id).await?; - return crate::ledger::ledger_data_fetch(&canister, &ledger_local).await; + return crate::ledger::ledger_data_fetch(&canister, &mut ledger_local).await; } LedgerRemoteCommands::DataPushAuthorize | LedgerRemoteCommands::DataPush => { let identity = diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 539b6c5..2f61691 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -45,7 +45,14 @@ pub async fn handle_command( match command { Commands::Keygen(args) => handle_keygen_command(args, identity).await, Commands::Account(args) => { - handle_account_command(args, network_url, ledger_canister_id, identity).await + handle_account_command( + args, + network_url, + ledger_canister_id, + identity, + &ledger_local, + ) + .await } Commands::Np(args) => { handle_np_command( diff --git a/cli/src/commands/np.rs b/cli/src/commands/np.rs index cab1de2..1c5fed4 100644 --- a/cli/src/commands/np.rs +++ b/cli/src/commands/np.rs @@ -14,7 +14,7 @@ pub async fn handle_np_command( network_url: &str, ledger_canister_id: candid::Principal, identity: Option, - ledger_local: LedgerMap, + mut ledger_local: LedgerMap, ) -> Result<(), Box> { match np_cmd { NpCommands::List(list_args) => { @@ -73,7 +73,7 @@ pub async fn handle_np_command( let canister = LedgerCanister::new_without_identity(network_url, ledger_canister_id) .await?; - ledger_data_fetch(&canister, &ledger_local).await?; + ledger_data_fetch(&canister, &mut ledger_local).await?; dcc_common::refresh_caches_from_ledger(&ledger_local) .expect("Loading balances from ledger failed"); diff --git a/cli/src/ledger/data_operations.rs b/cli/src/ledger/data_operations.rs index f88ecbc..d421374 100644 --- a/cli/src/ledger/data_operations.rs +++ b/cli/src/ledger/data_operations.rs @@ -16,7 +16,7 @@ const PUSH_BLOCK_SIZE: u64 = 1024 * 1024; pub async fn ledger_data_fetch( ledger_canister: &LedgerCanister, - ledger_local: &LedgerMap, + ledger_local: &mut LedgerMap, ) -> Result<(), Box> { let ledger_file_path = ledger_local .get_file_path() @@ -100,6 +100,10 @@ pub async fn ledger_data_fetch( // Set the modified time to the current time, to mark that the data is up-to-date filetime::set_file_mtime(ledger_file_path, std::time::SystemTime::now().into())?; + if !data.is_empty() { + ledger_local.refresh_ledger()?; + } + Ok(()) }