From 55db70d96f723c8ecb08a05236727e7188737b22 Mon Sep 17 00:00:00 2001 From: Dima Date: Fri, 12 Jun 2020 19:54:09 +0300 Subject: [PATCH] 'sender' field in FunctionCall (#908) --- package.json | 2 +- src/fluence_client.ts | 12 ++++++------ src/fluence_connection.ts | 14 +++++++------- src/function_call.ts | 28 +++++++++++++++++----------- src/test/address.spec.ts | 2 ++ 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 7b5776f4..99848b15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fluence", - "version": "0.5.4", + "version": "0.5.5", "description": "the browser js-libp2p client for the Fluence network", "main": "./dist/fluence.js", "typings": "./dist/fluence.d.ts", diff --git a/src/fluence_client.ts b/src/fluence_client.ts index 745b6c35..f504c839 100644 --- a/src/fluence_client.ts +++ b/src/fluence_client.ts @@ -41,8 +41,8 @@ export class FluenceClient { /** * Makes call with response from function. Without reply_to field. */ - private static responseCall(target: Address, args: any): FunctionCall { - return makeFunctionCall(genUUID(), target, args, undefined, "response"); + private responseCall(target: Address, args: any): FunctionCall { + return makeFunctionCall(genUUID(), target, this.connection.sender, args, undefined, "response"); } /** @@ -155,14 +155,14 @@ export class FluenceClient { // if the request hasn't been applied, there is no such service. Return an error. if (!applied) { console.log(`there is no service ${lastProtocol.value}`); - return FluenceClient.responseCall(call.reply_to, { + return this.responseCall(call.reply_to, { reason: `there is no such service`, msg: call }); } } catch (e) { // if service throw an error, return it to the sender - return FluenceClient.responseCall(call.reply_to, { + return this.responseCall(call.reply_to, { reason: `error on execution: ${e}`, msg: call }); @@ -174,7 +174,7 @@ export class FluenceClient { console.log(`relay call: ${call}`); } else { console.warn(`this relay call is not for me: ${callToString(call)}`); - return FluenceClient.responseCall(call.reply_to, { + return this.responseCall(call.reply_to, { reason: `this relay call is not for me`, msg: call }); @@ -185,7 +185,7 @@ export class FluenceClient { console.log(`peer call: ${call}`); } else { console.warn(`this peer call is not for me: ${callToString(call)}`); - return FluenceClient.responseCall(call.reply_to, { + return this.responseCall(call.reply_to, { reason: `this relay call is not for me`, msg: call }); diff --git a/src/fluence_connection.ts b/src/fluence_connection.ts index ea84e68d..4addf75d 100644 --- a/src/fluence_connection.ts +++ b/src/fluence_connection.ts @@ -47,7 +47,7 @@ enum Status { export class FluenceConnection { private readonly selfPeerInfo: PeerInfo; - readonly replyToAddress: Address; + readonly sender: Address; private node: LibP2p; private readonly address: Multiaddr; private readonly nodePeerId: PeerId; @@ -60,7 +60,7 @@ export class FluenceConnection { this.selfPeerId = selfPeerInfo.id.toB58String(); this.address = multiaddr; this.nodePeerId = hostPeerId; - this.replyToAddress = replyToAddress + this.sender = replyToAddress } async connect() { @@ -90,7 +90,7 @@ export class FluenceConnection { * Sends remote service_id call. */ async sendServiceCall(serviceId: string, args: any, name?: string) { - let regMsg = makeCall(serviceId, args, this.replyToAddress, name); + let regMsg = makeCall(serviceId, args, this.sender, this.sender, name); await this.sendCall(regMsg); } @@ -98,7 +98,7 @@ export class FluenceConnection { * Sends custom message to the peer. */ async sendPeerCall(peer: string, msg: any, name?: string) { - let regMsg = makePeerCall(PeerId.createFromB58String(peer), msg, this.replyToAddress, name); + let regMsg = makePeerCall(PeerId.createFromB58String(peer), msg, this.sender, this.sender, name); await this.sendCall(regMsg); } @@ -106,7 +106,7 @@ export class FluenceConnection { * Sends custom message to the peer through relay. */ async sendRelayCall(peer: string, relay: string, msg: any, name?: string) { - let regMsg = await makeRelayCall(PeerId.createFromB58String(peer), PeerId.createFromB58String(relay), msg, this.replyToAddress, name); + let regMsg = await makeRelayCall(PeerId.createFromB58String(peer), PeerId.createFromB58String(relay), msg, this.sender, this.sender, name); await this.sendCall(regMsg); } @@ -184,9 +184,9 @@ export class FluenceConnection { this.checkConnectedOrThrow(); let replyTo; - if (reply) replyTo = this.replyToAddress; + if (reply) replyTo = this.sender; - let call = makeFunctionCall(genUUID(), target, args, replyTo, name); + let call = makeFunctionCall(genUUID(), target, args, this.sender, replyTo, name); await this.sendCall(call); } diff --git a/src/function_call.ts b/src/function_call.ts index 2ceff534..6b5eeb4b 100644 --- a/src/function_call.ts +++ b/src/function_call.ts @@ -26,6 +26,7 @@ export interface FunctionCall { uuid: string, target: Address, reply_to?: Address, + sender: Address, arguments: any, name?: string, action: "FunctionCall" @@ -43,12 +44,13 @@ export function callToString(call: FunctionCall) { return JSON.stringify(obj) } -export function makeFunctionCall(uuid: string, target: Address, args: object, replyTo?: Address, name?: string): FunctionCall { +export function makeFunctionCall(uuid: string, target: Address, sender: Address, args: object, replyTo?: Address, name?: string): FunctionCall { return { uuid: uuid, target: target, reply_to: replyTo, + sender: sender, arguments: args, name: name, action: "FunctionCall" @@ -64,13 +66,16 @@ export function parseFunctionCall(str: string): FunctionCall { if (!json.uuid) throw Error(`there is no 'uuid' field in json.\n${str}`); if (!json.target) throw Error(`there is no 'uuid' field in json.\n${str}`); + if (!json.sender) throw Error(`there is no 'sender' field in json.\n${str}`); let target = parseAddress(json.target); + let sender = parseAddress(json.sender); return { uuid: json.uuid, target: target, reply_to: replyTo, + sender: sender, arguments: json.arguments, name: json.name, action: "FunctionCall" @@ -85,28 +90,28 @@ export function genUUID() { /** * Message to peer through relay */ -export async function makeRelayCall(client: PeerId, relay: PeerId, msg: any, replyTo?: Address, name?: string): Promise { +export async function makeRelayCall(client: PeerId, relay: PeerId, msg: any, sender: Address, replyTo?: Address, name?: string): Promise { let relayAddress = await createRelayAddress(relay.toB58String(), client, false); - return makeFunctionCall(genUUID(), relayAddress, msg, replyTo, name); + return makeFunctionCall(genUUID(), relayAddress, sender, msg, replyTo, name); } /** * Message to peer */ -export function makePeerCall(client: PeerId, msg: any, replyTo?: Address, name?: string): FunctionCall { +export function makePeerCall(client: PeerId, msg: any, sender: Address, replyTo?: Address, name?: string): FunctionCall { let peerAddress = createPeerAddress(client.toB58String()); - return makeFunctionCall(genUUID(), peerAddress, msg, replyTo, name); + return makeFunctionCall(genUUID(), peerAddress, msg, sender, replyTo, name); } /** * Message to call remote service_id */ -export function makeCall(functionId: string, args: any, replyTo?: Address, name?: string): FunctionCall { +export function makeCall(functionId: string, args: any, sender: Address, replyTo?: Address, name?: string): FunctionCall { let target = createServiceAddress(functionId); - return makeFunctionCall(genUUID(), target, args, replyTo, name); + return makeFunctionCall(genUUID(), target, args, sender, replyTo, name); } /** @@ -116,11 +121,12 @@ export async function makeRegisterMessage(serviceId: string, relayPeerId: PeerId let target = createServiceAddress("provide"); let replyTo = await createRelayAddress(relayPeerId.toB58String(), selfPeerId, true); - return makeFunctionCall(genUUID(), target, {service_id: serviceId}, replyTo, "provide service_id"); + return makeFunctionCall(genUUID(), target, replyTo, {service_id: serviceId}, replyTo, "provide service_id"); } -export function makeUnregisterMessage(serviceId: string, peerId: PeerId): FunctionCall { +// TODO uncomment when this will be implemented in Fluence network +/*export function makeUnregisterMessage(serviceId: string, peerId: PeerId): FunctionCall { let target = createPeerAddress(peerId.toB58String()); - return makeFunctionCall(genUUID(), target, {key: serviceId}, undefined, "unregister"); -} + return makeFunctionCall(genUUID(), target, target, {key: serviceId}, undefined, "unregister"); +}*/ diff --git a/src/test/address.spec.ts b/src/test/address.spec.ts index c793bf35..d13dde14 100644 --- a/src/test/address.spec.ts +++ b/src/test/address.spec.ts @@ -61,6 +61,7 @@ describe("Typescript usage suite", () => { let functionCall = makeFunctionCall( "123", addr2, + addr2, { arg1: "123", arg2: 3, @@ -79,6 +80,7 @@ describe("Typescript usage suite", () => { let functionCallWithOptional = makeFunctionCall( "123", addr, + addr, { arg1: "123", arg2: 3,