|
| 1 | +import { Fr } from '@aztec/foundation/curves/bn254'; |
| 2 | +import { PublicDataWrite } from '@aztec/stdlib/avm'; |
| 3 | +import { AztecAddress } from '@aztec/stdlib/aztec-address'; |
| 4 | +import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash'; |
| 5 | +import type { MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server'; |
| 6 | +import { MerkleTreeId } from '@aztec/stdlib/trees'; |
| 7 | + |
| 8 | +import { type MockProxy, mock } from 'jest-mock-extended'; |
| 9 | + |
| 10 | +import { applyPublicDataOverrides } from './public_data_overrides.js'; |
| 11 | + |
| 12 | +describe('applyPublicDataOverrides', () => { |
| 13 | + let fork: MockProxy<MerkleTreeWriteOperations>; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + fork = mock<MerkleTreeWriteOperations>(); |
| 17 | + fork.sequentialInsert.mockResolvedValue({ lowLeavesWitnesses: [], insertionWitnesses: [] } as any); |
| 18 | + }); |
| 19 | + |
| 20 | + it('does nothing when overrides is undefined', async () => { |
| 21 | + await applyPublicDataOverrides(fork, undefined); |
| 22 | + expect(fork.sequentialInsert).not.toHaveBeenCalled(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('does nothing when overrides is empty', async () => { |
| 26 | + await applyPublicDataOverrides(fork, []); |
| 27 | + expect(fork.sequentialInsert).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('inserts a single override at the correct leaf slot', async () => { |
| 31 | + const contract = await AztecAddress.random(); |
| 32 | + const slot = Fr.random(); |
| 33 | + const value = Fr.random(); |
| 34 | + |
| 35 | + await applyPublicDataOverrides(fork, [{ contract, slot, value }]); |
| 36 | + |
| 37 | + const expectedLeafSlot = await computePublicDataTreeLeafSlot(contract, slot); |
| 38 | + const expectedWrite = new PublicDataWrite(expectedLeafSlot, value); |
| 39 | + |
| 40 | + expect(fork.sequentialInsert).toHaveBeenCalledTimes(1); |
| 41 | + expect(fork.sequentialInsert).toHaveBeenCalledWith(MerkleTreeId.PUBLIC_DATA_TREE, [expectedWrite.toBuffer()]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('inserts multiple overrides in a single batch call', async () => { |
| 45 | + const contract = await AztecAddress.random(); |
| 46 | + const overrides = [ |
| 47 | + { contract, slot: Fr.random(), value: Fr.random() }, |
| 48 | + { contract, slot: Fr.random(), value: Fr.random() }, |
| 49 | + ]; |
| 50 | + |
| 51 | + await applyPublicDataOverrides(fork, overrides); |
| 52 | + |
| 53 | + const expectedWrites = await Promise.all( |
| 54 | + overrides.map(async o => { |
| 55 | + const leafSlot = await computePublicDataTreeLeafSlot(o.contract, o.slot); |
| 56 | + return new PublicDataWrite(leafSlot, o.value); |
| 57 | + }), |
| 58 | + ); |
| 59 | + |
| 60 | + expect(fork.sequentialInsert).toHaveBeenCalledTimes(1); |
| 61 | + expect(fork.sequentialInsert).toHaveBeenCalledWith( |
| 62 | + MerkleTreeId.PUBLIC_DATA_TREE, |
| 63 | + expectedWrites.map(w => w.toBuffer()), |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('passes duplicate (contract, slot) writes through — last write wins via tree semantics', async () => { |
| 68 | + const contract = await AztecAddress.random(); |
| 69 | + const slot = Fr.random(); |
| 70 | + const firstValue = Fr.random(); |
| 71 | + const secondValue = Fr.random(); |
| 72 | + |
| 73 | + await applyPublicDataOverrides(fork, [ |
| 74 | + { contract, slot, value: firstValue }, |
| 75 | + { contract, slot, value: secondValue }, |
| 76 | + ]); |
| 77 | + |
| 78 | + // Both writes are passed to sequentialInsert; the tree handles last-wins ordering. |
| 79 | + expect(fork.sequentialInsert).toHaveBeenCalledTimes(1); |
| 80 | + const [treeId, leaves] = fork.sequentialInsert.mock.calls[0] as [MerkleTreeId, Buffer[]]; |
| 81 | + expect(treeId).toBe(MerkleTreeId.PUBLIC_DATA_TREE); |
| 82 | + expect(leaves).toHaveLength(2); |
| 83 | + }); |
| 84 | +}); |
0 commit comments