Skip to content

Commit

Permalink
Merge pull request #1348 from Oscar-Pepper/zingo_sync_reorg_logic
Browse files Browse the repository at this point in the history
Zingo sync reorg logic
  • Loading branch information
zancas authored Nov 22, 2024
2 parents 7bc2d8b + 5338143 commit b7360d1
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 204 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions libtonode-tests/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn sync_mainnet_test() {
let mut lightclient = LightClient::create_from_wallet_base_async(
WalletBase::from_string(HOSPITAL_MUSEUM_SEED.to_string()),
&config,
2_611_700,
2_715_150,
true,
)
.await
Expand Down Expand Up @@ -77,8 +77,8 @@ async fn sync_test() {
.await
.unwrap();

dbg!(recipient.wallet.wallet_transactions());
dbg!(recipient.wallet.wallet_blocks());
dbg!(recipient.wallet.nullifier_map());
dbg!(recipient.wallet.sync_state());
// dbg!(recipient.wallet.wallet_transactions());
// dbg!(recipient.wallet.wallet_blocks());
// dbg!(recipient.wallet.nullifier_map());
// dbg!(recipient.wallet.sync_state());
}
3 changes: 3 additions & 0 deletions zingo-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ rayon.workspace = true
# Minimise boilerplate
getset.workspace = true

# Error handling
thiserror.workspace = true

# temp
zcash_address.workspace = true
sha2.workspace = true
Expand Down
11 changes: 11 additions & 0 deletions zingo-sync/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Top level error module for the crate
use crate::scan::error::ScanError;

/// Top level error enum encapsulating any error that may occur during sync
#[derive(Debug, thiserror::Error)]
pub enum SyncError {
/// Errors associated with scanning
#[error("Scan error. {0}")]
ScanError(#[from] ScanError),
}
1 change: 1 addition & 0 deletions zingo-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! Entrypoint: [`crate::sync::sync`]
pub mod client;
pub mod error;
pub(crate) mod keys;
#[allow(missing_docs)]
pub mod primitives;
Expand Down
9 changes: 9 additions & 0 deletions zingo-sync/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl SyncState {
spend_locations: Vec::new(),
}
}

pub fn fully_scanned(&self) -> bool {
self.scan_ranges().iter().all(|scan_range| {
matches!(
scan_range.priority(),
zcash_client_backend::data_api::scanning::ScanPriority::Scanned
)
})
}
}

impl Default for SyncState {
Expand Down
14 changes: 8 additions & 6 deletions zingo-sync/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ use crate::{
witness::ShardTreeData,
};

use self::{compact_blocks::scan_compact_blocks, transactions::scan_transactions};
use self::{
compact_blocks::scan_compact_blocks, error::ScanError, transactions::scan_transactions,
};

mod compact_blocks;
pub mod error;
pub(crate) mod task;
pub(crate) mod transactions;

Expand Down Expand Up @@ -136,15 +139,15 @@ impl DecryptedNoteData {
}
}

// scans a given range and returns all data relevant to the specified keys
// `previous_wallet_block` is the wallet block with height [scan_range.start - 1]
/// Scans a given range and returns all data relevant to the specified keys.
/// `previous_wallet_block` is the wallet block with height [scan_range.start - 1].
pub(crate) async fn scan<P>(
fetch_request_sender: mpsc::UnboundedSender<FetchRequest>,
parameters: &P,
ufvks: &HashMap<AccountId, UnifiedFullViewingKey>,
scan_range: ScanRange,
previous_wallet_block: Option<WalletBlock>,
) -> Result<ScanResults, ()>
) -> Result<ScanResults, ScanError>
where
P: Parameters + Sync + Send + 'static,
{
Expand All @@ -166,8 +169,7 @@ where
.await
.unwrap();

let scan_data =
scan_compact_blocks(compact_blocks, parameters, ufvks, initial_scan_data).unwrap();
let scan_data = scan_compact_blocks(compact_blocks, parameters, ufvks, initial_scan_data)?;

let ScanData {
nullifiers,
Expand Down
22 changes: 16 additions & 6 deletions zingo-sync/src/scan/compact_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use crate::{

use self::runners::{BatchRunners, DecryptedOutput};

use super::{DecryptedNoteData, InitialScanData, ScanData};
use super::{
error::{ContinuityError, ScanError},
DecryptedNoteData, InitialScanData, ScanData,
};

mod runners;

Expand All @@ -32,11 +35,11 @@ pub(crate) fn scan_compact_blocks<P>(
parameters: &P,
ufvks: &HashMap<AccountId, UnifiedFullViewingKey>,
initial_scan_data: InitialScanData,
) -> Result<ScanData, ()>
) -> Result<ScanData, ScanError>
where
P: Parameters + Sync + Send + 'static,
{
check_continuity(&compact_blocks, initial_scan_data.previous_block.as_ref()).unwrap();
check_continuity(&compact_blocks, initial_scan_data.previous_block.as_ref())?;

let scanning_keys = ScanningKeys::from_account_ufvks(ufvks.clone());
let mut runners = trial_decrypt(parameters, &scanning_keys, &compact_blocks).unwrap();
Expand Down Expand Up @@ -162,7 +165,7 @@ where
fn check_continuity(
compact_blocks: &[CompactBlock],
previous_compact_block: Option<&WalletBlock>,
) -> Result<(), ()> {
) -> Result<(), ContinuityError> {
let mut prev_height: Option<BlockHeight> = None;
let mut prev_hash: Option<BlockHash> = None;

Expand All @@ -174,13 +177,20 @@ fn check_continuity(
for block in compact_blocks {
if let Some(prev_height) = prev_height {
if block.height() != prev_height + 1 {
panic!("height discontinuity");
return Err(ContinuityError::HeightDiscontinuity {
height: block.height(),
previous_block_height: prev_height,
});
}
}

if let Some(prev_hash) = prev_hash {
if block.prev_hash() != prev_hash {
panic!("hash discontinuity");
return Err(ContinuityError::HashDiscontinuity {
height: block.height(),
prev_hash: block.prev_hash(),
previous_block_hash: prev_hash,
});
}
}

Expand Down
22 changes: 22 additions & 0 deletions zingo-sync/src/scan/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use zcash_primitives::{block::BlockHash, consensus::BlockHeight};

#[derive(Debug, thiserror::Error)]
pub enum ScanError {
#[error("Continuity error. {0}")]
ContinuityError(#[from] ContinuityError),
}

#[derive(Debug, thiserror::Error)]
pub enum ContinuityError {
#[error("Height discontinuity. Block with height {height} is not continuous with previous block height {previous_block_height}")]
HeightDiscontinuity {
height: BlockHeight,
previous_block_height: BlockHeight,
},
#[error("Hash discontinuity. Block prev_hash {prev_hash} with height {height} does not match previous block hash {previous_block_hash}")]
HashDiscontinuity {
height: BlockHeight,
prev_hash: BlockHash,
previous_block_hash: BlockHash,
},
}
Loading

0 comments on commit b7360d1

Please sign in to comment.