Skip to content
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

fix(zcoin): resume previous sync if sync_params are not provided #1967

Merged
merged 3 commits into from
Sep 18, 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
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
Loading