zkcloudworker 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { zkCloudWorker, Cloud } from "../cloud/cloud";
2
+ import { blockchain } from "../networks";
2
3
  /**
3
4
  * API class for interacting with the zkCloudWorker
4
5
  * @property jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
@@ -7,13 +8,17 @@ import { zkCloudWorker, Cloud } from "../cloud/cloud";
7
8
  export declare class zkCloudWorkerClient {
8
9
  readonly jwt: string;
9
10
  readonly endpoint: string;
10
- readonly localJobs: Map<string, string>;
11
+ readonly chain: blockchain;
11
12
  readonly localWorker: (cloud: Cloud) => Promise<zkCloudWorker> | undefined;
12
13
  /**
13
14
  * Constructor for the API class
14
15
  * @param jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
15
16
  */
16
- constructor(jwt: string, zkcloudworker?: ((cloud: Cloud) => Promise<zkCloudWorker>) | undefined);
17
+ constructor(params: {
18
+ jwt: string;
19
+ zkcloudworker?: (cloud: Cloud) => Promise<zkCloudWorker>;
20
+ chain?: blockchain;
21
+ });
17
22
  /**
18
23
  * Starts a new job for the proof calculation using serverless api call
19
24
  * The developer and name should correspond to the BackupPlugin of the API
@@ -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(jwt, zkcloudworker = undefined) {
23
- this.localJobs = new Map();
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");
@@ -224,8 +225,27 @@ class zkCloudWorkerClient {
224
225
  if (this.jwt === "local") {
225
226
  switch (command) {
226
227
  case "recursiveProof": {
228
+ console.log("calculating recursive proof locally...");
229
+ const timeCreated = Date.now();
227
230
  const jobId = this.generateJobId();
228
- const cloud = new local_1.LocalCloud({ jobId, ...data });
231
+ const job = {
232
+ id: "local",
233
+ jobId: jobId,
234
+ developer: data.developer,
235
+ repo: data.repo,
236
+ task: data.task,
237
+ userId: data.userId,
238
+ args: data.args,
239
+ metadata: data.metadata,
240
+ filename: "recursiveProof.json",
241
+ txNumber: data.transactions.length,
242
+ timeCreated,
243
+ timeCreatedString: new Date(timeCreated).toISOString(),
244
+ timeStarted: timeCreated,
245
+ jobStatus: "started",
246
+ maxAttempts: 0,
247
+ };
248
+ const cloud = new local_1.LocalCloud({ job, chain: this.chain });
229
249
  const worker = await this.localWorker(cloud);
230
250
  if (worker === undefined)
231
251
  throw new Error("worker is undefined");
@@ -233,38 +253,66 @@ class zkCloudWorkerClient {
233
253
  worker,
234
254
  data,
235
255
  });
236
- this.localJobs.set(jobId, proof);
256
+ job.timeFinished = Date.now();
257
+ job.jobStatus = "finished";
258
+ job.result = proof;
259
+ job.maxAttempts = 1;
260
+ local_1.LocalStorage.jobs[jobId] = job;
237
261
  return {
238
262
  success: true,
239
263
  data: jobId,
240
264
  };
241
265
  }
242
- case "execute":
266
+ case "execute": {
267
+ console.log("executing locally...");
268
+ const timeCreated = Date.now();
269
+ const jobId = this.generateJobId();
270
+ const job = {
271
+ id: "local",
272
+ jobId: jobId,
273
+ developer: data.developer,
274
+ repo: data.repo,
275
+ task: data.task,
276
+ userId: data.userId,
277
+ args: data.args,
278
+ metadata: data.metadata,
279
+ txNumber: 1,
280
+ timeCreated,
281
+ timeCreatedString: new Date(timeCreated).toISOString(),
282
+ timeStarted: timeCreated,
283
+ jobStatus: "started",
284
+ maxAttempts: 0,
285
+ };
286
+ const cloud = new local_1.LocalCloud({ job, chain: this.chain });
287
+ const worker = await this.localWorker(cloud);
288
+ if (worker === undefined)
289
+ throw new Error("worker is undefined");
290
+ const result = await worker.execute();
291
+ job.timeFinished = Date.now();
292
+ job.jobStatus = "finished";
293
+ job.result = result;
294
+ job.maxAttempts = 1;
295
+ local_1.LocalStorage.jobs[jobId] = job;
243
296
  return {
244
297
  success: true,
245
- data: "local_execute",
298
+ data: jobId,
246
299
  };
247
- case "jobResult":
248
- const result = this.localJobs.get(data.jobId);
249
- if (result === undefined) {
300
+ }
301
+ case "jobResult": {
302
+ const job = local_1.LocalStorage.jobs[data.jobId];
303
+ if (job === undefined) {
250
304
  return {
251
- success: true,
252
- data: {
253
- success: false,
254
- error: "local job not found",
255
- },
305
+ success: false,
306
+ error: "local job not found",
256
307
  };
257
308
  }
258
309
  else {
259
310
  return {
260
311
  success: true,
261
- data: {
262
- result,
263
- jobId: data.jobId,
264
- jobStatus: "finished",
265
- },
312
+ data: job,
266
313
  };
267
314
  }
315
+ }
268
316
  case "deploy":
269
317
  return {
270
318
  success: true,
@@ -288,6 +336,7 @@ class zkCloudWorkerClient {
288
336
  command: command,
289
337
  jwtToken: this.jwt,
290
338
  data: data,
339
+ chain: this.chain,
291
340
  };
292
341
  try {
293
342
  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;
@@ -37,11 +39,18 @@ export declare abstract class Cloud {
37
39
  abstract loadFile(filename: string): Promise<Buffer | undefined>;
38
40
  abstract loadEnvironment(password: string): Promise<void>;
39
41
  }
42
+ export interface CloudTransaction {
43
+ txId: string;
44
+ transaction: string;
45
+ timeReceived: number;
46
+ }
40
47
  export declare abstract class zkCloudWorker {
41
48
  readonly cloud: Cloud;
42
49
  constructor(cloud: Cloud);
43
- abstract deployedContracts(): Promise<DeployedSmartContract[]>;
44
- abstract create(transaction: string): Promise<string | undefined>;
45
- abstract merge(proof1: string, proof2: string): Promise<string | undefined>;
46
- abstract execute(): Promise<string | undefined>;
50
+ deployedContracts(): Promise<DeployedSmartContract[]>;
51
+ create(transaction: string): Promise<string | undefined>;
52
+ merge(proof1: string, proof2: string): Promise<string | undefined>;
53
+ execute(): Promise<string | undefined>;
54
+ processTransactions(transactions: CloudTransaction[]): Promise<void>;
55
+ task(data: string): Promise<void>;
47
56
  }
@@ -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,24 @@ 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(data) { }
24
44
  }
25
45
  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;
@@ -0,0 +1,23 @@
1
+ export type JobStatus = "created" | "started" | "finished" | "failed" | "used";
2
+ export interface JobData {
3
+ id: string;
4
+ jobId: string;
5
+ developer: string;
6
+ repo: string;
7
+ task?: string;
8
+ userId?: string;
9
+ args?: string;
10
+ metadata?: string;
11
+ filename?: string;
12
+ txNumber: number;
13
+ timeCreated: number;
14
+ timeCreatedString: string;
15
+ timeStarted?: number;
16
+ timeFinished?: number;
17
+ timeFailed?: number;
18
+ timeUsed?: number;
19
+ billedDuration?: number;
20
+ jobStatus: JobStatus;
21
+ maxAttempts: number;
22
+ result?: string;
23
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,18 +1,14 @@
1
1
  /// <reference types="node" />
2
2
  import { Cache, PrivateKey } from "o1js";
3
3
  import { Cloud, zkCloudWorker } from "./cloud";
4
+ import { JobData } from "./job";
5
+ import { blockchain } from "../networks";
4
6
  export declare class LocalCloud extends Cloud {
5
- data: Map<string, string>;
6
7
  constructor(params: {
7
- jobId: string;
8
- stepId?: string;
8
+ job: JobData;
9
+ chain: blockchain;
9
10
  cache?: Cache;
10
- developer: string;
11
- repo: string;
12
- task?: string;
13
- userId?: string;
14
- args?: string;
15
- metadata?: string;
11
+ stepId?: string;
16
12
  });
17
13
  getDeployer(): Promise<PrivateKey>;
18
14
  log(msg: string): Promise<void>;
@@ -34,3 +30,22 @@ export declare class LocalCloud extends Cloud {
34
30
  };
35
31
  }): Promise<string>;
36
32
  }
33
+ export declare class LocalStorage {
34
+ static jobs: {
35
+ [key: string]: JobData;
36
+ };
37
+ static data: {
38
+ [key: string]: string;
39
+ };
40
+ static transactions: {
41
+ [key: string]: {
42
+ transaction: string;
43
+ timeReceived: number;
44
+ };
45
+ };
46
+ static tasks: {
47
+ [key: string]: string;
48
+ };
49
+ static saveData(name: string): Promise<void>;
50
+ static loadData(name: string): Promise<void>;
51
+ }
@@ -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 files_1 = require("./files");
6
7
  class LocalCloud extends cloud_1.Cloud {
7
8
  constructor(params) {
8
- const { jobId, stepId, cache, developer, repo, task, userId, args, metadata, } = params;
9
+ const { job, chain, cache, stepId } = params;
10
+ const { jobId, developer, repo, task, userId, args, metadata } = job;
9
11
  super({
10
12
  jobId: jobId,
11
13
  stepId: stepId ?? "stepId",
@@ -17,8 +19,8 @@ class LocalCloud extends cloud_1.Cloud {
17
19
  args: args,
18
20
  metadata: metadata,
19
21
  isLocalCloud: true,
22
+ chain,
20
23
  });
21
- this.data = new Map();
22
24
  }
23
25
  async getDeployer() {
24
26
  throw new Error("Method not implemented.");
@@ -27,17 +29,18 @@ class LocalCloud extends cloud_1.Cloud {
27
29
  console.log("LocalCloud:", msg);
28
30
  }
29
31
  async getDataByKey(key) {
30
- const value = this.data.get(key);
32
+ const value = LocalStorage.data[key];
31
33
  return value;
32
34
  }
33
35
  async saveDataByKey(key, value) {
34
- this.data.set(key, value);
36
+ LocalStorage.data[key] = value;
35
37
  }
36
38
  async saveFile(filename, value) {
37
- throw new Error("Method not implemented.");
39
+ await (0, files_1.saveBinaryFile)({ data: value, filename });
38
40
  }
39
41
  async loadFile(filename) {
40
- throw new Error("Method not implemented.");
42
+ const data = await (0, files_1.loadBinaryFile)(filename);
43
+ return data;
41
44
  }
42
45
  async loadEnvironment(password) {
43
46
  throw new Error("Method not implemented.");
@@ -65,3 +68,30 @@ class LocalCloud extends cloud_1.Cloud {
65
68
  }
66
69
  }
67
70
  exports.LocalCloud = LocalCloud;
71
+ class LocalStorage {
72
+ static async saveData(name) {
73
+ const data = {
74
+ jobs: LocalStorage.jobs,
75
+ data: LocalStorage.data,
76
+ transactions: LocalStorage.transactions,
77
+ tasks: LocalStorage.tasks,
78
+ };
79
+ const filename = name + ".cloud";
80
+ await (0, files_1.saveFile)({ data, filename });
81
+ }
82
+ static async loadData(name) {
83
+ const filename = name + ".cloud";
84
+ const data = await (0, files_1.loadFile)(filename);
85
+ if (data === undefined)
86
+ return;
87
+ LocalStorage.jobs = data.jobs;
88
+ LocalStorage.data = data.data;
89
+ LocalStorage.transactions = data.transactions;
90
+ LocalStorage.tasks = data.tasks;
91
+ }
92
+ }
93
+ exports.LocalStorage = LocalStorage;
94
+ LocalStorage.jobs = {};
95
+ LocalStorage.data = {};
96
+ LocalStorage.transactions = {};
97
+ LocalStorage.tasks = {};
@@ -1,6 +1,7 @@
1
1
  export * from "./api/api";
2
2
  export * from "./cloud/cloud";
3
3
  export * from "./cloud/local";
4
+ export * from "./cloud/job";
4
5
  export * from "./mina";
5
6
  export * from "./fee";
6
7
  export * from "./networks";
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./api/api"), exports);
18
18
  __exportStar(require("./cloud/cloud"), exports);
19
19
  __exportStar(require("./cloud/local"), exports);
20
+ __exportStar(require("./cloud/job"), exports);
20
21
  __exportStar(require("./mina"), exports);
21
22
  __exportStar(require("./fee"), exports);
22
23
  __exportStar(require("./networks"), exports);