Skip to content

Commit 55ce424

Browse files
committed
Check if the node's block height is < wallet height
- prevent an assert from causing a panic in the wallet sync when the node is out of sync
1 parent bbb04f4 commit 55ce424

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

wallet/wallet-controller/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use std::{
3333

3434
use mempool::tx_accumulator::PackingStrategy;
3535
use read::ReadOnlyController;
36+
use sync::InSync;
3637
use synced_controller::SyncedController;
3738
use utils::tap_error_log::LogError;
3839

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

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

138141
log::info!("Syncing the wallet...");
139-
controller.sync_once().await?;
142+
controller.try_sync_once().await?;
140143

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

528531
/// Synchronize the wallet to the current node tip height and return
529532
pub async fn sync_once(&mut self) -> Result<(), ControllerError<T>> {
533+
let res = sync::sync_once(
534+
&self.chain_config,
535+
&self.rpc_client,
536+
&mut self.wallet,
537+
&self.wallet_events,
538+
)
539+
.await?;
540+
541+
match res {
542+
InSync::Synced => Ok(()),
543+
InSync::NodeOutOfSync => Err(ControllerError::NodeNotInSyncYet),
544+
}
545+
}
546+
547+
pub async fn try_sync_once(&mut self) -> Result<(), ControllerError<T>> {
530548
sync::sync_once(
531549
&self.chain_config,
532550
&self.rpc_client,
533551
&mut self.wallet,
534552
&self.wallet_events,
535553
)
536554
.await?;
555+
537556
Ok(())
538557
}
539558

wallet/wallet-controller/src/sync/mod.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,17 @@ enum AccountType {
108108
UnusedAccount,
109109
}
110110

111+
pub enum InSync {
112+
Synced,
113+
NodeOutOfSync,
114+
}
115+
111116
pub async fn sync_once<T: NodeInterface>(
112117
chain_config: &ChainConfig,
113118
rpc_client: &T,
114119
wallet: &mut impl SyncingWallet,
115120
wallet_events: &impl WalletEvents,
116-
) -> Result<(), ControllerError<T>> {
121+
) -> Result<InSync, ControllerError<T>> {
117122
let mut print_flag = SetFlag::new();
118123
let mut _log_on_exit = None;
119124

@@ -131,7 +136,17 @@ pub async fn sync_once<T: NodeInterface>(
131136
.all(|wallet_best_block| chain_info.best_block_id == wallet_best_block.0)
132137
{
133138
// if all accounts are on the latest tip nothing to sync
134-
return Ok(());
139+
return Ok(InSync::Synced);
140+
}
141+
142+
if account_best_blocks
143+
.values()
144+
.chain(iter::once(&unused_account_best_block))
145+
.any(|wallet_best_block| chain_info.best_block_height < wallet_best_block.1)
146+
{
147+
// If the wallet's block height is > node block height wait for the node to sync first
148+
log::info!("Wallet syncing paused until the node syncs up to the height of the wallet");
149+
return Ok(InSync::NodeOutOfSync);
135150
}
136151

137152
wallet

0 commit comments

Comments
 (0)