diff --git a/src/service/service.ts b/src/service/service.ts index 49ed33a6..53eda9a5 100644 --- a/src/service/service.ts +++ b/src/service/service.ts @@ -194,11 +194,12 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) { const fullConfig = { ...defaultConfig, ...config }; const decodedEnr = typeof enr === "string" ? ENR.decodeTxt(enr) : enr; const rateLimiter = opts.rateLimiterOpts && new RateLimiter(opts.rateLimiterOpts, metrics ?? null); - const sessionService = new SessionService(fullConfig, { - enr: decodedEnr, - keypair: createKeypairFromPeerId(peerId), - transport: transport ?? new UDPTransportService(multiaddr, decodedEnr.nodeId, rateLimiter), - }); + const sessionService = new SessionService( + fullConfig, + decodedEnr, + createKeypairFromPeerId(peerId), + transport ?? new UDPTransportService(multiaddr, decodedEnr.nodeId, rateLimiter) + ); return new Discv5(fullConfig, sessionService, metrics); } diff --git a/src/session/service.ts b/src/session/service.ts index 1cd2c600..ff5f2700 100644 --- a/src/session/service.ts +++ b/src/session/service.ts @@ -134,11 +134,9 @@ export class SessionService extends (EventEmitter as { new (): StrictEventEmitte */ private sessions: LRUCache; - constructor(config: ISessionConfig, opts: SessionServiceOpts) { + constructor(config: ISessionConfig, enr: ENR, keypair: IKeypair, transport: ITransportService) { super(); - const { enr, keypair, transport } = opts; - // ensure the keypair matches the one that signed the ENR if (!keypair.publicKey.equals(enr.publicKey)) { throw new Error("Provided keypair does not match the provided ENR keypair"); @@ -758,11 +756,11 @@ export class SessionService extends (EventEmitter as { new (): StrictEventEmitte } private removeExpectedResponse(socketAddr: Multiaddr): void { - this.transport.addExpectedResponse(socketAddr.toOptions().host); + this.transport.addExpectedResponse?.(socketAddr.toOptions().host); } private addExpectedResponse(socketAddr: Multiaddr): void { - this.transport.removeExpectedResponse(socketAddr.toOptions().host); + this.transport.removeExpectedResponse?.(socketAddr.toOptions().host); } private handleRequestTimeout(nodeAddr: INodeAddress, requestCall: IRequestCall): void { diff --git a/src/transport/types.ts b/src/transport/types.ts index 4f4cda2a..f8f1ddbe 100644 --- a/src/transport/types.ts +++ b/src/transport/types.ts @@ -31,7 +31,7 @@ export interface ITransportService extends TransportEventEmitter { send(to: Multiaddr, toId: string, packet: IPacket): Promise; /** Add 1 expected response of unknown lenght from IP for rate limiter */ - addExpectedResponse(ipAddress: string): void; + addExpectedResponse?(ipAddress: string): void; /** Remove 1 expected response from IP added with addExpectedResponse */ - removeExpectedResponse(ipAddress: string): void; + removeExpectedResponse?(ipAddress: string): void; } diff --git a/test/unit/session/service.test.ts b/test/unit/session/service.test.ts index 9fcd9341..22430c74 100644 --- a/test/unit/session/service.test.ts +++ b/test/unit/session/service.test.ts @@ -42,8 +42,8 @@ describe("session service", () => { transport0 = new UDPTransportService(addr0, enr0.nodeId); transport1 = new UDPTransportService(addr1, enr1.nodeId); - service0 = new SessionService(defaultConfig, { enr: enr0, keypair: kp0, transport: transport0 }); - service1 = new SessionService(defaultConfig, { enr: enr1, keypair: kp1, transport: transport1 }); + service0 = new SessionService(defaultConfig, enr0, kp0, transport0); + service1 = new SessionService(defaultConfig, enr1, kp1, transport1); await service0.start(); await service1.start();