From fc3256730b1804b55fd19a31e3898824b7807cab Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 8 Feb 2024 13:10:39 +0000 Subject: [PATCH 1/2] test: testing historical header in contexts --- .../contracts/test_contract/src/main.nr | 15 ++++++---- .../src/client/private_execution.test.ts | 29 ++++++++++++++++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/yarn-project/noir-contracts/contracts/test_contract/src/main.nr b/yarn-project/noir-contracts/contracts/test_contract/src/main.nr index 05158e604d5..d381bcbcfb8 100644 --- a/yarn-project/noir-contracts/contracts/test_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/test_contract/src/main.nr @@ -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 @@ -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 diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 00ea626f4ff..74fc04355c4 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -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, @@ -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'); + }); + }); }); From 553a7f1bbb719a653b284a0323d57fd913f2b8ec Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 8 Feb 2024 13:27:20 +0000 Subject: [PATCH 2/2] testing header in public --- .../simulator/src/public/index.test.ts | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/yarn-project/simulator/src/public/index.test.ts b/yarn-project/simulator/src/public/index.test.ts index 96ae8bf7892..3b84353386e 100644 --- a/yarn-project/simulator/src/public/index.test.ts +++ b/yarn-project/simulator/src/public/index.test.ts @@ -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'; @@ -42,7 +43,9 @@ describe('ACIR public execution simulator', () => { publicContracts = mock(); commitmentsDb = mock(); - header = Header.empty(); + const randomInt = Math.floor(Math.random() * 1000000); + header = makeHeader(randomInt); + executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header); }, 10000); @@ -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'); + }); + }); });