Upgrade js-libp2p and related dependencies (#298)

* Upgrade js-libp2p and related dependencies

* Dial all multiaddr of a discovered peer in parallel

* Dial using peer id instead of dialling individual multiaddr

* Log peer id on discovery
This commit is contained in:
prathamesh0 2023-01-20 10:34:55 +05:30 committed by GitHub
parent 7069a7f455
commit ff64dcb43c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 554 additions and 522 deletions

View File

@ -16,7 +16,7 @@
"@ethersproject/providers": "^5.4.4",
"@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^8.0.0",
"@libp2p/interface-peer-id": "^1.1.2",
"@libp2p/interface-peer-id": "^2.0.0",
"apollo-server-express": "^3.11.1",
"debug": "^4.3.1",
"express": "^4.18.2",

View File

@ -51,7 +51,7 @@ function App() {
}
peer.node.peerStore.addEventListener('change:multiaddrs', forceUpdate)
peer.node.connectionManager.addEventListener('peer:connect', forceUpdate)
peer.node.addEventListener('peer:connect', forceUpdate)
let lastDisconnect = new Date()
const disconnectHandler = () => {
@ -63,14 +63,14 @@ function App() {
lastDisconnect = now;
}
peer.node.connectionManager.addEventListener('peer:disconnect', disconnectHandler)
peer.node.addEventListener('peer:disconnect', disconnectHandler)
return () => {
unsubscribeMessage()
unsubscribeTopic()
peer.node?.peerStore.removeEventListener('change:multiaddrs', forceUpdate)
peer.node?.connectionManager.removeEventListener('peer:connect', forceUpdate)
peer.node?.connectionManager.removeEventListener('peer:disconnect', disconnectHandler)
peer.node?.removeEventListener('peer:connect', forceUpdate)
peer.node?.removeEventListener('peer:disconnect', disconnectHandler)
}
}, [peer, forceUpdate])
@ -139,10 +139,10 @@ function App() {
peer && peer.node && (
<>
<Typography variant="subtitle1" color="inherit" noWrap>
Remote Peer Connections (Count: {peer.node.connectionManager.getConnections().length})
Remote Peer Connections (Count: {peer.node.getConnections().length})
</Typography>
<br/>
{peer.node.connectionManager.getConnections().map(connection => (
{peer.node.getConnections().map(connection => (
<TableContainer sx={{ mb: 2 }} key={connection.id} component={Paper}>
<Table size="small">
<TableBody>

View File

@ -27,12 +27,11 @@
"relay-node": "node dist/relay.js"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^10.2.0",
"@libp2p/floodsub": "^5.0.0",
"@libp2p/interface-peer-id": "^1.1.2",
"@chainsafe/libp2p-noise": "^11.0.0",
"@libp2p/floodsub": "^6.0.0",
"@libp2p/mplex": "^7.1.1",
"@libp2p/peer-id-factory": "^2.0.0",
"@libp2p/pubsub-peer-discovery": "^7.0.1",
"@libp2p/pubsub-peer-discovery": "^8.0.0",
"@libp2p/webrtc-star": "^5.0.3",
"@multiformats/multiaddr": "^11.1.4",
"debug": "^4.3.1",
@ -40,14 +39,14 @@
"it-map": "^2.0.0",
"it-pipe": "^2.0.5",
"it-pushable": "^3.1.2",
"libp2p": "^0.41.0",
"libp2p": "^0.42.2",
"node-pre-gyp": "^0.13.0",
"uint8arrays": "^4.0.3",
"wrtc": "^0.4.7",
"yargs": "^17.0.1"
},
"devDependencies": {
"@libp2p/webrtc-star-signalling-server": "^2.0.5",
"@libp2p/webrtc-star-signalling-server": "^3.0.0",
"@types/node": "16.11.7",
"@typescript-eslint/eslint-plugin": "^5.47.1",
"@typescript-eslint/parser": "^5.47.1",

View File

@ -19,7 +19,7 @@ import { mplex } from '@libp2p/mplex';
import type { Stream as P2PStream, Connection } from '@libp2p/interface-connection';
import type { PeerInfo } from '@libp2p/interface-peer-info';
import type { Message } from '@libp2p/interface-pubsub';
import { PeerId } from '@libp2p/interface-peer-id';
import type { PeerId } from '@libp2p/interface-peer-id';
import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
import { floodsub } from '@libp2p/floodsub';
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
@ -134,13 +134,13 @@ export class Peer {
});
// Listen for peers connection
this._node.connectionManager.addEventListener('peer:connect', async (evt) => {
this._node.addEventListener('peer:connect', async (evt) => {
console.log('event peer:connect', evt);
await this._handleConnect(evt.detail);
});
// Listen for peers disconnecting
this._node.connectionManager.addEventListener('peer:disconnect', (evt) => {
this._node.addEventListener('peer:disconnect', (evt) => {
console.log('event peer:disconnect', evt);
this._handleDisconnect(evt.detail);
});
@ -160,8 +160,8 @@ export class Peer {
assert(this._node);
this._node.removeEventListener('peer:discovery');
this._node.connectionManager.removeEventListener('peer:connect');
this._node.connectionManager.removeEventListener('peer:disconnect');
this._node.removeEventListener('peer:connect');
this._node.removeEventListener('peer:disconnect');
this._node.pubsub.removeEventListener('message');
await this._node.unhandle(CHAT_PROTOCOL);
@ -272,7 +272,7 @@ export class Peer {
_handleDiscovery (peer: PeerInfo): void {
// Check connected peers as they are discovered repeatedly.
if (![...this._remotePeerIds].some(remotePeerId => remotePeerId.toString() === peer.id.toString())) {
console.log('Discovered peer multiaddrs', peer.multiaddrs.map(addr => addr.toString()));
console.log(`Discovered peer ${peer.id.toString()} with multiaddrs`, peer.multiaddrs.map(addr => addr.toString()));
this._connectPeer(peer);
}
}
@ -359,30 +359,19 @@ export class Peer {
assert(this._node);
// Dial them when we discover them
// Attempt to dial all the multiaddrs of the discovered peer (to connect through relay)
for (const peerMultiaddr of peer.multiaddrs) {
// Relay nodes sometimes give an additional multiaddr of signalling server (without peer id) in discovery
// Eg. /ip4/127.0.0.1/tcp/13579/wss/p2p-webrtc-star
// Workaround to avoid dialling multiaddr(s) without peer id
if (!peerMultiaddr.toString().includes('p2p/')) {
continue;
}
try {
console.log(`Dialling peer ${peer.id.toString()} using multiaddr ${peerMultiaddr.toString()}`);
const stream = await this._node.dialProtocol(peerMultiaddr, CHAT_PROTOCOL);
this._handleStream(peer.id, stream);
break;
} catch (err: any) {
// Check if protocol negotiation failed (dial still succeeds)
// (happens in case of dialProtocol to relay nodes since they don't handle CHAT_PROTOCOL)
if ((err as Error).message === ERR_PROTOCOL_SELECTION) {
console.log(`Protocol selection failed with peer ${peerMultiaddr}`);
break;
} else {
console.log(`Could not dial ${peerMultiaddr.toString()}`, err);
}
const peerIdString = peer.id.toString();
try {
console.log(`Dialling peer ${peerIdString}`);
// When dialling with peer id, all multiaddr(s) (direct/relayed) of the discovered peer are dialled in parallel
const stream = await this._node.dialProtocol(peer.id, CHAT_PROTOCOL);
this._handleStream(peer.id, stream);
} catch (err: any) {
// Check if protocol negotiation failed (dial still succeeds)
// (happens in case of dialProtocol to relay nodes since they don't handle CHAT_PROTOCOL)
if ((err as Error).message === ERR_PROTOCOL_SELECTION) {
console.log(`Protocol selection failed with peer ${peerIdString}`);
} else {
console.log(`Could not dial ${peerIdString}`, err);
}
}
}

View File

@ -88,7 +88,7 @@ async function main (): Promise<void> {
console.log();
// Listen for peers connection
node.connectionManager.addEventListener('peer:connect', (evt) => {
node.addEventListener('peer:connect', (evt) => {
// console.log('event peer:connect', evt);
// Log connected peer
const connection: Connection = evt.detail;
@ -96,7 +96,7 @@ async function main (): Promise<void> {
});
// Listen for peers disconnecting
node.connectionManager.addEventListener('peer:disconnect', (evt) => {
node.addEventListener('peer:disconnect', (evt) => {
// console.log('event peer:disconnect', evt);
// Log disconnected peer
const connection: Connection = evt.detail;

998
yarn.lock

File diff suppressed because it is too large Load Diff