diff --git a/yarn-project/p2p/src/client/p2p_client.ts b/yarn-project/p2p/src/client/p2p_client.ts index d2752c3de6c0..1944df3f1d9a 100644 --- a/yarn-project/p2p/src/client/p2p_client.ts +++ b/yarn-project/p2p/src/client/p2p_client.ts @@ -444,9 +444,9 @@ export class P2PClient * @param txHashes - The hashes of the transactions to request. * @returns A promise that resolves to an array of transactions or undefined. */ - public requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]> { - const requestPromises = txHashes.map(txHash => this.requestTxByHash(txHash)); - return Promise.all(requestPromises); + public async requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]> { + const res = await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes); + return Promise.resolve(res ?? []); } /** diff --git a/yarn-project/p2p/src/services/dummy_service.ts b/yarn-project/p2p/src/services/dummy_service.ts index e108cd47ac67..1f74cc796f64 100644 --- a/yarn-project/p2p/src/services/dummy_service.ts +++ b/yarn-project/p2p/src/services/dummy_service.ts @@ -61,6 +61,19 @@ export class DummyP2PService implements P2PService { return Promise.resolve(undefined); } + /** + * Sends a batch request to a peer. + * @param _protocol - The protocol to send the request on. + * @param _requests - The requests to send. + * @returns The responses from the peer, otherwise undefined. + */ + public sendBatchRequest( + _protocol: Protocol, + _requests: InstanceType[], + ): Promise[]> { + return Promise.resolve([]); + } + /** * Returns the ENR of the peer. * @returns The ENR of the peer, otherwise undefined. diff --git a/yarn-project/p2p/src/services/libp2p/libp2p_service.ts b/yarn-project/p2p/src/services/libp2p/libp2p_service.ts index 8c0a1b24b09b..cde9c2ebf5b0 100644 --- a/yarn-project/p2p/src/services/libp2p/libp2p_service.ts +++ b/yarn-project/p2p/src/services/libp2p/libp2p_service.ts @@ -400,6 +400,19 @@ export class LibP2PService extends WithTracer implement return this.reqresp.sendRequest(protocol, request); } + /** + * Send a batch of requests to peers, and return the responses + * @param protocol - The request response protocol to use + * @param requests - The requests to send to the peers + * @returns The responses to the requests + */ + sendBatchRequest( + protocol: SubProtocol, + requests: InstanceType[], + ): Promise[] | undefined> { + return this.reqresp.sendBatchRequest(protocol, requests); + } + /** * Get the ENR of the node * @returns The ENR of the node diff --git a/yarn-project/p2p/src/services/reqresp/interface.ts b/yarn-project/p2p/src/services/reqresp/interface.ts index 820714c60b45..a28346a590f7 100644 --- a/yarn-project/p2p/src/services/reqresp/interface.ts +++ b/yarn-project/p2p/src/services/reqresp/interface.ts @@ -112,7 +112,10 @@ export const DEFAULT_SUB_PROTOCOL_HANDLERS: ReqRespSubProtocolHandlers = { * The Request Response Pair interface defines the methods that each * request response pair must implement */ -interface RequestResponsePair { +interface RequestResponsePair { + /** + * The request must implement the toBuffer method (generic serialisation) + */ request: new (...args: any[]) => Req; /** * The response must implement the static fromBuffer method (generic serialisation) diff --git a/yarn-project/p2p/src/services/service.ts b/yarn-project/p2p/src/services/service.ts index d668d6ca441b..91169b49094c 100644 --- a/yarn-project/p2p/src/services/service.ts +++ b/yarn-project/p2p/src/services/service.ts @@ -45,6 +45,18 @@ export interface P2PService { request: InstanceType, ): Promise | undefined>; + /** + * Send a batch of requests to peers, and return the responses + * + * @param protocol - The request response protocol to use + * @param requests - The requests to send to the peers + * @returns The responses to the requests + */ + sendBatchRequest( + protocol: Protocol, + requests: InstanceType[], + ): Promise[] | undefined>; + // Leaky abstraction: fix https://github.com/AztecProtocol/aztec-packages/issues/7963 registerBlockReceivedCallback(callback: (block: BlockProposal) => Promise): void;