Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Aug 16, 2024
1 parent a01a0b6 commit cfd46ee
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions noir-projects/aztec-nr/aztec/src/oracle/header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ unconstrained pub fn get_header_at_internal(block_number: u32) -> Header {
}

pub fn get_header_at(block_number: u32, context: PrivateContext) -> Header {
let historical_header = context.historical_header;
let historical_block_number = historical_header.global_variables.block_number as u32;
let header = context.historical_header;
let current_block_number = header.global_variables.block_number as u32;

if (block_number == historical_block_number) {
if (block_number == current_block_number) {
// If the block number we want to prove against is the same as the block number in the historical header we
// skip the inclusion proofs and just return the historical header from context.
historical_header
header
} else {
// 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 = historical_block_number - 1;
let last_archive_block_number = current_block_number - 1;

// 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"
Expand All @@ -35,19 +35,19 @@ pub fn get_header_at(block_number: u32, context: PrivateContext) -> Header {
);

// 3) Get the header hint of a given block from an oracle
let header_hint = get_header_at_internal(block_number);
let historical = get_header_at_internal(block_number);

// 4) We make sure that the header hint we received from the oracle exists in the state tree and is the actual header
// at the desired block number
constrain_get_header_at_internal(
header_hint,
historical,
block_number,
last_archive_block_number,
historical_header.last_archive.root
header.last_archive.root
);

// 8) Return the block header
header_hint
// 5) Return the block header
historical
}
}

Expand Down Expand Up @@ -78,15 +78,18 @@ fn constrain_get_header_at_internal(
fn fetching_a_valid_but_different_header_should_fail() {
let mut env = TestEnvironment::new();

// We get an arbitrary header at block 1
let old_header = get_header_at_internal(1);
env.advance_block_to(3);

// The next call to getHeader at block 5 should resolve with this old header.
// This header exists, but we make the call return it at an incorrect block number.
let _ = OracleMock::mock("getHeader").with_params((5)).returns(old_header.serialize()).times(1);
// We get our current header for the last archive values.
let current_header = env.private().historical_header;

env.advance_block_to(6);
let historical_header = get_header_at_internal(1);

// We now try to get the header that our mock sets up. The oracle returns the old, incorrect header and should fail.
let _ = get_header_at(5, env.private());
// We pass in a different block number than the header received
constrain_get_header_at_internal(
historical_header,
2,
current_header.global_variables.block_number as u32 - 1,
current_header.last_archive.root
);
}

0 comments on commit cfd46ee

Please sign in to comment.