|
7 | 7 | Header, |
8 | 8 | L1_TO_L2_MSG_TREE_HEIGHT, |
9 | 9 | } from '@aztec/circuits.js'; |
| 10 | +import { makeHeader } from '@aztec/circuits.js/factories'; |
10 | 11 | import { FunctionArtifact, FunctionSelector, encodeArguments } from '@aztec/foundation/abi'; |
11 | 12 | import { AztecAddress } from '@aztec/foundation/aztec-address'; |
12 | 13 | import { pedersenHash } from '@aztec/foundation/crypto'; |
@@ -42,7 +43,9 @@ describe('ACIR public execution simulator', () => { |
42 | 43 | publicContracts = mock<PublicContractsDB>(); |
43 | 44 | commitmentsDb = mock<CommitmentsDB>(); |
44 | 45 |
|
45 | | - header = Header.empty(); |
| 46 | + const randomInt = Math.floor(Math.random() * 1000000); |
| 47 | + header = makeHeader(randomInt); |
| 48 | + |
46 | 49 | executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header); |
47 | 50 | }, 10000); |
48 | 51 |
|
@@ -718,4 +721,50 @@ describe('ACIR public execution simulator', () => { |
718 | 721 | }); |
719 | 722 | }); |
720 | 723 | }); |
| 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 | + }); |
721 | 770 | }); |
0 commit comments