Skip to content

Commit

Permalink
Remove breaking change introduced in #206
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Oct 28, 2022
1 parent 122adbf commit bdc4a8d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 3 additions & 5 deletions src/session/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,9 @@ export class SessionService extends (EventEmitter as { new (): StrictEventEmitte
*/
private sessions: LRUCache<NodeAddressString, Session>;

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");
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/transport/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ITransportService extends TransportEventEmitter {
send(to: Multiaddr, toId: string, packet: IPacket): Promise<void>;

/** 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;
}
4 changes: 2 additions & 2 deletions test/unit/session/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit bdc4a8d

Please sign in to comment.