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

feat(js-client): Update libp2p ecosystem [fixes DXJ-551] #393

Merged
merged 7 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 14 additions & 13 deletions packages/core/js-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,28 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@chainsafe/libp2p-noise": "13.0.0",
"@chainsafe/libp2p-yamux": "5.0.0",
"@chainsafe/libp2p-noise": "14.0.0",
"@chainsafe/libp2p-yamux": "6.0.1",
"@fluencelabs/avm": "0.54.0",
"@fluencelabs/interfaces": "workspace:*",
"@fluencelabs/js-client-isomorphic": "workspace:*",
"@fluencelabs/marine-worker": "0.5.0",
"@fluencelabs/threads": "^2.0.0",
"@libp2p/crypto": "2.0.3",
"@libp2p/interface": "0.1.2",
"@libp2p/peer-id": "3.0.2",
"@libp2p/peer-id-factory": "3.0.3",
"@libp2p/websockets": "7.0.4",
"@libp2p/crypto": "3.0.1",
"@libp2p/identify": "1.0.4",
"@libp2p/interface": "1.0.1",
"@libp2p/peer-id": "4.0.1",
"@libp2p/peer-id-factory": "4.0.0",
"@libp2p/ping": "1.0.4",
"@libp2p/websockets": "8.0.5",
"@multiformats/multiaddr": "11.3.0",
"bs58": "5.0.0",
"buffer": "6.0.3",
"debug": "4.3.4",
"it-length-prefixed": "8.0.4",
"it-map": "2.0.0",
"it-pipe": "2.0.5",
"it-length-prefixed": "9.0.3",
"it-map": "3.0.5",
"it-pipe": "3.0.1",
"js-base64": "3.7.5",
"libp2p": "0.46.6",
"libp2p": "1.0.7",
"multiformats": "11.0.1",
"rxjs": "7.5.5",
"uint8arrays": "4.0.3",
Expand All @@ -59,7 +60,7 @@
},
"devDependencies": {
"@fluencelabs/aqua-api": "0.13.0",
"@rollup/plugin-inject": "5.0.3",
"@rollup/plugin-inject": "5.0.5",
"@types/bs58": "4.0.1",
"@types/debug": "4.1.7",
"@types/node": "20.7.0",
Expand Down
15 changes: 7 additions & 8 deletions packages/core/js-client/src/connection/RelayConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
import { noise } from "@chainsafe/libp2p-noise";
import { yamux } from "@chainsafe/libp2p-yamux";
import { PeerIdB58 } from "@fluencelabs/interfaces";
import type { Stream } from "@libp2p/interface/connection";
import type { PeerId } from "@libp2p/interface/peer-id";
import { identify } from "@libp2p/identify";
import type { PeerId, Stream } from "@libp2p/interface";
import { peerIdFromString } from "@libp2p/peer-id";
import { ping } from "@libp2p/ping";
import { webSockets } from "@libp2p/websockets";
import { all } from "@libp2p/websockets/filters";
import { multiaddr, type Multiaddr } from "@multiformats/multiaddr";
import { decode, encode } from "it-length-prefixed";
import map from "it-map";
import { pipe } from "it-pipe";
import { createLibp2p, Libp2p } from "libp2p";
import { identifyService } from "libp2p/identify";
import { pingService } from "libp2p/ping";
import { Subject } from "rxjs";
import { fromString } from "uint8arrays/from-string";
import { toString } from "uint8arrays/to-string";
Expand Down Expand Up @@ -139,8 +138,8 @@ export class RelayConnection implements IConnection {
},
},
services: {
identify: identifyService(),
ping: pingService(),
identify: identify(),
ping: ping(),
},
});

Expand Down Expand Up @@ -192,7 +191,7 @@ export class RelayConnection implements IConnection {
log.trace("created stream with id ", stream.id);
const sink = stream.sink;

await pipe([fromString(serializeToString(particle))], encode(), sink);
await pipe([fromString(serializeToString(particle))], encode, sink);
log.trace("data written to sink");
}

Expand Down Expand Up @@ -260,7 +259,7 @@ export class RelayConnection implements IConnection {
({ stream }) => {
void pipe(
stream.source,
decode(),
decode,
(source) => {
return map(source, (buf) => {
return toString(buf.subarray());
Expand Down
21 changes: 10 additions & 11 deletions packages/core/js-client/src/jsPeer/FluencePeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import { Buffer } from "buffer";

import {
deserializeAvmResult,
InterpreterResult,
Expand Down Expand Up @@ -165,7 +163,7 @@ export abstract class FluencePeer {
* @param serviceId - the service id by which the service can be accessed in aqua
*/
async registerMarineService(
wasm: SharedArrayBuffer | Buffer,
wasm: ArrayBuffer | SharedArrayBuffer,
serviceId: string,
): Promise<void> {
if (this.jsServiceHost.hasService(serviceId)) {
Expand Down Expand Up @@ -342,10 +340,7 @@ export abstract class FluencePeer {
private async sendParticleToRelay(
item: ParticleQueueItem & { result: InterpreterResult },
) {
const newParticle = cloneWithNewData(
item.particle,
Buffer.from(item.result.data),
);
const newParticle = cloneWithNewData(item.particle, item.result.data);

log_particle.debug(
"id %s. sending particle into network. Next peer ids: %s",
Expand Down Expand Up @@ -407,7 +402,10 @@ export abstract class FluencePeer {
retCode: res.retCode,
};

const newParticle = cloneWithNewData(item.particle, Buffer.from([]));
const newParticle = cloneWithNewData(
item.particle,
Uint8Array.from([]),
);

this._incomingParticles.next({
...item,
Expand Down Expand Up @@ -467,7 +465,7 @@ export abstract class FluencePeer {
private mapParticleGroup(
group$: GroupedObservable<string, ParticleQueueItem>,
) {
let prevData: Uint8Array = Buffer.from([]);
let prevData = Uint8Array.from([]);

return group$.pipe(
concatMap(async (item) => {
Expand Down Expand Up @@ -515,13 +513,14 @@ export abstract class FluencePeer {
const res = await this.marineHost.callService("avm", "invoke", args);

avmCallResult = deserializeAvmResult(res);
// TODO: This is bug in @fluencelabs/avm package. 'avmCallResult.data' actually number array, not Uint8Array as stated in type.
avmCallResult.data = Uint8Array.from(avmCallResult.data);
} catch (e) {
avmCallResult = e instanceof Error ? e : new Error(String(e));
}

if (!(avmCallResult instanceof Error) && avmCallResult.retCode === 0) {
const newData = Buffer.from(avmCallResult.data);
prevData = newData;
prevData = avmCallResult.data;
}

return {
Expand Down
7 changes: 3 additions & 4 deletions packages/core/js-client/src/keypair/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
generateKeyPair,
unmarshalPublicKey,
} from "@libp2p/crypto/keys";
import type { PrivateKey, PublicKey } from "@libp2p/interface/keys";
import type { PeerId } from "@libp2p/interface/peer-id";
import type { PrivateKey, PublicKey, PeerId } from "@libp2p/interface";
import { createFromPrivKey } from "@libp2p/peer-id-factory";
import bs58 from "bs58";
import { toUint8Array } from "js-base64";
Expand Down Expand Up @@ -91,11 +90,11 @@ export class KeyPair {
return this.privateKey.marshal().subarray(0, 32);
}

signBytes(data: Uint8Array): Promise<Uint8Array> {
async signBytes(data: Uint8Array): Promise<Uint8Array> {
return this.privateKey.sign(data);
}

verify(data: Uint8Array, signature: Uint8Array): Promise<boolean> {
async verify(data: Uint8Array, signature: Uint8Array): Promise<boolean> {
return this.publicKey.verify(data, signature);
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/core/js-client/src/services/SingleModuleSrv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import { Buffer } from "buffer";

import { v4 as uuidv4 } from "uuid";

import { ServiceFnArgs } from "../compilerSupport/types.js";
Expand Down Expand Up @@ -53,7 +51,12 @@ export class Srv {

try {
const newServiceId = uuidv4();
const buffer = Buffer.from(wasmContent, "base64");

const buffer = Uint8Array.from(atob(wasmContent), (m) => {
// codePointAt cannot return `undefined` value here as callback is called on every symbol
return m.codePointAt(0) ?? 0;
});

// TODO:: figure out why SharedArrayBuffer is not working here
// const sab = new SharedArrayBuffer(buffer.length);
// const tmp = new Uint8Array(sab);
Expand Down
4 changes: 1 addition & 3 deletions packages/core/js-client/src/services/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import { Buffer } from "buffer";

import { JSONValue } from "@fluencelabs/interfaces";
import bs58 from "bs58";
import { sha256 } from "multiformats/hashes/sha2";
Expand Down Expand Up @@ -332,7 +330,7 @@ export const builtInServices: Record<
}),

sha256_string: withSchema(z.tuple([z.string()]))(async ([input]) => {
const inBuffer = Buffer.from(input);
const inBuffer = new TextEncoder().encode(input);
const multihash = await sha256.digest(inBuffer);

return success(bs58.encode(multihash.bytes));
Expand Down
28 changes: 14 additions & 14 deletions packages/core/js-client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* Copyright 2023 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,11 +14,12 @@
* limitations under the License.
*/

import inject from "@rollup/plugin-inject";
import tsconfigPaths from "vite-tsconfig-paths";
import { createRequire } from "module";
import { PluginOption, UserConfig } from "vite";

import inject from "@rollup/plugin-inject";
import { transform } from "esbuild";
import { PluginOption, UserConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

const require = createRequire(import.meta.url);
const esbuildShim = require.resolve("node-stdlib-browser/helpers/esbuild/shim");
Expand All @@ -35,6 +36,7 @@ function minifyEs(): PluginOption {
) {
return await transform(code, { minify: true });
}

return code;
},
},
Expand All @@ -48,21 +50,19 @@ const config: UserConfig = {
lib: {
entry: "./src/index.ts",
name: "js-client",
fileName: () => "index.min.js",
fileName: () => {
return "index.min.js";
},
formats: ["es"],
},
outDir: "./dist/browser",
rollupOptions: {
plugins: [
{
// @ts-ignore
...inject({
global: [esbuildShim, "global"],
process: [esbuildShim, "process"],
Buffer: [esbuildShim, "Buffer"],
}),
enforce: "post",
},
inject({
global: [esbuildShim, "global"],
process: [esbuildShim, "process"],
Buffer: [esbuildShim, "Buffer"],
}),
],
},
},
Expand Down
Loading
Loading