zkcloudworker 0.2.8 → 0.2.10

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,5 +1,6 @@
1
1
  import { zkCloudWorker, Cloud } from "../cloud/cloud";
2
2
  import { blockchain } from "../networks";
3
+ export type ApiCommand = "recursiveProof" | "execute" | "jobResult" | "deploy" | "queryBilling";
3
4
  /**
4
5
  * API class for interacting with the zkCloudWorker
5
6
  * @property jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
@@ -9,7 +10,7 @@ export declare class zkCloudWorkerClient {
9
10
  readonly jwt: string;
10
11
  readonly endpoint: string;
11
12
  readonly chain: blockchain;
12
- readonly localWorker: (cloud: Cloud) => Promise<zkCloudWorker> | undefined;
13
+ readonly localWorker?: (cloud: Cloud) => Promise<zkCloudWorker>;
13
14
  /**
14
15
  * Constructor for the API class
15
16
  * @param jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
@@ -63,7 +64,8 @@ export declare class zkCloudWorkerClient {
63
64
  execute(data: {
64
65
  developer: string;
65
66
  repo: string;
66
- task?: string;
67
+ transactions: string[];
68
+ task: string;
67
69
  userId?: string;
68
70
  args?: string;
69
71
  metadata?: string;
@@ -223,76 +223,28 @@ class zkCloudWorkerClient {
223
223
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
224
224
  ) {
225
225
  if (this.jwt === "local") {
226
+ if (this.localWorker === undefined)
227
+ throw new Error("localWorker is undefined");
226
228
  switch (command) {
227
229
  case "recursiveProof": {
228
- console.log("calculating recursive proof locally...");
229
- const timeCreated = Date.now();
230
- const jobId = this.generateJobId();
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 });
249
- const worker = await this.localWorker(cloud);
250
- if (worker === undefined)
251
- throw new Error("worker is undefined");
252
- const proof = await local_1.LocalCloud.sequencer({
253
- worker,
230
+ const jobId = await local_1.LocalCloud.run({
231
+ command: "recursiveProof",
254
232
  data,
233
+ chain: this.chain,
234
+ localWorker: this.localWorker,
255
235
  });
256
- job.timeFinished = Date.now();
257
- job.jobStatus = "finished";
258
- job.result = proof;
259
- job.maxAttempts = 1;
260
- local_1.LocalStorage.jobs[jobId] = job;
261
236
  return {
262
237
  success: true,
263
238
  data: jobId,
264
239
  };
265
240
  }
266
241
  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;
242
+ const jobId = await local_1.LocalCloud.run({
243
+ command: "execute",
244
+ data,
245
+ chain: this.chain,
246
+ localWorker: this.localWorker,
247
+ });
296
248
  return {
297
249
  success: true,
298
250
  data: jobId,
@@ -38,6 +38,28 @@ export declare abstract class Cloud {
38
38
  abstract saveFile(filename: string, value: Buffer): Promise<void>;
39
39
  abstract loadFile(filename: string): Promise<Buffer | undefined>;
40
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
+ transactions: string[];
50
+ task: string;
51
+ userId?: string;
52
+ args?: string;
53
+ metadata?: string;
54
+ }): Promise<string>;
55
+ abstract addTask(data: {
56
+ task: string;
57
+ userId?: string;
58
+ args?: string;
59
+ metadata?: string;
60
+ }): Promise<string>;
61
+ abstract deleteTask(taskId: string): Promise<void>;
62
+ abstract processTasks(): Promise<void>;
41
63
  }
42
64
  export interface CloudTransaction {
43
65
  txId: string;
@@ -50,7 +72,7 @@ export declare abstract class zkCloudWorker {
50
72
  deployedContracts(): Promise<DeployedSmartContract[]>;
51
73
  create(transaction: string): Promise<string | undefined>;
52
74
  merge(proof1: string, proof2: string): Promise<string | undefined>;
53
- execute(): Promise<string | undefined>;
75
+ execute(transactions: string[]): Promise<string | undefined>;
54
76
  processTransactions(transactions: CloudTransaction[]): Promise<void>;
55
- task(data: string): Promise<void>;
77
+ task(): Promise<string | undefined>;
56
78
  }
@@ -34,12 +34,14 @@ class zkCloudWorker {
34
34
  return undefined;
35
35
  }
36
36
  // Those methods should be implemented for anything except for recursive proofs
37
- async execute() {
37
+ async execute(transactions) {
38
38
  return undefined;
39
39
  }
40
40
  // process the transactions received by the cloud
41
41
  async processTransactions(transactions) { }
42
42
  // process the task defined by the developer
43
- async task(data) { }
43
+ async task() {
44
+ return undefined;
45
+ }
44
46
  }
45
47
  exports.zkCloudWorker = zkCloudWorker;
@@ -2,13 +2,17 @@
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";
5
6
  import { blockchain } from "../networks";
7
+ import { ApiCommand } from "../api/api";
6
8
  export declare class LocalCloud extends Cloud {
9
+ readonly localWorker: (cloud: Cloud) => Promise<zkCloudWorker>;
7
10
  constructor(params: {
8
11
  job: JobData;
9
12
  chain: blockchain;
10
13
  cache?: Cache;
11
14
  stepId?: string;
15
+ localWorker: (cloud: Cloud) => Promise<zkCloudWorker>;
12
16
  });
13
17
  getDeployer(): Promise<PrivateKey>;
14
18
  log(msg: string): Promise<void>;
@@ -17,6 +21,43 @@ export declare class LocalCloud extends Cloud {
17
21
  saveFile(filename: string, value: Buffer): Promise<void>;
18
22
  loadFile(filename: string): Promise<Buffer | undefined>;
19
23
  loadEnvironment(password: string): Promise<void>;
24
+ private static generateId;
25
+ static run(params: {
26
+ command: ApiCommand;
27
+ data: {
28
+ developer: string;
29
+ repo: string;
30
+ transactions: string[];
31
+ task: string;
32
+ userId?: string;
33
+ args?: string;
34
+ metadata?: string;
35
+ };
36
+ chain: blockchain;
37
+ localWorker: (cloud: Cloud) => Promise<zkCloudWorker>;
38
+ }): Promise<string>;
39
+ recursiveProof(data: {
40
+ transactions: string[];
41
+ task?: string;
42
+ userId?: string;
43
+ args?: string;
44
+ metadata?: string;
45
+ }): Promise<string>;
46
+ execute(data: {
47
+ transactions: string[];
48
+ task: string;
49
+ userId?: string;
50
+ args?: string;
51
+ metadata?: string;
52
+ }): Promise<string>;
53
+ addTask(data: {
54
+ task: string;
55
+ userId?: string;
56
+ args?: string;
57
+ metadata?: string;
58
+ }): Promise<string>;
59
+ deleteTask(taskId: string): Promise<void>;
60
+ processTasks(): Promise<void>;
20
61
  static sequencer(params: {
21
62
  worker: zkCloudWorker;
22
63
  data: {
@@ -44,7 +85,7 @@ export declare class LocalStorage {
44
85
  };
45
86
  };
46
87
  static tasks: {
47
- [key: string]: string;
88
+ [key: string]: TaskData;
48
89
  };
49
90
  static saveData(name: string): Promise<void>;
50
91
  static loadData(name: string): Promise<void>;
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
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");
6
7
  const files_1 = require("./files");
7
8
  class LocalCloud extends cloud_1.Cloud {
8
9
  constructor(params) {
9
- const { job, chain, cache, stepId } = params;
10
+ const { job, chain, cache, stepId, localWorker } = params;
10
11
  const { jobId, developer, repo, task, userId, args, metadata } = job;
11
12
  super({
12
13
  jobId: jobId,
@@ -21,6 +22,7 @@ class LocalCloud extends cloud_1.Cloud {
21
22
  isLocalCloud: true,
22
23
  chain,
23
24
  });
25
+ this.localWorker = localWorker;
24
26
  }
25
27
  async getDeployer() {
26
28
  throw new Error("Method not implemented.");
@@ -45,6 +47,150 @@ class LocalCloud extends cloud_1.Cloud {
45
47
  async loadEnvironment(password) {
46
48
  throw new Error("Method not implemented.");
47
49
  }
50
+ static generateId() {
51
+ return "local." + Date.now().toString() + "." + (0, mina_1.makeString)(32);
52
+ }
53
+ static async run(params) {
54
+ const { command, data, chain, localWorker } = params;
55
+ console.log("executing locally command", command);
56
+ const { developer, repo, transactions, task, userId, args, metadata } = data;
57
+ const timeCreated = Date.now();
58
+ const jobId = LocalCloud.generateId();
59
+ const job = {
60
+ id: "local",
61
+ jobId,
62
+ developer,
63
+ repo,
64
+ task,
65
+ userId,
66
+ args,
67
+ metadata,
68
+ txNumber: command === "recursiveProof" ? transactions.length : 1,
69
+ timeCreated,
70
+ timeCreatedString: new Date(timeCreated).toISOString(),
71
+ timeStarted: timeCreated,
72
+ jobStatus: "started",
73
+ maxAttempts: 0,
74
+ };
75
+ const cloud = new LocalCloud({
76
+ job,
77
+ chain,
78
+ localWorker,
79
+ });
80
+ const worker = await localWorker(cloud);
81
+ if (worker === undefined)
82
+ throw new Error("worker is undefined");
83
+ const result = command === "recursiveProof"
84
+ ? await LocalCloud.sequencer({
85
+ worker,
86
+ data,
87
+ })
88
+ : command === "execute"
89
+ ? await worker.execute(transactions)
90
+ : undefined;
91
+ const timeFinished = Date.now();
92
+ if (result !== undefined) {
93
+ job.jobStatus = "finished";
94
+ job.timeFinished = timeFinished;
95
+ job.result = result;
96
+ }
97
+ else {
98
+ job.jobStatus = "failed";
99
+ job.timeFailed = timeFinished;
100
+ }
101
+ job.maxAttempts = 1;
102
+ job.billedDuration = timeFinished - timeCreated;
103
+ LocalStorage.jobs[jobId] = job;
104
+ return jobId;
105
+ }
106
+ async recursiveProof(data) {
107
+ return await LocalCloud.run({
108
+ command: "recursiveProof",
109
+ data: {
110
+ developer: this.developer,
111
+ repo: this.repo,
112
+ transactions: data.transactions,
113
+ task: data.task ?? "recursiveProof",
114
+ userId: data.userId,
115
+ args: data.args,
116
+ metadata: data.metadata,
117
+ },
118
+ chain: this.chain,
119
+ localWorker: this.localWorker,
120
+ });
121
+ }
122
+ async execute(data) {
123
+ return await LocalCloud.run({
124
+ command: "execute",
125
+ data: {
126
+ developer: this.developer,
127
+ repo: this.repo,
128
+ transactions: data.transactions,
129
+ task: data.task,
130
+ userId: data.userId,
131
+ args: data.args,
132
+ metadata: data.metadata,
133
+ },
134
+ chain: this.chain,
135
+ localWorker: this.localWorker,
136
+ });
137
+ }
138
+ async addTask(data) {
139
+ const taskId = LocalCloud.generateId();
140
+ LocalStorage.tasks[taskId] = {
141
+ ...data,
142
+ id: "local",
143
+ taskId,
144
+ developer: this.developer,
145
+ repo: this.repo,
146
+ };
147
+ return taskId;
148
+ }
149
+ async deleteTask(taskId) {
150
+ delete LocalStorage.tasks[taskId];
151
+ }
152
+ async processTasks() {
153
+ for (const taskId in LocalStorage.tasks) {
154
+ const data = LocalStorage.tasks[taskId];
155
+ const jobId = LocalCloud.generateId();
156
+ const timeCreated = Date.now();
157
+ const job = {
158
+ id: "local",
159
+ jobId: jobId,
160
+ developer: this.developer,
161
+ repo: this.repo,
162
+ task: data.task,
163
+ userId: data.userId,
164
+ args: data.args,
165
+ metadata: data.metadata,
166
+ txNumber: 1,
167
+ timeCreated: timeCreated,
168
+ timeCreatedString: new Date(timeCreated).toISOString(),
169
+ timeStarted: Date.now(),
170
+ jobStatus: "started",
171
+ maxAttempts: 0,
172
+ };
173
+ const cloud = new LocalCloud({
174
+ job,
175
+ chain: this.chain,
176
+ localWorker: this.localWorker,
177
+ });
178
+ const worker = await this.localWorker(cloud);
179
+ console.log("Executing task", { taskId, data });
180
+ const result = await worker.task();
181
+ job.timeFinished = Date.now();
182
+ job.maxAttempts = 1;
183
+ job.billedDuration = job.timeFinished - timeCreated;
184
+ if (result !== undefined) {
185
+ job.jobStatus = "finished";
186
+ job.result = result;
187
+ }
188
+ else {
189
+ job.jobStatus = "failed";
190
+ }
191
+ LocalStorage.jobs[jobId] = job;
192
+ }
193
+ }
48
194
  static async sequencer(params) {
49
195
  const { worker, data } = params;
50
196
  const { transactions } = data;
@@ -0,0 +1,10 @@
1
+ export interface TaskData {
2
+ id: string;
3
+ taskId: string;
4
+ developer: string;
5
+ repo: string;
6
+ task: string;
7
+ userId?: string;
8
+ args?: string;
9
+ metadata?: string;
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });