This commit is contained in:
Alexey Proshutinskiy 2022-03-24 17:58:41 +04:00
commit 848e91c0d7
8 changed files with 8159 additions and 0 deletions

7495
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

25
package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"compile-aqua": "aqua -i ./src/aqua -o ./src/generated",
"build": "npm run compile-aqua && tsc",
"prestart": "npm run build",
"start": "node dist/index.js"
},
"keywords": [],
"author": "Fluence Labs",
"license": "MIT",
"dependencies": {
"@fluencelabs/registry": "0.3.3",
"@fluencelabs/aqua-lib": "^0.4.3",
"@fluencelabs/aqua": "^0.7.0-285",
"@fluencelabs/fluence": "0.21.6",
"@fluencelabs/fluence-network-environment": "^1.0.13"
},
"devDependencies": {
"typescript": "^4.4.3"
}
}

9
src/aqua/export.aqua Normal file
View File

@ -0,0 +1,9 @@
module Export
import createRouteAndRegister, resolveRoute from "@fluencelabs/registry/routing.aqua"
import Messaging from "./service.aqua"
export createRouteAndRegister, resolveRoute, Messaging, send_message
func send_message(relay: string, peer_id: string, service_id: string, message: string):
on peer_id via HOST_PEER_ID via relay:
Messaging service_id
Messaging.receive(message)

4
src/aqua/service.aqua Normal file
View File

@ -0,0 +1,4 @@
module Service declares Messaging
service Messaging:
receive(msg: string)

15
src/client.ts Normal file
View File

@ -0,0 +1,15 @@
import { resolveRoute, send_message} from "./generated/export"
import { krasnodar } from "@fluencelabs/fluence-network-environment"
import {Fluence} from "@fluencelabs/fluence"
async function main(route_id: string) {
await Fluence.start({connectTo: krasnodar[0]});
console.log(route_id);
let providers = await resolveRoute(route_id, 3);
console.log("Providers:", providers);
let provider = providers[0];
await send_message(provider.relay_id[0], provider.peer_id, provider.service_id[0], "hello world");
}
let route_id = process.argv[2];
main(route_id).then(() => process.exit(0)).catch((err) => console.error(err));

511
src/generated/export.ts Normal file
View File

@ -0,0 +1,511 @@
/**
*
* This file is auto-generated. Do not edit manually: changes may be erased.
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
* Aqua version: 0.7.0-286
*
*/
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
import {
CallParams,
callFunction,
registerService,
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v3';
// Services
export interface MessagingDef {
receive: (msg: string, callParams: CallParams<'msg'>) => void | Promise<void>;
}
export function registerMessaging(serviceId: string, service: MessagingDef): void;
export function registerMessaging(peer: FluencePeer, serviceId: string, service: MessagingDef): void;
export function registerMessaging(...args: any) {
registerService(
args,
{
"functions" : {
"tag" : "labeledProduct",
"fields" : {
"receive" : {
"tag" : "arrow",
"domain" : {
"tag" : "labeledProduct",
"fields" : {
"msg" : {
"tag" : "scalar",
"name" : "string"
}
}
},
"codomain" : {
"tag" : "nil"
}
}
}
}
}
);
}
// Functions
export function resolveRoute(
route_id: string,
ack: number,
config?: {ttl?: number}
): Promise<{ peer_id: string; relay_id: string[]; route_id: string; service_id: string[]; set_by: string; signature: number[]; solution: number[]; timestamp_created: number; value: string; }[]>;
export function resolveRoute(
peer: FluencePeer,
route_id: string,
ack: number,
config?: {ttl?: number}
): Promise<{ peer_id: string; relay_id: string[]; route_id: string; service_id: string[]; set_by: string; signature: number[]; solution: number[]; timestamp_created: number; value: string; }[]>;
export function resolveRoute(...args: any) {
let script = `
(xor
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "route_id") [] route_id)
)
(call %init_peer_id% ("getDataSrv" "ack") [] ack)
)
(new $res
(xor
(seq
(seq
(seq
(seq
(call -relay- ("op" "string_to_b58") [route_id] k)
(call -relay- ("kad" "neighborhood") [k [] []] nodes)
)
(par
(fold nodes n
(par
(seq
(xor
(xor
(seq
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("registry" "get_records") [route_id t] get_result)
)
(ap get_result.$.result! $res)
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
(call -relay- ("op" "noop") [])
)
(next n)
)
)
(null)
)
)
(call -relay- ("op" "noop") [$res.$.[ack]!])
)
(call -relay- ("registry" "merge") [$res] result)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [result.$.result!])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
)
`
return callFunction(
args,
{
"functionName" : "resolveRoute",
"arrow" : {
"tag" : "arrow",
"domain" : {
"tag" : "labeledProduct",
"fields" : {
"route_id" : {
"tag" : "scalar",
"name" : "string"
},
"ack" : {
"tag" : "scalar",
"name" : "i16"
}
}
},
"codomain" : {
"tag" : "unlabeledProduct",
"items" : [
{
"tag" : "array",
"type" : {
"tag" : "struct",
"name" : "Record",
"fields" : {
"relay_id" : {
"tag" : "array",
"type" : {
"tag" : "scalar",
"name" : "string"
}
},
"signature" : {
"tag" : "array",
"type" : {
"tag" : "scalar",
"name" : "u8"
}
},
"solution" : {
"tag" : "array",
"type" : {
"tag" : "scalar",
"name" : "u8"
}
},
"route_id" : {
"tag" : "scalar",
"name" : "string"
},
"set_by" : {
"tag" : "scalar",
"name" : "string"
},
"peer_id" : {
"tag" : "scalar",
"name" : "string"
},
"service_id" : {
"tag" : "array",
"type" : {
"tag" : "scalar",
"name" : "string"
}
},
"value" : {
"tag" : "scalar",
"name" : "string"
},
"timestamp_created" : {
"tag" : "scalar",
"name" : "u64"
}
}
}
}
]
}
},
"names" : {
"relay" : "-relay-",
"getDataSrv" : "getDataSrv",
"callbackSrv" : "callbackSrv",
"responseSrv" : "callbackSrv",
"responseFnName" : "response",
"errorHandlingSrv" : "errorHandlingSrv",
"errorFnName" : "error"
}
},
script
)
}
export function createRouteAndRegister(
label: string,
value: string,
relay_id: string | null,
service_id: string | null,
config?: {ttl?: number}
): Promise<string>;
export function createRouteAndRegister(
peer: FluencePeer,
label: string,
value: string,
relay_id: string | null,
service_id: string | null,
config?: {ttl?: number}
): Promise<string>;
export function createRouteAndRegister(...args: any) {
let script = `
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "label") [] label)
)
(call %init_peer_id% ("getDataSrv" "value") [] value)
)
(call %init_peer_id% ("getDataSrv" "relay_id") [] relay_id)
)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
)
(call %init_peer_id% ("peer" "timestamp_sec") [] t)
)
(xor
(call -relay- ("registry" "get_route_bytes") [label [] t [] ""] bytes)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
(call %init_peer_id% ("sig" "sign") [bytes] signature)
)
(xor
(call -relay- ("registry" "get_route_id") [label %init_peer_id%] route_id)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
(xor
(call -relay- ("registry" "get_record_bytes") [route_id value relay_id service_id t []] bytes-0)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
)
(call %init_peer_id% ("sig" "sign") [bytes-0] signature-0)
)
(xor
(seq
(seq
(call -relay- ("op" "string_to_b58") [route_id] k)
(call -relay- ("kad" "neighborhood") [k [] []] nodes)
)
(par
(fold nodes n
(par
(xor
(xor
(seq
(seq
(seq
(seq
(seq
(call n ("peer" "timestamp_sec") [] t-0)
(call n ("trust-graph" "get_weight") [%init_peer_id% t-0] weight)
)
(call n ("registry" "register_route") [label [] t [] "" signature.$.signature.[0]! false weight t-0] result)
)
(call n ("peer" "timestamp_sec") [] t-1)
)
(call n ("trust-graph" "get_weight") [%init_peer_id% t-1] weight-0)
)
(call n ("registry" "put_record") [route_id value relay_id service_id t [] signature-0.$.signature.[0]! weight-0 t-1] result-0)
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
)
)
(next n)
)
)
(null)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
)
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [route_id])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 6])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 7])
)
`
return callFunction(
args,
{
"functionName" : "createRouteAndRegister",
"arrow" : {
"tag" : "arrow",
"domain" : {
"tag" : "labeledProduct",
"fields" : {
"label" : {
"tag" : "scalar",
"name" : "string"
},
"value" : {
"tag" : "scalar",
"name" : "string"
},
"relay_id" : {
"tag" : "option",
"type" : {
"tag" : "scalar",
"name" : "string"
}
},
"service_id" : {
"tag" : "option",
"type" : {
"tag" : "scalar",
"name" : "string"
}
}
}
},
"codomain" : {
"tag" : "unlabeledProduct",
"items" : [
{
"tag" : "scalar",
"name" : "string"
}
]
}
},
"names" : {
"relay" : "-relay-",
"getDataSrv" : "getDataSrv",
"callbackSrv" : "callbackSrv",
"responseSrv" : "callbackSrv",
"responseFnName" : "response",
"errorHandlingSrv" : "errorHandlingSrv",
"errorFnName" : "error"
}
},
script
)
}
export function send_message(
relay: string,
peer_id: string,
service_id: string,
message: string,
config?: {ttl?: number}
): Promise<void>;
export function send_message(
peer: FluencePeer,
relay: string,
peer_id: string,
service_id: string,
message: string,
config?: {ttl?: number}
): Promise<void>;
export function send_message(...args: any) {
let script = `
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "relay") [] relay)
)
(call %init_peer_id% ("getDataSrv" "peer_id") [] peer_id)
)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
)
(call %init_peer_id% ("getDataSrv" "message") [] message)
)
(call -relay- ("op" "noop") [])
)
(call relay ("op" "noop") [])
)
(xor
(seq
(seq
(call peer_id (service_id "receive") [message])
(call relay ("op" "noop") [])
)
(call -relay- ("op" "noop") [])
)
(seq
(seq
(call relay ("op" "noop") [])
(call -relay- ("op" "noop") [])
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`
return callFunction(
args,
{
"functionName" : "send_message",
"arrow" : {
"tag" : "arrow",
"domain" : {
"tag" : "labeledProduct",
"fields" : {
"relay" : {
"tag" : "scalar",
"name" : "string"
},
"peer_id" : {
"tag" : "scalar",
"name" : "string"
},
"service_id" : {
"tag" : "scalar",
"name" : "string"
},
"message" : {
"tag" : "scalar",
"name" : "string"
}
}
},
"codomain" : {
"tag" : "nil"
}
},
"names" : {
"relay" : "-relay-",
"getDataSrv" : "getDataSrv",
"callbackSrv" : "callbackSrv",
"responseSrv" : "callbackSrv",
"responseFnName" : "response",
"errorHandlingSrv" : "errorHandlingSrv",
"errorFnName" : "error"
}
},
script
)
}

31
src/index.ts Normal file
View File

@ -0,0 +1,31 @@
import {createRouteAndRegister, registerMessaging, resolveRoute, send_message} from "./generated/export"
import { krasnodar } from "@fluencelabs/fluence-network-environment"
import {Fluence} from "@fluencelabs/fluence"
async function main() {
await Fluence.start({connectTo: krasnodar[0]});
console.log('our peer id', Fluence.getStatus().peerId);
let relay_id = Fluence.getStatus().relayPeerId;
console.log('connected to', Fluence.getStatus().relayPeerId);
let label = "messaging_label";
let value = "so important";
let service_id = "my_messaging";
await registerMessaging(service_id, {
receive: (msg) => {
console.log("Message received:", msg);
}
})
let route_id = await createRouteAndRegister(label, value, relay_id, service_id);
console.log("Route id:", route_id);
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', async () => {
await Fluence.stop();
process.exit(0);
});
}
main().catch((err) => console.error(err));

69
tsconfig.json Normal file
View File

@ -0,0 +1,69 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}