|
| 1 | +import { Fr } from '@aztec/foundation/curves/bn254'; |
| 2 | +import { ProtocolContractAddress } from '@aztec/protocol-contracts'; |
| 3 | +import { AztecAddress } from '@aztec/stdlib/aztec-address'; |
| 4 | +import { SerializableContractInstance } from '@aztec/stdlib/contract'; |
| 5 | +import { |
| 6 | + DELAYED_PUBLIC_MUTABLE_VALUES_LEN, |
| 7 | + DelayedPublicMutableValuesWithHash, |
| 8 | +} from '@aztec/stdlib/delayed-public-mutable'; |
| 9 | +import type { AztecNode } from '@aztec/stdlib/interfaces/client'; |
| 10 | + |
| 11 | +import { type MockProxy, mock } from 'jest-mock-extended'; |
| 12 | + |
| 13 | +import { fastForwardContractUpdate } from './fastforward_contract_update.js'; |
| 14 | + |
| 15 | +describe('fastForwardContractUpdate', () => { |
| 16 | + let node: MockProxy<AztecNode>; |
| 17 | + let instanceAddress: AztecAddress; |
| 18 | + let originalClassId: Fr; |
| 19 | + let newClassId: Fr; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + node = mock<AztecNode>(); |
| 23 | + instanceAddress = await AztecAddress.random(); |
| 24 | + originalClassId = Fr.random(); |
| 25 | + newClassId = Fr.random(); |
| 26 | + |
| 27 | + const instance = ( |
| 28 | + await SerializableContractInstance.random({ |
| 29 | + currentContractClassId: originalClassId, |
| 30 | + originalContractClassId: originalClassId, |
| 31 | + }) |
| 32 | + ).withAddress(instanceAddress); |
| 33 | + |
| 34 | + node.getContract.mockResolvedValue(instance); |
| 35 | + node.getContractClass.mockResolvedValue({ |
| 36 | + id: newClassId, |
| 37 | + artifactHash: Fr.random(), |
| 38 | + packedBytecodeCommitments: [], |
| 39 | + privateFunctionsRoot: Fr.random(), |
| 40 | + publicBytecodeCommitment: Fr.random(), |
| 41 | + version: 1, |
| 42 | + privateFunctions: [], |
| 43 | + utilityFunctions: [], |
| 44 | + publicFunctions: [], |
| 45 | + packedBytecode: Buffer.alloc(0), |
| 46 | + } as any); |
| 47 | + }); |
| 48 | + |
| 49 | + it('produces overrides with bumped currentContractClassId and registry storage writes', async () => { |
| 50 | + const overrides = await fastForwardContractUpdate({ instanceAddress, newClassId, node }); |
| 51 | + |
| 52 | + const upgraded = overrides.contracts?.[instanceAddress.toString()]; |
| 53 | + expect(upgraded).toBeDefined(); |
| 54 | + expect(upgraded!.instance.address).toEqual(instanceAddress); |
| 55 | + expect(upgraded!.instance.currentContractClassId).toEqual(newClassId); |
| 56 | + expect(upgraded!.instance.originalContractClassId).toEqual(originalClassId); |
| 57 | + |
| 58 | + const expectedSlots = await DelayedPublicMutableValuesWithHash.getContractUpdateSlots(instanceAddress); |
| 59 | + expect(overrides.publicStorage).toHaveLength(DELAYED_PUBLIC_MUTABLE_VALUES_LEN + 1); |
| 60 | + for (const entry of overrides.publicStorage!) { |
| 61 | + expect(entry.contract).toEqual(ProtocolContractAddress.ContractInstanceRegistry); |
| 62 | + } |
| 63 | + const baseSlot = expectedSlots.delayedPublicMutableSlot; |
| 64 | + expect(overrides.publicStorage![0].slot).toEqual(baseSlot); |
| 65 | + expect(overrides.publicStorage![overrides.publicStorage!.length - 1].slot).toEqual( |
| 66 | + expectedSlots.delayedPublicMutableHashSlot, |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('throws when the instance is not deployed', async () => { |
| 71 | + node.getContract.mockResolvedValue(undefined); |
| 72 | + await expect(fastForwardContractUpdate({ instanceAddress, newClassId, node })).rejects.toThrow(/not deployed/); |
| 73 | + }); |
| 74 | + |
| 75 | + it('throws when the new class is not registered', async () => { |
| 76 | + node.getContractClass.mockResolvedValue(undefined); |
| 77 | + await expect(fastForwardContractUpdate({ instanceAddress, newClassId, node })).rejects.toThrow(/not registered/); |
| 78 | + }); |
| 79 | + |
| 80 | + it('throws when the instance is already on the target class', async () => { |
| 81 | + const sameClassInstance = ( |
| 82 | + await SerializableContractInstance.random({ |
| 83 | + currentContractClassId: newClassId, |
| 84 | + originalContractClassId: originalClassId, |
| 85 | + }) |
| 86 | + ).withAddress(instanceAddress); |
| 87 | + node.getContract.mockResolvedValue(sameClassInstance); |
| 88 | + |
| 89 | + await expect(fastForwardContractUpdate({ instanceAddress, newClassId, node })).rejects.toThrow(/already on class/); |
| 90 | + }); |
| 91 | +}); |
0 commit comments