zkcloudworker 0.2.9 → 0.2.11

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
@@ -63,6 +64,7 @@ export declare class zkCloudWorkerClient {
63
64
  execute(data: {
64
65
  developer: string;
65
66
  repo: string;
67
+ transactions: string[];
66
68
  task: string;
67
69
  userId?: string;
68
70
  args?: string;
@@ -227,92 +227,24 @@ class zkCloudWorkerClient {
227
227
  throw new Error("localWorker is undefined");
228
228
  switch (command) {
229
229
  case "recursiveProof": {
230
- console.log("calculating recursive proof locally...");
231
- const timeCreated = Date.now();
232
- const jobId = this.generateJobId();
233
- const job = {
234
- id: "local",
235
- jobId: jobId,
236
- developer: data.developer,
237
- repo: data.repo,
238
- task: data.task,
239
- userId: data.userId,
240
- args: data.args,
241
- metadata: data.metadata,
242
- filename: "recursiveProof.json",
243
- txNumber: data.transactions.length,
244
- timeCreated,
245
- timeCreatedString: new Date(timeCreated).toISOString(),
246
- timeStarted: timeCreated,
247
- jobStatus: "started",
248
- maxAttempts: 0,
249
- };
250
- const cloud = new local_1.LocalCloud({
251
- job,
230
+ const jobId = await local_1.LocalCloud.run({
231
+ command: "recursiveProof",
232
+ data,
252
233
  chain: this.chain,
253
234
  localWorker: this.localWorker,
254
235
  });
255
- const worker = await this.localWorker(cloud);
256
- if (worker === undefined)
257
- throw new Error("worker is undefined");
258
- const proof = await local_1.LocalCloud.sequencer({
259
- worker,
260
- data,
261
- });
262
- job.timeFinished = Date.now();
263
- job.maxAttempts = 1;
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;
272
236
  return {
273
237
  success: true,
274
238
  data: jobId,
275
239
  };
276
240
  }
277
241
  case "execute": {
278
- console.log("executing locally...");
279
- const timeCreated = Date.now();
280
- const jobId = this.generateJobId();
281
- const job = {
282
- id: "local",
283
- jobId: jobId,
284
- developer: data.developer,
285
- repo: data.repo,
286
- task: data.task,
287
- userId: data.userId,
288
- args: data.args,
289
- metadata: data.metadata,
290
- txNumber: 1,
291
- timeCreated,
292
- timeCreatedString: new Date(timeCreated).toISOString(),
293
- timeStarted: timeCreated,
294
- jobStatus: "started",
295
- maxAttempts: 0,
296
- };
297
- const cloud = new local_1.LocalCloud({
298
- job,
242
+ const jobId = await local_1.LocalCloud.run({
243
+ command: "execute",
244
+ data,
299
245
  chain: this.chain,
300
246
  localWorker: this.localWorker,
301
247
  });
302
- const worker = await this.localWorker(cloud);
303
- if (worker === undefined)
304
- throw new Error("worker is undefined");
305
- const result = await worker.execute();
306
- job.timeFinished = Date.now();
307
- job.maxAttempts = 1;
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;
316
248
  return {
317
249
  success: true,
318
250
  data: jobId,
@@ -31,7 +31,7 @@ export declare abstract class Cloud {
31
31
  isLocalCloud?: boolean;
32
32
  chain: blockchain;
33
33
  });
34
- abstract getDeployer(): Promise<PrivateKey>;
34
+ abstract getDeployer(): Promise<PrivateKey | undefined>;
35
35
  abstract log(msg: string): void;
36
36
  abstract getDataByKey(key: string): Promise<string | undefined>;
37
37
  abstract saveDataByKey(key: string, value: string): Promise<void>;
@@ -46,6 +46,7 @@ export declare abstract class Cloud {
46
46
  metadata?: string;
47
47
  }): Promise<string>;
48
48
  abstract execute(data: {
49
+ transactions: string[];
49
50
  task: string;
50
51
  userId?: string;
51
52
  args?: string;
@@ -71,7 +72,7 @@ export declare abstract class zkCloudWorker {
71
72
  deployedContracts(): Promise<DeployedSmartContract[]>;
72
73
  create(transaction: string): Promise<string | undefined>;
73
74
  merge(proof1: string, proof2: string): Promise<string | undefined>;
74
- execute(): Promise<string | undefined>;
75
+ execute(transactions: string[]): Promise<string | undefined>;
75
76
  processTransactions(transactions: CloudTransaction[]): Promise<void>;
76
77
  task(): Promise<string | undefined>;
77
78
  }
@@ -34,7 +34,7 @@ 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
@@ -4,6 +4,7 @@ import { Cloud, zkCloudWorker } from "./cloud";
4
4
  import { JobData } from "./job";
5
5
  import { TaskData } from "./task";
6
6
  import { blockchain } from "../networks";
7
+ import { ApiCommand } from "../api/api";
7
8
  export declare class LocalCloud extends Cloud {
8
9
  readonly localWorker: (cloud: Cloud) => Promise<zkCloudWorker>;
9
10
  constructor(params: {
@@ -13,14 +14,28 @@ export declare class LocalCloud extends Cloud {
13
14
  stepId?: string;
14
15
  localWorker: (cloud: Cloud) => Promise<zkCloudWorker>;
15
16
  });
16
- getDeployer(): Promise<PrivateKey>;
17
+ getDeployer(): Promise<PrivateKey | undefined>;
17
18
  log(msg: string): Promise<void>;
18
19
  getDataByKey(key: string): Promise<string | undefined>;
19
20
  saveDataByKey(key: string, value: string): Promise<void>;
20
21
  saveFile(filename: string, value: Buffer): Promise<void>;
21
22
  loadFile(filename: string): Promise<Buffer | undefined>;
22
23
  loadEnvironment(password: string): Promise<void>;
23
- private generateId;
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>;
24
39
  recursiveProof(data: {
25
40
  transactions: string[];
26
41
  task?: string;
@@ -29,6 +44,7 @@ export declare class LocalCloud extends Cloud {
29
44
  metadata?: string;
30
45
  }): Promise<string>;
31
46
  execute(data: {
47
+ transactions: string[];
32
48
  task: string;
33
49
  userId?: string;
34
50
  args?: string;
@@ -25,7 +25,7 @@ class LocalCloud extends cloud_1.Cloud {
25
25
  this.localWorker = localWorker;
26
26
  }
27
27
  async getDeployer() {
28
- throw new Error("Method not implemented.");
28
+ return (0, mina_1.getDeployer)();
29
29
  }
30
30
  async log(msg) {
31
31
  console.log("LocalCloud:", msg);
@@ -47,24 +47,25 @@ class LocalCloud extends cloud_1.Cloud {
47
47
  async loadEnvironment(password) {
48
48
  throw new Error("Method not implemented.");
49
49
  }
50
- generateId() {
50
+ static generateId() {
51
51
  return "local." + Date.now().toString() + "." + (0, mina_1.makeString)(32);
52
52
  }
53
- async recursiveProof(data) {
54
- console.log("calculating recursive proof locally...");
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;
55
57
  const timeCreated = Date.now();
56
- const jobId = this.generateId();
58
+ const jobId = LocalCloud.generateId();
57
59
  const job = {
58
60
  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,
61
+ jobId,
62
+ developer,
63
+ repo,
64
+ task,
65
+ userId,
66
+ args,
67
+ metadata,
68
+ txNumber: command === "recursiveProof" ? transactions.length : 1,
68
69
  timeCreated,
69
70
  timeCreatedString: new Date(timeCreated).toISOString(),
70
71
  timeStarted: timeCreated,
@@ -73,61 +74,69 @@ class LocalCloud extends cloud_1.Cloud {
73
74
  };
74
75
  const cloud = new LocalCloud({
75
76
  job,
76
- chain: this.chain,
77
- localWorker: this.localWorker,
77
+ chain,
78
+ localWorker,
78
79
  });
79
- const worker = await this.localWorker(cloud);
80
+ const worker = await localWorker(cloud);
80
81
  if (worker === undefined)
81
82
  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;
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
+ }
89
101
  job.maxAttempts = 1;
102
+ job.billedDuration = timeFinished - timeCreated;
90
103
  LocalStorage.jobs[jobId] = job;
91
104
  return jobId;
92
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
+ }
93
122
  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,
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
+ },
115
134
  chain: this.chain,
116
135
  localWorker: this.localWorker,
117
136
  });
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
137
  }
129
138
  async addTask(data) {
130
- const taskId = this.generateId();
139
+ const taskId = LocalCloud.generateId();
131
140
  LocalStorage.tasks[taskId] = {
132
141
  ...data,
133
142
  id: "local",
@@ -143,7 +152,7 @@ class LocalCloud extends cloud_1.Cloud {
143
152
  async processTasks() {
144
153
  for (const taskId in LocalStorage.tasks) {
145
154
  const data = LocalStorage.tasks[taskId];
146
- const jobId = this.generateId();
155
+ const jobId = LocalCloud.generateId();
147
156
  const timeCreated = Date.now();
148
157
  const job = {
149
158
  id: "local",
@@ -1,4 +1,4 @@
1
- export { initBlockchain, Memory, makeString, sleep, accountBalance, accountBalanceMina, formatTime, MinaNetworkInstance, currentNetwork, getNetworkIdHash, };
1
+ export { initBlockchain, Memory, makeString, sleep, accountBalance, accountBalanceMina, formatTime, MinaNetworkInstance, currentNetwork, getNetworkIdHash, getDeployer, };
2
2
  import { PublicKey, PrivateKey, UInt64, Field } from "o1js";
3
3
  import { blockchain, MinaNetwork } from "./networks";
4
4
  interface MinaNetworkInstance {
@@ -11,6 +11,7 @@ interface MinaNetworkInstance {
11
11
  }
12
12
  declare let currentNetwork: MinaNetworkInstance | undefined;
13
13
  declare function getNetworkIdHash(): Field;
14
+ declare function getDeployer(): PrivateKey;
14
15
  declare function initBlockchain(instance: blockchain, deployersNumber?: number): MinaNetworkInstance;
15
16
  declare function accountBalance(address: PublicKey): Promise<UInt64>;
16
17
  declare function accountBalanceMina(address: PublicKey): Promise<number>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNetworkIdHash = exports.currentNetwork = exports.formatTime = exports.accountBalanceMina = exports.accountBalance = exports.sleep = exports.makeString = exports.Memory = exports.initBlockchain = void 0;
3
+ exports.getDeployer = exports.getNetworkIdHash = exports.currentNetwork = exports.formatTime = exports.accountBalanceMina = exports.accountBalance = exports.sleep = exports.makeString = exports.Memory = exports.initBlockchain = void 0;
4
4
  const o1js_1 = require("o1js");
5
5
  const networks_1 = require("./networks");
6
6
  let currentNetwork = undefined;
@@ -12,6 +12,13 @@ function getNetworkIdHash() {
12
12
  return currentNetwork.networkIdHash;
13
13
  }
14
14
  exports.getNetworkIdHash = getNetworkIdHash;
15
+ function getDeployer() {
16
+ if (currentNetwork === undefined) {
17
+ throw new Error("Network is not initialized");
18
+ }
19
+ return currentNetwork.keys[0].privateKey;
20
+ }
21
+ exports.getDeployer = getDeployer;
15
22
  /*function getNetworkIdHash(params: {
16
23
  chainId?: blockchain;
17
24
  verbose?: boolean;