Skip to content

Commit

Permalink
chore: use topology interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 14, 2019
1 parent cdfd023 commit b48661e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 181 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"it-protocol-buffers": "^0.2.0",
"latency-monitor": "~0.2.1",
"libp2p-crypto": "^0.17.1",
"libp2p-interfaces": "^0.1.3",
"libp2p-interfaces": "^0.1.4",
"mafmt": "^7.0.0",
"merge-options": "^1.0.1",
"moving-average": "^1.0.0",
Expand Down
108 changes: 0 additions & 108 deletions src/connection-manager/topology.js

This file was deleted.

12 changes: 3 additions & 9 deletions src/registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ log.error = debug('libp2p:peer-store:error')

const { Connection } = require('libp2p-interfaces/src/connection')
const PeerInfo = require('peer-info')
const Toplogy = require('./connection-manager/topology')

/**
* Responsible for notifying registered protocols of events in the network.
Expand Down Expand Up @@ -106,17 +105,12 @@ class Registrar {

/**
* Register handlers for a set of multicodecs given
* @param {Object} topologyProps properties for topology
* @param {Array<string>|string} topologyProps.multicodecs
* @param {Object} topologyProps.handlers
* @param {function} topologyProps.handlers.onConnect
* @param {function} topologyProps.handlers.onDisconnect
* @param {Topology} topology protocol topology
* @return {string} registrar identifier
*/
register (topologyProps) {
// Create multicodec topology
register (topology) {
// Create topology
const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
const topology = new Toplogy(topologyProps)

this.topologies.set(id, topology)

Expand Down
77 changes: 14 additions & 63 deletions test/registrar/registrar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { expect } = chai
const pDefer = require('p-defer')

const PeerInfo = require('peer-info')
const Topology = require('libp2p-interfaces/src/topology')
const PeerStore = require('../../src/peer-store')
const Registrar = require('../../src/registrar')
const { createMockConnection } = require('./utils')
Expand All @@ -32,53 +33,7 @@ describe('registrar', () => {
throw new Error('should fail to register a protocol if no multicodec is provided')
})

it('should fail to register a protocol if no handlers are provided', () => {
const topologyProps = {
multicodecs: multicodec
}

try {
registrar.register(topologyProps)
} catch (err) {
expect(err).to.exist()
return
}
throw new Error('should fail to register a protocol if no handlers are provided')
})

it('should fail to register a protocol if the onConnect handler is not provided', () => {
const topologyProps = {
multicodecs: multicodec,
handlers: {
onDisconnect: () => { }
}
}

try {
registrar.register(topologyProps)
} catch (err) {
expect(err).to.exist()
return
}
throw new Error('should fail to register a protocol if the onConnect handler is not provided')
})

it('should fail to register a protocol if the onDisconnect handler is not provided', () => {
const topologyProps = {
multicodecs: multicodec,
handlers: {
onConnect: () => { }
}
}

try {
registrar.register(topologyProps)
} catch (err) {
expect(err).to.exist()
return
}
throw new Error('should fail to register a protocol if the onDisconnect handler is not provided')
})
// TODO: not valid topology
})

describe('registration', () => {
Expand All @@ -88,27 +43,27 @@ describe('registrar', () => {
})

it('should be able to register a protocol', () => {
const topologyProps = {
const topologyProps = new Topology({
multicodecs: multicodec,
handlers: {
onConnect: () => { },
onDisconnect: () => { }
},
multicodecs: multicodec
}
}
})

const identifier = registrar.register(topologyProps)

expect(identifier).to.exist()
})

it('should be able to unregister a protocol', () => {
const topologyProps = {
const topologyProps = new Topology({
multicodecs: multicodec,
handlers: {
onConnect: () => { },
onDisconnect: () => { }
},
multicodecs: multicodec
}
}
})

const identifier = registrar.register(topologyProps)
const success = registrar.unregister(identifier)
Expand Down Expand Up @@ -138,7 +93,7 @@ describe('registrar', () => {
registrar.onConnect(remotePeerInfo, conn)
expect(registrar.connections.size).to.eql(1)

const topologyProps = {
const topologyProps = new Topology({
multicodecs: multicodec,
handlers: {
onConnect: (peerInfo, connection) => {
Expand All @@ -153,19 +108,17 @@ describe('registrar', () => {
onDisconnectDefer.resolve()
}
}
}
})

// Register protocol
const identifier = registrar.register(topologyProps)
const topology = registrar.topologies.get(identifier)

// Topology created
expect(topology).to.exist()
expect(topology.peers.size).to.eql(1)

registrar.onDisconnect(remotePeerInfo)
expect(registrar.connections.size).to.eql(0)
expect(topology.peers.size).to.eql(1) // topology should keep the peer

// Wait for handlers to be called
return Promise.all([
Expand All @@ -178,7 +131,7 @@ describe('registrar', () => {
const onConnectDefer = pDefer()
const onDisconnectDefer = pDefer()

const topologyProps = {
const topologyProps = new Topology({
multicodecs: multicodec,
handlers: {
onConnect: () => {
Expand All @@ -188,15 +141,14 @@ describe('registrar', () => {
onDisconnectDefer.resolve()
}
}
}
})

// Register protocol
const identifier = registrar.register(topologyProps)
const topology = registrar.topologies.get(identifier)

// Topology created
expect(topology).to.exist()
expect(topology.peers.size).to.eql(0)
expect(registrar.connections.size).to.eql(0)

// Setup connections before registrar
Expand All @@ -212,7 +164,6 @@ describe('registrar', () => {
peerStore.put(peerInfo)

await onConnectDefer.promise
expect(topology.peers.size).to.eql(1)

// Remove protocol to peer and update it
peerInfo.protocols.delete(multicodec)
Expand Down

0 comments on commit b48661e

Please sign in to comment.