Skip to content

Commit

Permalink
Make get_header constrained
Browse files Browse the repository at this point in the history
  • Loading branch information
LogvinovLeon committed Jan 24, 2024
1 parent e5c8fae commit 4aad4ae
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions circuit/src/get_header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,40 @@ struct BlockHeaderPartial {
#[oracle(get_header)]
unconstrained fn get_header_oracle(_block_no: Field) -> BlockHeaderPartial {}

unconstrained pub(crate) fn get_header(block_no: Field) -> BlockHeaderPartial {
let header = get_header_oracle(block_no);
unconstrained fn get_header_unconstrained(block_no: Field) -> BlockHeaderPartial {
get_header_oracle(block_no)
}

pub(crate) fn get_header(block_no: Field) -> BlockHeaderPartial {
let header = get_header_unconstrained(block_no);
let rlp_list:RLP_List<HEADER_FIELDS_COUNT> = decode1(header.encoded);
assert(
sub_array_equals(
header.stateRoot,
header.encoded,
rlp_list.offset[STATE_ROOT_INDEX]
)
), "stateRoot does not match"
);
assert(
sub_array_equals(
header.transactionsRoot,
header.encoded,
rlp_list.offset[TRANSACTIONS_ROOT_INDEX]
)
), "transactionsRoot does not match"
);
assert(
sub_array_equals(
header.receiptsRoot,
header.encoded,
rlp_list.offset[RECEIPTS_ROOT_INDEX]
)
), "receiptsRoot does not match"
);
assert(
sub_array_equals(
keccak256(header.encoded, header.encoded_len as u32),
header.hash,
0
)
), "hash does not match"
);
header
}
Expand Down Expand Up @@ -99,31 +103,31 @@ fn test_get_header_success() {
assert(get_header(0).stateRoot == stateRoot);
}

#[test(should_fail_with = "explicit trap hit in brillig")]
#[test(should_fail_with = "stateRoot does not match")]
fn test_get_header_invalid_state_root() {
let stateRoot = alter_array(stateRoot);
let blockPartial = BlockHeaderPartial { stateRoot, transactionsRoot, receiptsRoot, number, hash, encoded_len, encoded };
let _ = OracleMock::mock("get_header").returns(blockPartial);
assert(get_header(0).stateRoot == stateRoot);
}

#[test(should_fail_with = "explicit trap hit in brillig")]
#[test(should_fail_with = "transactionsRoot does not match")]
fn test_get_header_invalid_transactions_root() {
let transactionsRoot = alter_array(transactionsRoot);
let blockPartial = BlockHeaderPartial { stateRoot, transactionsRoot, receiptsRoot, number, hash, encoded_len, encoded };
let _ = OracleMock::mock("get_header").returns(blockPartial);
assert(get_header(0).stateRoot == stateRoot);
}

#[test(should_fail_with = "explicit trap hit in brillig")]
#[test(should_fail_with = "receiptsRoot does not match")]
fn test_get_header_invalid_receipt_root() {
let receiptsRoot = alter_array(receiptsRoot);
let blockPartial = BlockHeaderPartial { stateRoot, transactionsRoot, receiptsRoot, number, hash, encoded_len, encoded };
let _ = OracleMock::mock("get_header").returns(blockPartial);
assert(get_header(0).stateRoot == stateRoot);
}

#[test(should_fail_with = "explicit trap hit in brillig")]
#[test(should_fail_with = "hash does not match")]
fn test_get_header_invalid_hash() {
let hash = alter_array(hash);
let blockPartial = BlockHeaderPartial { stateRoot, transactionsRoot, receiptsRoot, number, hash, encoded_len, encoded };
Expand Down

0 comments on commit 4aad4ae

Please sign in to comment.