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

fix: do not register libp2p handler twice for same protocol #6455

Merged
merged 1 commit into from
Feb 20, 2024
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
21 changes: 10 additions & 11 deletions packages/reqresp/src/ReqResp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export class ReqResp {
private readonly protocolPrefix: string;

/** `${protocolPrefix}/${method}/${version}/${encoding}` */
// Use any to avoid TS error on registering protocol
// Type 'unknown' is not assignable to type 'Resp'
private readonly registeredProtocols = new Map<ProtocolID, MixedProtocol>();
private readonly dialOnlyProtocols = new Map<ProtocolID, boolean>();

Expand All @@ -79,28 +77,29 @@ export class ReqResp {
*
* Made it explicit method to avoid any developer mistake
*/
registerDialOnlyProtocol(protocol: DialOnlyProtocol, opts?: ReqRespRegisterOpts): void {
registerDialOnlyProtocol(protocol: DialOnlyProtocol): void {
const protocolID = this.formatProtocolID(protocol);

// libp2p will throw on error on duplicates, allow to overwrite behavior
if (opts?.ignoreIfDuplicate && this.registeredProtocols.has(protocolID)) {
return;
}

this.registeredProtocols.set(protocolID, protocol);
this.dialOnlyProtocols.set(protocolID, true);
}

/**
* Register protocol as supported and to libp2p.
* async because libp2p registar persists the new protocol list in the peer-store.
* async because libp2p registrar persists the new protocol list in the peer-store.
* Throws if the same protocol is registered twice.
* Can be called at any time, no concept of started / stopped
*/
async registerProtocol(protocol: Protocol, opts?: ReqRespRegisterOpts): Promise<void> {
const protocolID = this.formatProtocolID(protocol);

// libp2p will throw if handler for protocol is already registered, allow to overwrite behavior
if (opts?.ignoreIfDuplicate && this.registeredProtocols.has(protocolID)) {
return;
}

const {handler: _handler, inboundRateLimits, ...rest} = protocol;
this.registerDialOnlyProtocol(rest, opts);
this.registerDialOnlyProtocol(rest);
this.dialOnlyProtocols.set(protocolID, false);

if (inboundRateLimits) {
Expand All @@ -112,7 +111,7 @@ export class ReqResp {

/**
* Remove protocol as supported and from libp2p.
* async because libp2p registar persists the new protocol list in the peer-store.
* async because libp2p registrar persists the new protocol list in the peer-store.
* Does NOT throw if the protocolID is unknown.
* Can be called at any time, no concept of started / stopped
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/reqresp/test/unit/ReqResp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,13 @@ describe("ResResp", () => {
expect(reqresp.getRegisteredProtocols()).toEqual(["/eth2/beacon_chain/req/number_to_string/1/ssz_snappy"]);
expect(libp2p.handle).toHaveBeenCalledOnce();
});

it("should not register handler twice for same protocol if ignoreIfDuplicate=true", async () => {
await reqresp.registerProtocol(numberToStringProtocol, {ignoreIfDuplicate: true});
expect(libp2p.handle).toHaveBeenCalledOnce();

await reqresp.registerProtocol(numberToStringProtocol, {ignoreIfDuplicate: true});
expect(libp2p.handle).toHaveBeenCalledOnce();
});
});
});
Loading