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 2 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
4 changes: 3 additions & 1 deletion mm2src/coins/z_coin/storage/walletdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use mm2_err_handle::prelude::*;
use zcash_primitives::zip32::ExtendedSpendingKey;

cfg_native!(
use crate::z_coin::{CheckPointBlockInfo, ZcoinConsensusParams};
use crate::z_coin::{CheckPointBlockInfo, SyncStartPoint, ZcoinConsensusParams};
use crate::z_coin::z_rpc::create_wallet_db;

use parking_lot::Mutex;
Expand Down Expand Up @@ -40,6 +40,7 @@ impl<'a> WalletDbShared {
zcoin_builder: &ZCoinBuilder<'a>,
checkpoint_block: Option<CheckPointBlockInfo>,
z_spending_key: &ExtendedSpendingKey,
sync_params: Option<SyncStartPoint>,
shamardy marked this conversation as resolved.
Show resolved Hide resolved
) -> 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),
sync_params,
)
.await
.mm_err(WalletDbError::ZcoinClientInitError)?;
Expand Down
26 changes: 20 additions & 6 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,
sync_params: Option<SyncStartPoint>,
) -> Result<WalletDb<ZcoinConsensusParams>, MmError<ZcoinClientInitError>> {
async_blocking({
move || -> Result<WalletDb<ZcoinConsensusParams>, MmError<ZcoinClientInitError>> {
Expand All @@ -378,10 +379,18 @@ pub async fn create_wallet_db(
.map_to_mm(|err| ZcoinClientInitError::ZcashDBError(err.to_string()))?;
init_wallet_db(&db).map_to_mm(|err| ZcoinClientInitError::ZcashDBError(err.to_string()))?;

// check if no sync_params was provided and continue syncing from last height in db if it's > 0.
let active_sync_block = min_sync_height.unwrap_or(0) > 0 && sync_params.is_none();
// 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()
|| (!active_sync_block && 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 && sync_params.is_some() {
shamardy marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -478,13 +487,18 @@ pub(super) async fn init_light_client<'a>(
.checkpoint_block_from_height(sync_height.max(sapling_activation_height))
.await?;

let wallet_db = WalletDbShared::new(builder, maybe_checkpoint_block, z_spending_key)
let wallet_db = WalletDbShared::new(builder, maybe_checkpoint_block, z_spending_key, sync_params.clone())
.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 {
// check if no sync_params was provided and continue syncing from last height in db if it's > 0.
let active_sync_block = min_height > 0 && sync_params.is_none();
if !active_sync_block && (sync_height != min_height as u64) {
// let user know we're clearing cache and resyncing from new provided height.
if min_height > 0 && sync_params.is_some() {
shamardy marked this conversation as resolved.
Show resolved Hide resolved
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 +560,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, None)
.await
.mm_err(|err| ZcoinClientInitError::ZcashDBError(err.to_string()))?;

Expand Down
Loading