Separate NodeJS demo, switch to Krasnodar

This commit is contained in:
folex
2021-08-12 17:48:50 +03:00
parent fcfe06768d
commit b0e726cc92
11 changed files with 9726 additions and 286 deletions

View File

@ -0,0 +1,110 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { provideFile } from "./provider";
import { set_timeout } from "@fluencelabs/aqua-ipfs-ts";
import { createClient, setLogLevel } from "@fluencelabs/fluence";
import { stage, krasnodar, Node, testNet } from "@fluencelabs/fluence-network-environment";
import { deploy_service, put_file_size, remove_service } from "@fluencelabs/ipfs-execution-aqua";
import { globSource, urlSource }from 'ipfs-http-client';
async function main(environment: Node[]) {
// setLogLevel('DEBUG');
let providerHost = environment[0];
let providerClient = await createClient(providerHost);
console.log("📘 uploading .wasm to node %s", providerHost.multiaddr);
let path = globSource('../service/artifacts/process_files.wasm');
let { file, swarmAddr, rpcAddr } = await provideFile(path, providerClient);
console.log("📗 swarmAddr", swarmAddr);
console.log("📗 rpcAddr", rpcAddr);
const fluence = await createClient(environment[1]);
console.log("📗 created a fluence client %s with relay %s", fluence.selfPeerId, fluence.relayPeerId);
// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
await set_timeout(fluence, environment[2].peerId, 10);
console.log("\n\n📘 Will deploy ProcessFiles service");
var service_id = await deploy_service(
fluence,
environment[2].peerId, file.cid.toString(), rpcAddr,
(label, error) => { console.error("📕 deploy_service failed: ", label, error) },
{ ttl: 10000 }
)
service_id = fromOption(service_id);
if (service_id === null) {
return;
}
console.log("📗 ProcessFiles service is now deployed and available as", service_id);
console.log("\n\n📘 Will upload file & calculate its size");
let { file: newFile } = await provideFile(urlSource("https://i.imgur.com/NZgK6DB.png"), providerClient);
var putResult = await put_file_size(
fluence,
environment[2].peerId, newFile.cid.toString(), rpcAddr, service_id,
fileSize => console.log("📗 Calculated file size:", fileSize),
(label, error) => { console.error("📕 put_file_size failed: ", label, error) },
{ ttl: 10000 }
)
putResult = fromOption(putResult);
if (putResult !== null) {
console.log("📗 File size is saved to IPFS:", putResult);
}
let result = await remove_service(fluence, environment[2].peerId, service_id);
console.log("📗 ProcessFiles service removed", result);
return;
}
function fromOption<T>(opt: T | T[] | null): T | null {
if (Array.isArray(opt)) {
if (opt.length === 0) { return null; }
opt = opt[0];
}
if (opt === null) { return null; }
return opt;
}
let args = process.argv.slice(2);
var environment: Node[];
if (args.length >= 1 && args[0] == "testnet") {
environment = testNet;
console.log("📘 Will connect to testNet");
} else if (args[0] == "stage") {
environment = stage;
console.log("📘 Will connect to stage");
} else if (args[0] == "krasnodar") {
environment = krasnodar;
console.log("📘 Will connect to krasnodar");
} else if (args[0] == "testnet") {
environment = testNet;
console.log("📘 Will connect to testNet");
} else {
throw "Specify environment";
}
main(environment)
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
{
"name": "@fluencelabs/ipfs-execution",
"version": "0.1.0",
"description": "An example of executing WASM code from IPFS over IPFS files",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"prestart:local": "npm run build",
"start:local": "node dist/demo.js local",
"prestart:remote": "npm run build",
"start:remote": "node dist/demo.js krasnodar",
"start": "npm run start:remote"
},
"keywords": [
"fluence",
"wasm",
"ipfs",
"functions",
"faas",
"decentralization",
"p2p",
"libp2p"
],
"author": "Fluence Labs",
"license": "MIT",
"dependencies": {
"@fluencelabs/ipfs-execution-aqua": "file:../aqua",
"@fluencelabs/aqua-ipfs-ts": "0.3.4",
"@fluencelabs/fluence": "0.9.53",
"@fluencelabs/fluence-network-environment": "1.0.10",
"@fluencelabs/aqua-lib": "0.1.14",
"ipfs-http-client": "^50.1.2",
"it-all": "^1.0.5",
"uint8arrays": "^2.1.5",
"multiaddr": "^10.0.0"
},
"devDependencies": {
"typescript": "^3.9.5",
"@fluencelabs/aqua-cli": "0.1.9-164"
}
}

View File

@ -0,0 +1,45 @@
import { create, CID }from 'ipfs-http-client';
import { AddResult } from 'ipfs-core-types/src/root';
import { Multiaddr, protocols } from 'multiaddr';
import { get_external_swarm_multiaddr, get_external_api_multiaddr } from "@fluencelabs/aqua-ipfs-ts";
import { FluenceClient } from "@fluencelabs/fluence";
export async function provideFile(source: any, provider: FluenceClient): Promise<{ file: AddResult, swarmAddr: string, rpcAddr: string }> {
var swarmAddr;
var result = await get_external_swarm_multiaddr(provider, provider.relayPeerId!, { ttl: 20000 });
if (result.success) {
swarmAddr = result.multiaddr;
} else {
console.error("Failed to retrieve external swarm multiaddr from %s: ", provider.relayPeerId);
throw result.error;
}
var rpcAddr;
var result = await get_external_api_multiaddr(provider, provider.relayPeerId!);
if (result.success) {
rpcAddr = result.multiaddr;
} else {
console.error("Failed to retrieve external api multiaddr from %s: ", provider.relayPeerId);
throw result.error;
}
var rpcMaddr = new Multiaddr(rpcAddr).decapsulateCode(protocols.names.p2p.code);
// HACK: `as any` is needed because ipfs-http-client forgot to add `| Multiaddr` to the `create` types
const ipfs = create(rpcMaddr as any);
console.log("📗 created ipfs client to %s", rpcMaddr);
await ipfs.id();
console.log("📗 connected to ipfs");
const file = await ipfs.add(source);
console.log("📗 uploaded file:", file);
// To download the file, uncomment the following code:
// let files = await ipfs.get(file.cid);
// for await (const file of files) {
// const content = uint8ArrayConcat(await all(file.content));
// console.log("📗 downloaded file of length ", content.length);
// }
return { file, swarmAddr, rpcAddr };
}

View 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. */
}
}