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

Commit

Permalink
feat: add get and getAddrs helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Mar 27, 2017
1 parent 943be0f commit 94f301d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
21 changes: 21 additions & 0 deletions src/index.js
Expand Up @@ -23,6 +23,16 @@ function PeerBook () {
return peers
}

/**
* Get the info to the given PeerId.
*
* @param {PeerId} id
* @returns {PeerInfo}
*/
this.get = (id) => {
return this.getByB58String(id.toB58String())
}

this.getByB58String = (b58String) => {
const peerInfo = peers[b58String]
if (peerInfo) {
Expand All @@ -47,5 +57,16 @@ function PeerBook () {
this.removeByB58String(b58multihash)
}

/**
* Return all multiaddrs for a given PeerId.
*
* @param {PeerId} id
* @returns {Array<Multiaddr>}
*/
this.getAddrs = (id) => {
const info = this.get(id)
return info.multiaddrs
}

// TODO serialize PeerBook into MerkleDAG Objects
}
35 changes: 20 additions & 15 deletions test/peer-book.spec.js
Expand Up @@ -36,31 +36,31 @@ describe('peer-book', function () {
})
})

it('create PeerBook', (done) => {
it('create PeerBook', () => {
pb = new PeerBook()
expect(pb).to.exist
done()
})

it('put peerInfo', (done) => {
it('put peerInfo', () => {
pb.put(p1)
pb.put(p2)
pb.put(p3)

done()
})

it('get all peerInfo', (done) => {
it('get all peerInfo', () => {
const peers = pb.getAll()
expect(Object.keys(peers).length).to.equal(3)
done()
})

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

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

it('getByB58String non existent', (done) => {
Expand All @@ -72,11 +72,10 @@ describe('peer-book', function () {
}
})

it('getByMultihash', (done) => {
it('getByMultihash', () => {
const p1Id = p1.id.toBytes()
const peer = pb.getByMultihash(p1Id)
expect(peer).to.deep.equal(p1)
done()
})

it('getByMultihash non existent', (done) => {
Expand Down Expand Up @@ -110,21 +109,27 @@ describe('peer-book', function () {
}
})

it('add repeated Id, merge info', (done) => {
it('add repeated Id, merge info', () => {
const peerA = new PeerInfo(p3.id)
peerA.multiaddr.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)
done()
})

it('add repeated Id, replace info', (done) => {
it('add repeated Id, replace info', () => {
const peerA = new PeerInfo(p3.id)
peerA.multiaddr.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)
done()
})

it('getAddrs', () => {
const pb = new PeerBook()
const peer = new PeerInfo(p3.id)
peer.multiaddr.add(new Multiaddr('/ip4/127.0.0.1/tcp/1234'))
pb.put(peer)
expect(pb.getAddrs(p3.id)).to.be.eql(peer.multiaddrs)
})
})

0 comments on commit 94f301d

Please sign in to comment.