Skip to content

Commit

Permalink
fix(zcoin): resume previous sync if sync_params are not provided (#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced authored Sep 18, 2023
1 parent d055cba commit b34c91a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions mm2src/coins/z_coin/storage/walletdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl<'a> WalletDbShared {
zcoin_builder: &ZCoinBuilder<'a>,
checkpoint_block: Option<CheckPointBlockInfo>,
z_spending_key: &ExtendedSpendingKey,
continue_from_prev_sync: bool,
) -> MmResult<Self, WalletDbError> {
let wallet_db = create_wallet_db(
zcoin_builder
Expand All @@ -48,6 +49,7 @@ impl<'a> WalletDbShared {
zcoin_builder.protocol_info.consensus_params.clone(),
checkpoint_block,
ExtendedFullViewingKey::from(z_spending_key),
continue_from_prev_sync,
)
.await
.mm_err(WalletDbError::ZcoinClientInitError)?;
Expand Down
27 changes: 19 additions & 8 deletions mm2src/coins/z_coin/z_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ pub async fn create_wallet_db(
consensus_params: ZcoinConsensusParams,
checkpoint_block: Option<CheckPointBlockInfo>,
evk: ExtendedFullViewingKey,
continue_from_prev_sync: bool,
) -> Result<WalletDb<ZcoinConsensusParams>, MmError<ZcoinClientInitError>> {
async_blocking({
move || -> Result<WalletDb<ZcoinConsensusParams>, MmError<ZcoinClientInitError>> {
Expand All @@ -380,8 +381,14 @@ pub async fn create_wallet_db(

// Check if the initial block height is less than the previous synchronization height and
// Rewind walletdb to the minimum possible height.
if db.get_extended_full_viewing_keys()?.is_empty() || init_block_height != min_sync_height {
info!("Older/Newer sync height detected!, rewinding walletdb to new height: {init_block_height:?}");
if db.get_extended_full_viewing_keys()?.is_empty()
|| (!continue_from_prev_sync && init_block_height != min_sync_height)
{
// let user know we're clearing cache and resyncing from new provided height.
if min_sync_height.unwrap_or(0) > 0 {
info!("Older/Newer sync height detected!, rewinding walletdb to new height: {init_block_height:?}");
}

let mut wallet_ops = db.get_update_ops().expect("get_update_ops always returns Ok");
wallet_ops
.rewind_to_height(u32::MIN.into())
Expand Down Expand Up @@ -477,14 +484,18 @@ pub(super) async fn init_light_client<'a>(
let maybe_checkpoint_block = light_rpc_clients
.checkpoint_block_from_height(sync_height.max(sapling_activation_height))
.await?;

let wallet_db = WalletDbShared::new(builder, maybe_checkpoint_block, z_spending_key)
let min_height = blocks_db.get_earliest_block().await?;
// check if no sync_params was provided and continue syncing from last height in db if it's > 0.
let continue_from_prev_sync = min_height > 0 && sync_params.is_none();
let wallet_db = WalletDbShared::new(builder, maybe_checkpoint_block, z_spending_key, continue_from_prev_sync)
.await
.mm_err(|err| ZcoinClientInitError::ZcashDBError(err.to_string()))?;

// Get min_height in blocks_db and rewind blocks_db to 0 if sync_height != min_height
let min_height = blocks_db.get_earliest_block().await?;
if sync_height != min_height as u64 {
if !continue_from_prev_sync && (sync_height != min_height as u64) {
// let user know we're clearing cache and resyncing from new provided height.
if min_height > 0 {
info!("Older/Newer sync height detected!, rewinding blocks_db to new height: {sync_height:?}");
}
blocks_db
.rewind_to_height(u32::MIN)
.map_err(|err| ZcoinClientInitError::ZcashDBError(err.to_string()))?;
Expand Down Expand Up @@ -546,7 +557,7 @@ pub(super) async fn init_native_client<'a>(
is_pre_sapling: false,
actual: checkpoint_height,
};
let wallet_db = WalletDbShared::new(builder, checkpoint_block, z_spending_key)
let wallet_db = WalletDbShared::new(builder, checkpoint_block, z_spending_key, true)
.await
.mm_err(|err| ZcoinClientInitError::ZcashDBError(err.to_string()))?;

Expand Down

0 comments on commit b34c91a

Please sign in to comment.