mirror of
https://github.com/fluencelabs/examples
synced 2025-04-24 18:22:15 +00:00
init
This commit is contained in:
parent
7d9755f8dc
commit
c24b2a8555
1
snapshot/.bic_cache
Normal file
1
snapshot/.bic_cache
Normal file
@ -0,0 +1 @@
|
||||
{"aqua-scripts/builtin.aqua":[1626052678698,"ae36062795185a7756d915836210c5533e4af454"],"aqua-scripts/timestamp_getter.aqua":[1626052736727,"2d95c6b6c4466bde127642aa3e5771c7e91b7ca8"],"typescript/dist/typescript/index.js":[1626052337425,"249aaa793924f5bed6ecaf77c592e551152972be"],"typescript/dist/typescript/timestamp_getter.js":[1626052337425,"c99ebed601687fd8e7f5e68ca5e4e077bbe321c6"],"typescript/index.js":[1626052337427,"715fe7e937929262f05b0f3f7b522074918cd63c"],"typescript/index.ts":[1626052337427,"fd55bb8547dcdaafae203bf27f91c92bff7e7f5f"],"typescript/timestamp_getter.ts":[1626052701327,"57b765922f39146106cdffc37f01e86aef239127"],"package-lock.json":[1626052624166,"9e9820a07d9baf050439718aefbe19d876f9bc04"]}
|
54
snapshot/air-scripts/timestamp_getter.ts_getter.air
Normal file
54
snapshot/air-scripts/timestamp_getter.ts_getter.air
Normal file
@ -0,0 +1,54 @@
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "n_neighborhood") [] n_neighborhood)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "n_returnvalues") [] n_returnvalues)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call node ("op" "string_to_b58") [node] k)
|
||||
(call node ("kad" "neighborhood") [k false] nodes)
|
||||
)
|
||||
(fold nodes n
|
||||
(par
|
||||
(seq
|
||||
(xor
|
||||
(call n ("peer" "timestamp_ms") [] $res)
|
||||
(null)
|
||||
)
|
||||
(call node ("op" "noop") [])
|
||||
)
|
||||
(next n)
|
||||
)
|
||||
)
|
||||
)
|
||||
(call node ("op" "identity") [$res.$.[9]!])
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "noop") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [$res])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
183
snapshot/aqua-scripts/builtin.aqua
Normal file
183
snapshot/aqua-scripts/builtin.aqua
Normal file
@ -0,0 +1,183 @@
|
||||
-- Default public interface of Fluence nodes
|
||||
|
||||
alias Field : []string
|
||||
alias Argument : []string
|
||||
alias Bytes : []u8
|
||||
alias PeerId : string
|
||||
|
||||
data Service:
|
||||
id: string
|
||||
blueprint_id: string
|
||||
owner_id: string
|
||||
|
||||
data FunctionSignature:
|
||||
arguments: []Argument
|
||||
name: string
|
||||
output_types: []string
|
||||
|
||||
data RecordType:
|
||||
fields: []Field
|
||||
id: u64
|
||||
name: string
|
||||
|
||||
data Interface:
|
||||
function_signatures: []FunctionSignature
|
||||
record_types: []RecordType
|
||||
|
||||
data ServiceInfo:
|
||||
blueprint_id: string
|
||||
service_id: string
|
||||
interface: Interface
|
||||
|
||||
data Info:
|
||||
external_addresses: []string
|
||||
|
||||
data ModuleConfig:
|
||||
name: string
|
||||
|
||||
data Module:
|
||||
name: string
|
||||
hash: string
|
||||
config: ModuleConfig
|
||||
|
||||
data AddBlueprint:
|
||||
name: string
|
||||
dependencies: []string
|
||||
|
||||
data Blueprint:
|
||||
id: string
|
||||
name: string
|
||||
dependencies: []string
|
||||
|
||||
data ScriptInfo:
|
||||
id: string
|
||||
src: string
|
||||
failures: u32
|
||||
interval: string
|
||||
owner: string
|
||||
|
||||
data Contact:
|
||||
peer_id: string
|
||||
addresses: []string
|
||||
|
||||
service Op("op"):
|
||||
-- does nothing
|
||||
noop()
|
||||
-- takes any number of arguments and wraps them into a single array
|
||||
array(a: string, b: string, c: string) -> []string
|
||||
array_length(array: []string) -> u32
|
||||
-- takes any number of arrays and flattens them by concatenating
|
||||
concat(a: []string, b: []string, c: []string) -> []string
|
||||
-- takes a single argument and returns it back
|
||||
identity(s: ?string) -> ?string
|
||||
string_to_b58(s: string) -> string
|
||||
string_from_b58(b: string) -> string
|
||||
bytes_to_b58(bs: []u8) -> string
|
||||
bytes_from_b58(b: string) -> []u8
|
||||
-- Applies SHA256 to the given string
|
||||
-- Argument: s - string to apply sha256 to
|
||||
-- Returns: returns sha256 multihash encoded as base58
|
||||
sha256_string(s: string) -> string
|
||||
|
||||
service Peer("peer"):
|
||||
-- Checks if there is a direct connection to the peer identified by a given PeerId
|
||||
-- Argument: PeerId – id of the peer to check if there's a connection with
|
||||
-- Returns: bool - true if connected to the peer, false otherwise
|
||||
is_connected(peer: PeerId) -> bool
|
||||
|
||||
-- Initiates a connection to the specified peer
|
||||
-- Arguments:
|
||||
-- PeerId – id of the target peer
|
||||
-- [Multiaddr] – an array of target peer's addresses
|
||||
-- Returns: bool - true if connection was successful
|
||||
connect(id: PeerId, multiaddrs: []string) -> bool
|
||||
-- Resolves the contact of a peer via Kademlia
|
||||
-- Argument: PeerId – id of the target peer
|
||||
-- Returns: Contact - true if connection was successful
|
||||
get_contact(peer: PeerId) -> Contact
|
||||
|
||||
-- Get information about the peer
|
||||
identify() -> Info
|
||||
|
||||
-- Get Unix timestamp in milliseconds
|
||||
timestamp_ms() -> u64
|
||||
|
||||
-- Get Unix timestamp in seconds
|
||||
timestamp_sec() -> u64
|
||||
|
||||
service Kademlia("kad"):
|
||||
-- Instructs node to return the locally-known nodes
|
||||
-- in the Kademlia neighborhood for a given key
|
||||
neighborhood(key: PeerId, already_hashed: bool) -> []PeerId
|
||||
-- Merges given lists and sorts them by distance to target
|
||||
-- Arguments:
|
||||
-- target – base58 string; result is sorted by XOR distance to target
|
||||
-- left – list of base58 strings
|
||||
-- right – list of base58 strings
|
||||
-- count – how many items to return
|
||||
-- Returns: list of base58 strings sorted by distance to target; list will contain at most count elements
|
||||
merge(target: string, left: []string, right: []string, count: u32) -> []string
|
||||
|
||||
service Srv("srv"):
|
||||
-- Used to create a service on a certain node
|
||||
-- Arguments:
|
||||
-- blueprint_id – ID of the blueprint that has been added to the node specified in the service call by the dist add_blueprint service.
|
||||
-- Returns: service_id – the service ID of the created service.
|
||||
create(blueprint_id: string) -> string
|
||||
|
||||
-- Used to remove a service from a certain node
|
||||
-- Arguments:
|
||||
-- service_id – ID of the service to remove
|
||||
remove(service_id: string)
|
||||
|
||||
-- Returns a list of services running on a peer
|
||||
list() -> []Service
|
||||
|
||||
-- Adds an alias on service, so, service could be called
|
||||
-- not only by service_id but by alias as well.
|
||||
-- Argument:
|
||||
-- alias - settable service name
|
||||
-- service_id – ID of the service whose interface you want to name.
|
||||
add_alias(alias: string, service_id: string)
|
||||
|
||||
-- Resolves given alias to a service id
|
||||
-- If there's no such alias, throws an error
|
||||
-- Returns: service id associated with the given alias
|
||||
resolve_alias(alias: string) -> string
|
||||
|
||||
-- Retrieves the functional interface of a service running
|
||||
-- on the node specified in the service call
|
||||
-- Argument: service_id – ID of the service whose interface you want to retrieve.
|
||||
get_interface(service_id: string) -> ServiceInfo
|
||||
|
||||
service Dist("dist"):
|
||||
-- Used to add modules to the node specified in the service call
|
||||
-- Arguments:
|
||||
-- bytes – a base64 string containing the .wasm module to add.
|
||||
-- config – module info
|
||||
-- Returns: blake3 hash of the module
|
||||
add_module(wasm_b56_content: Bytes, conf: ModuleConfig) -> string
|
||||
|
||||
-- Get a list of modules available on the node
|
||||
list_modules() -> []Module
|
||||
|
||||
-- Get the interface of a module
|
||||
get_interface(module_id: string) -> Interface
|
||||
|
||||
-- Used to add a blueprint to the node specified in the service call
|
||||
add_blueprint(blueprint: AddBlueprint) -> string
|
||||
|
||||
-- Used to get the blueprints available on the node specified in the service call.
|
||||
-- A blueprint is an object of the following structure
|
||||
list_blueprints() -> []Blueprint
|
||||
|
||||
service Script("script"):
|
||||
-- Adds the given script to a node
|
||||
add(air_content: string, run_every: string) -> string
|
||||
|
||||
-- Removes recurring script from a node. Only a creator of the script can delete it
|
||||
remove(script_id: string) -> bool
|
||||
|
||||
-- Returns a list of existing scripts on the node.
|
||||
-- Each object in the list is of the following structure
|
||||
list() -> ScriptInfo
|
31
snapshot/aqua-scripts/timestamp_getter.aqua
Normal file
31
snapshot/aqua-scripts/timestamp_getter.aqua
Normal file
@ -0,0 +1,31 @@
|
||||
import "builtin.aqua"
|
||||
-- import "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
|
||||
-- const nsize = 20
|
||||
|
||||
data TSResult:
|
||||
timestamp: u64
|
||||
peer_id: string
|
||||
|
||||
service OpTS("op"):
|
||||
identity(s: u64)
|
||||
array(a: string, b: u64) -> TSResult
|
||||
|
||||
-- func ts_getter(node: string, n_neighborhood: u32, n_returnvalues: u32) -> []u64: -- ]TSResult:
|
||||
func ts_getter(node: string) -> []u64: -- ]TSResult:
|
||||
res: *u64 --TResult
|
||||
|
||||
on node:
|
||||
k <- Op.string_to_b58(node)
|
||||
-- nodes <- Kademlia.neighborhood(k, false, 20)
|
||||
nodes <- Kademlia.neighborhood(k, false)
|
||||
for n <- nodes par:
|
||||
on n:
|
||||
try:
|
||||
res <- Peer.timestamp_ms()
|
||||
-- res <- OpTS.array(n, Peer.timestamp_ms())
|
||||
|
||||
-- OpTs.identity(res!Op.array_length(nodes))
|
||||
-- OpTS.identity(res!n_returnvalues)
|
||||
OpTS.identity(res!9)
|
||||
<- res
|
3569
snapshot/package-lock.json
generated
Normal file
3569
snapshot/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
43
snapshot/package.json
Normal file
43
snapshot/package.json
Normal file
@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "timestamp-oracle",
|
||||
"version": "0.1.0",
|
||||
"description": "Fluence Timestamp Oracle Demo",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/*"
|
||||
],
|
||||
"bic": [
|
||||
"typescript/*",
|
||||
"*.aqua",
|
||||
"package-lock.json"
|
||||
],
|
||||
"dependencies": {
|
||||
"@fluencelabs/aqua-lib": "^0.1.5",
|
||||
"@fluencelabs/fluence": "^0.9.50",
|
||||
"@fluencelabs/fluence-network-environment": "^1.0.10",
|
||||
"ethers": "^5.4.1"
|
||||
},
|
||||
"scripts": {
|
||||
"compile-aqua": "aqua-cli -i ./aqua-scripts -o ./typescript",
|
||||
"prebuild": "npm run compile-aqua",
|
||||
"build": "tsc",
|
||||
"prestart": "npx bic -s",
|
||||
"start": "node typescript/dist/index.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": ""
|
||||
},
|
||||
"keywords": [
|
||||
"aqua",
|
||||
"fluence"
|
||||
],
|
||||
"author": "Fluence Labs",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@fluencelabs/aqua-cli": "^0.1.8-160",
|
||||
"build-if-changed": "^1.5.5",
|
||||
"typescript": "^3.9.5"
|
||||
}
|
||||
}
|
62
snapshot/tsconfig.json
Normal file
62
snapshot/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": "./typescript/dist", /* Redirect output structure to the directory. */
|
||||
"rootDir": "./typescript", /* 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. */
|
||||
}
|
||||
}
|
81
snapshot/typescript/dist/air-scripts/timestamp_getter.js
vendored
Normal file
81
snapshot/typescript/dist/air-scripts/timestamp_getter.js
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ts_getter = void 0;
|
||||
var api_unstable_1 = require("@fluencelabs/fluence/dist/api.unstable");
|
||||
function ts_getter(client, node, config) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var request, promise;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
promise = new Promise(function (resolve, reject) {
|
||||
request = new api_unstable_1.RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withTTL((config === null || config === void 0 ? void 0 : config.ttl) || 5000)
|
||||
.withRawScript("\n(xor\n (seq\n (seq\n (seq\n (seq\n (seq\n (call %init_peer_id% (\"getDataSrv\" \"-relay-\") [] -relay-)\n (call %init_peer_id% (\"getDataSrv\" \"node\") [] node)\n )\n (call -relay- (\"op\" \"noop\") [])\n )\n (xor\n (seq\n (seq\n (seq\n (call node (\"op\" \"string_to_b58\") [node] k)\n (call node (\"kad\" \"neighborhood\") [k false] nodes)\n )\n (fold nodes n\n (par\n (seq\n (xor\n (call n (\"peer\" \"timestamp_ms\") [] $res)\n (null)\n )\n (call node (\"op\" \"noop\") [])\n )\n (next n)\n )\n )\n )\n (call node (\"op\" \"identity\") [$res.$.[9]!])\n )\n (seq\n (call -relay- (\"op\" \"noop\") [])\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 1])\n )\n )\n )\n (call -relay- (\"op\" \"noop\") [])\n )\n (xor\n (call %init_peer_id% (\"callbackSrv\" \"response\") [$res])\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 2])\n )\n )\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 3])\n)\n\n ")
|
||||
.configHandler(function (h) {
|
||||
h.on('getDataSrv', '-relay-', function () {
|
||||
return client.relayPeerId;
|
||||
});
|
||||
h.on('getDataSrv', 'node', function () { return node; });
|
||||
h.onEvent('callbackSrv', 'response', function (args) {
|
||||
var res = args[0];
|
||||
resolve(res);
|
||||
});
|
||||
h.onEvent('errorHandlingSrv', 'error', function (args) {
|
||||
// assuming error is the single argument
|
||||
var err = args[0];
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(function () {
|
||||
reject('Request timed out for ts_getter');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
return [4 /*yield*/, client.initiateFlow(request)];
|
||||
case 1:
|
||||
_a.sent();
|
||||
return [2 /*return*/, promise];
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
exports.ts_getter = ts_getter;
|
127
snapshot/typescript/dist/index.js
vendored
Normal file
127
snapshot/typescript/dist/index.js
vendored
Normal file
@ -0,0 +1,127 @@
|
||||
"use strict";
|
||||
/*
|
||||
* Copyright 2021 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.
|
||||
*/
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fluence_1 = require("@fluencelabs/fluence");
|
||||
var fluence_network_environment_1 = require("@fluencelabs/fluence-network-environment");
|
||||
// import { ethers } from "ethers";
|
||||
var timestamp_getter_1 = require("./timestamp_getter");
|
||||
function timestamp_delta(proposal_ts_ms, network_ts_ms) {
|
||||
var acceptable_ts_diff = 60 * 1000; // 1 Minute
|
||||
var valid_ts = [];
|
||||
var invalid_ts = [];
|
||||
for (var _i = 0, network_ts_ms_1 = network_ts_ms; _i < network_ts_ms_1.length; _i++) {
|
||||
var t = network_ts_ms_1[_i];
|
||||
var threshold_ts = t + acceptable_ts_diff;
|
||||
console.log(t, threshold_ts);
|
||||
if (threshold_ts > proposal_ts_ms) {
|
||||
// results.get("valid"); // .valid_ts.push(t);
|
||||
valid_ts.push(t);
|
||||
}
|
||||
else {
|
||||
// results.set("invalid", invalid_ts.push(t));
|
||||
invalid_ts.push(t);
|
||||
}
|
||||
}
|
||||
console.log("valid_ts: ", valid_ts);
|
||||
console.log("invalid_ts: ", invalid_ts);
|
||||
var results = new Map();
|
||||
results.set("valid", valid_ts);
|
||||
results.set("invalid", invalid_ts);
|
||||
return results;
|
||||
}
|
||||
function main() {
|
||||
var _a, _b;
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var fluence, network_result, now, utc_ts, ts_diffs, valid_ts, invalid_ts;
|
||||
return __generator(this, function (_c) {
|
||||
switch (_c.label) {
|
||||
case 0:
|
||||
console.log("hello");
|
||||
return [4 /*yield*/, fluence_1.createClient(fluence_network_environment_1.krasnodar[2])];
|
||||
case 1:
|
||||
fluence = _c.sent();
|
||||
return [4 /*yield*/, timestamp_getter_1.ts_getter(fluence, fluence_network_environment_1.krasnodar[2].peerId)];
|
||||
case 2:
|
||||
network_result = _c.sent();
|
||||
console.log(network_result);
|
||||
console.log(network_result[5]);
|
||||
now = new Date;
|
||||
utc_ts = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
|
||||
console.log(utc_ts);
|
||||
ts_diffs = timestamp_delta(utc_ts, network_result);
|
||||
console.log(ts_diffs);
|
||||
if (ts_diffs.has("valid") && ts_diffs.has("invalid")) {
|
||||
valid_ts = (_a = ts_diffs.get("valid")) === null || _a === void 0 ? void 0 : _a.length;
|
||||
invalid_ts = (_b = ts_diffs.get("invalid")) === null || _b === void 0 ? void 0 : _b.length;
|
||||
console.log(valid_ts, invalid_ts);
|
||||
if (valid_ts !== undefined && invalid_ts !== undefined && (valid_ts / (valid_ts + invalid_ts)) >= (2 / 3)) {
|
||||
console.log("consensus");
|
||||
}
|
||||
else {
|
||||
console.log("no consensus");
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.log("error: something went wrong with getting our timestamp validated");
|
||||
}
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
main()
|
||||
.then(function () { return process.exit(0); })
|
||||
.catch(function (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
83
snapshot/typescript/dist/timestamp_getter.js
vendored
Normal file
83
snapshot/typescript/dist/timestamp_getter.js
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ts_getter = void 0;
|
||||
var api_unstable_1 = require("@fluencelabs/fluence/dist/api.unstable");
|
||||
function ts_getter(client, node, config) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var request, promise;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
promise = new Promise(function (resolve, reject) {
|
||||
var r = new api_unstable_1.RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript("\n(xor\n (seq\n (seq\n (seq\n (seq\n (seq\n (call %init_peer_id% (\"getDataSrv\" \"-relay-\") [] -relay-)\n (call %init_peer_id% (\"getDataSrv\" \"node\") [] node)\n )\n (call -relay- (\"op\" \"noop\") [])\n )\n (xor\n (seq\n (seq\n (seq\n (call node (\"op\" \"string_to_b58\") [node] k)\n (call node (\"kad\" \"neighborhood\") [k false] nodes)\n )\n (fold nodes n\n (par\n (seq\n (xor\n (call n (\"peer\" \"timestamp_ms\") [] $res)\n (null)\n )\n (call node (\"op\" \"noop\") [])\n )\n (next n)\n )\n )\n )\n (call node (\"op\" \"identity\") [$res.$.[9]!])\n )\n (seq\n (call -relay- (\"op\" \"noop\") [])\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 1])\n )\n )\n )\n (call -relay- (\"op\" \"noop\") [])\n )\n (xor\n (call %init_peer_id% (\"callbackSrv\" \"response\") [$res])\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 2])\n )\n )\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 3])\n)\n\n ")
|
||||
.configHandler(function (h) {
|
||||
h.on('getDataSrv', '-relay-', function () {
|
||||
return client.relayPeerId;
|
||||
});
|
||||
h.on('getDataSrv', 'node', function () { return node; });
|
||||
h.onEvent('callbackSrv', 'response', function (args) {
|
||||
var res = args[0];
|
||||
resolve(res);
|
||||
});
|
||||
h.onEvent('errorHandlingSrv', 'error', function (args) {
|
||||
// assuming error is the single argument
|
||||
var err = args[0];
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(function () {
|
||||
reject('Request timed out for ts_getter');
|
||||
});
|
||||
if (config === null || config === void 0 ? void 0 : config.ttl) {
|
||||
r.withTTL(config.ttl);
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
return [4 /*yield*/, client.initiateFlow(request)];
|
||||
case 1:
|
||||
_a.sent();
|
||||
return [2 /*return*/, promise];
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
exports.ts_getter = ts_getter;
|
114
snapshot/typescript/dist/typescript/index.js
vendored
Normal file
114
snapshot/typescript/dist/typescript/index.js
vendored
Normal file
@ -0,0 +1,114 @@
|
||||
"use strict";
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
// const { create, globSource, urlSource } = require('ipfs-http-client');
|
||||
// const all = require('it-all');
|
||||
// const uint8ArrayConcat = require('uint8arrays/concat')
|
||||
// import { timestamp_getter } from "timestamp_getter";
|
||||
var local = [
|
||||
{
|
||||
peerId: "12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK",
|
||||
multiaddr: "/ip4/127.0.0.1/tcp/9990/ws/p2p/12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK"
|
||||
},
|
||||
{
|
||||
peerId: "12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv",
|
||||
multiaddr: "/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv"
|
||||
},
|
||||
{
|
||||
peerId: "12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
|
||||
multiaddr: "/ip4/127.0.0.1/tcp/9992/ws/p2p/12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5"
|
||||
},
|
||||
];
|
||||
function main() {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
console.log("hello");
|
||||
// setLogLevel('DEBUG');
|
||||
// const fluence = await createClient(testNet[1]);
|
||||
// console.log("created a fluence client %s with relay %s", fluence.selfPeerId, fluence.relayPeerId);
|
||||
/*
|
||||
let ipfsAddr = 'https://stage.fluence.dev:15001';
|
||||
let ipfsMultiaddr = '/ip4/134.209.186.43/tcp/4001/p2p/12D3KooWEhCqQ9NBnmtSfNeXSNfhgccmH86xodkCUxZNEXab6pkw';
|
||||
const ipfs = create(ipfsAddr);
|
||||
console.log("created ipfs client");
|
||||
|
||||
await ipfs.id();
|
||||
console.log("connected to ipfs");
|
||||
|
||||
let source = urlSource('https://images.adsttc.com/media/images/5ecd/d4ac/b357/65c6/7300/009d/large_jpg/02C.jpg?1590547607');
|
||||
const file = await ipfs.add(source);
|
||||
console.log("uploaded file:", file);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
console.log("file hash: ", file.cid);
|
||||
let getResult = await get_from(fluence, testNet[2].peerId, file.cid.toString(), ipfsMultiaddr, { ttl: 10000 });
|
||||
console.log("Ipfs.get", getResult);
|
||||
|
||||
let putResult = await put(fluence, testNet[2].peerId, getResult.path, { ttl: 10000 });
|
||||
console.log("Ipfs.put", putResult);
|
||||
*/
|
||||
return [2 /*return*/];
|
||||
});
|
||||
});
|
||||
}
|
||||
main()
|
||||
.then(function () { return process.exit(0); })
|
||||
.catch(function (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
81
snapshot/typescript/dist/typescript/timestamp_getter.js
vendored
Normal file
81
snapshot/typescript/dist/typescript/timestamp_getter.js
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ts_getter = void 0;
|
||||
var api_unstable_1 = require("@fluencelabs/fluence/dist/api.unstable");
|
||||
function ts_getter(client, node, config) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var request, promise;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
promise = new Promise(function (resolve, reject) {
|
||||
request = new api_unstable_1.RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withTTL((config === null || config === void 0 ? void 0 : config.ttl) || 5000)
|
||||
.withRawScript("\n(xor\n (seq\n (seq\n (seq\n (seq\n (seq\n (call %init_peer_id% (\"getDataSrv\" \"-relay-\") [] -relay-)\n (call %init_peer_id% (\"getDataSrv\" \"node\") [] node)\n )\n (call -relay- (\"op\" \"noop\") [])\n )\n (xor\n (seq\n (seq\n (seq\n (call node (\"op\" \"string_to_b58\") [node] k)\n (call node (\"kad\" \"neighborhood\") [k false] nodes)\n )\n (fold nodes n\n (par\n (seq\n (xor\n (call n (\"peer\" \"timestamp_ms\") [] $res)\n (null)\n )\n (call node (\"op\" \"noop\") [])\n )\n (next n)\n )\n )\n )\n (call node (\"op\" \"identity\") [$res.$.[9]!])\n )\n (seq\n (call -relay- (\"op\" \"noop\") [])\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 1])\n )\n )\n )\n (call -relay- (\"op\" \"noop\") [])\n )\n (xor\n (call %init_peer_id% (\"callbackSrv\" \"response\") [$res])\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 2])\n )\n )\n (call %init_peer_id% (\"errorHandlingSrv\" \"error\") [%last_error% 3])\n)\n\n ")
|
||||
.configHandler(function (h) {
|
||||
h.on('getDataSrv', '-relay-', function () {
|
||||
return client.relayPeerId;
|
||||
});
|
||||
h.on('getDataSrv', 'node', function () { return node; });
|
||||
h.onEvent('callbackSrv', 'response', function (args) {
|
||||
var res = args[0];
|
||||
resolve(res);
|
||||
});
|
||||
h.onEvent('errorHandlingSrv', 'error', function (args) {
|
||||
// assuming error is the single argument
|
||||
var err = args[0];
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(function () {
|
||||
reject('Request timed out for ts_getter');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
return [4 /*yield*/, client.initiateFlow(request)];
|
||||
case 1:
|
||||
_a.sent();
|
||||
return [2 /*return*/, promise];
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
exports.ts_getter = ts_getter;
|
112
snapshot/typescript/index.js
Normal file
112
snapshot/typescript/index.js
Normal file
@ -0,0 +1,112 @@
|
||||
"use strict";
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
exports.__esModule = true;
|
||||
// const { create, globSource, urlSource } = require('ipfs-http-client');
|
||||
// const all = require('it-all');
|
||||
// const uint8ArrayConcat = require('uint8arrays/concat')
|
||||
var local = [
|
||||
{
|
||||
peerId: "12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK",
|
||||
multiaddr: "/ip4/127.0.0.1/tcp/9990/ws/p2p/12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK"
|
||||
},
|
||||
{
|
||||
peerId: "12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv",
|
||||
multiaddr: "/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv"
|
||||
},
|
||||
{
|
||||
peerId: "12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
|
||||
multiaddr: "/ip4/127.0.0.1/tcp/9992/ws/p2p/12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5"
|
||||
},
|
||||
];
|
||||
function main() {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
console.log("hello");
|
||||
// setLogLevel('DEBUG');
|
||||
// const fluence = await createClient(testNet[1]);
|
||||
// console.log("created a fluence client %s with relay %s", fluence.selfPeerId, fluence.relayPeerId);
|
||||
/*
|
||||
let ipfsAddr = 'https://stage.fluence.dev:15001';
|
||||
let ipfsMultiaddr = '/ip4/134.209.186.43/tcp/4001/p2p/12D3KooWEhCqQ9NBnmtSfNeXSNfhgccmH86xodkCUxZNEXab6pkw';
|
||||
const ipfs = create(ipfsAddr);
|
||||
console.log("created ipfs client");
|
||||
|
||||
await ipfs.id();
|
||||
console.log("connected to ipfs");
|
||||
|
||||
let source = urlSource('https://images.adsttc.com/media/images/5ecd/d4ac/b357/65c6/7300/009d/large_jpg/02C.jpg?1590547607');
|
||||
const file = await ipfs.add(source);
|
||||
console.log("uploaded file:", file);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
console.log("file hash: ", file.cid);
|
||||
let getResult = await get_from(fluence, testNet[2].peerId, file.cid.toString(), ipfsMultiaddr, { ttl: 10000 });
|
||||
console.log("Ipfs.get", getResult);
|
||||
|
||||
let putResult = await put(fluence, testNet[2].peerId, getResult.path, { ttl: 10000 });
|
||||
console.log("Ipfs.put", putResult);
|
||||
*/
|
||||
return [2 /*return*/];
|
||||
});
|
||||
});
|
||||
}
|
||||
main()
|
||||
.then(function () { return process.exit(0); })["catch"](function (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
98
snapshot/typescript/index.ts
Normal file
98
snapshot/typescript/index.ts
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2021 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 { createClient, setLogLevel, FluenceClient } from "@fluencelabs/fluence";
|
||||
import { krasnodar, Node } from "@fluencelabs/fluence-network-environment";
|
||||
// import { ethers } from "ethers";
|
||||
import { ts_getter } from "./timestamp_getter";
|
||||
|
||||
|
||||
function timestamp_delta(proposal_ts_ms: number, network_ts_ms: number[]): Map<string, Array<number>> {
|
||||
|
||||
const acceptable_ts_diff: number = 60 * 1_000; // 1 Minute
|
||||
let valid_ts: Array<number> = [];
|
||||
let invalid_ts: Array<number> = [];
|
||||
|
||||
for (var t of network_ts_ms) {
|
||||
let threshold_ts: number = t + acceptable_ts_diff;
|
||||
console.log(t, threshold_ts);
|
||||
|
||||
if (threshold_ts > proposal_ts_ms) {
|
||||
// results.get("valid"); // .valid_ts.push(t);
|
||||
valid_ts.push(t);
|
||||
}
|
||||
else {
|
||||
// results.set("invalid", invalid_ts.push(t));
|
||||
invalid_ts.push(t);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
console.log("valid_ts: ", valid_ts);
|
||||
console.log("invalid_ts: ", invalid_ts);
|
||||
|
||||
let results = new Map<string, Array<number>>();
|
||||
results.set("valid", valid_ts);
|
||||
results.set("invalid", invalid_ts);
|
||||
return results;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log("hello");
|
||||
// setLogLevel('DEBUG');
|
||||
const fluence = await createClient(krasnodar[2]);
|
||||
// console.log("created a fluence client %s with relay %s", fluence.selfPeerId, fluence.relayPeerId);
|
||||
|
||||
const network_result = await ts_getter(fluence, krasnodar[2].peerId);
|
||||
console.log(network_result);
|
||||
console.log(network_result[5]);
|
||||
|
||||
var now = new Date;
|
||||
var utc_ts = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
|
||||
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
|
||||
console.log(utc_ts);
|
||||
|
||||
const ts_diffs = timestamp_delta(utc_ts, network_result);
|
||||
console.log(ts_diffs);
|
||||
|
||||
if (ts_diffs.has("valid") && ts_diffs.has("invalid")) {
|
||||
let valid_ts = ts_diffs.get("valid")?.length;
|
||||
let invalid_ts = ts_diffs.get("invalid")?.length;
|
||||
|
||||
console.log(valid_ts, invalid_ts);
|
||||
|
||||
if (valid_ts !== undefined && invalid_ts !== undefined && (valid_ts / (valid_ts + invalid_ts)) >= (2 / 3)) {
|
||||
console.log("consensus");
|
||||
}
|
||||
else {
|
||||
console.log("no consensus");
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
console.log("error: something went wrong with getting our timestamp validated");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => process.exit(0))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
101
snapshot/typescript/timestamp_getter.ts
Normal file
101
snapshot/typescript/timestamp_getter.ts
Normal file
@ -0,0 +1,101 @@
|
||||
/**
|
||||
*
|
||||
* 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.1.8-161
|
||||
*
|
||||
*/
|
||||
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
|
||||
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
|
||||
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
|
||||
|
||||
|
||||
|
||||
export async function ts_getter(client: FluenceClient, node: string, config?: {ttl?: number}): Promise<number[]> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<number[]>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call node ("op" "string_to_b58") [node] k)
|
||||
(call node ("kad" "neighborhood") [k false] nodes)
|
||||
)
|
||||
(fold nodes n
|
||||
(par
|
||||
(seq
|
||||
(xor
|
||||
(call n ("peer" "timestamp_ms") [] $res)
|
||||
(null)
|
||||
)
|
||||
(call node ("op" "noop") [])
|
||||
)
|
||||
(next n)
|
||||
)
|
||||
)
|
||||
)
|
||||
(call node ("op" "identity") [$res.$.[9]!])
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "noop") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [$res])
|
||||
(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 client.relayPeerId!;
|
||||
});
|
||||
h.on('getDataSrv', 'node', () => {return node;});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for ts_getter');
|
||||
})
|
||||
if(config?.ttl) {
|
||||
r.withTTL(config.ttl)
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user