|
| 1 | +import { PUBLIC_DATA_TREE_HEIGHT } from '@aztec/constants'; |
| 2 | +import { Fr } from '@aztec/foundation/curves/bn254'; |
| 3 | +import { SiblingPath } from '@aztec/foundation/trees'; |
| 4 | +import type { KeyStore } from '@aztec/key-store'; |
| 5 | +import { ProtocolContractAddress } from '@aztec/protocol-contracts'; |
| 6 | +import { AztecAddress } from '@aztec/stdlib/aztec-address'; |
| 7 | +import { DelayedPublicMutableValuesWithHash } from '@aztec/stdlib/delayed-public-mutable'; |
| 8 | +import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash'; |
| 9 | +import type { AztecNode } from '@aztec/stdlib/interfaces/client'; |
| 10 | +import { PublicDataTreeLeaf, PublicDataTreeLeafPreimage, PublicDataWitness } from '@aztec/stdlib/trees'; |
| 11 | +import { BlockHeader } from '@aztec/stdlib/tx'; |
| 12 | + |
| 13 | +import { mock } from 'jest-mock-extended'; |
| 14 | + |
| 15 | +import type { ContractStore } from '../storage/contract_store/contract_store.js'; |
| 16 | +import { PrivateKernelOracle } from './private_kernel_oracle.js'; |
| 17 | + |
| 18 | +describe('PrivateKernelOracle', () => { |
| 19 | + let oracle: PrivateKernelOracle; |
| 20 | + let node: ReturnType<typeof mock<AztecNode>>; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + node = mock<AztecNode>(); |
| 24 | + oracle = new PrivateKernelOracle(mock<ContractStore>(), mock<KeyStore>(), node, BlockHeader.empty()); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('getUpdatedClassIdHints', () => { |
| 28 | + it('skips storage reads when contract class was never updated', async () => { |
| 29 | + const contractAddress = await AztecAddress.random(); |
| 30 | + const hashLeafSlot = await getHashLeafSlot(contractAddress); |
| 31 | + |
| 32 | + // Non-matching slot simulates a low-leaf witness (slot was never written) |
| 33 | + const unrelatedSlot = new Fr(hashLeafSlot.toBigInt() - 1n); |
| 34 | + node.getPublicDataWitness.mockResolvedValue(makeWitness(unrelatedSlot)); |
| 35 | + |
| 36 | + const result = await oracle.getUpdatedClassIdHints(contractAddress); |
| 37 | + |
| 38 | + expect(result.updatedClassIdValues.isEmpty()).toBe(true); |
| 39 | + expect(node.getPublicStorageAt).not.toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('reads storage when contract class was updated', async () => { |
| 43 | + const contractAddress = await AztecAddress.random(); |
| 44 | + const hashLeafSlot = await getHashLeafSlot(contractAddress); |
| 45 | + |
| 46 | + // Matching slot means the contract class was updated |
| 47 | + node.getPublicDataWitness.mockResolvedValue(makeWitness(hashLeafSlot, Fr.random())); |
| 48 | + node.getPublicStorageAt.mockResolvedValue(new Fr(42)); |
| 49 | + |
| 50 | + const result = await oracle.getUpdatedClassIdHints(contractAddress); |
| 51 | + |
| 52 | + expect(result.updatedClassIdValues.isEmpty()).toBe(false); |
| 53 | + expect(node.getPublicStorageAt).toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + async function getHashLeafSlot(contractAddress: AztecAddress) { |
| 58 | + const { delayedPublicMutableHashSlot } = |
| 59 | + await DelayedPublicMutableValuesWithHash.getContractUpdateSlots(contractAddress); |
| 60 | + return computePublicDataTreeLeafSlot( |
| 61 | + ProtocolContractAddress.ContractInstanceRegistry, |
| 62 | + delayedPublicMutableHashSlot, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + function makeWitness(slot: Fr, value: Fr = Fr.ZERO) { |
| 67 | + return new PublicDataWitness( |
| 68 | + 0n, |
| 69 | + new PublicDataTreeLeafPreimage(new PublicDataTreeLeaf(slot, value), Fr.ZERO, 0n), |
| 70 | + SiblingPath.random(PUBLIC_DATA_TREE_HEIGHT), |
| 71 | + ); |
| 72 | + } |
| 73 | +}); |
0 commit comments