|
| 1 | +import { ARCHIVE_HEIGHT } from '@aztec/constants'; |
| 2 | +import { BlockNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types'; |
| 3 | +import { Fr } from '@aztec/foundation/curves/bn254'; |
| 4 | +import { promiseWithResolvers } from '@aztec/foundation/promise'; |
| 5 | +import { MembershipWitness } from '@aztec/foundation/trees'; |
| 6 | +import { AztecAddress } from '@aztec/stdlib/aztec-address'; |
| 7 | +import { BlockHash } from '@aztec/stdlib/block'; |
| 8 | +import type { AztecNode } from '@aztec/stdlib/interfaces/server'; |
| 9 | +import { PublicDataWitness } from '@aztec/stdlib/trees'; |
| 10 | +import { MinedTxReceipt, TxEffect, TxExecutionResult, TxHash, TxStatus } from '@aztec/stdlib/tx'; |
| 11 | + |
| 12 | +import { mock } from 'jest-mock-extended'; |
| 13 | + |
| 14 | +import { AztecNodeReadCache } from './aztec_node_read_cache.js'; |
| 15 | + |
| 16 | +describe('AztecNodeReadCache', () => { |
| 17 | + let aztecNode: ReturnType<typeof mock<AztecNode>>; |
| 18 | + let cache: AztecNodeReadCache; |
| 19 | + |
| 20 | + const makeMinedReceipt = (txHash: TxHash) => |
| 21 | + new MinedTxReceipt( |
| 22 | + txHash, |
| 23 | + TxStatus.FINALIZED, |
| 24 | + TxExecutionResult.SUCCESS, |
| 25 | + 0n, |
| 26 | + BlockHash.random(), |
| 27 | + BlockNumber(1), |
| 28 | + SlotNumber(1), |
| 29 | + 0, |
| 30 | + EpochNumber(1), |
| 31 | + TxEffect.empty(), |
| 32 | + ); |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + aztecNode = mock<AztecNode>(); |
| 36 | + cache = new AztecNodeReadCache(aztecNode); |
| 37 | + }); |
| 38 | + |
| 39 | + it('shares concurrent tx receipt reads', async () => { |
| 40 | + const txHash = TxHash.random(); |
| 41 | + const deferred = promiseWithResolvers<MinedTxReceipt<{ includeTxEffect: true }>>(); |
| 42 | + aztecNode.getTxReceipt.mockReturnValue(deferred.promise); |
| 43 | + |
| 44 | + const first = cache.getTxReceiptWithEffect(txHash); |
| 45 | + const second = cache.getTxReceiptWithEffect(txHash); |
| 46 | + const receipt = makeMinedReceipt(txHash); |
| 47 | + deferred.resolve(receipt); |
| 48 | + |
| 49 | + await expect(Promise.all([first, second])).resolves.toEqual([receipt, receipt]); |
| 50 | + expect(aztecNode.getTxReceipt).toHaveBeenCalledTimes(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it('evicts rejected reads so callers can retry', async () => { |
| 54 | + const txHash = TxHash.random(); |
| 55 | + const receipt = makeMinedReceipt(txHash); |
| 56 | + aztecNode.getTxReceipt.mockRejectedValueOnce(new Error('temporary failure')); |
| 57 | + aztecNode.getTxReceipt.mockResolvedValueOnce(receipt); |
| 58 | + |
| 59 | + await expect(cache.getTxReceiptWithEffect(txHash)).rejects.toThrow('temporary failure'); |
| 60 | + await expect(cache.getTxReceiptWithEffect(txHash)).resolves.toBe(receipt); |
| 61 | + expect(aztecNode.getTxReceipt).toHaveBeenCalledTimes(2); |
| 62 | + }); |
| 63 | + |
| 64 | + it('keeps cache entries separate by method and arguments', async () => { |
| 65 | + const blockHash = BlockHash.random(); |
| 66 | + const leafSlot = Fr.random(); |
| 67 | + const blockWitness = MembershipWitness.empty(ARCHIVE_HEIGHT); |
| 68 | + const publicDataWitness = PublicDataWitness.random(); |
| 69 | + aztecNode.getBlockHashMembershipWitness.mockResolvedValue(blockWitness); |
| 70 | + aztecNode.getPublicDataWitness.mockResolvedValue(publicDataWitness); |
| 71 | + |
| 72 | + await expect(cache.getBlockHashMembershipWitness(blockHash, blockHash)).resolves.toBe(blockWitness); |
| 73 | + await expect(cache.getPublicDataWitness(blockHash, leafSlot)).resolves.toBe(publicDataWitness); |
| 74 | + |
| 75 | + expect(aztecNode.getBlockHashMembershipWitness).toHaveBeenCalledTimes(1); |
| 76 | + expect(aztecNode.getPublicDataWitness).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('caches successful undefined results', async () => { |
| 80 | + const referenceBlockHash = BlockHash.random(); |
| 81 | + const blockHash = BlockHash.random(); |
| 82 | + aztecNode.getBlockHashMembershipWitness.mockResolvedValue(undefined); |
| 83 | + |
| 84 | + await expect(cache.getBlockHashMembershipWitness(referenceBlockHash, blockHash)).resolves.toBeUndefined(); |
| 85 | + await expect(cache.getBlockHashMembershipWitness(referenceBlockHash, blockHash)).resolves.toBeUndefined(); |
| 86 | + |
| 87 | + expect(aztecNode.getBlockHashMembershipWitness).toHaveBeenCalledTimes(1); |
| 88 | + }); |
| 89 | + |
| 90 | + it('reuses cached slots across overlapping public storage ranges', async () => { |
| 91 | + const blockHash = BlockHash.random(); |
| 92 | + const contractAddress = await AztecAddress.random(); |
| 93 | + const startStorageSlot = new Fr(100); |
| 94 | + aztecNode.getPublicStorageAt.mockImplementation((_block, _contract, slot) => |
| 95 | + Promise.resolve(new Fr(slot.value + 1n)), |
| 96 | + ); |
| 97 | + |
| 98 | + await expect(cache.getPublicStorageRange(blockHash, contractAddress, startStorageSlot, 2)).resolves.toEqual([ |
| 99 | + new Fr(101), |
| 100 | + new Fr(102), |
| 101 | + ]); |
| 102 | + await expect(cache.getPublicStorageRange(blockHash, contractAddress, new Fr(101), 2)).resolves.toEqual([ |
| 103 | + new Fr(102), |
| 104 | + new Fr(103), |
| 105 | + ]); |
| 106 | + |
| 107 | + expect(aztecNode.getPublicStorageAt).toHaveBeenCalledTimes(3); |
| 108 | + }); |
| 109 | +}); |
0 commit comments