Skip to content

Commit

Permalink
feat!: update libp2p interfaces (#32)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: uses new single-issue libp2p interface modules
  • Loading branch information
achingbrain authored Jun 15, 2022
1 parent 00e09cc commit b60a381
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 32 deletions.
61 changes: 46 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
# js-libp2p Pubsub Peer Discovery

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![Build Status](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/actions/workflows/js-test-and-release.yml/badge.svg?branch=main)](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/actions/workflows/js-test-and-release.yml)

> A js-libp2p module that uses pubsub for interval broadcast peer discovery
# @libp2p/pubsub-peer-discovery <!-- omit in toc -->

[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![IRC](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-pubsub-peer-discovery.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-pubsub-peer-discovery)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/actions/workflows/js-test-and-release.yml)

> A libp2p module that uses pubsub for mdns like peer discovery
## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Design](#design)
- [Flow](#flow)
- [Security Considerations](#security-considerations)
- [Usage](#usage)
- [Requirements](#requirements)
- [Usage in js-libp2p](#usage-in-js-libp2p)
- [Customizing Pubsub Peer Discovery](#customizing-pubsub-peer-discovery)
- [Options](#options)
- [Default Topic](#default-topic)
- [Contribute](#contribute)
- [License](#license)
- [Contribution](#contribution)

## Install

```console
$ npm i @libp2p/pubsub-peer-discovery
```

## Design

### Flow

- When the discovery module is started by libp2p it subscribes to the discovery pubsub topic(s)
- It will immediately broadcast your peer data via pubsub and repeat the broadcast on the configured `interval`

### Security Considerations

It is worth noting that this module does not include any message signing for broadcasts. The reason for this is that libp2p-pubsub supports message signing and enables it by default, which means the message you received has been verified to be from the originator, so we can trust that the peer information we have received is indeed from the peer who owns it. This doesn't mean the peer can't falsify its own records, but this module isn't currently concerned with that scenario.

## Usage
Expand Down Expand Up @@ -82,11 +106,11 @@ const node = await createLibp2p({

#### Options

| Name | Type | Description |
|------|------|-------------|
| interval | `number` | How often (in `ms`), after initial broadcast, your node should broadcast your peer data. Default (`10000ms`)|
| topics | `Array<string>` | An Array of topic strings. If set, the default topic will not be used and must be included explicitly here |
| listenOnly | `boolean` | If true it will not broadcast peer data. Dont set this unless you have a specific reason to. Default (`false`) |
| Name | Type | Description |
| ---------- | --------------- | -------------------------------------------------------------------------------------------------------------- |
| interval | `number` | How often (in `ms`), after initial broadcast, your node should broadcast your peer data. Default (`10000ms`) |
| topics | `Array<string>` | An Array of topic strings. If set, the default topic will not be used and must be included explicitly here |
| listenOnly | `boolean` | If true it will not broadcast peer data. Dont set this unless you have a specific reason to. Default (`false`) |

#### Default Topic

Expand All @@ -102,4 +126,11 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c

## License

MIT - Protocol Labs 2020
Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

## Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
],
"exports": {
".": {
"types": "./src/index.d.ts",
"import": "./dist/src/index.js"
}
},
Expand Down Expand Up @@ -138,16 +139,21 @@
"release": "semantic-release"
},
"dependencies": {
"@libp2p/interfaces": "^2.0.1",
"@libp2p/logger": "^1.1.2",
"@libp2p/components": "^1.0.0",
"@libp2p/interface-peer-discovery": "^1.0.0",
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/interface-pubsub": "^1.0.1",
"@libp2p/interfaces": "^3.0.2",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^1.1.8",
"@multiformats/multiaddr": "^10.1.7",
"protons-runtime": "^1.0.3"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^2.0.1",
"@libp2p/interface-address-manager": "^1.0.1",
"@libp2p/interface-peer-discovery-compliance-tests": "^1.0.0",
"@libp2p/peer-id-factory": "^1.0.8",
"aegir": "^37.0.15",
"aegir": "^37.2.0",
"p-defer": "^4.0.0",
"p-wait-for": "^4.1.0",
"protons": "^3.0.3",
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import type { Startable } from '@libp2p/interfaces/startable'
import { Multiaddr } from '@multiformats/multiaddr'
import { Peer as PBPeer } from './peer.js'
import { peerIdFromKeys } from '@libp2p/peer-id'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
import { Components, Initializable } from '@libp2p/interfaces/components'
import type { Message } from '@libp2p/interfaces/pubsub'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import { symbol } from '@libp2p/interfaces/peer-discovery'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
import { Components, Initializable } from '@libp2p/components'
import type { Message } from '@libp2p/interface-pubsub'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import { symbol } from '@libp2p/interface-peer-discovery'

const log = logger('libp2p:discovery:pubsub')
export const TOPIC = '_peer-discovery._p2p._pubsub'
Expand Down
8 changes: 4 additions & 4 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-env mocha */

import tests from '@libp2p/interface-compliance-tests/peer-discovery'
import tests from '@libp2p/interface-peer-discovery-compliance-tests'
import { PubSubPeerDiscovery, TOPIC } from '../src/index.js'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { Components } from '@libp2p/interfaces/components'
import { Components } from '@libp2p/components'
import { stubInterface } from 'ts-sinon'
import type { PubSub } from '@libp2p/interfaces/pubsub'
import type { PubSub } from '@libp2p/interface-pubsub'
import { CustomEvent } from '@libp2p/interfaces/events'
import type { AddressManager } from '@libp2p/interfaces/address-manager'
import type { AddressManager } from '@libp2p/interface-address-manager'
import { Multiaddr } from '@multiformats/multiaddr'
import { Peer as PBPeer } from '../src/peer.js'

Expand Down
8 changes: 4 additions & 4 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { PubSubPeerDiscovery, TOPIC } from '../src/index.js'
import * as PB from '../src/peer.js'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { StubbedInstance, stubInterface } from 'ts-sinon'
import type { PubSub } from '@libp2p/interfaces/pubsub'
import { Components } from '@libp2p/interfaces/components'
import type { PubSub } from '@libp2p/interface-pubsub'
import { Components } from '@libp2p/components'
import { peerIdFromKeys } from '@libp2p/peer-id'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import { CustomEvent } from '@libp2p/interfaces/events'
import type { AddressManager } from '@libp2p/interfaces/address-manager'
import type { AddressManager } from '@libp2p/interface-address-manager'
import { start, stop } from '@libp2p/interfaces/startable'

const listeningMultiaddr = new Multiaddr('/ip4/127.0.0.1/tcp/9000/ws')
Expand Down

0 comments on commit b60a381

Please sign in to comment.