Skip to content

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
vasco-santos and jacobheun committed Apr 16, 2020
1 parent 060d69a commit 7248b4c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
28 changes: 27 additions & 1 deletion doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
* [`metrics.forPeer`](#metricsforpeer)
* [`metrics.forProtocol`](#metricsforprotocol)
* [Events](#events)
* [`libp2p`](#eventslibp2p)
* [`libp2p.peerStore`](#eventslibp2ppeerStore)
* [Types](#types)
* [`Stats`](#stats)

Expand Down Expand Up @@ -1099,7 +1101,9 @@ console.log(peerStats.toJSON())

## Events

Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.

### libp2p

#### An error has occurred

Expand Down Expand Up @@ -1132,6 +1136,28 @@ This event will be triggered anytime we are disconnected from another peer, rega

- `peer`: instance of [`PeerInfo`][peer-info]

### libp2p.peerStore

#### A new peer is added to the peerStore

`libp2p.peerStore.on('peer', (peerId) => {})`

- `peerId`: instance of [`PeerId`][peer-id]

#### Knownw multiaddrs for a peer change

`libp2p.peerStore.on('change:multiaddrs', ({ peerId, multiaddrs}) => {})`

- `peerId`: instance of [`PeerId`][peer-id]
- `multiaddrs`: array of known [`multiaddr`][multiaddr] for a the peer

#### Knownw protocols for a peer change

`libp2p.peerStore.on('change:protocols', ({ peerId, protocols}) => {})`

- `peerId`: instance of [`PeerId`][peer-id]
- `protocols`: array of known supported protocols for the peer (string identifiers)

## Types

### Stats
Expand Down
6 changes: 0 additions & 6 deletions src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,6 @@ class AddressBook extends Book {
return multiaddrInfos
}

_setPeerId (peerId) {
if (!this._ps.peerIds.get(peerId)) {
this._ps.peerIds.set(peerId.toString(), peerId)
}
}

/**
* Get the known multiaddrs for a given peer. All returned multiaddrs
* will include the encapsulated `PeerId` of the peer.
Expand Down
6 changes: 6 additions & 0 deletions src/peer-store/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class Book {

return true
}

_setPeerId (peerId) {
if (!this._ps.peerIds.get(peerId)) {
this._ps.peerIds.set(peerId.toB58String(), peerId)
}
}
}

module.exports = Book
2 changes: 1 addition & 1 deletion src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class PeerStore extends EventEmitter {
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
}

const id = this.peerIds.get(peerId.toString())
const id = this.peerIds.get(peerId.toB58String())
const multiaddrInfos = this.addressBook.get(peerId)
const protocols = this.protoBook.get(peerId)

Expand Down
6 changes: 0 additions & 6 deletions src/peer-store/proto-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ class ProtoBook extends Book {

return this
}

_setPeerId (peerId) {
if (!this._ps.peerIds.get(peerId)) {
this._ps.peerIds.set(peerId.toString(), peerId)
}
}
}

module.exports = ProtoBook

0 comments on commit 7248b4c

Please sign in to comment.