zkcloudworker 0.2.7 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/ts/src/api/api.d.ts +9 -5
- package/lib/ts/src/api/api.js +35 -11
- package/lib/ts/src/cloud/cloud.d.ts +34 -4
- package/lib/ts/src/cloud/cloud.js +23 -1
- package/lib/ts/src/cloud/files.d.ts +11 -0
- package/lib/ts/src/cloud/files.js +74 -0
- package/lib/ts/src/cloud/local.d.ts +46 -1
- package/lib/ts/src/cloud/local.js +173 -7
- package/lib/ts/src/cloud/task.d.ts +10 -0
- package/lib/ts/src/cloud/task.js +2 -0
- package/lib/ts/tsconfig.tsbuildinfo +1 -1
- package/lib/web/src/api/api.d.ts +9 -5
- package/lib/web/src/api/api.js +36 -12
- package/lib/web/src/api/api.js.map +1 -1
- package/lib/web/src/cloud/cloud.d.ts +34 -4
- package/lib/web/src/cloud/cloud.js +36 -1
- package/lib/web/src/cloud/cloud.js.map +1 -1
- package/lib/web/src/cloud/files.d.ts +11 -0
- package/lib/web/src/cloud/files.js +74 -0
- package/lib/web/src/cloud/files.js.map +1 -0
- package/lib/web/src/cloud/local.d.ts +46 -1
- package/lib/web/src/cloud/local.js +179 -6
- package/lib/web/src/cloud/local.js.map +1 -1
- package/lib/web/src/cloud/task.d.ts +10 -0
- package/lib/web/src/cloud/task.js +2 -0
- package/lib/web/src/cloud/task.js.map +1 -0
- package/lib/web/tsconfig.web.tsbuildinfo +1 -1
- package/package.json +1 -1
package/lib/ts/src/api/api.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { zkCloudWorker, Cloud } from "../cloud/cloud";
|
2
|
-
import {
|
2
|
+
import { blockchain } from "../networks";
|
3
3
|
/**
|
4
4
|
* API class for interacting with the zkCloudWorker
|
5
5
|
* @property jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
|
@@ -8,13 +8,17 @@ import { JobData } from "../cloud/job";
|
|
8
8
|
export declare class zkCloudWorkerClient {
|
9
9
|
readonly jwt: string;
|
10
10
|
readonly endpoint: string;
|
11
|
-
readonly
|
12
|
-
readonly localWorker
|
11
|
+
readonly chain: blockchain;
|
12
|
+
readonly localWorker?: (cloud: Cloud) => Promise<zkCloudWorker>;
|
13
13
|
/**
|
14
14
|
* Constructor for the API class
|
15
15
|
* @param jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
|
16
16
|
*/
|
17
|
-
constructor(
|
17
|
+
constructor(params: {
|
18
|
+
jwt: string;
|
19
|
+
zkcloudworker?: (cloud: Cloud) => Promise<zkCloudWorker>;
|
20
|
+
chain?: blockchain;
|
21
|
+
});
|
18
22
|
/**
|
19
23
|
* Starts a new job for the proof calculation using serverless api call
|
20
24
|
* The developer and name should correspond to the BackupPlugin of the API
|
@@ -59,7 +63,7 @@ export declare class zkCloudWorkerClient {
|
|
59
63
|
execute(data: {
|
60
64
|
developer: string;
|
61
65
|
repo: string;
|
62
|
-
task
|
66
|
+
task: string;
|
63
67
|
userId?: string;
|
64
68
|
args?: string;
|
65
69
|
metadata?: string;
|
package/lib/ts/src/api/api.js
CHANGED
@@ -19,10 +19,11 @@ class zkCloudWorkerClient {
|
|
19
19
|
* Constructor for the API class
|
20
20
|
* @param jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
|
21
21
|
*/
|
22
|
-
constructor(
|
23
|
-
|
22
|
+
constructor(params) {
|
23
|
+
const { jwt, zkcloudworker, chain } = params;
|
24
24
|
this.jwt = jwt;
|
25
25
|
this.endpoint = ZKCLOUDWORKER_API;
|
26
|
+
this.chain = chain ?? "berkeley";
|
26
27
|
if (jwt === "local") {
|
27
28
|
if (zkcloudworker === undefined)
|
28
29
|
throw new Error("worker is required for local mode");
|
@@ -222,8 +223,11 @@ class zkCloudWorkerClient {
|
|
222
223
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
223
224
|
) {
|
224
225
|
if (this.jwt === "local") {
|
226
|
+
if (this.localWorker === undefined)
|
227
|
+
throw new Error("localWorker is undefined");
|
225
228
|
switch (command) {
|
226
229
|
case "recursiveProof": {
|
230
|
+
console.log("calculating recursive proof locally...");
|
227
231
|
const timeCreated = Date.now();
|
228
232
|
const jobId = this.generateJobId();
|
229
233
|
const job = {
|
@@ -243,7 +247,11 @@ class zkCloudWorkerClient {
|
|
243
247
|
jobStatus: "started",
|
244
248
|
maxAttempts: 0,
|
245
249
|
};
|
246
|
-
const cloud = new local_1.LocalCloud({
|
250
|
+
const cloud = new local_1.LocalCloud({
|
251
|
+
job,
|
252
|
+
chain: this.chain,
|
253
|
+
localWorker: this.localWorker,
|
254
|
+
});
|
247
255
|
const worker = await this.localWorker(cloud);
|
248
256
|
if (worker === undefined)
|
249
257
|
throw new Error("worker is undefined");
|
@@ -252,16 +260,22 @@ class zkCloudWorkerClient {
|
|
252
260
|
data,
|
253
261
|
});
|
254
262
|
job.timeFinished = Date.now();
|
255
|
-
job.jobStatus = "finished";
|
256
|
-
job.result = proof;
|
257
263
|
job.maxAttempts = 1;
|
258
|
-
|
264
|
+
if (proof !== undefined) {
|
265
|
+
job.jobStatus = "finished";
|
266
|
+
job.result = proof;
|
267
|
+
}
|
268
|
+
else {
|
269
|
+
job.jobStatus = "failed";
|
270
|
+
}
|
271
|
+
local_1.LocalStorage.jobs[jobId] = job;
|
259
272
|
return {
|
260
273
|
success: true,
|
261
274
|
data: jobId,
|
262
275
|
};
|
263
276
|
}
|
264
277
|
case "execute": {
|
278
|
+
console.log("executing locally...");
|
265
279
|
const timeCreated = Date.now();
|
266
280
|
const jobId = this.generateJobId();
|
267
281
|
const job = {
|
@@ -280,23 +294,32 @@ class zkCloudWorkerClient {
|
|
280
294
|
jobStatus: "started",
|
281
295
|
maxAttempts: 0,
|
282
296
|
};
|
283
|
-
const cloud = new local_1.LocalCloud({
|
297
|
+
const cloud = new local_1.LocalCloud({
|
298
|
+
job,
|
299
|
+
chain: this.chain,
|
300
|
+
localWorker: this.localWorker,
|
301
|
+
});
|
284
302
|
const worker = await this.localWorker(cloud);
|
285
303
|
if (worker === undefined)
|
286
304
|
throw new Error("worker is undefined");
|
287
305
|
const result = await worker.execute();
|
288
306
|
job.timeFinished = Date.now();
|
289
|
-
job.jobStatus = "finished";
|
290
|
-
job.result = result;
|
291
307
|
job.maxAttempts = 1;
|
292
|
-
|
308
|
+
if (result !== undefined) {
|
309
|
+
job.jobStatus = "finished";
|
310
|
+
job.result = result;
|
311
|
+
}
|
312
|
+
else {
|
313
|
+
job.jobStatus = "failed";
|
314
|
+
}
|
315
|
+
local_1.LocalStorage.jobs[jobId] = job;
|
293
316
|
return {
|
294
317
|
success: true,
|
295
318
|
data: jobId,
|
296
319
|
};
|
297
320
|
}
|
298
321
|
case "jobResult": {
|
299
|
-
const job =
|
322
|
+
const job = local_1.LocalStorage.jobs[data.jobId];
|
300
323
|
if (job === undefined) {
|
301
324
|
return {
|
302
325
|
success: false,
|
@@ -333,6 +356,7 @@ class zkCloudWorkerClient {
|
|
333
356
|
command: command,
|
334
357
|
jwtToken: this.jwt,
|
335
358
|
data: data,
|
359
|
+
chain: this.chain,
|
336
360
|
};
|
337
361
|
try {
|
338
362
|
const response = await axios_1.default.post(this.endpoint, apiData);
|
@@ -16,6 +16,7 @@ export declare abstract class Cloud {
|
|
16
16
|
readonly userId?: string;
|
17
17
|
readonly args?: string;
|
18
18
|
readonly metadata?: string;
|
19
|
+
readonly chain: blockchain;
|
19
20
|
readonly isLocalCloud: boolean;
|
20
21
|
constructor(params: {
|
21
22
|
jobId: string;
|
@@ -28,6 +29,7 @@ export declare abstract class Cloud {
|
|
28
29
|
args?: string;
|
29
30
|
metadata?: string;
|
30
31
|
isLocalCloud?: boolean;
|
32
|
+
chain: blockchain;
|
31
33
|
});
|
32
34
|
abstract getDeployer(): Promise<PrivateKey>;
|
33
35
|
abstract log(msg: string): void;
|
@@ -36,12 +38,40 @@ export declare abstract class Cloud {
|
|
36
38
|
abstract saveFile(filename: string, value: Buffer): Promise<void>;
|
37
39
|
abstract loadFile(filename: string): Promise<Buffer | undefined>;
|
38
40
|
abstract loadEnvironment(password: string): Promise<void>;
|
41
|
+
abstract recursiveProof(data: {
|
42
|
+
transactions: string[];
|
43
|
+
task?: string;
|
44
|
+
userId?: string;
|
45
|
+
args?: string;
|
46
|
+
metadata?: string;
|
47
|
+
}): Promise<string>;
|
48
|
+
abstract execute(data: {
|
49
|
+
task: string;
|
50
|
+
userId?: string;
|
51
|
+
args?: string;
|
52
|
+
metadata?: string;
|
53
|
+
}): Promise<string>;
|
54
|
+
abstract addTask(data: {
|
55
|
+
task: string;
|
56
|
+
userId?: string;
|
57
|
+
args?: string;
|
58
|
+
metadata?: string;
|
59
|
+
}): Promise<string>;
|
60
|
+
abstract deleteTask(taskId: string): Promise<void>;
|
61
|
+
abstract processTasks(): Promise<void>;
|
62
|
+
}
|
63
|
+
export interface CloudTransaction {
|
64
|
+
txId: string;
|
65
|
+
transaction: string;
|
66
|
+
timeReceived: number;
|
39
67
|
}
|
40
68
|
export declare abstract class zkCloudWorker {
|
41
69
|
readonly cloud: Cloud;
|
42
70
|
constructor(cloud: Cloud);
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
71
|
+
deployedContracts(): Promise<DeployedSmartContract[]>;
|
72
|
+
create(transaction: string): Promise<string | undefined>;
|
73
|
+
merge(proof1: string, proof2: string): Promise<string | undefined>;
|
74
|
+
execute(): Promise<string | undefined>;
|
75
|
+
processTransactions(transactions: CloudTransaction[]): Promise<void>;
|
76
|
+
task(): Promise<string | undefined>;
|
47
77
|
}
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.zkCloudWorker = exports.Cloud = void 0;
|
4
4
|
class Cloud {
|
5
5
|
constructor(params) {
|
6
|
-
const { jobId, stepId, cache, developer, repo, task, userId, args, metadata, isLocalCloud, } = params;
|
6
|
+
const { jobId, stepId, cache, developer, repo, task, userId, args, metadata, isLocalCloud, chain, } = params;
|
7
7
|
this.jobId = jobId;
|
8
8
|
this.stepId = stepId;
|
9
9
|
this.cache = cache;
|
@@ -14,6 +14,7 @@ class Cloud {
|
|
14
14
|
this.args = args;
|
15
15
|
this.metadata = metadata;
|
16
16
|
this.isLocalCloud = isLocalCloud ?? false;
|
17
|
+
this.chain = chain;
|
17
18
|
}
|
18
19
|
}
|
19
20
|
exports.Cloud = Cloud;
|
@@ -21,5 +22,26 @@ class zkCloudWorker {
|
|
21
22
|
constructor(cloud) {
|
22
23
|
this.cloud = cloud;
|
23
24
|
}
|
25
|
+
// To verify the SmartContract code
|
26
|
+
async deployedContracts() {
|
27
|
+
return [];
|
28
|
+
}
|
29
|
+
// Those methods should be implemented for recursive proofs calculations
|
30
|
+
async create(transaction) {
|
31
|
+
return undefined;
|
32
|
+
}
|
33
|
+
async merge(proof1, proof2) {
|
34
|
+
return undefined;
|
35
|
+
}
|
36
|
+
// Those methods should be implemented for anything except for recursive proofs
|
37
|
+
async execute() {
|
38
|
+
return undefined;
|
39
|
+
}
|
40
|
+
// process the transactions received by the cloud
|
41
|
+
async processTransactions(transactions) { }
|
42
|
+
// process the task defined by the developer
|
43
|
+
async task() {
|
44
|
+
return undefined;
|
45
|
+
}
|
24
46
|
}
|
25
47
|
exports.zkCloudWorker = zkCloudWorker;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
export declare function saveFile(params: {
|
3
|
+
data: any;
|
4
|
+
filename: string;
|
5
|
+
}): Promise<string | undefined>;
|
6
|
+
export declare function loadFile(filename: string): Promise<any>;
|
7
|
+
export declare function saveBinaryFile(params: {
|
8
|
+
data: any;
|
9
|
+
filename: string;
|
10
|
+
}): Promise<string | undefined>;
|
11
|
+
export declare function loadBinaryFile(filename: string): Promise<Buffer | undefined>;
|
@@ -0,0 +1,74 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.loadBinaryFile = exports.saveBinaryFile = exports.loadFile = exports.saveFile = void 0;
|
7
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
8
|
+
async function saveFile(params) {
|
9
|
+
const { data, filename } = params;
|
10
|
+
const folder = "./data/";
|
11
|
+
const name = folder + filename + ".json";
|
12
|
+
try {
|
13
|
+
await promises_1.default.access("./data");
|
14
|
+
}
|
15
|
+
catch (e) {
|
16
|
+
// if not, create it
|
17
|
+
await promises_1.default.mkdir("./data");
|
18
|
+
}
|
19
|
+
try {
|
20
|
+
await promises_1.default.writeFile(name, JSON.stringify(data, null, 2));
|
21
|
+
return name;
|
22
|
+
}
|
23
|
+
catch (e) {
|
24
|
+
console.error(`Error writing file ${name}`);
|
25
|
+
return undefined;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
exports.saveFile = saveFile;
|
29
|
+
async function loadFile(filename) {
|
30
|
+
const name = "./data/" + filename + ".json";
|
31
|
+
try {
|
32
|
+
const filedata = await promises_1.default.readFile(name, "utf8");
|
33
|
+
const data = JSON.parse(filedata);
|
34
|
+
return data;
|
35
|
+
}
|
36
|
+
catch (e) {
|
37
|
+
console.error(`File ${name} does not exist or has wrong format`);
|
38
|
+
return undefined;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
exports.loadFile = loadFile;
|
42
|
+
async function saveBinaryFile(params) {
|
43
|
+
const { data, filename } = params;
|
44
|
+
const folder = "./data/";
|
45
|
+
const name = folder + filename + ".bin";
|
46
|
+
try {
|
47
|
+
await promises_1.default.access("./data");
|
48
|
+
}
|
49
|
+
catch (e) {
|
50
|
+
// if not, create it
|
51
|
+
await promises_1.default.mkdir("./data");
|
52
|
+
}
|
53
|
+
try {
|
54
|
+
await promises_1.default.writeFile(name, data);
|
55
|
+
return name;
|
56
|
+
}
|
57
|
+
catch (e) {
|
58
|
+
console.error(`Error writing file ${name}`);
|
59
|
+
return undefined;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
exports.saveBinaryFile = saveBinaryFile;
|
63
|
+
async function loadBinaryFile(filename) {
|
64
|
+
const name = "./data/" + filename + ".bin";
|
65
|
+
try {
|
66
|
+
const data = await promises_1.default.readFile(name);
|
67
|
+
return data;
|
68
|
+
}
|
69
|
+
catch (e) {
|
70
|
+
console.error(`File ${name} does not exist or has wrong format`);
|
71
|
+
return undefined;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
exports.loadBinaryFile = loadBinaryFile;
|
@@ -2,12 +2,16 @@
|
|
2
2
|
import { Cache, PrivateKey } from "o1js";
|
3
3
|
import { Cloud, zkCloudWorker } from "./cloud";
|
4
4
|
import { JobData } from "./job";
|
5
|
+
import { TaskData } from "./task";
|
6
|
+
import { blockchain } from "../networks";
|
5
7
|
export declare class LocalCloud extends Cloud {
|
6
|
-
|
8
|
+
readonly localWorker: (cloud: Cloud) => Promise<zkCloudWorker>;
|
7
9
|
constructor(params: {
|
8
10
|
job: JobData;
|
11
|
+
chain: blockchain;
|
9
12
|
cache?: Cache;
|
10
13
|
stepId?: string;
|
14
|
+
localWorker: (cloud: Cloud) => Promise<zkCloudWorker>;
|
11
15
|
});
|
12
16
|
getDeployer(): Promise<PrivateKey>;
|
13
17
|
log(msg: string): Promise<void>;
|
@@ -16,6 +20,28 @@ export declare class LocalCloud extends Cloud {
|
|
16
20
|
saveFile(filename: string, value: Buffer): Promise<void>;
|
17
21
|
loadFile(filename: string): Promise<Buffer | undefined>;
|
18
22
|
loadEnvironment(password: string): Promise<void>;
|
23
|
+
private generateId;
|
24
|
+
recursiveProof(data: {
|
25
|
+
transactions: string[];
|
26
|
+
task?: string;
|
27
|
+
userId?: string;
|
28
|
+
args?: string;
|
29
|
+
metadata?: string;
|
30
|
+
}): Promise<string>;
|
31
|
+
execute(data: {
|
32
|
+
task: string;
|
33
|
+
userId?: string;
|
34
|
+
args?: string;
|
35
|
+
metadata?: string;
|
36
|
+
}): Promise<string>;
|
37
|
+
addTask(data: {
|
38
|
+
task: string;
|
39
|
+
userId?: string;
|
40
|
+
args?: string;
|
41
|
+
metadata?: string;
|
42
|
+
}): Promise<string>;
|
43
|
+
deleteTask(taskId: string): Promise<void>;
|
44
|
+
processTasks(): Promise<void>;
|
19
45
|
static sequencer(params: {
|
20
46
|
worker: zkCloudWorker;
|
21
47
|
data: {
|
@@ -29,3 +55,22 @@ export declare class LocalCloud extends Cloud {
|
|
29
55
|
};
|
30
56
|
}): Promise<string>;
|
31
57
|
}
|
58
|
+
export declare class LocalStorage {
|
59
|
+
static jobs: {
|
60
|
+
[key: string]: JobData;
|
61
|
+
};
|
62
|
+
static data: {
|
63
|
+
[key: string]: string;
|
64
|
+
};
|
65
|
+
static transactions: {
|
66
|
+
[key: string]: {
|
67
|
+
transaction: string;
|
68
|
+
timeReceived: number;
|
69
|
+
};
|
70
|
+
};
|
71
|
+
static tasks: {
|
72
|
+
[key: string]: TaskData;
|
73
|
+
};
|
74
|
+
static saveData(name: string): Promise<void>;
|
75
|
+
static loadData(name: string): Promise<void>;
|
76
|
+
}
|
@@ -1,11 +1,13 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.LocalCloud = void 0;
|
3
|
+
exports.LocalStorage = exports.LocalCloud = void 0;
|
4
4
|
const o1js_1 = require("o1js");
|
5
5
|
const cloud_1 = require("./cloud");
|
6
|
+
const mina_1 = require("../mina");
|
7
|
+
const files_1 = require("./files");
|
6
8
|
class LocalCloud extends cloud_1.Cloud {
|
7
9
|
constructor(params) {
|
8
|
-
const { job, cache, stepId } = params;
|
10
|
+
const { job, chain, cache, stepId, localWorker } = params;
|
9
11
|
const { jobId, developer, repo, task, userId, args, metadata } = job;
|
10
12
|
super({
|
11
13
|
jobId: jobId,
|
@@ -18,8 +20,9 @@ class LocalCloud extends cloud_1.Cloud {
|
|
18
20
|
args: args,
|
19
21
|
metadata: metadata,
|
20
22
|
isLocalCloud: true,
|
23
|
+
chain,
|
21
24
|
});
|
22
|
-
this.
|
25
|
+
this.localWorker = localWorker;
|
23
26
|
}
|
24
27
|
async getDeployer() {
|
25
28
|
throw new Error("Method not implemented.");
|
@@ -28,21 +31,157 @@ class LocalCloud extends cloud_1.Cloud {
|
|
28
31
|
console.log("LocalCloud:", msg);
|
29
32
|
}
|
30
33
|
async getDataByKey(key) {
|
31
|
-
const value =
|
34
|
+
const value = LocalStorage.data[key];
|
32
35
|
return value;
|
33
36
|
}
|
34
37
|
async saveDataByKey(key, value) {
|
35
|
-
|
38
|
+
LocalStorage.data[key] = value;
|
36
39
|
}
|
37
40
|
async saveFile(filename, value) {
|
38
|
-
|
41
|
+
await (0, files_1.saveBinaryFile)({ data: value, filename });
|
39
42
|
}
|
40
43
|
async loadFile(filename) {
|
41
|
-
|
44
|
+
const data = await (0, files_1.loadBinaryFile)(filename);
|
45
|
+
return data;
|
42
46
|
}
|
43
47
|
async loadEnvironment(password) {
|
44
48
|
throw new Error("Method not implemented.");
|
45
49
|
}
|
50
|
+
generateId() {
|
51
|
+
return "local." + Date.now().toString() + "." + (0, mina_1.makeString)(32);
|
52
|
+
}
|
53
|
+
async recursiveProof(data) {
|
54
|
+
console.log("calculating recursive proof locally...");
|
55
|
+
const timeCreated = Date.now();
|
56
|
+
const jobId = this.generateId();
|
57
|
+
const job = {
|
58
|
+
id: "local",
|
59
|
+
jobId: jobId,
|
60
|
+
developer: this.developer,
|
61
|
+
repo: this.repo,
|
62
|
+
task: data.task,
|
63
|
+
userId: data.userId,
|
64
|
+
args: data.args,
|
65
|
+
metadata: data.metadata,
|
66
|
+
filename: "recursiveProof.json",
|
67
|
+
txNumber: data.transactions.length,
|
68
|
+
timeCreated,
|
69
|
+
timeCreatedString: new Date(timeCreated).toISOString(),
|
70
|
+
timeStarted: timeCreated,
|
71
|
+
jobStatus: "started",
|
72
|
+
maxAttempts: 0,
|
73
|
+
};
|
74
|
+
const cloud = new LocalCloud({
|
75
|
+
job,
|
76
|
+
chain: this.chain,
|
77
|
+
localWorker: this.localWorker,
|
78
|
+
});
|
79
|
+
const worker = await this.localWorker(cloud);
|
80
|
+
if (worker === undefined)
|
81
|
+
throw new Error("worker is undefined");
|
82
|
+
const proof = await LocalCloud.sequencer({
|
83
|
+
worker,
|
84
|
+
data: { ...data, developer: this.developer, repo: this.repo },
|
85
|
+
});
|
86
|
+
job.timeFinished = Date.now();
|
87
|
+
job.jobStatus = "finished";
|
88
|
+
job.result = proof;
|
89
|
+
job.maxAttempts = 1;
|
90
|
+
LocalStorage.jobs[jobId] = job;
|
91
|
+
return jobId;
|
92
|
+
}
|
93
|
+
async execute(data) {
|
94
|
+
console.log("executing locally...");
|
95
|
+
const timeCreated = Date.now();
|
96
|
+
const jobId = this.generateId();
|
97
|
+
const job = {
|
98
|
+
id: "local",
|
99
|
+
jobId: jobId,
|
100
|
+
developer: this.developer,
|
101
|
+
repo: this.repo,
|
102
|
+
task: data.task,
|
103
|
+
userId: data.userId,
|
104
|
+
args: data.args,
|
105
|
+
metadata: data.metadata,
|
106
|
+
txNumber: 1,
|
107
|
+
timeCreated,
|
108
|
+
timeCreatedString: new Date(timeCreated).toISOString(),
|
109
|
+
timeStarted: timeCreated,
|
110
|
+
jobStatus: "started",
|
111
|
+
maxAttempts: 0,
|
112
|
+
};
|
113
|
+
const cloud = new LocalCloud({
|
114
|
+
job,
|
115
|
+
chain: this.chain,
|
116
|
+
localWorker: this.localWorker,
|
117
|
+
});
|
118
|
+
const worker = await this.localWorker(cloud);
|
119
|
+
if (worker === undefined)
|
120
|
+
throw new Error("worker is undefined");
|
121
|
+
const result = await worker.execute();
|
122
|
+
job.timeFinished = Date.now();
|
123
|
+
job.jobStatus = "finished";
|
124
|
+
job.result = result;
|
125
|
+
job.maxAttempts = 1;
|
126
|
+
LocalStorage.jobs[jobId] = job;
|
127
|
+
return jobId;
|
128
|
+
}
|
129
|
+
async addTask(data) {
|
130
|
+
const taskId = this.generateId();
|
131
|
+
LocalStorage.tasks[taskId] = {
|
132
|
+
...data,
|
133
|
+
id: "local",
|
134
|
+
taskId,
|
135
|
+
developer: this.developer,
|
136
|
+
repo: this.repo,
|
137
|
+
};
|
138
|
+
return taskId;
|
139
|
+
}
|
140
|
+
async deleteTask(taskId) {
|
141
|
+
delete LocalStorage.tasks[taskId];
|
142
|
+
}
|
143
|
+
async processTasks() {
|
144
|
+
for (const taskId in LocalStorage.tasks) {
|
145
|
+
const data = LocalStorage.tasks[taskId];
|
146
|
+
const jobId = this.generateId();
|
147
|
+
const timeCreated = Date.now();
|
148
|
+
const job = {
|
149
|
+
id: "local",
|
150
|
+
jobId: jobId,
|
151
|
+
developer: this.developer,
|
152
|
+
repo: this.repo,
|
153
|
+
task: data.task,
|
154
|
+
userId: data.userId,
|
155
|
+
args: data.args,
|
156
|
+
metadata: data.metadata,
|
157
|
+
txNumber: 1,
|
158
|
+
timeCreated: timeCreated,
|
159
|
+
timeCreatedString: new Date(timeCreated).toISOString(),
|
160
|
+
timeStarted: Date.now(),
|
161
|
+
jobStatus: "started",
|
162
|
+
maxAttempts: 0,
|
163
|
+
};
|
164
|
+
const cloud = new LocalCloud({
|
165
|
+
job,
|
166
|
+
chain: this.chain,
|
167
|
+
localWorker: this.localWorker,
|
168
|
+
});
|
169
|
+
const worker = await this.localWorker(cloud);
|
170
|
+
console.log("Executing task", { taskId, data });
|
171
|
+
const result = await worker.task();
|
172
|
+
job.timeFinished = Date.now();
|
173
|
+
job.maxAttempts = 1;
|
174
|
+
job.billedDuration = job.timeFinished - timeCreated;
|
175
|
+
if (result !== undefined) {
|
176
|
+
job.jobStatus = "finished";
|
177
|
+
job.result = result;
|
178
|
+
}
|
179
|
+
else {
|
180
|
+
job.jobStatus = "failed";
|
181
|
+
}
|
182
|
+
LocalStorage.jobs[jobId] = job;
|
183
|
+
}
|
184
|
+
}
|
46
185
|
static async sequencer(params) {
|
47
186
|
const { worker, data } = params;
|
48
187
|
const { transactions } = data;
|
@@ -66,3 +205,30 @@ class LocalCloud extends cloud_1.Cloud {
|
|
66
205
|
}
|
67
206
|
}
|
68
207
|
exports.LocalCloud = LocalCloud;
|
208
|
+
class LocalStorage {
|
209
|
+
static async saveData(name) {
|
210
|
+
const data = {
|
211
|
+
jobs: LocalStorage.jobs,
|
212
|
+
data: LocalStorage.data,
|
213
|
+
transactions: LocalStorage.transactions,
|
214
|
+
tasks: LocalStorage.tasks,
|
215
|
+
};
|
216
|
+
const filename = name + ".cloud";
|
217
|
+
await (0, files_1.saveFile)({ data, filename });
|
218
|
+
}
|
219
|
+
static async loadData(name) {
|
220
|
+
const filename = name + ".cloud";
|
221
|
+
const data = await (0, files_1.loadFile)(filename);
|
222
|
+
if (data === undefined)
|
223
|
+
return;
|
224
|
+
LocalStorage.jobs = data.jobs;
|
225
|
+
LocalStorage.data = data.data;
|
226
|
+
LocalStorage.transactions = data.transactions;
|
227
|
+
LocalStorage.tasks = data.tasks;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
exports.LocalStorage = LocalStorage;
|
231
|
+
LocalStorage.jobs = {};
|
232
|
+
LocalStorage.data = {};
|
233
|
+
LocalStorage.transactions = {};
|
234
|
+
LocalStorage.tasks = {};
|