From 0fe64f62f3cb970295fffdf4bd98a836f47b7fe7 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 22 Dec 2022 18:16:44 +0400 Subject: [PATCH 1/5] Add a more convenient API for calling aqua functions programmatically --- .../compilerSupport/v3impl/callFunction.ts | 62 +++++++++++++------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts index 6b9e3f209..c731b7aa4 100644 --- a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts +++ b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts @@ -1,10 +1,8 @@ import { FnConfig, FunctionCallDef } from './interface'; import { FluencePeer } from '../../FluencePeer'; import { Fluence } from '../../../index'; -import { Particle } from '../../Particle'; import { injectRelayService, - argToServiceDef, registerParticleScopeService, responseService, errorHandlingService, @@ -21,18 +19,31 @@ import { * @param def - function definition generated by the Aqua compiler * @param script - air script with function execution logic generated by the Aqua compiler */ -export function callFunction(rawFnArgs: Array, def: FunctionCallDef, script: string) { - if (def.arrow.domain.tag !== 'labeledProduct') { - throw new Error('Should be impossible'); - } +export function callFunction(rawFnArgs: Array, def: FunctionCallDef, script: string): Promise { + const { args, peer, config } = extractArgs(rawFnArgs, def); - const argumentTypes = Object.entries(def.arrow.domain.fields); - const expectedNumberOfArguments = argumentTypes.length; - const { args, peer, config } = extractArgs(rawFnArgs, expectedNumberOfArguments); + return callFunctionImpl(def, script, config || {}, peer, args); +} - if (args.length !== expectedNumberOfArguments) { - throw new Error('Incorrect number of arguments. Expecting ${def.argDefs.length}'); - } +/** + * Convenience function which does all the internal work of creating particles + * and making necessary service registrations in order to support Aqua function calls + * + * @param def - function definition generated by the Aqua compiler + * @param script - air script with function execution logic generated by the Aqua compiler + * @param config - options to configure Aqua function execution + * @param peer - Fluence Peer to invoke the function at + * @param args - args in the form of JSON where each key corresponds to the name of the argument + * @returns + */ +export function callFunctionImpl( + def: FunctionCallDef, + script: string, + config: FnConfig, + peer: FluencePeer, + args: { [key: string]: any }, +): Promise { + const argumentTypes = getArgumentTypes(def); const promise = new Promise((resolve, reject) => { const particle = peer.internals.createNewParticle(script, config?.ttl); @@ -41,13 +52,13 @@ export function callFunction(rawFnArgs: Array, def: FunctionCallDef, script return reject(particle.message); } - for (let i = 0; i < expectedNumberOfArguments; i++) { - const [name, type] = argumentTypes[i]; + for (let [name, argVal] of Object.entries(args)) { + const type = argumentTypes[name]; let service: ServiceDescription; if (type.tag === 'arrow') { - service = userHandlerService(def.names.callbackSrv, [name, type], args[i]); + service = userHandlerService(def.names.callbackSrv, [name, type], argVal); } else { - service = injectValueService(def.names.getDataSrv, name, type, args[i]); + service = injectValueService(def.names.getDataSrv, name, type, argVal); } registerParticleScopeService(peer, particle, service); } @@ -105,15 +116,18 @@ const isReturnTypeVoid = (def: FunctionCallDef) => { */ const extractArgs = ( args: any[], - numberOfExpectedArgs: number, + def: FunctionCallDef, ): { peer: FluencePeer; config?: FnConfig; args: any[]; } => { + const argumentTypes = getArgumentTypes(def); + const numberOfExpectedArgs = Object.entries(argumentTypes).length; + let peer: FluencePeer; let structuredArgs: any[]; - let config: any; + let config: FnConfig; if (FluencePeer.isInstance(args[0])) { peer = args[0]; structuredArgs = args.slice(1, numberOfExpectedArgs + 1); @@ -124,9 +138,21 @@ const extractArgs = ( config = args[numberOfExpectedArgs]; } + if (args.length !== numberOfExpectedArgs) { + throw new Error(`Incorrect number of arguments. Expecting ${numberOfExpectedArgs}`); + } + return { peer: peer, config: config, args: structuredArgs, }; }; + +const getArgumentTypes = (def: FunctionCallDef) => { + if (def.arrow.domain.tag !== 'labeledProduct') { + throw new Error('Should be impossible'); + } + + return def.arrow.domain.fields; +}; From 10627aceb5fea4af69f4c7058d50a84a9eeae9d4 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 22 Dec 2022 18:17:57 +0400 Subject: [PATCH 2/5] Bump version --- packages/fluence-js/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fluence-js/package.json b/packages/fluence-js/package.json index cdcbc10c5..98b28d438 100644 --- a/packages/fluence-js/package.json +++ b/packages/fluence-js/package.json @@ -1,6 +1,6 @@ { "name": "@fluencelabs/fluence", - "version": "0.27.4", + "version": "0.27.5", "description": "TypeScript implementation of Fluence Peer", "main": "./dist/index.js", "typings": "./dist/index.d.ts", From 60b6e3a0ec070e42c2fc4c5ddd9ddff2784c044d Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 22 Dec 2022 18:59:13 +0400 Subject: [PATCH 3/5] fix tests --- .../internal/compilerSupport/v3impl/callFunction.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts index c731b7aa4..c2c3850c0 100644 --- a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts +++ b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts @@ -120,7 +120,7 @@ const extractArgs = ( ): { peer: FluencePeer; config?: FnConfig; - args: any[]; + args: { [key: string]: any }; } => { const argumentTypes = getArgumentTypes(def); const numberOfExpectedArgs = Object.entries(argumentTypes).length; @@ -138,14 +138,18 @@ const extractArgs = ( config = args[numberOfExpectedArgs]; } - if (args.length !== numberOfExpectedArgs) { + if (structuredArgs.length !== numberOfExpectedArgs) { throw new Error(`Incorrect number of arguments. Expecting ${numberOfExpectedArgs}`); } + const argsRes = Object.entries(argumentTypes) + .map(([name, _], ix) => ({ [name]: structuredArgs[ix] })) + .reduce((acc, kp) => ({ ...acc, ...kp }), {}); + return { peer: peer, config: config, - args: structuredArgs, + args: argsRes, }; }; From ec58725d44040500adcb9cb0b11846fb9434b16c Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 22 Dec 2022 20:41:58 +0400 Subject: [PATCH 4/5] fix PR comments --- .../src/internal/compilerSupport/v3impl/callFunction.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts index c2c3850c0..007cdf5e3 100644 --- a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts +++ b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts @@ -123,7 +123,8 @@ const extractArgs = ( args: { [key: string]: any }; } => { const argumentTypes = getArgumentTypes(def); - const numberOfExpectedArgs = Object.entries(argumentTypes).length; + const argumentNames = Object.keys(argumentTypes); + const numberOfExpectedArgs = argumentNames.length; let peer: FluencePeer; let structuredArgs: any[]; @@ -142,9 +143,7 @@ const extractArgs = ( throw new Error(`Incorrect number of arguments. Expecting ${numberOfExpectedArgs}`); } - const argsRes = Object.entries(argumentTypes) - .map(([name, _], ix) => ({ [name]: structuredArgs[ix] })) - .reduce((acc, kp) => ({ ...acc, ...kp }), {}); + const argsRes = argumentNames.reduce((acc, name, index) => ({ ...acc, [name]: index }), {}); return { peer: peer, From ac9f9e31ec51ed55aabdd9bdc20506d4fca4be35 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 22 Dec 2022 21:01:52 +0400 Subject: [PATCH 5/5] fix tests --- .../src/internal/compilerSupport/v3impl/callFunction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts index 007cdf5e3..95c117e64 100644 --- a/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts +++ b/packages/fluence-js/src/internal/compilerSupport/v3impl/callFunction.ts @@ -143,7 +143,7 @@ const extractArgs = ( throw new Error(`Incorrect number of arguments. Expecting ${numberOfExpectedArgs}`); } - const argsRes = argumentNames.reduce((acc, name, index) => ({ ...acc, [name]: index }), {}); + const argsRes = argumentNames.reduce((acc, name, index) => ({ ...acc, [name]: structuredArgs[index] }), {}); return { peer: peer,