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

fix: get the daemon working outside of tests #127

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ea9fdc1
Fix handshake types.
Sep 9, 2022
bd12e5f
Fix stream client test.
Sep 9, 2022
74bbfb4
Fix yargs issues.
Sep 9, 2022
6f2bbf1
Get libp2p to run.
Sep 9, 2022
dae4325
Add ipfs boostrap peers as default.
Sep 9, 2022
f73d832
Add websockets to transports.
Sep 9, 2022
11ecd7e
Add pubsub support.
Sep 9, 2022
2a21cae
Remove yargs-promise.
Sep 9, 2022
a99dd27
Add default option for hostAddrs.
Sep 9, 2022
0828d1e
Enable cli tests.
Sep 9, 2022
544deb4
Get the daemon-server to start.
Sep 9, 2022
774f320
Fix listen address and argument issues.
Sep 9, 2022
3f0fcc5
Handle announceAddrs argument.
Sep 9, 2022
a79b220
Only bootstrap peers if enabled.
Sep 9, 2022
d7cf214
Handle the id argument.
Sep 9, 2022
f71bf2c
Add DHT support.
Sep 10, 2022
8de058e
Documentation.
Sep 10, 2022
452cb8e
Fix linter errors.
Sep 10, 2022
cf89521
Specify the internal port.
Sep 10, 2022
52c9577
Add global flag to the install command.
Sep 10, 2022
260ff6d
Fix argument condition statements.
Sep 11, 2022
8a51bdd
Add option for PSK.
Sep 11, 2022
b5f7ae7
Fix boolean argument conditions.
Sep 11, 2022
5dcf6ad
Merge branch 'master' into daemon-fixes
Sep 11, 2022
c552285
Add option for pubsub peer discovery.
Sep 12, 2022
d087784
Add option for specifying the peer discovery interval.
Sep 12, 2022
00cde14
Add options for relay configuration.
Sep 12, 2022
881206e
Fix type errors.
Sep 12, 2022
ffd2e64
Refactor createLibp2pServer into separate file.
Sep 13, 2022
ab64429
Fix cli tests.
Sep 13, 2022
68424f9
Merge branch 'master' into daemon-fixes
Sep 14, 2022
086e4dd
Bump @libp2p/tcp.
Sep 14, 2022
e3e12be
Add missing deps.
Sep 14, 2022
111784d
Set the default listen address to a unix domain socket.
Sep 14, 2022
c2da702
Merge branch 'master' into daemon-fixes
Nov 1, 2022
47cb010
Fix type issue.
Nov 1, 2022
41a1d61
Bump packages.
Nov 1, 2022
3c2da35
Fix multiaddr in tests.
Nov 1, 2022
a16d9d7
Migrate server to libp2p 0.40.
Nov 2, 2022
2b75a0e
Prevent importsNotUsedAsValues error in libp2p-gossipsub.
Nov 2, 2022
c8f7438
Fix linting in libp2p-daemon-client.
Nov 2, 2022
8e58de1
Change hostAddrs default to localhost.
Nov 2, 2022
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
2 changes: 1 addition & 1 deletion packages/libp2p-daemon-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/tcp": "^3.0.0",
"@libp2p/tcp": "^3.1.0",
"@multiformats/multiaddr": "^10.1.8",
"err-code": "^3.0.1",
"it-stream-types": "^1.0.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p-daemon-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Client implements DaemonClient {
throw errcode(new Error(response.error?.msg ?? 'Open stream failed'), 'ERR_OPEN_STREAM_FAILED')
}

return sh.rest()
return sh.rest() as Duplex<Uint8ArrayList, Uint8Array>
}

/**
Expand Down Expand Up @@ -211,7 +211,7 @@ class Client implements DaemonClient {
throw errcode(new Error('Incorrect protocol'), 'ERR_OPEN_STREAM_FAILED')
}

await handler(sh.rest())
await handler(sh.rest() as Duplex<Uint8ArrayList, Uint8Array>)
})
.finally(() => {
connection.close()
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p-daemon-protocol/src/stream-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class StreamHandler {
*/
rest () {
this.shake.rest()
return this.shake.stream
return this.shake.stream as Duplex<Uint8Array | Uint8ArrayList>
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p-daemon-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"@libp2p/interfaces": "^3.0.2",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/tcp": "^3.0.0",
"@libp2p/tcp": "^3.1.0",
"@multiformats/multiaddr": "^10.1.8",
"it-drain": "^1.0.5",
"it-length-prefixed": "^8.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p-daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
## Install

```console
$ npm i @libp2p/daemon
$ npm i --location=global @libp2p/daemon
```

## Specs
Expand Down
20 changes: 17 additions & 3 deletions packages/libp2p-daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,25 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/daemon-server": "^3.0.0",
"@chainsafe/libp2p-gossipsub": "^4.1.1",
"@chainsafe/libp2p-noise": "^8.0.1",
"@libp2p/bootstrap": "^2.0.0",
"@libp2p/connection": "^4.0.1",
"@libp2p/crypto": "^1.0.4",
"@libp2p/daemon-server": "^2.0.0",
saul-jb marked this conversation as resolved.
Show resolved Hide resolved
"@libp2p/floodsub": "^3.0.7",
"@libp2p/kad-dht": "^3.0.4",
"@libp2p/mplex": "^5.2.1",
"@libp2p/peer-id-factory": "^1.0.18",
"@libp2p/peer-record": "^4.0.2",
"@libp2p/peer-store": "^3.1.3",
"@libp2p/pubsub-peer-discovery": "^6.0.2",
"@libp2p/tcp": "^3.1.0",
"@libp2p/websockets": "^3.0.3",
"@multiformats/multiaddr": "^10.1.8",
"es-main": "^1.0.2",
"yargs": "^17.3.1",
"yargs-promise": "^1.1.0"
"libp2p": "^0.39.0",
saul-jb marked this conversation as resolved.
Show resolved Hide resolved
"yargs": "^17.3.1"
},
"devDependencies": {
"aegir": "^37.2.0",
Expand Down
79 changes: 49 additions & 30 deletions packages/libp2p-daemon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@

import { Multiaddr } from '@multiformats/multiaddr'
import yargs from 'yargs'
// @ts-expect-error no types
import YargsPromise from 'yargs-promise'
import type { Libp2pServer } from '@libp2p/daemon-server'
import { hideBin } from 'yargs/helpers'
import esMain from 'es-main'

const args = process.argv.slice(2)
const parser = new YargsPromise(yargs)
import server from './server.js'

const log = console.log

export default async function main (processArgs: string[]) {
parser.yargs
const argv: { [key: string]: any } = yargs(hideBin(processArgs))
.option('listen', {
desc: 'daemon control listen multiaddr',
type: 'string',
Expand All @@ -28,13 +24,12 @@ export default async function main (processArgs: string[]) {
})
.option('id', {
desc: 'peer identity; private key file',
type: 'string',
default: ''
type: 'string'
})
.option('hostAddrs', {
desc: 'Comma separated list of multiaddrs the host should listen on',
type: 'string',
default: ''
default: '/ip4/0.0.0.0/tcp/0'
saul-jb marked this conversation as resolved.
Show resolved Hide resolved
})
.option('announceAddrs', {
desc: 'Comma separated list of multiaddrs the host should announce to the network',
Expand All @@ -50,7 +45,7 @@ export default async function main (processArgs: string[]) {
.option('bootstrapPeers', {
desc: 'Comma separated list of bootstrap peers; defaults to the IPFS DHT peers',
type: 'string',
default: ''
default: '/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ,/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN,/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb,/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp,/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa,/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
})
.option('dht', {
desc: 'Enables the DHT in full node mode',
Expand All @@ -63,7 +58,7 @@ export default async function main (processArgs: string[]) {
default: false
})
.option('nat', {
desc: 'Enables UPnP NAT hole punching',
desc: '(Not yet supported) Enables UPnP NAT hole punching',
type: 'boolean',
default: false
})
Expand All @@ -90,29 +85,60 @@ export default async function main (processArgs: string[]) {
type: 'string',
default: 'gossipsub'
})
.option('pubsubDiscovery', {
desc: 'Enables pubsub peer discovery',
type: 'boolean',
default: false
})
.option('psk', {
desc: 'Pre-shared key file',
type: 'string'
})
.option('discoveryInterval', {
desc: 'The interval (ms) to perform peer discovery',
type: 'number',
default: 60e3
})
.option('relay', {
desc: 'Enables relay',
type: 'boolean',
default: false
})
.option('relayHop', {
desc: 'Enables relay HOP',
type: 'boolean',
default: false
})
.option('relayAdvertise', {
desc: 'Enables realy HOP advertisement',
type: 'boolean',
default: false
})
.option('relayAuto', {
desc: 'Enables Auto Relay',
type: 'boolean',
default: false
})
.option('relayAutoListeners', {
desc: 'Maximum number of simultaneous HOP connections for Auto Relay to open',
type: 'number',
default: 2
})
.fail((msg: string, err: Error | undefined, yargs?: any) => {
if (err != null) {
throw err // preserve stack
}

if (args.length > 0) {
if (hideBin(processArgs).length > 0) {
// eslint-disable-next-line
log(msg)
}

yargs.showHelp()
})
.parse()

const { data, argv } = await parser.parse(processArgs)

if (data != null) {
// Log help and exit
// eslint-disable-next-line
log(data)
process.exit(0)
}

const daemon = await createLibp2pServer(new Multiaddr(argv.listen), argv)
const daemon = await server.createLibp2pServer(new Multiaddr(argv.listen), argv)
await daemon.start()

if (argv.quiet !== true) {
Expand All @@ -121,13 +147,6 @@ export default async function main (processArgs: string[]) {
}
}

export async function createLibp2pServer (listenAddr: Multiaddr, argv: any): Promise<Libp2pServer> {
// const libp2p = await createLibp2p(argv)
// const daemon = await createServer(new Multiaddr(argv.listen), libp2p)

throw new Error('Not implemented yet')
}

if (esMain(import.meta)) {
main(process.argv)
.catch((err) => {
Expand Down
119 changes: 119 additions & 0 deletions packages/libp2p-daemon/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { promises as fs } from 'fs'
import type { Multiaddr } from '@multiformats/multiaddr'
import { createServer, Libp2pServer } from '@libp2p/daemon-server'
import { Libp2p, createLibp2p, Libp2pOptions } from 'libp2p'
import { PreSharedKeyConnectionProtector } from 'libp2p/pnet'
import { Noise } from '@chainsafe/libp2p-noise'
import { Mplex } from '@libp2p/mplex'
import { TCP } from '@libp2p/tcp'
import { WebSockets } from '@libp2p/websockets'
import { Bootstrap } from '@libp2p/bootstrap'
import { GossipSub } from '@chainsafe/libp2p-gossipsub'
import { FloodSub } from '@libp2p/floodsub'
import { PubSubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
import { unmarshalPrivateKey } from '@libp2p/crypto/keys'
import { createFromPrivKey } from '@libp2p/peer-id-factory'
import { KadDHT } from '@libp2p/kad-dht'

export default {
createLibp2pServer: async function (listenAddr: Multiaddr, argv: any): Promise<Libp2pServer> {
// Minimum libp2p setup.
const options: Libp2pOptions = {
addresses: {
listen: argv.hostAddrs.split(','),
announce: argv.announceAddrs.split(',')
},

transports: [new TCP(), new WebSockets()],
connectionEncryption: [new Noise()],
streamMuxers: [new Mplex()],
peerDiscovery: []
}

// Load key file as peer ID.
if (argv.id != null) {
const marshaledKey: Buffer = await fs.readFile(argv.id)
const unmarshaledKey = await unmarshalPrivateKey(marshaledKey)
const peerId = await createFromPrivKey(unmarshaledKey)

options.peerId = peerId
}

// Enable bootstrap peers.
if (argv.bootstrap === true && options.peerDiscovery != null) {
options.peerDiscovery.push(
new Bootstrap({
interval: argv.discoveryInterval,
list: argv.bootstrapPeers.split(',')
})
)
}

// Configure PubSub
if (argv.pubsub === true) {
// Router implementation.
switch (argv.pubsubRouter) {
case 'gossipsub':
options.pubsub = new GossipSub({ allowPublishToZeroPeers: true })
break
case 'floodsub':
options.pubsub = new FloodSub()
break
default:
throw new Error('invalid pubsubRouter type')
}

// Peer discovery
if (argv.pubsubDiscovery === true && options.peerDiscovery != null) {
options.peerDiscovery.push(
new PubSubPeerDiscovery({ interval: argv.discoveryInterval })
)
}
}

// Enable DHT
if (argv.dht === true) {
options.dht = new KadDHT()
}

// Configure PSK
if (argv.psk != null) {
const swarmKey: Buffer = await fs.readFile(argv.psk)

options.connectionProtector = new PreSharedKeyConnectionProtector({
psk: swarmKey
})
}

// Configure relay
if (argv.relay === true) {
options.relay = {
enabled: true
}

if (argv.relayAuto === true) {
options.relay.autoRelay = {
enabled: true,
maxListeners: argv.relayAutoListeners
}
}

if (argv.relayHop === true) {
options.relay.hop = {
enabled: true
}
}

if (argv.relayAdvertise === true) {
options.relay.advertise = {
enabled: true
}
}
}

const libp2p: Libp2p = await createLibp2p(options)
const daemon: Libp2pServer = createServer(listenAddr, libp2p)

return daemon
}
}
Loading