Skip to content

Commit

Permalink
Merge pull request #1344 from mintlayer/fix/wallet-panic-on-sync
Browse files Browse the repository at this point in the history
Check if the node's block height is < wallet height
  • Loading branch information
TheQuantumPhysicist authored Nov 4, 2023
2 parents bbb04f4 + 55ce424 commit a0a53e5
Show file tree
Hide file tree
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
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

0 comments on commit a0a53e5

Please sign in to comment.