From a9ab67b6fe8d51a14886f6983cf6795715bd7694 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Sat, 11 Sep 2021 16:00:50 +0300 Subject: [PATCH] Add network communication for hello-world example --- .../hello-world/aqua/hello-world.aqua | 9 ++- js-sdk-examples/hello-world/package-lock.json | 13 ++++ js-sdk-examples/hello-world/package.json | 1 + .../hello-world/src/_aqua/hello-world.ts | 69 +++++++++++++++++++ js-sdk-examples/hello-world/src/index.ts | 13 +++- 5 files changed, 102 insertions(+), 3 deletions(-) diff --git a/js-sdk-examples/hello-world/aqua/hello-world.aqua b/js-sdk-examples/hello-world/aqua/hello-world.aqua index d15f603..7e5e5fe 100644 --- a/js-sdk-examples/hello-world/aqua/hello-world.aqua +++ b/js-sdk-examples/hello-world/aqua/hello-world.aqua @@ -1,5 +1,12 @@ +import Peer from "@fluencelabs/aqua-lib/builtin.aqua" + service HelloWorld("hello-world"): hello(str: string) func sayHello(): - HelloWorld.hello("Hello, world!") \ No newline at end of file + HelloWorld.hello("Hello, world!") + +func getRelayTime() -> u64: + on HOST_PEER_ID: + ts <- Peer.timestamp_ms() + <- ts diff --git a/js-sdk-examples/hello-world/package-lock.json b/js-sdk-examples/hello-world/package-lock.json index 081a835..6c2078c 100644 --- a/js-sdk-examples/hello-world/package-lock.json +++ b/js-sdk-examples/hello-world/package-lock.json @@ -14,6 +14,7 @@ }, "devDependencies": { "@fluencelabs/aqua": "^0.3.0-226", + "@fluencelabs/aqua-lib": "^0.1.14", "chokidar-cli": "^3.0.0", "ts-node": "^10.2.1", "typescript": "^4.4.2" @@ -82,6 +83,12 @@ "aqua-j": "index-java.js" } }, + "node_modules/@fluencelabs/aqua-lib": { + "version": "0.1.14", + "resolved": "https://registry.npmjs.org/@fluencelabs/aqua-lib/-/aqua-lib-0.1.14.tgz", + "integrity": "sha512-H2Q4gIvociUxc4J2mwmH0D+mrU2N2Z+enKCHgBCanMVEE2wZDsZ80GTbDKsQjEq+gpqbnJIk8lJBYW6lyvLJTg==", + "dev": true + }, "node_modules/@fluencelabs/avm": { "version": "0.14.4", "resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.14.4.tgz", @@ -3241,6 +3248,12 @@ "integrity": "sha512-9o0TdgsVNcBvifqo7VqIkN62P9EReE0LUxgWG1rGHi9yxJiGElvEBvrVUzqShffF66Ene7VGEe85lhFIGCfDgg==", "dev": true }, + "@fluencelabs/aqua-lib": { + "version": "0.1.14", + "resolved": "https://registry.npmjs.org/@fluencelabs/aqua-lib/-/aqua-lib-0.1.14.tgz", + "integrity": "sha512-H2Q4gIvociUxc4J2mwmH0D+mrU2N2Z+enKCHgBCanMVEE2wZDsZ80GTbDKsQjEq+gpqbnJIk8lJBYW6lyvLJTg==", + "dev": true + }, "@fluencelabs/avm": { "version": "0.14.4", "resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.14.4.tgz", diff --git a/js-sdk-examples/hello-world/package.json b/js-sdk-examples/hello-world/package.json index 6071ed4..4cdc32a 100644 --- a/js-sdk-examples/hello-world/package.json +++ b/js-sdk-examples/hello-world/package.json @@ -12,6 +12,7 @@ "license": "ISC", "devDependencies": { "@fluencelabs/aqua": "^0.3.0-226", + "@fluencelabs/aqua-lib": "^0.1.14", "chokidar-cli": "^3.0.0", "ts-node": "^10.2.1", "typescript": "^4.4.2" diff --git a/js-sdk-examples/hello-world/src/_aqua/hello-world.ts b/js-sdk-examples/hello-world/src/_aqua/hello-world.ts index 1bf7425..eedb85c 100644 --- a/js-sdk-examples/hello-world/src/_aqua/hello-world.ts +++ b/js-sdk-examples/hello-world/src/_aqua/hello-world.ts @@ -142,3 +142,72 @@ export function registerHelloWorld(peer: FluencePeer, serviceId: string, service return Promise.race([promise, Promise.resolve()]); } + + + export function getRelayTime(config?: {ttl?: number}) : Promise; + export function getRelayTime(peer: FluencePeer, config?: {ttl?: number}) : Promise; + export function getRelayTime(...args: any) { + let peer: FluencePeer; + + let config: any; + if (FluencePeer.isInstance(args[0])) { + peer = args[0]; + config = args[1]; + } else { + peer = Fluence.getPeer(); + config = args[0]; + } + + let request: RequestFlow; + const promise = new Promise((resolve, reject) => { + const r = new RequestFlowBuilder() + .disableInjections() + .withRawScript( + ` + (xor + (seq + (seq + (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-) + (xor + (call -relay- ("peer" "timestamp_ms") [] ts) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1]) + ) + ) + (xor + (call %init_peer_id% ("callbackSrv" "response") [ts]) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2]) + ) + ) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3]) +) + + `, + ) + .configHandler((h) => { + h.on('getDataSrv', '-relay-', () => { + return peer.getStatus().relayPeerId; + }); + + h.onEvent('callbackSrv', 'response', (args) => { + const [res] = args; + resolve(res); +}); + + h.onEvent('errorHandlingSrv', 'error', (args) => { + const [err] = args; + reject(err); + }); + }) + .handleScriptError(reject) + .handleTimeout(() => { + reject('Request timed out for getRelayTime'); + }) + if(config && config.ttl) { + r.withTTL(config.ttl) + } + request = r.build(); + }); + peer.internals.initiateFlow(request!); + return promise; +} + diff --git a/js-sdk-examples/hello-world/src/index.ts b/js-sdk-examples/hello-world/src/index.ts index 34ab820..db21cce 100644 --- a/js-sdk-examples/hello-world/src/index.ts +++ b/js-sdk-examples/hello-world/src/index.ts @@ -1,8 +1,13 @@ import { Fluence } from "@fluencelabs/fluence"; -import { registerHelloWorld, sayHello } from "./_aqua/hello-world"; +import { krasnodar } from "@fluencelabs/fluence-network-environment"; +import { + registerHelloWorld, + sayHello, + getRelayTime, +} from "./_aqua/hello-world"; async function main() { - await Fluence.start(); + await Fluence.start({ connectTo: krasnodar[0] }); registerHelloWorld({ hello: async (str) => { @@ -12,6 +17,10 @@ async function main() { await sayHello(); + const relayTime = await getRelayTime(); + + console.log("The relay time is: ", new Date(relayTime).toLocaleString()); + await Fluence.stop(); }