Skip to content

Commit

Permalink
Merge pull request #54 from ChainSafe/cayman/string-enr-input
Browse files Browse the repository at this point in the history
Input enr as string
  • Loading branch information
wemeetagain committed May 7, 2020
2 parents b90a692 + 119e459 commit 8405732
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/libp2p/discv5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import PeerId = require("peer-id");
import Multiaddr = require("multiaddr");
import { randomBytes } from "libp2p-crypto";

import { Discv5 } from "../service";
import { ENR, createNodeId } from "../enr";
import { Discv5, ENRInput } from "../service";
import { createNodeId } from "../enr";

export interface IDiscv5DiscoveryInputOptions {
/**
* Local ENR associated with the local libp2p peer id
*/
enr: ENR;
enr: ENRInput;
/**
* The bind multiaddr for the discv5 UDP server
*
Expand All @@ -20,7 +20,7 @@ export interface IDiscv5DiscoveryInputOptions {
/**
* Remote ENRs used to bootstrap the network
*/
bootEnrs: ENR[];
bootEnrs: ENRInput[];
}

export interface IDiscv5DiscoveryOptions extends IDiscv5DiscoveryInputOptions {
Expand Down
26 changes: 17 additions & 9 deletions src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Message, RequestMessage, ResponseMessage, createPingMessage, createFindNodeMessage, createNodesMessage, MessageType,
IFindNodeMessage, INodesMessage, IPongMessage, IPingMessage, requestMatchesResponse, createPongMessage,
} from "../message";
import { Discv5EventEmitter, IActiveRequest, INodesResponse } from "./types";
import { Discv5EventEmitter, ENRInput, IActiveRequest, INodesResponse } from "./types";
import { AddrVotes } from "./addrVotes";
import { TimeoutMap } from "../util";

Expand Down Expand Up @@ -130,13 +130,14 @@ export class Discv5 extends (EventEmitter as { new(): Discv5EventEmitter }) {
* @param multiaddr The multiaddr which contains the the network interface and port to which the UDP server binds
*/
public static create(
enr: ENR,
enr: ENRInput,
peerId: PeerId,
multiaddr: Multiaddr,
): Discv5 {
const magic = createMagic(enr.nodeId);
const decodedEnr = typeof enr === "string" ? ENR.decodeTxt(enr) : enr;
const magic = createMagic(decodedEnr.nodeId);
const udpTransport = new UDPTransportService(multiaddr, magic);
const sessionService = new SessionService(enr, createKeypairFromPeerId(peerId), udpTransport);
const sessionService = new SessionService(decodedEnr, createKeypairFromPeerId(peerId), udpTransport);
return new Discv5(sessionService);
}

Expand Down Expand Up @@ -198,11 +199,18 @@ export class Discv5 extends (EventEmitter as { new(): Discv5EventEmitter }) {
* so that they can be used immediately in following DHT operations involving one of these peers,
* without having to dial them upfront.
*/
public addEnr(enr: ENR): void {
if (this.kbuckets.getWithPending(enr.nodeId)) {
this.kbuckets.updateValue(enr);
} else if (this.kbuckets.add(enr, EntryStatus.Disconnected)) {
this.emit("enrAdded", enr);
public addEnr(enr: ENRInput): void {
let decodedEnr: ENR;
try {
decodedEnr = typeof enr === "string" ? ENR.decodeTxt(enr) : enr;
} catch (e) {
log("Unable to add enr: %o", enr);
return;
}
if (this.kbuckets.getWithPending(decodedEnr.nodeId)) {
this.kbuckets.updateValue(decodedEnr);
} else if (this.kbuckets.add(decodedEnr, EntryStatus.Disconnected)) {
this.emit("enrAdded", decodedEnr);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ export interface IActiveRequest {
dstId: NodeId;
lookupId?: number;
}

export type ENRInput = ENR | string;

0 comments on commit 8405732

Please sign in to comment.