Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
utreexo: verify method (#410)
Browse files Browse the repository at this point in the history
* utreexo: add Forest::verify

* zkvm: iterators for inputs and outputs

* spec: xref fixed
  • Loading branch information
oleganza authored Mar 17, 2020
1 parent 40385be commit 22b0b61
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
30 changes: 30 additions & 0 deletions blockchain/src/utreexo/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ pub enum Proof {
Committed(Path),
}

impl Proof {
/// Converts the proof to path.
/// Returns None for Transient proof.
pub fn as_path(&self) -> Option<&Path> {
match self {
Proof::Transient => None,
Proof::Committed(path) => Some(path),
}
}
}

impl Forest {
/// Creates a new instance of Forest.
pub fn new() -> Self {
Expand All @@ -82,6 +93,25 @@ impl Forest {
.fold(0u64, |total, (level, _)| total + (1 << level))
}

/// Verifies that the given item and a path belong to the forest.
pub fn verify<M: MerkleItem>(
&self,
item: &M,
path: &Path,
hasher: &Hasher<M>,
) -> Result<(), UtreexoError> {
let computed_root = path.compute_root(item, hasher);
if let Some((_i, level)) =
find_root(self.roots_iter().map(|(level, _)| level), path.position)
{
// unwrap won't fail because `find_root` returns level for the actually existing root.
if self.roots[level].unwrap() == computed_root {
return Ok(());
}
}
Err(UtreexoError::InvalidProof)
}

/// Lets use modify the utreexo and yields a new state of the utreexo,
/// along with a catchup structure.
pub fn work_forest(&self) -> WorkForest {
Expand Down
4 changes: 4 additions & 0 deletions blockchain/src/utreexo/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ fn transaction_success() {
wf.delete(&Item(0), &proofs1[0], &hasher)
.expect("Should not fail.");

forest1
.verify(&Item(1), &proofs1[1].as_path().unwrap(), &hasher)
.expect("Should not fail.");

// d
// |\
// a b c new
Expand Down
2 changes: 1 addition & 1 deletion zkvm/docs/zkvm-stubnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Periodically, every 60 seconds:
When [`GetBlock`](#getblock) message is received,
we reply immediately with the block requested using [`Block`](#block) message.

When [`Blocks`](#blocks) message is received:
When [`Block`](#block) message is received:
1. If the block is a direct descendant:
1. It is verified and advances the state.
2. Orphan blocks from other peers are tried to be applied.
Expand Down
16 changes: 16 additions & 0 deletions zkvm/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ impl TxLog {
pub fn push(&mut self, item: TxEntry) {
self.0.push(item);
}

/// Iterator over the input entries
pub fn inputs(&self) -> impl Iterator<Item = &ContractID> {
self.0.iter().filter_map(|entry| match entry {
TxEntry::Input(contract_id) => Some(contract_id),
_ => None,
})
}

/// Iterator over the output entries
pub fn outputs(&self) -> impl Iterator<Item = &Contract> {
self.0.iter().filter_map(|entry| match entry {
TxEntry::Output(contract) => Some(contract),
_ => None,
})
}
}

impl From<Vec<TxEntry>> for TxLog {
Expand Down

0 comments on commit 22b0b61

Please sign in to comment.