Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(p2p): validator use batch requests #11332

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions yarn-project/p2p/src/client/p2p_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
* @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 ?? []);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions yarn-project/p2p/src/services/dummy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends ReqRespSubProtocol>(
_protocol: Protocol,
_requests: InstanceType<SubProtocolMap[Protocol]['request']>[],
): Promise<InstanceType<SubProtocolMap[Protocol]['response']>[]> {
return Promise.resolve([]);
}

/**
* Returns the ENR of the peer.
* @returns The ENR of the peer, otherwise undefined.
Expand Down
13 changes: 13 additions & 0 deletions yarn-project/p2p/src/services/libp2p/libp2p_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ export class LibP2PService<T extends P2PClientType> 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<SubProtocol extends ReqRespSubProtocol>(
protocol: SubProtocol,
requests: InstanceType<SubProtocolMap[SubProtocol]['request']>[],
): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']>[] | undefined> {
return this.reqresp.sendBatchRequest(protocol, requests);
}

/**
* Get the ENR of the node
* @returns The ENR of the node
Expand Down
5 changes: 4 additions & 1 deletion yarn-project/p2p/src/services/reqresp/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Req, Res> {
interface RequestResponsePair<Req extends { toBuffer(): Buffer }, Res> {
/**
* The request must implement the toBuffer method (generic serialisation)
*/
request: new (...args: any[]) => Req;
/**
* The response must implement the static fromBuffer method (generic serialisation)
Expand Down
12 changes: 12 additions & 0 deletions yarn-project/p2p/src/services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ export interface P2PService {
request: InstanceType<SubProtocolMap[Protocol]['request']>,
): Promise<InstanceType<SubProtocolMap[Protocol]['response']> | 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 extends ReqRespSubProtocol>(
protocol: Protocol,
requests: InstanceType<SubProtocolMap[Protocol]['request']>[],
): Promise<InstanceType<SubProtocolMap[Protocol]['response']>[] | undefined>;

// Leaky abstraction: fix https://github.com/AztecProtocol/aztec-packages/issues/7963
registerBlockReceivedCallback(callback: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void;

Expand Down
Loading