Skip to content

Data as base64 #2

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

Merged
merged 5 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fluence",
"version": "0.7.106",
"version": "0.7.107",
"description": "the browser js-libp2p client for the Fluence network",
"main": "./dist/fluence.js",
"typings": "./dist/fluence.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/aqua/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @param {string} log_level
* @returns {string}
*/
export function invoke(wasm: any, init_user_id: string, aqua: string, prev_data: string, data: string, log_level: string): string;
export function invoke(wasm: any, init_user_id: string, aqua: string, prev_data: Uint8Array, data: Uint8Array, log_level: string): string;
export function ast(wasm: any, script: string): string;
export function return_current_peer_id(wasm: any, peerId: string, arg0: any): void;
export function return_call_service_result(wasm: any, ret: string, arg0: any): void;
Expand Down
12 changes: 10 additions & 2 deletions src/aqua/index_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ cachedTextDecoder.decode();
export function getStringFromWasm0(wasm, ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0(wasm).subarray(ptr, ptr + len));
}

function passArray8ToWasm0(wasm, arg, malloc) {
const ptr = malloc(arg.length * 1);
getUint8Memory0(wasm).set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}

/**
* @param {any} wasm
* @param {string} init_user_id
Expand All @@ -106,9 +114,9 @@ export function invoke(wasm, init_user_id, aqua, prev_data, data, log_level) {
var len0 = WASM_VECTOR_LEN;
var ptr1 = passStringToWasm0(wasm, aqua, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
var ptr2 = passStringToWasm0(wasm, prev_data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var ptr2 = passArray8ToWasm0(wasm, prev_data, wasm.__wbindgen_malloc);
var len2 = WASM_VECTOR_LEN;
var ptr3 = passStringToWasm0(wasm, data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var ptr3 = passArray8ToWasm0(wasm, data, wasm.__wbindgen_malloc);
var len3 = WASM_VECTOR_LEN;
var ptr4 = passStringToWasm0(wasm, log_level, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len4 = WASM_VECTOR_LEN;
Expand Down
8 changes: 4 additions & 4 deletions src/fluenceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class FluenceClient {
log.info(`Particle expired. Now: ${now}, ttl: ${particle.ttl}, ts: ${particle.timestamp}`);
} else {
// if there is no subscription yet, previous data is empty
let prevData = [];
let prevData: Uint8Array = Buffer.from([]);
let prevParticle = this.subscriptions.get(particle.id);
if (prevParticle) {
prevData = prevParticle.data;
Expand All @@ -79,8 +79,8 @@ export class FluenceClient {
let stepperOutcomeStr = this.interpreter(
particle.init_peer_id,
particle.script,
JSON.stringify(prevData),
JSON.stringify(particle.data),
prevData,
particle.data,
);
let stepperOutcome: StepperOutcome = JSON.parse(stepperOutcomeStr);

Expand Down Expand Up @@ -131,7 +131,7 @@ export class FluenceClient {
let _this = this;

return async (particle: Particle) => {
let data = particle.data;
let data: any = particle.data;
let error: any = data['protocol!error'];
if (error !== undefined) {
log.error('error in external particle: ');
Expand Down
7 changes: 4 additions & 3 deletions src/fluenceConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import pipe from 'it-pipe';
import Multiaddr from 'multiaddr';
import PeerId from 'peer-id';
import * as log from 'loglevel';
import { build, parseParticle, Particle, stringifyParticle } from './particle';
import { build, parseParticle, Particle, toAction } from './particle';

export const PROTOCOL_NAME = '/fluence/faas/1.0.0';

Expand Down Expand Up @@ -121,8 +121,9 @@ export class FluenceConnection {
async sendParticle(particle: Particle): Promise<void> {
this.checkConnectedOrThrow();

let particleStr = stringifyParticle(particle);
log.debug('send particle: \n' + JSON.stringify(particle, undefined, 2));
let action = toAction(particle)
let particleStr = JSON.stringify(action);
log.debug('send particle: \n' + JSON.stringify(action, undefined, 2));

// create outgoing substream
const conn = (await this.node.dialProtocol(this.address, PROTOCOL_NAME)) as {
Expand Down
41 changes: 30 additions & 11 deletions src/particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { v4 as uuidv4 } from 'uuid';
import { fromByteArray } from 'base64-js';
import PeerId from 'peer-id';
import { encode } from 'bs58';
import { addData } from './dataStorage';
Expand All @@ -29,7 +30,21 @@ export interface Particle {
script: string;
// sign upper fields
signature: string;
data: any;
data: Uint8Array;
}

/**
* Represents particle action to send to a node
*/
interface ParticleAction {
action: 'Particle'
id: string;
init_peer_id: string;
timestamp: number;
ttl: number;
script: string;
signature: number[];
data: string;
}

function wrapScript(selfPeerId: string, script: string, fields: string[]): string {
Expand Down Expand Up @@ -63,7 +78,7 @@ export async function build(peerId: PeerId, script: string, data: Map<string, an
ttl: ttl,
script: script,
signature: '',
data: [],
data: Buffer.from([]),
};

particle.signature = await signParticle(peerId, particle);
Expand All @@ -72,16 +87,20 @@ export async function build(peerId: PeerId, script: string, data: Map<string, an
}

/**
* Copies a particle and stringify it.
* Creates an action to send to a node.
*/
export function stringifyParticle(call: Particle): string {
let obj: any = { ...call };
obj.action = 'Particle';

// delete it after signatures will be implemented on nodes
obj.signature = [];

return JSON.stringify(obj);
export function toAction(particle: Particle): ParticleAction {
return {
action: "Particle",
id: particle.id,
init_peer_id: particle.init_peer_id,
timestamp: particle.timestamp,
ttl: particle.ttl,
script: particle.script,
// TODO: copy signature from a particle after signatures will be implemented on nodes
signature: [],
data: fromByteArray(particle.data)
};
}

export function parseParticle(str: string): Particle {
Expand Down
4 changes: 2 additions & 2 deletions src/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Instance = WebAssembly.Instance;
import Exports = WebAssembly.Exports;
import ExportValue = WebAssembly.ExportValue;

export type InterpreterInvoke = (init_user_id: string, script: string, prev_data: string, data: string) => string;
export type InterpreterInvoke = (init_user_id: string, script: string, prev_data: Uint8Array, data: Uint8Array) => string;
type ImportObject = {
'./aquamarine_client_bg.js': {
// fn call_service_impl(service_id: String, fn_name: String, args: String, security_tetraplets: String) -> String;
Expand Down Expand Up @@ -173,7 +173,7 @@ export async function instantiateInterpreter(peerId: PeerId): Promise<Interprete
});
let instance = await interpreterInstance(cfg);

return (init_user_id: string, script: string, prev_data: string, data: string) => {
return (init_user_id: string, script: string, prev_data: Uint8Array, data: Uint8Array) => {
let logLevel = log.getLevel();
let logLevelStr = 'info';
if (logLevel === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/stepperOutcome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

export interface StepperOutcome {
ret_code: number;
data: number[];
data: Uint8Array;
next_peer_pks: string[];
}