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

feat: add introspection #45

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"sinon": "^9.0.0"
},
"dependencies": {
"@libp2p/observer-data": "^1.1.0",
"@libp2p/observer-proto": "^1.1.0",
"base32.js": "^0.1.0",
"cids": "^1.1.5",
"debug": "^4.3.1",
"it-buffer": "^0.1.2",
Expand All @@ -60,7 +63,7 @@
"libp2p-bootstrap": "^0.12.1",
"libp2p-floodsub": "^0.24.1",
"libp2p-gossipsub": "^0.8.0",
"libp2p-kad-dht": "^0.20.6",
"libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#feat/peer-eviction",
"libp2p-mplex": "^0.10.0",
"libp2p-noise": "^2.0.0",
"libp2p-secio": "^0.13.1",
Expand Down
10 changes: 7 additions & 3 deletions src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ const main = async (processArgs) => {
.option('secio', {
desc: 'Enables secio connection encryption',
type: 'boolean',
default: true
default: false
})
.option('noise', {
desc: 'Enables noise connection encryption',
type: 'boolean',
default: false
default: true
})
.option('bootstrap', {
alias: 'b',
Expand All @@ -63,7 +63,7 @@ const main = async (processArgs) => {
.option('dht', {
desc: 'Enables the DHT in full node mode',
type: 'boolean',
default: false
default: true
})
.option('dhtClient', {
desc: '(Not yet supported) Enables the DHT in client mode',
Expand Down Expand Up @@ -118,6 +118,10 @@ const main = async (processArgs) => {
if (!argv.quiet) {
// eslint-disable-next-line
log('daemon has started')
log('daemon is listening on:')
daemon.libp2p.multiaddrs.forEach((ma) => {
log(`${ma}/p2p/${daemon.libp2p.peerId.toB58String()}`)
})
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const TCP = require('libp2p-tcp')
const Libp2p = require('./libp2p')
const createIntrospection = require('./introspection')
const PeerId = require('peer-id')
const ma = require('multiaddr')
const CID = require('cids')
Expand Down Expand Up @@ -42,6 +43,8 @@ class Daemon {
}) {
this.multiaddr = ma(multiaddr)
this.libp2p = libp2pNode
// TODO: Add custom port + enabled (y/n)
this.introspection = createIntrospection({ libp2p: this.libp2p })
this.tcp = new TCP({ upgrader: passThroughUpgrader })
this.listener = this.tcp.createListener((maConn) => {
this.handleConnection(maConn)
Expand Down Expand Up @@ -165,8 +168,22 @@ class Daemon {
*/
async start () {
this._listen()
await this.introspection.start()
await this.libp2p.start()
await this.listener.listen(this.multiaddr)

this._start()
}

// TMP for demo
async _start () {
await new Promise(resolve => setTimeout(resolve, 20e3))
// Find providers
const cid = new CID('QmZKcfhEUbY6GumNmL7rga58qnRdMqMtv64smChiHJyfJf')
console.log('Searching for %s', cid.toString())
for await (const provider of this.libp2p.contentRouting.findProviders(cid, { maxNumProviders: 1 })) {
console.log('Found provider %j', provider)
}
}

/**
Expand All @@ -177,6 +194,7 @@ class Daemon {
* @returns {Promise<void>}
*/
async stop (options = { exit: false }) {
await this.introspection.stop()
await this.libp2p.stop()
await this.listener.close()
if (options.exit) {
Expand Down
71 changes: 71 additions & 0 deletions src/introspection/create-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

const {
createResponseServerMessage,
createRuntimeServerMessage,
createStateServerMessage
} = require('./messages/server-message')

const {
createConnection
} = require('./messages/connections')

const {
createState: createStateMessage
} = require('./messages/states')

const {
createDHT,
updateDHT,
} = require('./messages/dht')

const {
createCommandMessage
} = require('./messages/command-response')

const {
createBufferSegment
} = require('./utils')

// TODO: Perhaps use in this context
function createVersion () {
const versionBuf = Buffer.alloc(4)
versionBuf.writeUInt32LE(1, 0)
return versionBuf
}

function createRuntimeMessage (options = {}, runtime = createRuntime(options)) {
const runtimePacket = createRuntimeServerMessage(runtime)
return createBufferSegment(runtimePacket)
}

function createCommandResponse (options = {}, response = createCommandMessage(options)) {
const responsePacket = createResponseServerMessage(response)
return createBufferSegment(responsePacket)
}

function createConnections (connections = []) {
const connectionsPbs = []

connections.forEach((c) => {
connectionsPbs.push(createConnection(c))
})

return connectionsPbs
}

function createState (connections, utcNow, dht, durationSnapshot) {
const state = createStateMessage(connections, utcNow, dht, durationSnapshot)
const statePacket = createStateServerMessage(state)
return createBufferSegment(statePacket)
}

module.exports = {
createDHT,
createConnections,
createRuntimeMessage,
createVersion,
createState,
createCommandResponse,
updateDHT
}
Loading