mirror of
https://github.com/fluencelabs/registry.git
synced 2025-04-24 17:52:14 +00:00
gitbook example
This commit is contained in:
parent
a79de9a313
commit
7e7f8a4b3c
5
registry-examples/aqua/export.aqua
Normal file
5
registry-examples/aqua/export.aqua
Normal file
@ -0,0 +1,5 @@
|
||||
module Export
|
||||
import createRouteAndRegisterBlocking, resolveRoute from "@fluencelabs/registry/routing.aqua"
|
||||
export createRouteAndRegisterBlocking, resolveRoute
|
||||
|
||||
|
5422
registry-examples/package-lock.json
generated
Normal file
5422
registry-examples/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
32
registry-examples/package.json
Normal file
32
registry-examples/package.json
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "registry-experiments",
|
||||
"version": "0.1.0",
|
||||
"description": "Fluence Registry experiments",
|
||||
"main": "./dist/src/index.js",
|
||||
"typings": "./dist/src/index.d.ts",
|
||||
"files": [
|
||||
"dist/*"
|
||||
],
|
||||
"bic": [
|
||||
"client-peer/*",
|
||||
"*.aqua",
|
||||
"package-lock.json"
|
||||
],
|
||||
"scripts": {
|
||||
"prestart": "npm run compile-aqua",
|
||||
"prebuild": "npm run compile-aqua",
|
||||
"compile-aqua": "aqua --import . -i ./aqua/ -o ./src/generated",
|
||||
"start": "node -r ts-node/register src/index.ts",
|
||||
"build": "tsc",
|
||||
"watch-aqua": "chokidar \"**/*.aqua\" -c \"npm run compile-aqua\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluencelabs/fluence": "0.21.5",
|
||||
"@fluencelabs/fluence-network-environment": "1.0.13",
|
||||
"@fluencelabs/registry": "0.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts-node": "^10.7.0",
|
||||
"typescript": "^4.6.2"
|
||||
}
|
||||
}
|
437
registry-examples/src/generated/export.ts
Normal file
437
registry-examples/src/generated/export.ts
Normal file
@ -0,0 +1,437 @@
|
||||
/**
|
||||
*
|
||||
* 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-285
|
||||
*
|
||||
*/
|
||||
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||
import {
|
||||
CallParams,
|
||||
callFunction,
|
||||
registerService,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v3';
|
||||
|
||||
|
||||
// Services
|
||||
|
||||
// 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 createRouteAndRegisterBlocking(
|
||||
label: string,
|
||||
value: string,
|
||||
relay_id: string | null,
|
||||
service_id: string | null,
|
||||
progress: (arg0: string, callParams: CallParams<'arg0'>) => void | Promise<void>,
|
||||
ack: number,
|
||||
config?: {ttl?: number}
|
||||
): Promise<string>;
|
||||
|
||||
export function createRouteAndRegisterBlocking(
|
||||
peer: FluencePeer,
|
||||
label: string,
|
||||
value: string,
|
||||
relay_id: string | null,
|
||||
service_id: string | null,
|
||||
progress: (arg0: string, callParams: CallParams<'arg0'>) => void | Promise<void>,
|
||||
ack: number,
|
||||
config?: {ttl?: number}
|
||||
): Promise<string>;
|
||||
|
||||
export function createRouteAndRegisterBlocking(...args: any) {
|
||||
|
||||
let script = `
|
||||
(xor
|
||||
(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% ("getDataSrv" "ack") [] ack)
|
||||
)
|
||||
(new $results
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(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
|
||||
(seq
|
||||
(seq
|
||||
(xor
|
||||
(xor
|
||||
(seq
|
||||
(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! false weight t-0] result-0)
|
||||
)
|
||||
(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! weight-0 t-1] result)
|
||||
)
|
||||
(xor
|
||||
(match result.$.success! true
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(ap result $results)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(call %init_peer_id% ("callbackSrv" "progress") [n])
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(seq
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "noop") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "noop") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 6])
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(call %init_peer_id% ("op" "noop") [])
|
||||
)
|
||||
(next n)
|
||||
)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 7])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("op" "noop") [$results.$.[ack]!])
|
||||
)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [route_id])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 8])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 9])
|
||||
)
|
||||
`
|
||||
return callFunction(
|
||||
args,
|
||||
{
|
||||
"functionName" : "createRouteAndRegisterBlocking",
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"progress" : {
|
||||
"tag" : "arrow",
|
||||
"domain" : {
|
||||
"tag" : "unlabeledProduct",
|
||||
"items" : [
|
||||
{
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
]
|
||||
},
|
||||
"codomain" : {
|
||||
"tag" : "nil"
|
||||
}
|
||||
},
|
||||
"ack" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "i16"
|
||||
}
|
||||
}
|
||||
},
|
||||
"codomain" : {
|
||||
"tag" : "unlabeledProduct",
|
||||
"items" : [
|
||||
{
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"names" : {
|
||||
"relay" : "-relay-",
|
||||
"getDataSrv" : "getDataSrv",
|
||||
"callbackSrv" : "callbackSrv",
|
||||
"responseSrv" : "callbackSrv",
|
||||
"responseFnName" : "response",
|
||||
"errorHandlingSrv" : "errorHandlingSrv",
|
||||
"errorFnName" : "error"
|
||||
}
|
||||
},
|
||||
script
|
||||
)
|
||||
}
|
9
registry-examples/src/index.ts
Normal file
9
registry-examples/src/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { main } from './main';
|
||||
|
||||
main()
|
||||
.then(() => process.exit(0))
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
24
registry-examples/src/main.ts
Normal file
24
registry-examples/src/main.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { Fluence } from "@fluencelabs/fluence";
|
||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
import { createRouteAndRegisterBlocking, resolveRoute } from "./generated/export";
|
||||
|
||||
export async function main() {
|
||||
// connect to the Fluence network
|
||||
await Fluence.start({ connectTo: krasnodar[1] });
|
||||
|
||||
let label = "myLabel";
|
||||
let value = "put anything useful here";
|
||||
let serviceId = "Foo";
|
||||
let ack = 5;
|
||||
|
||||
// create route and register for it
|
||||
let relay = Fluence.getStatus().relayPeerId;
|
||||
let route_id = await createRouteAndRegisterBlocking(
|
||||
label, value, relay, serviceId,
|
||||
(s) => console.log(`node ${s} saved the record`),
|
||||
ack
|
||||
);
|
||||
|
||||
// this will contain peer as route provider
|
||||
let providers = await resolveRoute(route_id, ack);
|
||||
}
|
62
registry-examples/tsconfig.json
Normal file
62
registry-examples/tsconfig.json
Normal file
@ -0,0 +1,62 @@
|
||||
{
|
||||
"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. */
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user