Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: deprecate old peer store api #598

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`](#libp2p)
* [`libp2p.peerStore`](#libp2ppeerStore)
* [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]

#### Known 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 the peer

#### Known 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"cids": "^0.8.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"interop-libp2p": "~0.0.1",
"interop-libp2p": "libp2p/interop#chore/update-libp2p-daemon-with-peerstore",
"it-concat": "^1.0.0",
"it-pair": "^1.0.0",
"it-pushable": "^1.4.0",
Expand All @@ -93,7 +93,7 @@
"libp2p-delegated-peer-routing": "^0.4.0",
"libp2p-floodsub": "^0.20.0",
"libp2p-gossipsub": "^0.2.0",
"libp2p-kad-dht": "^0.18.2",
"libp2p-kad-dht": "^0.19.0-pre.0",
"libp2p-mdns": "^0.13.0",
"libp2p-mplex": "^0.9.1",
"libp2p-secio": "^0.12.1",
Expand Down
4 changes: 3 additions & 1 deletion src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AddressBook extends Book {

/**
* @constructor
* @param {EventEmitter} peerStore
* @param {PeerStore} peerStore
*/
constructor (peerStore) {
/**
Expand Down Expand Up @@ -80,6 +80,7 @@ class AddressBook extends Book {
}

this.data.set(id, multiaddrInfos)
this._setPeerId(peerId)
log(`stored provided multiaddrs for ${id}`)

// TODO: Remove peerInfo and its usage on peer-info deprecate
Expand Down Expand Up @@ -133,6 +134,7 @@ class AddressBook extends Book {
return this
}

this._setPeerId(peerId)
this.data.set(id, multiaddrInfos)

log(`added provided multiaddrs for ${id}`)
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
102 changes: 10 additions & 92 deletions src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,97 +43,13 @@ class PeerStore extends EventEmitter {
* ProtoBook containing a map of peerIdStr to supported protocols.
*/
this.protoBook = new ProtoBook(this)
}

// TODO: Temporary adapter for modules using PeerStore
// This should be removed under a breaking change
/**
* Stores the peerInfo of a new peer on each book.
* @param {PeerInfo} peerInfo
* @param {object} [options]
* @param {boolean} [options.replace = true]
* @return {PeerInfo}
*/
put (peerInfo, options) {
const multiaddrs = peerInfo.multiaddrs.toArray()
const protocols = Array.from(peerInfo.protocols || new Set())

this.addressBook.set(peerInfo.id, multiaddrs, options)
this.protoBook.set(peerInfo.id, protocols, options)

const peer = this.find(peerInfo.id)
const pInfo = new PeerInfo(peerInfo.id)

if (!peer) {
return pInfo
}

peer.protocols.forEach((p) => pInfo.protocols.add(p))
peer.multiaddrInfos.forEach((mi) => pInfo.multiaddrs.add(mi.multiaddr))

return pInfo
}

// TODO: Temporary adapter for modules using PeerStore
// This should be removed under a breaking change
/**
* Get the info of the given id.
* @param {peerId} peerId
* @returns {PeerInfo}
*/
get (peerId) {
const peer = this.find(peerId)

const pInfo = new PeerInfo(peerId)
peer.protocols.forEach((p) => pInfo.protocols.add(p))
peer.multiaddrInfos.forEach((mi) => pInfo.multiaddrs.add(mi.multiaddr))

return pInfo
}

// TODO: Temporary adapter for modules using PeerStore
// This should be removed under a breaking change
/**
* Has the info to the given id.
* @param {PeerId} peerId
* @returns {boolean}
*/
has (peerId) {
return Boolean(this.find(peerId))
}

// TODO: Temporary adapter for modules using PeerStore
// This should be removed under a breaking change
/**
* Removes the peer provided.
* @param {PeerId} peerId
* @returns {boolean} true if found and removed
*/
remove (peerId) {
return this.delete(peerId)
}

// TODO: Temporary adapter for modules using PeerStore
// This should be removed under a breaking change
/**
* Completely replaces the existing peers metadata with the given `peerInfo`
* @param {PeerInfo} peerInfo
* @returns {void}
*/
replace (peerInfo) {
this.put(peerInfo)
}

// TODO: Temporary adapter for modules using PeerStore
// This should be removed under a breaking change
/**
* Returns the known multiaddrs for a given `PeerInfo`. All returned multiaddrs
* will include the encapsulated `PeerId` of the peer.
* @param {PeerInfo} peerInfo
* @returns {Array<Multiaddr>}
*/
multiaddrsForPeer (peerInfo) {
return this.addressBook.getMultiaddrsForPeer(peerInfo.id)
/**
* TODO: this should only exist until we have the key-book
* Map known peers to their peer-id.
* @type {Map<string, Array<PeerId>}
*/
this.peerIds = new Map()
}

/**
Expand Down Expand Up @@ -195,15 +111,16 @@ class PeerStore extends EventEmitter {
}

/**
* Find the stored information of a given peer.
* Get the stored information of a given peer.
* @param {PeerId} peerId
* @returns {peerInfo}
*/
find (peerId) {
get (peerId) {
if (!PeerId.isPeerId(peerId)) {
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
}

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

Expand All @@ -212,6 +129,7 @@ class PeerStore extends EventEmitter {
}

return {
id: id || peerId,
multiaddrInfos: multiaddrInfos || [],
protocols: protocols || []
}
Expand Down
4 changes: 3 additions & 1 deletion src/peer-store/proto-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
class ProtoBook extends Book {
/**
* @constructor
* @param {EventEmitter} peerStore
* @param {PeerStore} peerStore
*/
constructor (peerStore) {
/**
Expand Down Expand Up @@ -71,6 +71,7 @@ class ProtoBook extends Book {
}

this.data.set(id, newSet)
this._setPeerId(peerId)
log(`stored provided protocols for ${id}`)

// TODO: Remove peerInfo and its usage on peer-info deprecate
Expand Down Expand Up @@ -118,6 +119,7 @@ class ProtoBook extends Book {
protocols = [...newSet]

this.data.set(id, newSet)
this._setPeerId(peerId)
log(`added provided protocols for ${id}`)

// TODO: Remove peerInfo and its usage on peer-info deprecate
Expand Down
2 changes: 1 addition & 1 deletion test/content-routing/dht/operation.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('DHT subsystem operates correctly', () => {
])

await libp2p.contentRouting.put(key, value)

const fetchedValue = await remoteLibp2p.contentRouting.get(key)

expect(fetchedValue).to.eql(value)
})
})
Expand Down
Loading