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

refactor: examples/pnet #523

Merged
merged 3 commits into from
Jan 7, 2020
Merged
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
145 changes: 0 additions & 145 deletions examples/pnet-ipfs/index.js

This file was deleted.

60 changes: 0 additions & 60 deletions examples/pnet-ipfs/libp2p-bundle.js

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions examples/pnet-ipfs/README.md → examples/pnet/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Private Networking with IPFS
This example shows how to set up a private network of IPFS nodes.
# Private Networking
This example shows how to set up a private network of libp2p nodes.

## Setup
Install dependencies:
Expand Down
50 changes: 50 additions & 0 deletions examples/pnet/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint no-console: ["off"] */
'use strict'

const { generate } = require('libp2p/src/pnet')
const privateLibp2pNode = require('./libp2p-node')

const pipe = require('it-pipe')

// Create a buffer and write the swarm key to it
const swarmKey = Buffer.alloc(95)
generate(swarmKey)

// This key is for testing a different key not working
const otherSwarmKey = Buffer.alloc(95)
generate(otherSwarmKey)

;(async () => {
const node1 = await privateLibp2pNode(swarmKey)

// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
const node2 = await privateLibp2pNode(swarmKey)
// const node2 = await privateLibp2pNode(otherSwarmKey)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacobheun I am having an issue using a invalid key.

On the transport listener, for example libp2p-tcp https://github.com/libp2p/js-libp2p-tcp/blob/master/src/listener.js#L23 the upgradeInbound throws and the promise rejection is not handled. What do you recommend to do here? A try...catch in the encrypt and just log the error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vasco-santos the upgradeInbound should probably still throw. The Transport needs to know that there has been a failure so that it doesn't do any emitting. I think the best thing to do is to make sure that the transport listeners are catching that and logging errors. We should add a test for this to the transport interface and roll out patches to each of the transports.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I agree that should be the transports to handle this. Will PR everything


await Promise.all([
node1.start(),
node2.start()
])

console.log('nodes started...')

await node1.dial(node2.peerInfo)

node2.handle('/private', ({ stream }) => {
pipe(
stream,
async function (source) {
for await (const msg of source) {
console.log(msg.toString())
}
}
)
})

const { stream } = await node1.dialProtocol(node2.peerInfo, '/private')

await pipe(
['This message is sent on a private network'],
stream
)
})();
36 changes: 36 additions & 0 deletions examples/pnet/libp2p-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const Protector = require('libp2p/src/pnet')

/**
* privateLibp2pNode returns a libp2p node function that will use the swarm
* key at the given `swarmKeyPath` to create the Protector
*
* @param {Buffer} swarmKey
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
*/
const privateLibp2pNode = async (swarmKeyPath) => {
const node = await Libp2p.create({
modules: {
transport: [TCP], // We're only using the TCP transport for this example
streamMuxer: [MPLEX], // We're only using mplex muxing
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
// care about node identity, and only the presence of private keys
connEncryption: [SECIO],
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
// being left in for explicit readability.
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
peerDiscovery: [],
connProtector: new Protector(swarmKeyPath)
}
})

node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
return node
}

module.exports = privateLibp2pNode
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"ipfs": "^0.38.0",
"libp2p": "^0.26.2",
"libp2p-mplex": "^0.8.5",
"libp2p-secio": "^0.11.1",
"libp2p-tcp": "^0.13.2"
"libp2p": "../..",
"libp2p-mplex": "^0.9.3",
"libp2p-secio": "^0.12.1",
"libp2p-tcp": "^0.14.2"
}
}
File renamed without changes.