This commit is contained in:
Pavel Murygin 2022-08-25 17:43:31 +03:00
parent 16fdbce17d
commit cc4ee3e5cf
3 changed files with 1150 additions and 190 deletions

View File

@ -16,18 +16,20 @@
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@fluencelabs/interfaces": "workspace:0.1.0", "@fluencelabs/interfaces": "workspace:0.1.0",
"peer-id": "0.16.0", "@chainsafe/libp2p-noise": "^8.0.1",
"it-length-prefixed": "5.0.3", "@libp2p/mplex": "^3.0.0",
"it-pipe": "1.1.0", "@libp2p/peer-id": "^1.1.10",
"@chainsafe/libp2p-noise": "^4.1.1", "@libp2p/websockets": "^3.0.0",
"libp2p": "0.36.2", "@multiformats/multiaddr": "^10.1.8",
"libp2p-interfaces": "^4.0.6", "it-length-prefixed": "7.0.1",
"libp2p-mplex": "0.10.7", "it-pipe": "2.0.3",
"libp2p-websockets": "0.16.2", "libp2p": "0.38.0",
"loglevel": "1.8.0", "loglevel": "1.8.0"
"multiaddr": "10.0.1"
}, },
"devDependencies": { "devDependencies": {
"@libp2p/interface-connection": "^2.0.0",
"@libp2p/interface-peer-id": "^1.0.2",
"@types/node": "^18.7.13",
"typescript": "^4.6.4" "typescript": "^4.6.4"
} }
} }

View File

@ -14,21 +14,17 @@
* limitations under the License. * limitations under the License.
*/ */
import { FluenceConnection, ParticleHandler, PeerIdB58 } from '@fluencelabs/interfaces'; import { FluenceConnection, ParticleHandler, PeerIdB58 } from '@fluencelabs/interfaces';
// @ts-ignore import { WebSockets } from '@libp2p/websockets';
import Websockets from 'libp2p-websockets'; import { Mplex } from '@libp2p/mplex';
// @ts-ignore import { Libp2p, createLibp2p } from 'libp2p';
import Mplex from 'libp2p-mplex';
import Lib2p2Peer from 'libp2p';
import { decode, encode } from 'it-length-prefixed'; import { decode, encode } from 'it-length-prefixed';
import { pipe } from 'it-pipe'; import { pipe } from 'it-pipe';
import * as log from 'loglevel'; import * as log from 'loglevel';
import { Noise } from '@chainsafe/libp2p-noise'; import { Noise } from '@chainsafe/libp2p-noise';
import PeerId from 'peer-id'; import type { PeerId } from '@libp2p/interface-peer-id';
import type { MultiaddrInput } from 'multiaddr'; import type { MultiaddrInput } from '@multiformats/multiaddr';
import { Multiaddr } from 'multiaddr'; import { Multiaddr } from '@multiformats/multiaddr/dist/src';
// @ts-ignore import { Connection } from '@libp2p/interface-connection';
import { all as allow_all } from 'libp2p-websockets/src/filters';
import { Connection } from 'libp2p-interfaces/src/topology';
export const PROTOCOL_NAME = '/fluence/particle/2.0.0'; export const PROTOCOL_NAME = '/fluence/particle/2.0.0';
@ -58,7 +54,7 @@ export interface FluenceConnectionOptions {
export class RelayConnection extends FluenceConnection { export class RelayConnection extends FluenceConnection {
constructor( constructor(
public peerId: PeerIdB58, public peerId: PeerIdB58,
private _lib2p2Peer: Lib2p2Peer, private _lib2p2Peer: Libp2p,
private _relayAddress: Multiaddr, private _relayAddress: Multiaddr,
public readonly relayPeerId: PeerIdB58, public readonly relayPeerId: PeerIdB58,
) { ) {
@ -68,21 +64,11 @@ export class RelayConnection extends FluenceConnection {
private _connection?: Connection; private _connection?: Connection;
static async createConnection(options: FluenceConnectionOptions): Promise<RelayConnection> { static async createConnection(options: FluenceConnectionOptions): Promise<RelayConnection> {
const transportKey = Websockets.prototype[Symbol.toStringTag]; const lib2p2Peer = await createLibp2p({
const lib2p2Peer = await Lib2p2Peer.create({
peerId: options.peerId, peerId: options.peerId,
modules: { transports: [new WebSockets()],
transport: [Websockets], streamMuxers: [new Mplex()],
streamMuxer: [Mplex], connectionEncryption: [new Noise()],
connEncryption: [new Noise()],
},
config: {
transport: {
[transportKey]: {
filter: allow_all,
},
},
},
dialer: { dialer: {
dialTimeout: options?.dialTimeoutMs, dialTimeout: options?.dialTimeoutMs,
}, },
@ -103,11 +89,6 @@ export class RelayConnection extends FluenceConnection {
); );
} }
async disconnect() {
await this._lib2p2Peer.unhandle(PROTOCOL_NAME);
await this._lib2p2Peer.stop();
}
async sendParticle(nextPeerIds: PeerIdB58[], particle: string): Promise<void> { async sendParticle(nextPeerIds: PeerIdB58[], particle: string): Promise<void> {
if (nextPeerIds.length !== 1 && nextPeerIds[0] !== this.relayPeerId) { if (nextPeerIds.length !== 1 && nextPeerIds[0] !== this.relayPeerId) {
throw new Error( throw new Error(
@ -126,8 +107,8 @@ export class RelayConnection extends FluenceConnection {
const sink = this._connection.streams[0].sink; const sink = this._connection.streams[0].sink;
*/ */
const conn = await this._lib2p2Peer.dialProtocol(this._relayAddress, PROTOCOL_NAME); const stream = await this._lib2p2Peer.dialProtocol(this._relayAddress, PROTOCOL_NAME);
const sink = conn.stream.sink; const sink = stream.sink;
pipe( pipe(
// force new line // force new line
@ -161,7 +142,7 @@ export class RelayConnection extends FluenceConnection {
); );
}); });
log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId.toB58String()); log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId.toString());
try { try {
this._connection = await this._lib2p2Peer.dial(this._relayAddress); this._connection = await this._lib2p2Peer.dial(this._relayAddress);

1269
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff