Skip to content

Check if the node's block height is < wallet height #1344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion wallet/wallet-controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::{

use mempool::tx_accumulator::PackingStrategy;
use read::ReadOnlyController;
use sync::InSync;
use synced_controller::SyncedController;
use utils::tap_error_log::LogError;

Expand Down Expand Up @@ -89,6 +90,8 @@ pub enum ControllerError<T: NodeInterface> {
StakingRunning,
#[error("End-to-end encryption error: {0}")]
EndToEndEncryptionError(#[from] crypto::ephemeral_e2e::error::Error),
#[error("The node is not in sync yet")]
NodeNotInSyncYet,
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -136,7 +139,7 @@ impl<T: NodeInterface + Clone + Send + Sync + 'static, W: WalletEvents> Controll
};

log::info!("Syncing the wallet...");
controller.sync_once().await?;
controller.try_sync_once().await?;

Ok(controller)
}
Expand Down Expand Up @@ -527,13 +530,29 @@ impl<T: NodeInterface + Clone + Send + Sync + 'static, W: WalletEvents> Controll

/// Synchronize the wallet to the current node tip height and return
pub async fn sync_once(&mut self) -> Result<(), ControllerError<T>> {
let res = sync::sync_once(
&self.chain_config,
&self.rpc_client,
&mut self.wallet,
&self.wallet_events,
)
.await?;

match res {
InSync::Synced => Ok(()),
InSync::NodeOutOfSync => Err(ControllerError::NodeNotInSyncYet),
}
}

pub async fn try_sync_once(&mut self) -> Result<(), ControllerError<T>> {
sync::sync_once(
&self.chain_config,
&self.rpc_client,
&mut self.wallet,
&self.wallet_events,
)
.await?;

Ok(())
}

Expand Down
19 changes: 17 additions & 2 deletions wallet/wallet-controller/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ enum AccountType {
UnusedAccount,
}

pub enum InSync {
Synced,
NodeOutOfSync,
}

pub async fn sync_once<T: NodeInterface>(
chain_config: &ChainConfig,
rpc_client: &T,
wallet: &mut impl SyncingWallet,
wallet_events: &impl WalletEvents,
) -> Result<(), ControllerError<T>> {
) -> Result<InSync, ControllerError<T>> {
let mut print_flag = SetFlag::new();
let mut _log_on_exit = None;

Expand All @@ -131,7 +136,17 @@ pub async fn sync_once<T: NodeInterface>(
.all(|wallet_best_block| chain_info.best_block_id == wallet_best_block.0)
{
// if all accounts are on the latest tip nothing to sync
return Ok(());
return Ok(InSync::Synced);
}

if account_best_blocks
.values()
.chain(iter::once(&unused_account_best_block))
.any(|wallet_best_block| chain_info.best_block_height < wallet_best_block.1)
{
// If the wallet's block height is > node block height wait for the node to sync first
log::info!("Wallet syncing paused until the node syncs up to the height of the wallet");
return Ok(InSync::NodeOutOfSync);
}

wallet
Expand Down