Skip to content

Commit

Permalink
fix: split state validation status into kernel and rproof updates. (#…
Browse files Browse the repository at this point in the history
…3096)

* fix: split state validation status into kernel and rproof updates. And fix sync status for these two states

* fix: show correct number of leaves for pruned MMR as well as unpruned

* docs: better docs for kernel/range proof validation

* fix: ordering of kernel and rproofs validation in TUI

* fix: typo in rangeproofs api and comments
  • Loading branch information
JosephGoulden authored and hashmap committed Nov 17, 2019
1 parent 739a190 commit 6d864a8
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 199 deletions.
19 changes: 11 additions & 8 deletions api/src/handlers/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ fn sync_status_to_api(sync_status: SyncStatus) -> (String, Option<serde_json::Va
"txhashset_download".to_string(),
Some(json!({ "downloaded_size": downloaded_size, "total_size": total_size })),
),
SyncStatus::TxHashsetValidation {
kernels,
kernel_total,
SyncStatus::TxHashsetRangeProofsValidation {
rproofs,
rproof_total,
rproofs_total,
} => (
"txhashset_rangeproofs_validation".to_string(),
Some(json!({ "rproofs": rproofs, "rproofs_total": rproofs_total })),
),
SyncStatus::TxHashsetKernelsValidation {
kernels,
kernels_total,
} => (
"txhashset_validation".to_string(),
Some(
json!({ "kernels": kernels, "kernel_total": kernel_total ,"rproofs": rproofs, "rproof_total": rproof_total }),
),
"txhashset_kernels_validation".to_string(),
Some(json!({ "kernels": kernels, "kernels_total": kernels_total })),
),
SyncStatus::BodySync {
current_height,
Expand Down
12 changes: 6 additions & 6 deletions chain/src/txhashset/txhashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ impl<'a> Extension<'a> {
TxKernel::batch_sig_verify(&tx_kernels)?;
kern_count += tx_kernels.len() as u64;
tx_kernels.clear();
status.on_validation(kern_count, total_kernels, 0, 0);
status.on_validation_kernels(kern_count, total_kernels);
debug!(
"txhashset: verify_kernel_signatures: verified {} signatures",
kern_count,
Expand All @@ -1305,7 +1305,8 @@ impl<'a> Extension<'a> {
let mut proofs: Vec<RangeProof> = Vec::with_capacity(1_000);

let mut proof_count = 0;
let total_rproofs = pmmr::n_leaves(self.output_pmmr.unpruned_size());
let total_rproofs = self.output_pmmr.n_unpruned_leaves();

for pos in self.output_pmmr.leaf_pos_iter() {
let output = self.output_pmmr.get_data(pos);
let proof = self.rproof_pmmr.get_data(pos);
Expand All @@ -1331,10 +1332,9 @@ impl<'a> Extension<'a> {
"txhashset: verify_rangeproofs: verified {} rangeproofs",
proof_count,
);
}

if proof_count % 1_000 == 0 {
status.on_validation(0, 0, proof_count, total_rproofs);
if proof_count % 1_000 == 0 {
status.on_validation_rproofs(proof_count, total_rproofs);
}
}
}

Expand Down
69 changes: 25 additions & 44 deletions chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ pub enum SyncStatus {
},
/// Setting up before validation
TxHashsetSetup,
/// Validating the full state
TxHashsetValidation {
/// Validating the kernels
TxHashsetKernelsValidation {
kernels: u64,
kernel_total: u64,
kernels_total: u64,
},
/// Validating the range proofs
TxHashsetRangeProofsValidation {
rproofs: u64,
rproof_total: u64,
rproofs_total: u64,
},
/// Finalizing the new state
TxHashsetSave,
Expand Down Expand Up @@ -155,43 +158,18 @@ impl TxHashsetWriteStatus for SyncState {
self.update(SyncStatus::TxHashsetSetup);
}

fn on_validation(&self, vkernels: u64, vkernel_total: u64, vrproofs: u64, vrproof_total: u64) {
let mut status = self.current.write();
match *status {
SyncStatus::TxHashsetValidation {
kernels,
kernel_total,
rproofs,
rproof_total,
} => {
let ks = if vkernels > 0 { vkernels } else { kernels };
let kt = if vkernel_total > 0 {
vkernel_total
} else {
kernel_total
};
let rps = if vrproofs > 0 { vrproofs } else { rproofs };
let rpt = if vrproof_total > 0 {
vrproof_total
} else {
rproof_total
};
*status = SyncStatus::TxHashsetValidation {
kernels: ks,
kernel_total: kt,
rproofs: rps,
rproof_total: rpt,
};
}
_ => {
*status = SyncStatus::TxHashsetValidation {
kernels: 0,
kernel_total: 0,
rproofs: 0,
rproof_total: 0,
}
}
}
fn on_validation_kernels(&self, kernels: u64, kernels_total: u64) {
self.update(SyncStatus::TxHashsetKernelsValidation {
kernels,
kernels_total,
});
}

fn on_validation_rproofs(&self, rproofs: u64, rproofs_total: u64) {
self.update(SyncStatus::TxHashsetRangeProofsValidation {
rproofs,
rproofs_total,
});
}

fn on_save(&self) {
Expand Down Expand Up @@ -315,8 +293,10 @@ pub trait ChainAdapter {
pub trait TxHashsetWriteStatus {
/// First setup of the txhashset
fn on_setup(&self);
/// Starting validation
fn on_validation(&self, kernels: u64, kernel_total: u64, rproofs: u64, rproof_total: u64);
/// Starting kernel validation
fn on_validation_kernels(&self, kernels: u64, kernel_total: u64);
/// Starting rproof validation
fn on_validation_rproofs(&self, rproofs: u64, rproof_total: u64);
/// Starting to save the txhashset and related data
fn on_save(&self);
/// Done writing a new txhashset
Expand All @@ -328,7 +308,8 @@ pub struct NoStatus;

impl TxHashsetWriteStatus for NoStatus {
fn on_setup(&self) {}
fn on_validation(&self, _ks: u64, _kts: u64, _rs: u64, _rt: u64) {}
fn on_validation_kernels(&self, _ks: u64, _kts: u64) {}
fn on_validation_rproofs(&self, _rs: u64, _rt: u64) {}
fn on_save(&self) {}
fn on_done(&self) {}
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/core/pmmr/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub trait Backend<T: PMMRable> {
/// Iterator over current (unpruned, unremoved) leaf positions.
fn leaf_pos_iter(&self) -> Box<dyn Iterator<Item = u64> + '_>;

/// Number of leaves
fn n_unpruned_leaves(&self) -> u64;

/// Remove Hash by insertion position. An index is also provided so the
/// underlying backend can implement some rollback of positions up to a
/// given index (practically the index is the height of a block that
Expand Down
6 changes: 5 additions & 1 deletion core/src/core/pmmr/pmmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ where
self.backend.leaf_pos_iter()
}

/// Number of leafs in the MMR
pub fn n_unpruned_leaves(&self) -> u64 {
self.backend.n_unpruned_leaves()
}

/// Returns a vec of the peaks of this MMR.
pub fn peaks(&self) -> Vec<Hash> {
let peaks_pos = peaks(self.last_pos);
Expand Down Expand Up @@ -490,7 +495,6 @@ pub fn peak_map_height(mut pos: u64) -> (u64, u64) {
/// The height of a node in a full binary tree from its postorder traversal
/// index. This function is the base on which all others, as well as the MMR,
/// are built.
pub fn bintree_postorder_height(num: u64) -> u64 {
if num == 0 {
return 0;
Expand Down
4 changes: 4 additions & 0 deletions core/tests/vec_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ impl<T: PMMRable> Backend<T> for VecBackend<T> {
unimplemented!()
}

fn n_unpruned_leaves(&self) -> u64 {
unimplemented!()
}

fn remove(&mut self, position: u64) -> Result<(), String> {
self.remove_list.push(position);
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion servers/src/grin/sync/syncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ impl SyncRunner {
match self.sync_state.status() {
SyncStatus::TxHashsetDownload { .. }
| SyncStatus::TxHashsetSetup
| SyncStatus::TxHashsetValidation { .. }
| SyncStatus::TxHashsetRangeProofsValidation { .. }
| SyncStatus::TxHashsetKernelsValidation { .. }
| SyncStatus::TxHashsetSave
| SyncStatus::TxHashsetDone => check_state_sync = true,
_ => {
Expand Down
Loading

0 comments on commit 6d864a8

Please sign in to comment.