Skip to content

Commit c00229a

Browse files
authored
test: testing historical header in contexts (#4509)
Testing that header is set as expected.
1 parent 87a9663 commit c00229a

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

yarn-project/noir-contracts/contracts/test_contract/src/main.nr

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ contract Test {
1313
},
1414
hash::hash_args,
1515
};
16-
// The following import is here in order to make the event macro work because the macro doesn't add the import.
17-
// It doesn't add the import because in the future we will re-export all the types via aztec-nr and aztec-nr is
18-
// already auto-imported by the macros.
19-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Remove this once the issue is fixed.
20-
use dep::aztec::protocol_types;
2116
// docs:start:unencrypted_import
2217
use dep::aztec::log::emit_unencrypted_log;
2318
// docs:end:unencrypted_import
@@ -324,6 +319,16 @@ contract Test {
324319
assert(context.fee_recipient() == fee_recipient, "Invalid fee recipient");
325320
}
326321

322+
#[aztec(private)]
323+
fn assert_header_private(header_hash: Field) {
324+
assert(context.historical_header.hash() == header_hash, "Invalid header hash");
325+
}
326+
327+
#[aztec(public)]
328+
fn assert_header_public(header_hash: Field) {
329+
assert(context.historical_header.hash() == header_hash, "Invalid header hash");
330+
}
331+
327332
unconstrained fn get_constant() -> pub Field {
328333
let constant = storage.example_constant.view_note();
329334
constant.value

yarn-project/simulator/src/client/private_execution.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
computeVarArgsHash,
2626
siloCommitment,
2727
} from '@aztec/circuits.js/abis';
28-
import { makeContractDeploymentData } from '@aztec/circuits.js/factories';
28+
import { makeContractDeploymentData, makeHeader } from '@aztec/circuits.js/factories';
2929
import {
3030
FunctionArtifact,
3131
FunctionSelector,
@@ -1166,4 +1166,31 @@ describe('Private Execution test suite', () => {
11661166
).rejects.toThrowError('Invalid version');
11671167
});
11681168
});
1169+
1170+
describe('Historical header in private context', () => {
1171+
let artifact: FunctionArtifact;
1172+
1173+
beforeAll(() => {
1174+
artifact = getFunctionArtifact(TestContractArtifact, 'assert_header_private');
1175+
oracle.getFunctionArtifact.mockImplementation(() => Promise.resolve(artifact));
1176+
1177+
header = makeHeader();
1178+
1179+
oracle.getHeader.mockClear();
1180+
oracle.getHeader.mockResolvedValue(header);
1181+
});
1182+
1183+
it('Header is correctly set', () => {
1184+
const args = [header.hash()];
1185+
1186+
expect(() => runSimulator({ artifact, msgSender: owner, args })).not.toThrow();
1187+
});
1188+
1189+
it('Throws when header is not as expected', async () => {
1190+
const unexpectedHeaderHash = Fr.random();
1191+
const args = [unexpectedHeaderHash];
1192+
1193+
await expect(runSimulator({ artifact, msgSender: owner, args })).rejects.toThrowError('Invalid header hash');
1194+
});
1195+
});
11691196
});

yarn-project/simulator/src/public/index.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Header,
88
L1_TO_L2_MSG_TREE_HEIGHT,
99
} from '@aztec/circuits.js';
10+
import { makeHeader } from '@aztec/circuits.js/factories';
1011
import { FunctionArtifact, FunctionSelector, encodeArguments } from '@aztec/foundation/abi';
1112
import { AztecAddress } from '@aztec/foundation/aztec-address';
1213
import { pedersenHash } from '@aztec/foundation/crypto';
@@ -42,7 +43,9 @@ describe('ACIR public execution simulator', () => {
4243
publicContracts = mock<PublicContractsDB>();
4344
commitmentsDb = mock<CommitmentsDB>();
4445

45-
header = Header.empty();
46+
const randomInt = Math.floor(Math.random() * 1000000);
47+
header = makeHeader(randomInt);
48+
4649
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);
4750
}, 10000);
4851

@@ -718,4 +721,50 @@ describe('ACIR public execution simulator', () => {
718721
});
719722
});
720723
});
724+
725+
describe('Historical header in public context', () => {
726+
let contractAddress: AztecAddress;
727+
let callContext: CallContext;
728+
let assertHeaderPublicArtifact: FunctionArtifact;
729+
let functionData: FunctionData;
730+
731+
beforeAll(() => {
732+
contractAddress = AztecAddress.random();
733+
callContext = CallContext.from({
734+
msgSender: AztecAddress.random(),
735+
storageContractAddress: AztecAddress.random(),
736+
portalContractAddress: EthAddress.ZERO,
737+
functionSelector: FunctionSelector.empty(),
738+
isContractDeployment: false,
739+
isDelegateCall: false,
740+
isStaticCall: false,
741+
startSideEffectCounter: 0,
742+
});
743+
assertHeaderPublicArtifact = TestContractArtifact.functions.find(f => f.name === 'assert_header_public')!;
744+
functionData = FunctionData.fromAbi(assertHeaderPublicArtifact);
745+
});
746+
747+
beforeEach(() => {
748+
publicContracts.getBytecode.mockResolvedValue(Buffer.from(assertHeaderPublicArtifact.bytecode, 'base64'));
749+
});
750+
751+
it('Header is correctly set', () => {
752+
const args = encodeArguments(assertHeaderPublicArtifact, [header.hash()]);
753+
754+
const execution: PublicExecution = { contractAddress, functionData, args, callContext };
755+
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);
756+
757+
expect(() => executor.simulate(execution, GlobalVariables.empty())).not.toThrow();
758+
});
759+
760+
it('Throws when header is not as expected', async () => {
761+
const unexpectedHeaderHash = Fr.random();
762+
const args = encodeArguments(assertHeaderPublicArtifact, [unexpectedHeaderHash]);
763+
764+
const execution: PublicExecution = { contractAddress, functionData, args, callContext };
765+
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);
766+
767+
await expect(executor.simulate(execution, GlobalVariables.empty())).rejects.toThrowError('Invalid header hash');
768+
});
769+
});
721770
});

0 commit comments

Comments
 (0)