-
Notifications
You must be signed in to change notification settings - Fork 459
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
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 | ||
) | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theupgradeInbound
throws and the promise rejection is not handled. What do you recommend to do here? Atry...catch
in the encrypt and just log the error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, the error comes from:
https://github.com/libp2p/js-libp2p/blob/refactor/async-await/src/upgrader.js#L328
https://github.com/multiformats/js-multistream-select/blob/master/src/handle.js#L14
https://github.com/multiformats/js-multistream-select/blob/master/src/multistream.js#L37
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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