Skip to content

Commit

Permalink
piecrust: add test for proofs of inclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Leegwater Simões committed Nov 6, 2023
1 parent 16e0194 commit 4139620
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion piecrust/tests/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use piecrust::{contract_bytecode, ContractData, Error, SessionData, VM};
use piecrust::{
contract_bytecode, ContractData, Error, PageOpening, SessionData, VM,
};

const OWNER: [u8; 32] = [0u8; 32];
const LIMIT: u64 = 1_000_000;
Expand Down Expand Up @@ -56,3 +58,64 @@ pub fn state_root_calculation() -> Result<(), Error> {
assert_eq!(root_2, root_3, "The root of a session should be the same if no modifications were made");
Ok(())
}

#[test]
pub fn inclusion_proofs() -> Result<(), Error> {
let vm = VM::ephemeral()?;
let mut session = vm.session(SessionData::builder())?;

let counter_id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
LIMIT,
)?;

fn mapper(
(_, page, opening): (usize, &[u8], PageOpening),
) -> (Vec<u8>, PageOpening) {
(page.to_vec(), opening)
}

session.call::<_, ()>(counter_id, "increment", &(), LIMIT)?;

let pages = session
.memory_pages(counter_id)
.expect("There must be memory pages for the contract");

let (page_1, opening_1) = pages
.map(mapper)
.next()
.expect("There must be at least one page");

assert!(
opening_1.verify(&page_1),
"The page must be valid for the opening"
);

session.call::<_, ()>(counter_id, "increment", &(), LIMIT)?;

let pages = session
.memory_pages(counter_id)
.expect("There must be memory pages for the contract");

let (page_2, opening_2) = pages
.map(mapper)
.next()
.expect("There must be at least one page");

assert!(
opening_2.verify(&page_2),
"The page must be valid for the opening"
);

assert!(
!opening_1.verify(&page_2),
"The page must be invalid for the opening"
);
assert!(
!opening_2.verify(&page_1),
"The page must be invalid for the opening"
);

Ok(())
}

0 comments on commit 4139620

Please sign in to comment.