|
| 1 | +import { BlockNumber } from '@aztec/foundation/branded-types'; |
| 2 | +import { Secp256k1Signer } from '@aztec/foundation/crypto/secp256k1-signer'; |
| 3 | +import { Fr } from '@aztec/foundation/curves/bn254'; |
| 4 | +import type { BlockProposal } from '@aztec/stdlib/p2p'; |
| 5 | +import { makeBlockHeader, makeBlockProposal } from '@aztec/stdlib/testing'; |
| 6 | +import { Tx, TxHash, TxHashArray } from '@aztec/stdlib/tx'; |
| 7 | + |
| 8 | +import type { PeerId } from '@libp2p/interface'; |
| 9 | +import { type MockProxy, mock } from 'jest-mock-extended'; |
| 10 | + |
| 11 | +import type { AttestationPool, TxPool } from '../../../../mem_pools/index.js'; |
| 12 | +import { ReqRespStatus } from '../../status.js'; |
| 13 | +import { BitVector } from './bitvector.js'; |
| 14 | +import { reqRespBlockTxsHandler } from './block_txs_handler.js'; |
| 15 | +import { BlockTxsRequest, BlockTxsResponse } from './block_txs_reqresp.js'; |
| 16 | + |
| 17 | +describe('reqRespBlockTxsHandler', () => { |
| 18 | + let attestationPool: MockProxy<AttestationPool>; |
| 19 | + let txPool: MockProxy<TxPool>; |
| 20 | + let peerId: PeerId; |
| 21 | + |
| 22 | + const makeTx = (txHash?: TxHash) => Tx.random({ txHash }) as Tx; |
| 23 | + |
| 24 | + const createBlockProposal = (txHashes: TxHash[]): Promise<BlockProposal> => { |
| 25 | + return makeBlockProposal({ |
| 26 | + signer: Secp256k1Signer.random(), |
| 27 | + blockHeader: makeBlockHeader(1, { blockNumber: BlockNumber(5) }), |
| 28 | + archiveRoot: Fr.random(), |
| 29 | + txHashes, |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + const callHandler = async (request: BlockTxsRequest) => { |
| 34 | + const handler = reqRespBlockTxsHandler(attestationPool, txPool); |
| 35 | + const responseBuffer = await handler(peerId, request.toBuffer()); |
| 36 | + return BlockTxsResponse.fromBuffer(responseBuffer); |
| 37 | + }; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + attestationPool = mock<AttestationPool>(); |
| 41 | + txPool = mock<TxPool>(); |
| 42 | + peerId = mock<PeerId>(); |
| 43 | + |
| 44 | + attestationPool.getBlockProposal.mockResolvedValue(undefined); |
| 45 | + txPool.getTxsByHash.mockResolvedValue([]); |
| 46 | + txPool.hasTxs.mockResolvedValue([]); |
| 47 | + txPool.getArchivedTxByHash.mockResolvedValue(undefined); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('no block proposal, no tx hashes', () => { |
| 51 | + it('throws NOT_FOUND', async () => { |
| 52 | + const request = new BlockTxsRequest(Fr.random(), new TxHashArray(), BitVector.init(0, [])); |
| 53 | + const handler = reqRespBlockTxsHandler(attestationPool, txPool); |
| 54 | + await expect(handler(peerId, request.toBuffer())).rejects.toMatchObject({ |
| 55 | + status: ReqRespStatus.NOT_FOUND, |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('no block proposal, explicit tx hashes', () => { |
| 61 | + it('returns txs found in active pool', async () => { |
| 62 | + const txHashes = [TxHash.random(), TxHash.random()]; |
| 63 | + const txs = txHashes.map(h => makeTx(h)); |
| 64 | + txPool.getTxsByHash.mockResolvedValue(txs); |
| 65 | + |
| 66 | + const request = new BlockTxsRequest(Fr.random(), new TxHashArray(...txHashes), BitVector.init(0, [])); |
| 67 | + const response = await callHandler(request); |
| 68 | + |
| 69 | + expect(response.txs.length).toBe(2); |
| 70 | + expect(response.archiveRoot).toEqual(Fr.zero()); |
| 71 | + }); |
| 72 | + |
| 73 | + it('falls back to archive for txs not in active pool', async () => { |
| 74 | + const txHashes = [TxHash.random(), TxHash.random(), TxHash.random()]; |
| 75 | + const poolTx = makeTx(txHashes[0]); |
| 76 | + const archivedTx = makeTx(txHashes[2]); |
| 77 | + |
| 78 | + txPool.getTxsByHash.mockResolvedValue([poolTx, undefined, undefined]); |
| 79 | + txPool.getArchivedTxByHash.mockImplementation((hash: TxHash) => { |
| 80 | + if (hash.equals(txHashes[2])) { |
| 81 | + return Promise.resolve(archivedTx); |
| 82 | + } |
| 83 | + return Promise.resolve(undefined); |
| 84 | + }); |
| 85 | + |
| 86 | + const request = new BlockTxsRequest(Fr.random(), new TxHashArray(...txHashes), BitVector.init(0, [])); |
| 87 | + const response = await callHandler(request); |
| 88 | + |
| 89 | + expect(response.txs.length).toBe(2); |
| 90 | + expect(txPool.getArchivedTxByHash).toHaveBeenCalledTimes(2); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns empty when txs not in pool or archive', async () => { |
| 94 | + const txHashes = [TxHash.random()]; |
| 95 | + txPool.getTxsByHash.mockResolvedValue([undefined]); |
| 96 | + |
| 97 | + const request = new BlockTxsRequest(Fr.random(), new TxHashArray(...txHashes), BitVector.init(0, [])); |
| 98 | + const response = await callHandler(request); |
| 99 | + |
| 100 | + expect(response.txs.length).toBe(0); |
| 101 | + expect(txPool.getArchivedTxByHash).toHaveBeenCalledTimes(1); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('with block proposal', () => { |
| 106 | + it('returns availability bitvector and requested txs', async () => { |
| 107 | + const txHashes = [TxHash.random(), TxHash.random(), TxHash.random()]; |
| 108 | + const proposal = await createBlockProposal(txHashes); |
| 109 | + const txs = txHashes.map(h => makeTx(h)); |
| 110 | + |
| 111 | + attestationPool.getBlockProposal.mockResolvedValue(proposal); |
| 112 | + txPool.hasTxs.mockResolvedValue([true, true, true]); |
| 113 | + txPool.getTxsByHash.mockResolvedValue(txs); |
| 114 | + |
| 115 | + const request = new BlockTxsRequest(proposal.archive, new TxHashArray(), BitVector.init(3, [0, 1, 2])); |
| 116 | + const response = await callHandler(request); |
| 117 | + |
| 118 | + expect(response.archiveRoot).toEqual(proposal.archive); |
| 119 | + expect(response.txs.length).toBe(3); |
| 120 | + expect(response.txIndices.getTrueIndices()).toEqual([0, 1, 2]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns partial availability when some txs missing from pool', async () => { |
| 124 | + const txHashes = [TxHash.random(), TxHash.random(), TxHash.random()]; |
| 125 | + const proposal = await createBlockProposal(txHashes); |
| 126 | + |
| 127 | + attestationPool.getBlockProposal.mockResolvedValue(proposal); |
| 128 | + txPool.hasTxs.mockResolvedValue([true, false, true]); |
| 129 | + txPool.getTxsByHash.mockResolvedValue([makeTx(txHashes[0]), undefined, makeTx(txHashes[2])]); |
| 130 | + |
| 131 | + const request = new BlockTxsRequest(proposal.archive, new TxHashArray(), BitVector.init(3, [0, 2])); |
| 132 | + const response = await callHandler(request); |
| 133 | + |
| 134 | + expect(response.txs.length).toBe(2); |
| 135 | + expect(response.txIndices.getTrueIndices()).toEqual([0, 2]); |
| 136 | + }); |
| 137 | + |
| 138 | + it('filters out undefined txs from pool response', async () => { |
| 139 | + const txHashes = [TxHash.random(), TxHash.random()]; |
| 140 | + const proposal = await createBlockProposal(txHashes); |
| 141 | + |
| 142 | + attestationPool.getBlockProposal.mockResolvedValue(proposal); |
| 143 | + txPool.hasTxs.mockResolvedValue([true, false]); |
| 144 | + txPool.getTxsByHash.mockResolvedValue([makeTx(txHashes[0]), undefined]); |
| 145 | + |
| 146 | + const request = new BlockTxsRequest(proposal.archive, new TxHashArray(), BitVector.init(2, [0, 1])); |
| 147 | + const response = await callHandler(request); |
| 148 | + |
| 149 | + expect(response.txs.length).toBe(1); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments