Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
new peer-book
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Mar 30, 2017
1 parent 8187fcc commit ab64395
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -44,8 +44,8 @@
"async": "^2.2.0",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"multiaddr": "^2.2.3",
"peer-info": "~0.8.5",
"multiaddr": "^2.3.0",
"peer-info": "~0.9.0",
"pre-commit": "^1.2.2"
},
"contributors": [
Expand Down
56 changes: 31 additions & 25 deletions src/index.js
Expand Up @@ -2,25 +2,30 @@

const bs58 = require('bs58')

module.exports = PeerBook

function PeerBook () {
if (!(this instanceof PeerBook)) {
return new PeerBook()
class PeerBook {
constructor () {
this._peers = {}
}

const peers = {}

this.put = (peerInfo, replace) => {
if (peers[peerInfo.id.toB58String()] && !replace) {
// peerInfo.replace merges by default
peers[peerInfo.id.toB58String()].multiaddr.replace([], peerInfo.multiaddrs)
/**
* Stores a peerInfo, if already exist, only adds the multiaddrs
*
* @param {PeerInfo} peerInfo
* @param {replace} boolean
* @returns {null}
*/
put (peerInfo, replace) {
if (this._peers[peerInfo.id.toB58String()] && !replace) {
// peerInfo.replace merges by default if none to replace are passed
this._peers[peerInfo.id.toB58String()]
.multiaddrs.replace([], peerInfo.multiaddrs.toArray())
}
peers[peerInfo.id.toB58String()] = peerInfo

this._peers[peerInfo.id.toB58String()] = peerInfo
}

this.getAll = () => {
return peers
getAll () {
return this._peers
}

/**
Expand All @@ -29,30 +34,31 @@ function PeerBook () {
* @param {PeerId} id
* @returns {PeerInfo}
*/
this.get = (id) => {
get (id) {
return this.getByB58String(id.toB58String())
}

this.getByB58String = (b58String) => {
const peerInfo = peers[b58String]
getByB58String (b58String) {
const peerInfo = this._peers[b58String]

if (peerInfo) {
return peerInfo
}
throw new Error('PeerInfo not found')
}

this.getByMultihash = (multihash) => {
getByMultihash (multihash) {
const b58multihash = bs58.encode(multihash).toString()
return this.getByB58String(b58multihash)
}

this.removeByB58String = (b58String) => {
if (peers[b58String]) {
delete peers[b58String]
removeByB58String (b58String) {
if (this._peers[b58String]) {
delete this._peers[b58String]
}
}

this.removeByMultihash = (multihash) => {
removeByMultihash (multihash) {
const b58multihash = bs58.encode(multihash).toString()
this.removeByB58String(b58multihash)
}
Expand All @@ -63,10 +69,10 @@ function PeerBook () {
* @param {PeerId} id
* @returns {Array<Multiaddr>}
*/
this.getAddrs = (id) => {
getAddrs (id) {
const info = this.get(id)
return info.multiaddrs
}

// TODO serialize PeerBook into MerkleDAG Objects
}

module.exports = PeerBook
12 changes: 6 additions & 6 deletions test/peer-book.spec.js
Expand Up @@ -57,13 +57,13 @@ describe('peer-book', function () {

it('get', () => {
const peer = pb.get(p1.id)
expect(peer).to.deep.equal(p1)
expect(peer).to.eql(p1)
})

it('getByB58String', () => {
const p1Id = p1.id.toB58String()
const peer = pb.getByB58String(p1Id)
expect(peer).to.deep.equal(p1)
expect(peer).to.eql(p1)
})

it('getByB58String non existent', (done) => {
Expand Down Expand Up @@ -114,15 +114,15 @@ describe('peer-book', function () {

it('add repeated Id, merge info', () => {
const peerA = new PeerInfo(p3.id)
peerA.multiaddr.add(new Multiaddr('/ip4/127.0.0.1/tcp/4001'))
peerA.multiaddrs.add(new Multiaddr('/ip4/127.0.0.1/tcp/4001'))
pb.put(peerA)
const peerB = pb.getByB58String(p3.id.toB58String())
expect(peerA).to.deep.equal(peerB)
expect(peerA).to.eql(peerB)
})

it('add repeated Id, replace info', () => {
const peerA = new PeerInfo(p3.id)
peerA.multiaddr.add(new Multiaddr('/ip4/188.0.0.1/tcp/5001'))
peerA.multiaddrs.add(new Multiaddr('/ip4/188.0.0.1/tcp/5001'))
pb.put(peerA, true)
const peerB = pb.getByB58String(p3.id.toB58String())
expect(peerA).to.deep.equal(peerB)
Expand All @@ -131,7 +131,7 @@ describe('peer-book', function () {
it('getAddrs', () => {
const pb = new PeerBook()
const peer = new PeerInfo(p3.id)
peer.multiaddr.add(new Multiaddr('/ip4/127.0.0.1/tcp/1234'))
peer.multiaddrs.add(new Multiaddr('/ip4/127.0.0.1/tcp/1234'))
pb.put(peer)
expect(pb.getAddrs(p3.id)).to.be.eql(peer.multiaddrs)
})
Expand Down

0 comments on commit ab64395

Please sign in to comment.