From cb8d46f4c4bed6623d151b535fb31d5b4e5884cb Mon Sep 17 00:00:00 2001 From: DieMyst Date: Fri, 29 Jan 2021 17:05:17 +0300 Subject: [PATCH 1/7] add-remove scripts --- src/internal/builtins.ts | 44 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/internal/builtins.ts b/src/internal/builtins.ts index 2a01f19d0..6a9eb0788 100644 --- a/src/internal/builtins.ts +++ b/src/internal/builtins.ts @@ -197,6 +197,7 @@ export const createService = async ( * Get all available blueprints hosted on a connected relay. @deprecated prefer using raw Particles instead * @param { FluenceClient } client - The Fluence Client instance. * @param {[string]} nodeId - Optional node peer id to get available blueprints from + * @param {[string]} nodeId - Optional node peer id to deploy service to * @param {[number]} ttl - Optional ttl for the particle which does the job * @returns { Array } - List of available blueprints */ @@ -246,6 +247,7 @@ export const addProvider = async ( /** * Get a provider from DHT network from neighborhood around a key. @deprecated prefer using raw Particles instead * @param { FluenceClient } client - The Fluence Client instance. + * @param {[buffer]} key - get provider by this key * @param {[string]} nodeId - Optional node peer id to get providers from * @param {[number]} ttl - Optional ttl for the particle which does the job * @returns { Array } - List of providers @@ -269,12 +271,48 @@ export const getProviders = async (client: FluenceClient, key: Buffer, nodeId?: * @param {[number]} ttl - Optional ttl for the particle which does the job * @returns { Array } - List of peer ids of neighbors of the node */ -export const neighborhood = async (client: FluenceClient, node: string, ttl?: number): Promise => { +export const neighborhood = async (client: FluenceClient, nodeId: string, ttl?: number): Promise => { let returnValue = 'neighborhood'; let call = (nodeId: string) => `(call "${nodeId}" ("dht" "neighborhood") [node] ${returnValue})`; let data = new Map(); - data.set('node', node); + data.set('node', nodeId); + + return requestResponse(client, 'neighborhood', call, returnValue, data, (args) => args[0] as string[], nodeId, ttl); +}; + +/** + * Upload an AIR script, that will be runned in a loop on a node. @deprecated prefer using raw Particles instead + * @param { FluenceClient } client - The Fluence Client instance. + * @param {[string]} script - script to upload + * @param {[string]} nodeId - Optional node peer id to get neighborhood from + * @param {[number]} ttl - Optional ttl for the particle which does the job + * @returns { Array } - List of peer ids of neighbors of the node + */ +export const addScript = async (client: FluenceClient, script: string, nodeId: string, ttl?: number): Promise => { + let returnValue = 'id'; + let call = (nodeId: string) => `(call "${nodeId}" ("script" "add") [script] ${returnValue})`; + + let data = new Map(); + data.set('script', script); + + return requestResponse(client, 'addScript', call, returnValue, data, (args) => args[0] as string[], nodeId, ttl); +}; + +/** + * Remove an AIR script from a node. @deprecated prefer using raw Particles instead + * @param { FluenceClient } client - The Fluence Client instance. + * @param {[string]} id - id of a script + * @param {[string]} nodeId - Optional node peer id to get neighborhood from + * @param {[number]} ttl - Optional ttl for the particle which does the job + * @returns { Array } - List of peer ids of neighbors of the node + */ +export const removeScript = async (client: FluenceClient, id: string, nodeId: string, ttl?: number): Promise => { + let returnValue = 'id'; + let call = (nodeId: string) => `(call "${nodeId}" ("script" "remove") [id] ${returnValue})`; + + let data = new Map(); + data.set('id', id); - return requestResponse(client, 'neighborhood', call, returnValue, data, (args) => args[0] as string[], node, ttl); + return requestResponse(client, 'removeScript', call, returnValue, data, (args) => args[0] as string[], nodeId, ttl); }; From 98883f44a001355761ec0dad74013b478106c0a9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 29 Jan 2021 18:54:54 +0300 Subject: [PATCH 2/7] Add logging for particle receive events (#13) * Add logging for particle receive events * Better log levels --- src/internal/FluenceClientImpl.ts | 15 ++++++++++----- src/internal/FluenceConnection.ts | 5 ++--- src/internal/ParticleProcessor.ts | 9 +++------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/internal/FluenceClientImpl.ts b/src/internal/FluenceClientImpl.ts index 9041e48a4..fce70de65 100644 --- a/src/internal/FluenceClientImpl.ts +++ b/src/internal/FluenceClientImpl.ts @@ -221,12 +221,17 @@ export class FluenceClientImpl extends FluenceClientBase implements FluenceClien executingParticle.reject(new Error(`particle ${particle.id} timed out`)); } }, - onLocalParticleRecieved: (particle: ParticleDto) => {}, - onExternalParticleRecieved: (particle: ParticleDto) => {}, - onStepperExecuting: (particle: ParticleDto) => {}, + onLocalParticleRecieved: (particle: ParticleDto) => { + log.debug('local particle received', particle); + }, + onExternalParticleRecieved: (particle: ParticleDto) => { + log.debug('external particle received', particle); + }, + onStepperExecuting: (particle: ParticleDto) => { + log.debug('stepper executing particle', particle); + }, onStepperExecuted: (stepperOutcome: StepperOutcome) => { - log.info('inner interpreter outcome:'); - log.info(stepperOutcome); + log.debug('inner interpreter outcome:', stepperOutcome); }, }; diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index 81dfa0c5f..fa1cf1153 100644 --- a/src/internal/FluenceConnection.ts +++ b/src/internal/FluenceConnection.ts @@ -81,7 +81,7 @@ export class FluenceConnection { if (this.status === Status.Initializing) { await this.node.start(); - log.debug(`dialing to the node with client's address: ` + this.node.peerId.toB58String()); + log.trace(`dialing to the node with client's address: ` + this.node.peerId.toB58String()); await this.node.dial(this.address); @@ -92,8 +92,7 @@ export class FluenceConnection { for await (const msg of source) { try { let particle = parseParticle(msg); - log.debug('Particle is received:'); - log.debug(JSON.stringify(particle, undefined, 2)); + log.trace('Particle is received:', JSON.stringify(particle, undefined, 2)); _this.handleParticle(particle); } catch (e) { log.error('error on handling a new incoming message: ' + e); diff --git a/src/internal/ParticleProcessor.ts b/src/internal/ParticleProcessor.ts index 1972ef3cc..b485b4745 100644 --- a/src/internal/ParticleProcessor.ts +++ b/src/internal/ParticleProcessor.ts @@ -26,10 +26,10 @@ let magicParticleStorage: Map> = new Map(); // HACK:: make an api for aqua stepper to accept variables in an easy way! export function injectDataIntoParticle(particleId: string, data: Map, ttl: number) { - log.debug(`setting data for ${particleId}`, data); + log.trace(`setting data for ${particleId}`, data); magicParticleStorage.set(particleId, data); setTimeout(() => { - log.debug(`data for ${particleId} is deleted`); + log.trace(`data for ${particleId} is deleted`); magicParticleStorage.delete(particleId); }, ttl); } @@ -215,11 +215,8 @@ export class ParticleProcessor { let data: any = particle.data; let error: any = data['protocol!error']; if (error !== undefined) { - log.error('error in external particle: '); - log.error(error); + log.error('error in external particle: ', error); } else { - log.info('handle external particle: '); - log.info(particle); await this.handleParticle(particle); } } From 90807c4d2600325ac47f50d17284c7dbdd6e85b0 Mon Sep 17 00:00:00 2001 From: DieMyst Date: Mon, 1 Feb 2021 18:08:54 +0300 Subject: [PATCH 3/7] add-remove scripts fixes, add test --- package-lock.json | 7 +++++- package.json | 1 + src/__test__/client.spec.ts | 48 ++++++++++++++++++++++++++++++------- src/internal/builtins.ts | 17 +++++++------ 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 183629cdd..a45567795 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fluencelabs/fluence", - "version": "0.9.2", + "version": "0.9.3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -29,6 +29,11 @@ "resolved": "https://registry.npmjs.org/@fluencelabs/aquamarine-stepper/-/aquamarine-stepper-0.3.4.tgz", "integrity": "sha512-0NPg9dWvANtc3If8C8O8XjKwzMCq2492lA73faKJdCwUv7m+xHX9G3l6UJULTfV1T4mWVtMQZIKopNOlRYSrKA==" }, + "@fluencelabs/fluence-network-environment": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@fluencelabs/fluence-network-environment/-/fluence-network-environment-1.0.8.tgz", + "integrity": "sha512-k1E48+r7tc5jWsnAeLrua7yRKN9lRISdJ1czkcOzUF6dZ9fI5+6ZfKKfUozJyC6n4hSjtCIcUV0G65vBvQfxBw==" + }, "@sinonjs/commons": { "version": "1.7.2", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.7.2.tgz", diff --git a/package.json b/package.json index 2d1193639..fee9beeb8 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "@fluencelabs/aquamarine-stepper": "0.3.4", "async": "3.2.0", + "@fluencelabs/fluence-network-environment": "1.0.8", "base64-js": "1.3.1", "bs58": "4.0.1", "cids": "0.8.1", diff --git a/src/__test__/client.spec.ts b/src/__test__/client.spec.ts index e1fe24332..dbf8618b2 100644 --- a/src/__test__/client.spec.ts +++ b/src/__test__/client.spec.ts @@ -7,11 +7,12 @@ import { TrustGraph } from '../internal/trust/trust_graph'; import { nodeRootCert } from '../internal/trust/misc'; import { generatePeerId, peerIdToSeed, seedToPeerId } from '../internal/peerIdUtils'; import { FluenceClientImpl } from '../internal/FluenceClientImpl'; -import { createConnectedClient, createLocalClient } from './util'; +import { createConnectedClient } from './util'; import log from 'loglevel'; import { createClient } from '../api'; import Multiaddr from 'multiaddr'; -import { getModules } from '../internal/builtins'; +import {addScript, getModules, removeScript} from '../internal/builtins'; +import {dev} from "@fluencelabs/fluence-network-environment"; const devNodeAddress = '/dns4/dev.fluence.dev/tcp/19001/wss/p2p/12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9'; const devNodePeerId = '12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9'; @@ -146,7 +147,7 @@ describe('Typescript usage suite', () => { it.skip('should make a call through the network', async function () { this.timeout(30000); // arrange - const client = await createConnectedClient(devNodeAddress); + const client = await createConnectedClient(dev[0].multiaddr); client.registerCallback('test', 'test', (args, _) => { log.trace('should make a call through the network, called "test" "test" with args', args); @@ -214,16 +215,45 @@ describe('Typescript usage suite', () => { expect(res).to.deep.equal(['some d', 'some c', 'some b', 'some a']); }); - it.skip('add_module', async function () { + it('get_modules', async function () { this.timeout(30000); // arrange - const client = await createConnectedClient( - '/dns4/dev.fluence.dev/tcp/19003/wss/p2p/12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb', - ); + console.log(dev[2].multiaddr); + const client = await createConnectedClient(dev[2].multiaddr); + + console.log("peerid: " + client.selfPeerId) + + let modulesList = await getModules(client); - let a = await getModules(client); + expect(modulesList).not.to.be.undefined; + }); + + it.skip('add and remove script', async function () { + this.timeout(30000); + // arrange + const client = await createConnectedClient(dev[2].multiaddr); + + console.log("peerid: " + client.selfPeerId) + + let script = ` + (seq + (call "${client.relayPeerId}" ("op" "identity") []) + (call "${client.selfPeerId}" ("test" "test1") ["1" "2" "3"] result) + ) + `; + + let scriptId = await addScript(client, script); + console.log(scriptId) + expect(scriptId).not.to.be.undefined; + + let resMakingPromise = new Promise((resolve) => { + client.registerCallback('test', 'test1', (args, _) => { + resolve([...args].reverse()); + return {}; + }); + }); - expect(a).not.to.be.undefined; + await removeScript(client, scriptId); }); it.skip('fetch should work', async function () { diff --git a/src/internal/builtins.ts b/src/internal/builtins.ts index 6a9eb0788..9be7dac66 100644 --- a/src/internal/builtins.ts +++ b/src/internal/builtins.ts @@ -287,16 +287,16 @@ export const neighborhood = async (client: FluenceClient, nodeId: string, ttl?: * @param {[string]} script - script to upload * @param {[string]} nodeId - Optional node peer id to get neighborhood from * @param {[number]} ttl - Optional ttl for the particle which does the job - * @returns { Array } - List of peer ids of neighbors of the node + * @returns {[string]} - script id */ -export const addScript = async (client: FluenceClient, script: string, nodeId: string, ttl?: number): Promise => { +export const addScript = async (client: FluenceClient, script: string, nodeId?: string, ttl?: number): Promise => { let returnValue = 'id'; let call = (nodeId: string) => `(call "${nodeId}" ("script" "add") [script] ${returnValue})`; let data = new Map(); data.set('script', script); - return requestResponse(client, 'addScript', call, returnValue, data, (args) => args[0] as string[], nodeId, ttl); + return requestResponse(client, 'addScript', call, returnValue, data, (args) => args[0] as string, nodeId, ttl); }; /** @@ -305,14 +305,13 @@ export const addScript = async (client: FluenceClient, script: string, nodeId: s * @param {[string]} id - id of a script * @param {[string]} nodeId - Optional node peer id to get neighborhood from * @param {[number]} ttl - Optional ttl for the particle which does the job - * @returns { Array } - List of peer ids of neighbors of the node */ -export const removeScript = async (client: FluenceClient, id: string, nodeId: string, ttl?: number): Promise => { - let returnValue = 'id'; - let call = (nodeId: string) => `(call "${nodeId}" ("script" "remove") [id] ${returnValue})`; +export const removeScript = async (client: FluenceClient, id: string, nodeId?: string, ttl?: number): Promise => { + let returnValue = 'empty'; + let call = (nodeId: string) => `(call "${nodeId}" ("script" "remove") [script_id] ${returnValue})`; let data = new Map(); - data.set('id', id); + data.set('script_id', id); - return requestResponse(client, 'removeScript', call, returnValue, data, (args) => args[0] as string[], nodeId, ttl); + return requestResponse(client, 'removeScript', call, returnValue, data, (args) => {}, nodeId, ttl); }; From 28d042b27545c299d9c0632cfc6710936e81bf0f Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 3 Feb 2021 00:04:26 +0300 Subject: [PATCH 4/7] update version --- package.json | 2 +- src/internal/builtins.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f529dec4c..fdccb6772 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fluencelabs/fluence", - "version": "0.9.9", + "version": "0.9.11", "description": "JS SDK for the Fluence network", "main": "./dist/index.js", "typings": "./dist/index.d.ts", diff --git a/src/internal/builtins.ts b/src/internal/builtins.ts index 9be7dac66..cd58b3c32 100644 --- a/src/internal/builtins.ts +++ b/src/internal/builtins.ts @@ -94,7 +94,7 @@ export const uploadModule = async ( name: string, moduleBase64: string, config?: ModuleConfig, -): Promise => { +): Promise => { if (!config) { config = { name: name, From 2255dbed6b04f0bd7d301c307e316727b7e4cb64 Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 3 Feb 2021 19:52:53 +0300 Subject: [PATCH 5/7] fix builtins, add tests --- package.json | 2 +- src/__test__/client.spec.ts | 93 +++++++++++++++++++++++++++++++------ src/internal/builtins.ts | 14 ++++-- 3 files changed, 89 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index fdccb6772..1a74a8124 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fluencelabs/fluence", - "version": "0.9.11", + "version": "0.9.13", "description": "JS SDK for the Fluence network", "main": "./dist/index.js", "typings": "./dist/index.d.ts", diff --git a/src/__test__/client.spec.ts b/src/__test__/client.spec.ts index dbf8618b2..9c4d1da12 100644 --- a/src/__test__/client.spec.ts +++ b/src/__test__/client.spec.ts @@ -11,7 +11,15 @@ import { createConnectedClient } from './util'; import log from 'loglevel'; import { createClient } from '../api'; import Multiaddr from 'multiaddr'; -import {addScript, getModules, removeScript} from '../internal/builtins'; +import { + addBlueprint, addProvider, + addScript, + createService, + getBlueprints, + getModules, getProviders, + removeScript, + uploadModule +} from '../internal/builtins'; import {dev} from "@fluencelabs/fluence-network-environment"; const devNodeAddress = '/dns4/dev.fluence.dev/tcp/19001/wss/p2p/12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9'; @@ -215,24 +223,76 @@ describe('Typescript usage suite', () => { expect(res).to.deep.equal(['some d', 'some c', 'some b', 'some a']); }); - it('get_modules', async function () { + it.skip('get_modules', async function () { this.timeout(30000); - // arrange - console.log(dev[2].multiaddr); const client = await createConnectedClient(dev[2].multiaddr); - console.log("peerid: " + client.selfPeerId) - let modulesList = await getModules(client); expect(modulesList).not.to.be.undefined; }); - it.skip('add and remove script', async function () { + it.skip('get_blueprints', async function () { + this.timeout(30000); + const client = await createConnectedClient(dev[2].multiaddr); + + let bpList = await getBlueprints(client); + + expect(bpList).not.to.be.undefined; + }); + + it.skip("upload_modules", async function () { + this.timeout(30000); + const client = await createConnectedClient(dev[2].multiaddr); + + console.log("peerid: " + client.selfPeerId) + + let base64 = "MjNy" + + await uploadModule(client, "test_broken_module", base64); + }); + + it.skip("add_blueprint", async function () { + this.timeout(30000); + const client = await createConnectedClient(dev[2].multiaddr); + + let bpId = "some" + + let bpIdReturned = await addBlueprint(client, "test_broken_blueprint", ["test_broken_module"], bpId); + + expect(bpIdReturned).to.be.equal(bpId); + }); + + it.skip("create_service", async function () { + this.timeout(30000); + const client = await createConnectedClient(dev[2].multiaddr); + + let serviceId = await createService(client, "test_broken_blueprint"); + + // TODO there is no error on broken blueprint from a node + expect(serviceId).not.to.be.undefined; + }); + + it.skip("add_provider", async function () { this.timeout(30000); - // arrange const client = await createConnectedClient(dev[2].multiaddr); + let key = Math.random().toString(36).substring(7); + let buf = Buffer.from(key) + + let r = Math.random().toString(36).substring(7); + await addProvider(client, buf, dev[2].peerId, r); + + let pr = await getProviders(client, buf); + console.log(pr) + console.log(r) + expect(r).to.be.equal(pr[0][0].service_id); + }); + + it.skip('add and remove script', async function () { + this.timeout(30000); + const client = await createConnectedClient(dev[3].multiaddr); + console.log("peerid: " + client.selfPeerId) let script = ` @@ -242,18 +302,23 @@ describe('Typescript usage suite', () => { ) `; - let scriptId = await addScript(client, script); - console.log(scriptId) - expect(scriptId).not.to.be.undefined; - let resMakingPromise = new Promise((resolve) => { client.registerCallback('test', 'test1', (args, _) => { - resolve([...args].reverse()); + resolve([...args]); return {}; }); }); - await removeScript(client, scriptId); + let scriptId = await addScript(client, script); + + await resMakingPromise.then((args) => { + console.log("final!") + expect(args as string[]).to.be.deep.equal(["1", "2", "3"]); + }).finally(() => { + removeScript(client, scriptId); + }) + + expect(scriptId).not.to.be.undefined; }); it.skip('fetch should work', async function () { diff --git a/src/internal/builtins.ts b/src/internal/builtins.ts index cd58b3c32..5f211ca1a 100644 --- a/src/internal/builtins.ts +++ b/src/internal/builtins.ts @@ -56,7 +56,7 @@ const requestResponse = async ( ) `; - const res = await sendParticleAsFetch(client, new Particle(script, data, ttl), ''); + const res = await sendParticleAsFetch(client, new Particle(script, data, ttl), name); return handleResponse(res); }; @@ -122,7 +122,7 @@ export const uploadModule = async ( ) `; - return sendParticleAsFetch(client, new Particle(script, data), 'result'); + return sendParticleAsFetch(client, new Particle(script, data), 'getModules', "_callback"); }; /** @@ -201,7 +201,7 @@ export const createService = async ( * @param {[number]} ttl - Optional ttl for the particle which does the job * @returns { Array } - List of available blueprints */ -export const getBlueprints = async (client: FluenceClient, nodeId: string, ttl?: number): Promise => { +export const getBlueprints = async (client: FluenceClient, nodeId?: string, ttl?: number): Promise => { let returnValue = 'blueprints'; let call = (nodeId: string) => `(call "${nodeId}" ("dist" "get_blueprints") [] ${returnValue})`; @@ -285,16 +285,20 @@ export const neighborhood = async (client: FluenceClient, nodeId: string, ttl?: * Upload an AIR script, that will be runned in a loop on a node. @deprecated prefer using raw Particles instead * @param { FluenceClient } client - The Fluence Client instance. * @param {[string]} script - script to upload + * @param period how often start script processing, in seconds * @param {[string]} nodeId - Optional node peer id to get neighborhood from * @param {[number]} ttl - Optional ttl for the particle which does the job * @returns {[string]} - script id */ -export const addScript = async (client: FluenceClient, script: string, nodeId?: string, ttl?: number): Promise => { +export const addScript = async (client: FluenceClient, script: string, period?: number, nodeId?: string, ttl?: number): Promise => { let returnValue = 'id'; - let call = (nodeId: string) => `(call "${nodeId}" ("script" "add") [script] ${returnValue})`; + let periodV = "" + if (period) periodV = period.toString() + let call = (nodeId: string) => `(call "${nodeId}" ("script" "add") [script ${periodV}] ${returnValue})`; let data = new Map(); data.set('script', script); + if (period) data.set('period', period) return requestResponse(client, 'addScript', call, returnValue, data, (args) => args[0] as string, nodeId, ttl); }; From f249024ce6b6281696db938e0e1608d8d48f3af0 Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 3 Feb 2021 19:55:00 +0300 Subject: [PATCH 6/7] nodeId optional --- src/internal/builtins.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/internal/builtins.ts b/src/internal/builtins.ts index 5f211ca1a..d03d1f641 100644 --- a/src/internal/builtins.ts +++ b/src/internal/builtins.ts @@ -271,12 +271,12 @@ export const getProviders = async (client: FluenceClient, key: Buffer, nodeId?: * @param {[number]} ttl - Optional ttl for the particle which does the job * @returns { Array } - List of peer ids of neighbors of the node */ -export const neighborhood = async (client: FluenceClient, nodeId: string, ttl?: number): Promise => { +export const neighborhood = async (client: FluenceClient, nodeId?: string, ttl?: number): Promise => { let returnValue = 'neighborhood'; let call = (nodeId: string) => `(call "${nodeId}" ("dht" "neighborhood") [node] ${returnValue})`; let data = new Map(); - data.set('node', nodeId); + if (nodeId) data.set('node', nodeId); return requestResponse(client, 'neighborhood', call, returnValue, data, (args) => args[0] as string[], nodeId, ttl); }; From e43926017dcf23aa72c2c9e2efc2abbf8eab3348 Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 3 Feb 2021 20:21:55 +0300 Subject: [PATCH 7/7] get_interfaces --- src/__test__/client.spec.ts | 11 ++++++++++- src/internal/builtins.ts | 28 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/__test__/client.spec.ts b/src/__test__/client.spec.ts index 9c4d1da12..a83cf48df 100644 --- a/src/__test__/client.spec.ts +++ b/src/__test__/client.spec.ts @@ -15,7 +15,7 @@ import { addBlueprint, addProvider, addScript, createService, - getBlueprints, + getBlueprints, getInterfaces, getModules, getProviders, removeScript, uploadModule @@ -232,6 +232,15 @@ describe('Typescript usage suite', () => { expect(modulesList).not.to.be.undefined; }); + it.skip('get_interfaces', async function () { + this.timeout(30000); + const client = await createConnectedClient(dev[2].multiaddr); + + let interfaces = await getInterfaces(client); + + expect(interfaces).not.to.be.undefined; + }); + it.skip('get_blueprints', async function () { this.timeout(30000); const client = await createConnectedClient(dev[2].multiaddr); diff --git a/src/internal/builtins.ts b/src/internal/builtins.ts index d03d1f641..d872b5ea2 100644 --- a/src/internal/builtins.ts +++ b/src/internal/builtins.ts @@ -66,11 +66,12 @@ const requestResponse = async ( * @returns { Array } - list of available modules on the connected relay */ export const getModules = async (client: FluenceClient): Promise => { + let callbackFn = "getModules" const particle = new Particle( ` (seq (call __relay ("dist" "get_modules") [] result) - (call myPeerId ("_callback" "getModules") [result]) + (call myPeerId ("_callback" "${callbackFn}") [result]) ) `, { @@ -79,7 +80,30 @@ export const getModules = async (client: FluenceClient): Promise => { }, ); - return sendParticleAsFetch(client, particle, 'getModules'); + return sendParticleAsFetch(client, particle, callbackFn); +}; + +/** + * Get all available modules hosted on a connected relay. @deprecated prefer using raw Particles instead + * @param { FluenceClient } client - The Fluence Client instance. + * @returns { Array } - list of available modules on the connected relay + */ +export const getInterfaces = async (client: FluenceClient): Promise => { + let callbackFn = "getInterfaces" + const particle = new Particle( + ` + (seq + (call __relay ("srv" "get_interfaces") [] result) + (call myPeerId ("_callback" "${callbackFn}") [result]) + ) + `, + { + __relay: client.relayPeerId, + myPeerId: client.selfPeerId, + }, + ); + + return sendParticleAsFetch(client, particle, callbackFn); }; /**