Skip to content

Commit

Permalink
Check if the node's block height is < wallet height
Browse files Browse the repository at this point in the history
- prevent an assert from causing a panic in the wallet sync when the
  node is out of sync
OBorce committed Nov 4, 2023
1 parent bbb04f4 commit 55ce424
Showing 2 changed files with 37 additions and 3 deletions.
21 changes: 20 additions & 1 deletion wallet/wallet-controller/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

@@ -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)]
@@ -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)
}
@@ -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(())
}

19 changes: 17 additions & 2 deletions wallet/wallet-controller/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

@@ -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

0 comments on commit 55ce424

Please sign in to comment.