Skip to content

Commit

Permalink
test: testing historical header in contexts (AztecProtocol#4509)
Browse files Browse the repository at this point in the history
Testing that header is set as expected.
  • Loading branch information
benesjan committed Feb 8, 2024
1 parent 87a9663 commit c00229a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
15 changes: 10 additions & 5 deletions yarn-project/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ contract Test {
},
hash::hash_args,
};
// The following import is here in order to make the event macro work because the macro doesn't add the import.
// It doesn't add the import because in the future we will re-export all the types via aztec-nr and aztec-nr is
// already auto-imported by the macros.
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Remove this once the issue is fixed.
use dep::aztec::protocol_types;
// docs:start:unencrypted_import
use dep::aztec::log::emit_unencrypted_log;
// docs:end:unencrypted_import
Expand Down Expand Up @@ -324,6 +319,16 @@ contract Test {
assert(context.fee_recipient() == fee_recipient, "Invalid fee recipient");
}

#[aztec(private)]
fn assert_header_private(header_hash: Field) {
assert(context.historical_header.hash() == header_hash, "Invalid header hash");
}

#[aztec(public)]
fn assert_header_public(header_hash: Field) {
assert(context.historical_header.hash() == header_hash, "Invalid header hash");
}

unconstrained fn get_constant() -> pub Field {
let constant = storage.example_constant.view_note();
constant.value
Expand Down
29 changes: 28 additions & 1 deletion yarn-project/simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
computeVarArgsHash,
siloCommitment,
} from '@aztec/circuits.js/abis';
import { makeContractDeploymentData } from '@aztec/circuits.js/factories';
import { makeContractDeploymentData, makeHeader } from '@aztec/circuits.js/factories';
import {
FunctionArtifact,
FunctionSelector,
Expand Down Expand Up @@ -1166,4 +1166,31 @@ describe('Private Execution test suite', () => {
).rejects.toThrowError('Invalid version');
});
});

describe('Historical header in private context', () => {
let artifact: FunctionArtifact;

beforeAll(() => {
artifact = getFunctionArtifact(TestContractArtifact, 'assert_header_private');
oracle.getFunctionArtifact.mockImplementation(() => Promise.resolve(artifact));

header = makeHeader();

oracle.getHeader.mockClear();
oracle.getHeader.mockResolvedValue(header);
});

it('Header is correctly set', () => {
const args = [header.hash()];

expect(() => runSimulator({ artifact, msgSender: owner, args })).not.toThrow();
});

it('Throws when header is not as expected', async () => {
const unexpectedHeaderHash = Fr.random();
const args = [unexpectedHeaderHash];

await expect(runSimulator({ artifact, msgSender: owner, args })).rejects.toThrowError('Invalid header hash');
});
});
});
51 changes: 50 additions & 1 deletion yarn-project/simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Header,
L1_TO_L2_MSG_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { makeHeader } from '@aztec/circuits.js/factories';
import { FunctionArtifact, FunctionSelector, encodeArguments } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { pedersenHash } from '@aztec/foundation/crypto';
Expand Down Expand Up @@ -42,7 +43,9 @@ describe('ACIR public execution simulator', () => {
publicContracts = mock<PublicContractsDB>();
commitmentsDb = mock<CommitmentsDB>();

header = Header.empty();
const randomInt = Math.floor(Math.random() * 1000000);
header = makeHeader(randomInt);

executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);
}, 10000);

Expand Down Expand Up @@ -718,4 +721,50 @@ describe('ACIR public execution simulator', () => {
});
});
});

describe('Historical header in public context', () => {
let contractAddress: AztecAddress;
let callContext: CallContext;
let assertHeaderPublicArtifact: FunctionArtifact;
let functionData: FunctionData;

beforeAll(() => {
contractAddress = AztecAddress.random();
callContext = CallContext.from({
msgSender: AztecAddress.random(),
storageContractAddress: AztecAddress.random(),
portalContractAddress: EthAddress.ZERO,
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
startSideEffectCounter: 0,
});
assertHeaderPublicArtifact = TestContractArtifact.functions.find(f => f.name === 'assert_header_public')!;
functionData = FunctionData.fromAbi(assertHeaderPublicArtifact);
});

beforeEach(() => {
publicContracts.getBytecode.mockResolvedValue(Buffer.from(assertHeaderPublicArtifact.bytecode, 'base64'));
});

it('Header is correctly set', () => {
const args = encodeArguments(assertHeaderPublicArtifact, [header.hash()]);

const execution: PublicExecution = { contractAddress, functionData, args, callContext };
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);

expect(() => executor.simulate(execution, GlobalVariables.empty())).not.toThrow();
});

it('Throws when header is not as expected', async () => {
const unexpectedHeaderHash = Fr.random();
const args = encodeArguments(assertHeaderPublicArtifact, [unexpectedHeaderHash]);

const execution: PublicExecution = { contractAddress, functionData, args, callContext };
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);

await expect(executor.simulate(execution, GlobalVariables.empty())).rejects.toThrowError('Invalid header hash');
});
});
});

0 comments on commit c00229a

Please sign in to comment.