Skip to content

Commit

Permalink
fixed inclusion e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 29, 2024
1 parent 90002bf commit a197591
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
14 changes: 8 additions & 6 deletions yarn-project/aztec-nr/aztec/src/oracle/get_header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ unconstrained pub fn get_header_internal(block_number: u32) -> Header {
}

pub fn get_header(block_number: u32, context: PrivateContext) -> Header {
// 1) Get block number corresponding to block header inside context
let header_block_number = context.header.global_variables.block_number as u32;
// 1) Get block number corresponding to the last_archive root in the header
// Note: We subtract 1 because the last_archive root is the root of the archive after applying the previous block
let last_archive_block_number = (context.header.global_variables.block_number - 1) as u32;

// 2) Check that the header block number is more than or equal to the block number we want to prove against
// We could not perform the proof otherwise because the archive root from the header would not "contain" the header we want to prove against
// 2) Check that the last archive block number is more than or equal to the block number we want to prove against
// We could not perform the proof otherwise because the last archive root from the header would not "contain"
// the header we want to prove against
assert(
header_block_number >= block_number, "Header block number is smaller than the block number we want to prove against"
last_archive_block_number >= block_number, "Last archive block number is smaller than the block number we want to prove against"
);

// 3) Get the header of a given block from oracle
Expand All @@ -34,7 +36,7 @@ pub fn get_header(block_number: u32, context: PrivateContext) -> Header {
let block_hash = header.block_hash();

// 5) Get the membership witness of the block in the archive
let witness = get_archive_membership_witness(header_block_number, block_hash);
let witness = get_archive_membership_witness(last_archive_block_number, block_hash);

// 6) Check that the block is in the archive (i.e. the witness is valid)
assert(
Expand Down
37 changes: 32 additions & 5 deletions yarn-project/end-to-end/src/e2e_inclusion_proofs_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ describe('e2e_inclusion_proofs_contract', () => {
expect(receivedOwner).toEqual(owner.toField());
}

// We advance block to be able to prove against block with `noteCreationBlockNumber` --> we have to do this
// because `last_archive` root in the `Header` of aztec-nr contexts is the root of the archive after applying
// the previous block and not the current one.
await advanceBlock();

{
// Prove note inclusion in a given block.
const ignoredCommitment = 0; // Not ignored only when the note doesn't exist
Expand All @@ -83,7 +88,7 @@ describe('e2e_inclusion_proofs_contract', () => {
// Prove that the note has not been nullified
// TODO(#3535): Prove the nullifier non-inclusion at older block to test archival node. This is currently not
// possible because of issue https://github.com/AztecProtocol/aztec-packages/issues/3535
const blockNumber = await pxe.getBlockNumber();
const blockNumber = await getLatestUsableBlockNumber();
const ignoredNullifier = 0; // Not ignored only when the note doesn't exist
await contract.methods.test_nullifier_non_inclusion_proof(owner, blockNumber, ignoredNullifier).send().wait();
}
Expand All @@ -94,7 +99,10 @@ describe('e2e_inclusion_proofs_contract', () => {
const { newNullifiers } = receipt.debugInfo!;
expect(newNullifiers.length).toBe(2);

const blockNumber = await pxe.getBlockNumber();
// Once again we advance the block to be able to access the correct archive root,
await advanceBlock();

const blockNumber = await getLatestUsableBlockNumber();
const nullifier = newNullifiers[1];
// Note: getLowNullifierMembershipWitness returns the membership witness of the nullifier itself and not
// the low nullifier when the nullifier already exists in the tree and for this reason the execution fails
Expand Down Expand Up @@ -126,6 +134,11 @@ describe('e2e_inclusion_proofs_contract', () => {
expect(receivedOwner).toEqual(owner.toField());
}

// We advance block to be able to prove against block with `noteCreationBlockNumber` --> we have to do this
// because `last_archive` root in the `Header` of aztec-nr contexts is the root of the archive after applying
// the previous block and not the current one.
await advanceBlock();

{
// Prove note validity
await contract.methods.test_note_validity_proof(owner, noteCreationBlockNumber).send().wait();
Expand Down Expand Up @@ -251,12 +264,26 @@ describe('e2e_inclusion_proofs_contract', () => {
});

const getRandomBlockNumberSinceDeployment = async () => {
const currentBlockNumber = await pxe.getBlockNumber();
return deploymentBlockNumber + Math.floor(Math.random() * (currentBlockNumber - deploymentBlockNumber));
return (
deploymentBlockNumber + Math.floor(Math.random() * ((await getLatestUsableBlockNumber()) - deploymentBlockNumber))
);
};

const getRandomBlockNumber = async () => {
return (
deploymentBlockNumber + Math.floor(Math.random() * ((await getLatestUsableBlockNumber()) - INITIAL_L2_BLOCK_NUM))
);
};

const getLatestUsableBlockNumber = async () => {
const currentBlockNumber = await pxe.getBlockNumber();
return deploymentBlockNumber + Math.floor(Math.random() * (currentBlockNumber - INITIAL_L2_BLOCK_NUM));
// Note: We subtract 1 from current because the `last_archive` root in the `Header` of aztec-nr contexts is
// the root of the archive after applying the previous block and not the current one.
return currentBlockNumber - 1;
};

// We advance block by sending a transaction calling empty function on our contract.
const advanceBlock = async () => {
await contract.methods.empty().send().wait();
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ contract InclusionProofs {
// Here typically the factory would add the contract address to its internal map of deployed contracts.
}

#[aztec(private)]
fn empty() {}

// Computes note hash and nullifier.
// Note 1: Needs to be defined by every contract producing logs.
// Note 2: Having it in all the contracts gives us the ability to compute the note hash and nullifier differently for different kind of notes.
Expand Down

0 comments on commit a197591

Please sign in to comment.