-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from ChainSafe/cayman/libp2p-compat
Add libp2p compatability module
- Loading branch information
Showing
6 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,40 @@ | ||
# discv5 | ||
|
||
A TypeScript implementation of the [DiscV5](https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md) protocol | ||
|
||
## Libp2p compatibility | ||
|
||
![Peer Discovery Compatible](https://github.com/libp2p/js-libp2p-interfaces/raw/master/src/peer-discovery/img/badge.png) | ||
|
||
Included is a libp2p peer-discovery compatibility module. | ||
|
||
#### Example | ||
|
||
```typescript | ||
import { Discv5Discovery, ENR } from "@chainsafe/discv5"; | ||
import Libp2p from "libp2p"; | ||
import PeerInfo from "peer-info"; | ||
|
||
const myPeerInfo: PeerInfo = ...; | ||
|
||
const bootstrapEnrs: ENR[] = [...]; | ||
|
||
const libp2p = new Libp2p({ | ||
peerInfo: myPeerInfo, | ||
modules: { | ||
peerDiscovery: [Discv5Discovery], | ||
}, | ||
config: { | ||
discv5: { | ||
enr: ENR.createFromPeerId(myPeerInfo.id), | ||
bindAddr: "/ip4/0.0.0.0/udp/9000", | ||
bootstrapEnrs: bootstrapEnrs, | ||
}, | ||
}, | ||
}); | ||
|
||
``` | ||
|
||
## License | ||
|
||
Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { EventEmitter } from "events"; | ||
import PeerInfo = require("peer-info"); | ||
import Multiaddr = require("multiaddr"); | ||
import { randomBytes } from "libp2p-crypto"; | ||
|
||
import { Discv5 } from "../service"; | ||
import { ENR, createNodeId } from "../enr"; | ||
|
||
export interface IDiscv5DiscoveryInputOptions { | ||
/** | ||
* Local ENR associated with the local libp2p peer id | ||
*/ | ||
enr: ENR; | ||
/** | ||
* The bind multiaddr for the discv5 UDP server | ||
* | ||
* NOTE: This MUST be a udp multiaddr | ||
*/ | ||
bindAddr: string; | ||
/** | ||
* Remote ENRs used to bootstrap the network | ||
*/ | ||
bootEnrs: ENR[]; | ||
} | ||
|
||
export interface IDiscv5DiscoveryOptions extends IDiscv5DiscoveryInputOptions { | ||
peerInfo: PeerInfo; | ||
} | ||
|
||
/** | ||
* Discv5Discovery is a libp2p peer-discovery compatable module | ||
*/ | ||
export class Discv5Discovery extends EventEmitter { | ||
static tag = "discv5"; | ||
|
||
public discv5: Discv5; | ||
private started: NodeJS.Timer | boolean; | ||
|
||
constructor(options: IDiscv5DiscoveryOptions) { | ||
super(); | ||
this.discv5 = Discv5.create(options.enr, options.peerInfo.id, Multiaddr(options.bindAddr)); | ||
this.started = false; | ||
options.bootEnrs.forEach((bootEnr) => this.discv5.addEnr(bootEnr)); | ||
} | ||
|
||
async start(): Promise<void> { | ||
if (this.started) { | ||
return; | ||
} | ||
this.started = true; | ||
await this.discv5.start(); | ||
setTimeout(() => this.findPeers, 1); | ||
} | ||
|
||
async stop(): Promise<void> { | ||
if (!this.started) { | ||
return; | ||
} | ||
this.started = false; | ||
await this.discv5.stop(); | ||
} | ||
|
||
async findPeers(): Promise<void> { | ||
while (!this.started) { | ||
// Search for random nodes | ||
// emit discovered on all finds | ||
const enrs = await this.discv5.findNode(createNodeId(randomBytes(32))); | ||
if (!this.started) { | ||
return; | ||
} | ||
for (const enr of enrs) { | ||
const peerInfo = new PeerInfo(await enr.peerId()); | ||
const multiaddrTCP = enr.multiaddrTCP; | ||
if (multiaddrTCP) { | ||
peerInfo.multiaddrs.add(multiaddrTCP); | ||
} | ||
this.emit("peer", peerInfo); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./discv5"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters