Skip to content

Commit 29f7ce4

Browse files
authored
feat(p2p): validator use batch requests (#11332)
1 parent 73a6698 commit 29f7ce4

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

yarn-project/p2p/src/client/p2p_client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
444444
* @param txHashes - The hashes of the transactions to request.
445445
* @returns A promise that resolves to an array of transactions or undefined.
446446
*/
447-
public requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
448-
const requestPromises = txHashes.map(txHash => this.requestTxByHash(txHash));
449-
return Promise.all(requestPromises);
447+
public async requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
448+
const res = await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes);
449+
return Promise.resolve(res ?? []);
450450
}
451451

452452
/**

yarn-project/p2p/src/services/dummy_service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ export class DummyP2PService implements P2PService {
6161
return Promise.resolve(undefined);
6262
}
6363

64+
/**
65+
* Sends a batch request to a peer.
66+
* @param _protocol - The protocol to send the request on.
67+
* @param _requests - The requests to send.
68+
* @returns The responses from the peer, otherwise undefined.
69+
*/
70+
public sendBatchRequest<Protocol extends ReqRespSubProtocol>(
71+
_protocol: Protocol,
72+
_requests: InstanceType<SubProtocolMap[Protocol]['request']>[],
73+
): Promise<InstanceType<SubProtocolMap[Protocol]['response']>[]> {
74+
return Promise.resolve([]);
75+
}
76+
6477
/**
6578
* Returns the ENR of the peer.
6679
* @returns The ENR of the peer, otherwise undefined.

yarn-project/p2p/src/services/libp2p/libp2p_service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,19 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
400400
return this.reqresp.sendRequest(protocol, request);
401401
}
402402

403+
/**
404+
* Send a batch of requests to peers, and return the responses
405+
* @param protocol - The request response protocol to use
406+
* @param requests - The requests to send to the peers
407+
* @returns The responses to the requests
408+
*/
409+
sendBatchRequest<SubProtocol extends ReqRespSubProtocol>(
410+
protocol: SubProtocol,
411+
requests: InstanceType<SubProtocolMap[SubProtocol]['request']>[],
412+
): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']>[] | undefined> {
413+
return this.reqresp.sendBatchRequest(protocol, requests);
414+
}
415+
403416
/**
404417
* Get the ENR of the node
405418
* @returns The ENR of the node

yarn-project/p2p/src/services/reqresp/interface.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ export const DEFAULT_SUB_PROTOCOL_HANDLERS: ReqRespSubProtocolHandlers = {
112112
* The Request Response Pair interface defines the methods that each
113113
* request response pair must implement
114114
*/
115-
interface RequestResponsePair<Req, Res> {
115+
interface RequestResponsePair<Req extends { toBuffer(): Buffer }, Res> {
116+
/**
117+
* The request must implement the toBuffer method (generic serialisation)
118+
*/
116119
request: new (...args: any[]) => Req;
117120
/**
118121
* The response must implement the static fromBuffer method (generic serialisation)

yarn-project/p2p/src/services/service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ export interface P2PService {
4545
request: InstanceType<SubProtocolMap[Protocol]['request']>,
4646
): Promise<InstanceType<SubProtocolMap[Protocol]['response']> | undefined>;
4747

48+
/**
49+
* Send a batch of requests to peers, and return the responses
50+
*
51+
* @param protocol - The request response protocol to use
52+
* @param requests - The requests to send to the peers
53+
* @returns The responses to the requests
54+
*/
55+
sendBatchRequest<Protocol extends ReqRespSubProtocol>(
56+
protocol: Protocol,
57+
requests: InstanceType<SubProtocolMap[Protocol]['request']>[],
58+
): Promise<InstanceType<SubProtocolMap[Protocol]['response']>[] | undefined>;
59+
4860
// Leaky abstraction: fix https://github.com/AztecProtocol/aztec-packages/issues/7963
4961
registerBlockReceivedCallback(callback: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void;
5062

0 commit comments

Comments
 (0)