Skip to content

Commit

Permalink
fix: Automatically refresh local ledger when pulling new data
Browse files Browse the repository at this point in the history
  • Loading branch information
yanliu38 committed Feb 2, 2025
1 parent 3614550 commit d8a8696
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
8 changes: 8 additions & 0 deletions cli/src/argparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down Expand Up @@ -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)]
Expand Down
7 changes: 7 additions & 0 deletions cli/src/commands/account.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,7 +14,12 @@ pub async fn handle_account_command(
network_url: &str,
ledger_canister_id: IcPrincipal,
identity: Option<String>,
ledger_local: &LedgerMap,
) -> Result<(), Box<dyn std::error::Error>> {
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))?;

Expand Down
9 changes: 6 additions & 3 deletions cli/src/commands/ledger.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -15,7 +16,9 @@ pub async fn handle_ledger_local_command(
local_args: LedgerLocalArgs,
ledger_local: LedgerMap,
) -> Result<(), Box<dyn std::error::Error>> {
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() {
Expand Down Expand Up @@ -44,7 +47,7 @@ pub async fn handle_ledger_remote_command(
network_url: &str,
ledger_canister_id: candid::Principal,
identity: Option<String>,
ledger_local: LedgerMap,
mut ledger_local: LedgerMap,
) -> Result<(), Box<dyn std::error::Error>> {
let local_ledger_path = ledger_local
.get_file_path()
Expand All @@ -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 =
Expand Down
9 changes: 8 additions & 1 deletion cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/np.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn handle_np_command(
network_url: &str,
ledger_canister_id: candid::Principal,
identity: Option<String>,
ledger_local: LedgerMap,
mut ledger_local: LedgerMap,
) -> Result<(), Box<dyn std::error::Error>> {
match np_cmd {
NpCommands::List(list_args) => {
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 5 additions & 1 deletion cli/src/ledger/data_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
let ledger_file_path = ledger_local
.get_file_path()
Expand Down Expand Up @@ -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(())
}

Expand Down

0 comments on commit d8a8696

Please sign in to comment.