Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 8ac029b

Browse files
monoidAkim MamedovfolexAkim
authored
feat(js-client)!: Multiformat MsgPack for particle data (#422)
* feat(particle)!: Multiformat MsgPack for particle data * Fix types * fix(ci): use nox with msgpack protocol * fix(avm): avm 0.59.0 * Fix uint64 * Fix * fix(ci): enable nox debug logs * Fix commonJS import * Revert "fix(ci): enable nox debug logs" This reverts commit ce5bc2e. --------- Co-authored-by: Akim Mamedov <akim99999999@gmail.com> Co-authored-by: folex <0xdxdy@gmail.com> Co-authored-by: Akim <59872966+akim-bow@users.noreply.github.com>
1 parent fe661db commit 8ac029b

File tree

6 files changed

+41
-24
lines changed

6 files changed

+41
-24
lines changed

.github/workflows/e2e.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ jobs:
4343
uses: fluencelabs/aqua/.github/workflows/tests.yml@main
4444
with:
4545
js-client-snapshots: "${{ needs.js-client.outputs.js-client-snapshots }}"
46-
nox-image: "fluencelabs/nox:unstable"
46+
nox-image: "docker.fluence.dev/nox:feat-VM-407-msgpack-particle"
4747
flox:
4848
needs:
4949
- js-client
5050

5151
uses: fluencelabs/flox/.github/workflows/tests.yml@main
5252
with:
5353
js-client-snapshots: "${{ needs.js-client.outputs.js-client-snapshots }}"
54-
nox-image: "fluencelabs/nox:unstable"
54+
nox-image: "docker.fluence.dev/nox:feat-VM-407-msgpack-particle"

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
uses: ./.github/workflows/tests.yml
2929
with:
3030
ref: ${{ github.ref }}
31-
nox-image: "fluencelabs/nox:unstable"
31+
nox-image: "docker.fluence.dev/nox:feat-VM-407-msgpack-particle"

packages/core/js-client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"author": "Fluence Labs",
3131
"license": "Apache-2.0",
3232
"dependencies": {
33-
"@libp2p/utils": "5.2.2",
3433
"@chainsafe/libp2p-noise": "14.0.0",
3534
"@chainsafe/libp2p-yamux": "6.0.1",
3635
"@fluencelabs/avm": "0.59.0",
@@ -44,10 +43,12 @@
4443
"@libp2p/peer-id": "4.0.5",
4544
"@libp2p/peer-id-factory": "4.0.5",
4645
"@libp2p/ping": "1.0.10",
46+
"@libp2p/utils": "5.2.2",
4747
"@libp2p/websockets": "8.0.12",
4848
"@multiformats/multiaddr": "12.1.12",
4949
"bs58": "5.0.0",
5050
"debug": "4.3.4",
51+
"int64-buffer": "1.0.1",
5152
"it-length-prefixed": "9.0.3",
5253
"it-map": "3.0.5",
5354
"it-pipe": "3.0.1",

packages/core/js-client/src/connection/RelayConnection.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ import map from "it-map";
2929
import { pipe } from "it-pipe";
3030
import { createLibp2p, Libp2p } from "libp2p";
3131
import { Subject } from "rxjs";
32-
import { fromString } from "uint8arrays/from-string";
33-
import { toString } from "uint8arrays/to-string";
3432

3533
import { KeyPair } from "../keypair/index.js";
3634
import { IParticle } from "../particle/interfaces.js";
3735
import {
3836
buildParticleMessage,
3937
Particle,
40-
serializeToString,
38+
serializeParticle,
4139
} from "../particle/Particle.js";
4240
import { throwHasNoPeerId } from "../util/libp2pUtils.js";
4341
import { logger } from "../util/logger.js";
@@ -216,7 +214,7 @@ export class RelayConnection implements IConnection {
216214

217215
const sink = stream.sink;
218216

219-
await pipe([fromString(serializeToString(particle))], encode, sink);
217+
await pipe([serializeParticle(particle)], encode, sink);
220218

221219
log.trace(
222220
"particle %s sent to %s",
@@ -225,11 +223,11 @@ export class RelayConnection implements IConnection {
225223
);
226224
}
227225

228-
private async processIncomingMessage(msg: string) {
226+
private async processIncomingMessage(msg: Uint8Array) {
229227
let particle: Particle | undefined;
230228

231229
try {
232-
particle = Particle.fromString(msg);
230+
particle = Particle.deserialize(msg);
233231

234232
log.trace(
235233
"received particle %s from %s",
@@ -290,7 +288,7 @@ export class RelayConnection implements IConnection {
290288
decode,
291289
(source) => {
292290
return map(source, (buf) => {
293-
return toString(buf.subarray());
291+
return buf.subarray();
294292
});
295293
},
296294
async (source) => {

packages/core/js-client/src/particle/Particle.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { CallResultsArray } from "@fluencelabs/avm";
17+
import {
18+
CallResultsArray,
19+
MulticodecRepr,
20+
MsgPackRepr,
21+
} from "@fluencelabs/avm";
1822
import { JSONValue } from "@fluencelabs/interfaces";
19-
import { fromUint8Array, toUint8Array } from "js-base64";
23+
import int64Buffer from "int64-buffer";
2024
import { concat } from "uint8arrays/concat";
2125
import { v4 as uuidv4 } from "uuid";
2226
import { z } from "zod";
@@ -27,14 +31,16 @@ import { numberToLittleEndianBytes } from "../util/bytes.js";
2731

2832
import { IParticle } from "./interfaces.js";
2933

34+
const particleRepr = new MulticodecRepr(new MsgPackRepr());
35+
3036
const particleSchema = z.object({
3137
id: z.string(),
3238
timestamp: z.number().positive(),
3339
script: z.string(),
34-
data: z.string(),
40+
data: z.instanceof(Uint8Array),
3541
ttl: z.number().positive(),
3642
init_peer_id: z.string(),
37-
signature: z.array(z.number()),
43+
signature: z.instanceof(Uint8Array),
3844
});
3945

4046
export class Particle implements IParticle {
@@ -73,10 +79,10 @@ export class Particle implements IParticle {
7379
);
7480
}
7581

76-
static fromString(str: string): Particle {
77-
const json = JSON.parse(str);
82+
static deserialize(bytes: Uint8Array): Particle {
83+
const obj = particleRepr.fromBinary(bytes);
7884

79-
const res = particleSchema.safeParse(json);
85+
const res = particleSchema.safeParse(obj);
8086

8187
if (!res.success) {
8288
throw new Error(
@@ -92,10 +98,10 @@ export class Particle implements IParticle {
9298
data.id,
9399
data.timestamp,
94100
data.script,
95-
toUint8Array(data.data),
101+
data.data,
96102
data.ttl,
97103
data.init_peer_id,
98-
new Uint8Array(data.signature),
104+
data.signature,
99105
);
100106
}
101107
}
@@ -154,16 +160,16 @@ export const cloneWithNewData = (
154160
/**
155161
* Serializes particle into string suitable for sending through network
156162
*/
157-
export const serializeToString = (particle: IParticle): string => {
158-
return JSON.stringify({
163+
export const serializeParticle = (particle: IParticle): Uint8Array => {
164+
return particleRepr.toBinary({
159165
action: "Particle",
160166
id: particle.id,
161167
init_peer_id: particle.initPeerId,
162-
timestamp: particle.timestamp,
168+
timestamp: new int64Buffer.Uint64BE(particle.timestamp),
163169
ttl: particle.ttl,
164170
script: particle.script,
165171
signature: Array.from(particle.signature),
166-
data: fromUint8Array(particle.data),
172+
data: Array.from(particle.data),
167173
});
168174
};
169175

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)