-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
692 lines (605 loc) · 25.9 KB
/
runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
import { ethers } from 'ethers';
import * as ipfsClient from './ipfs';
import {
delay,
formatDate,
generateRandomHexOfSize,
generateWallet,
isAddress,
isNullOrEmpty,
parseTransactionBytes
} from './utils';
import { decryptWithPrivateKey, encryptWithCertificate, sha256 } from './crypto';
import EtnyContract from './contract/operation/etnyContract';
import EcldContract from './contract/operation/ecldContract';
import ImageRegistryContract from './contract/operation/imageRegistryContract';
import contractBloxberg from './contract/abi/etnyAbi';
import protocolContractPolygon from './contract/abi/polygonProtocolAbi';
import {
ECEvent,
ECStatus,
ECOrderTaskStatus,
ZERO_CHECKSUM,
ECAddress,
ECError,
ECNetworkName,
ECNetworkNameDictionary,
ECNetworkName1Dictionary
} from './enums';
import PolygonProtocolContract from './contract/operation/polygonProtocolContract';
import BloxbergProtocolContract from './contract/operation/bloxbergProtocolContract';
const { Buffer } = require('buffer/');
const LAST_BLOCKS = 20;
const VERSION = 'v3';
class EthernityCloudRunner extends EventTarget {
#nodeAddress = '';
#challengeHash = '';
#orderNumber = -1;
#doHash = null;
#doRequestId = -1;
#doRequest = -1;
#scriptHash = '';
#fileSetHash = '';
#interval = null;
#orderPlacedTimer = null;
#taskHasBeenPickedForApproval = false;
#getResultFromOrderRepeats = 1;
#runnerType = null;
#networkAddress = null;
#resource = 0;
#enclaveImageIPFSHash = '';
#enclavePublicKey = '';
#enclaveDockerComposeIPFSHash = '';
#tokenContract = null;
#protocolContract = null;
#protocolAbi = null;
#imageRegistryContract = null;
constructor(networkAddress = ECAddress.BLOXBERG.TESTNET_ADDRESS) {
if (!EthernityCloudRunner.instance) {
super();
this.#networkAddress = networkAddress;
switch (networkAddress) {
case ECAddress.BLOXBERG.TESTNET_ADDRESS:
case ECAddress.BLOXBERG.MAINNET_ADDRESS:
this.#tokenContract = new EtnyContract(networkAddress);
this.#protocolContract = new BloxbergProtocolContract(networkAddress);
this.#protocolAbi = contractBloxberg.abi;
break;
case ECAddress.POLYGON.MAINNET_ADDRESS:
this.#tokenContract = new EcldContract(networkAddress);
this.#protocolContract = new PolygonProtocolContract(ECAddress.POLYGON.MAINNET_PROTOCOL_ADDRESS);
this.#protocolAbi = protocolContractPolygon.abi;
break;
case ECAddress.POLYGON.TESTNET_ADDRESS:
this.#tokenContract = new EcldContract(networkAddress);
console.log(ECAddress.POLYGON.TESTNET_PROTOCOL_ADDRESS);
this.#protocolContract = new PolygonProtocolContract(ECAddress.POLYGON.TESTNET_PROTOCOL_ADDRESS);
this.#protocolAbi = protocolContractPolygon.abi;
break;
default:
}
EthernityCloudRunner.instance = this;
}
// eslint-disable-next-line no-constructor-return
return EthernityCloudRunner.instance;
}
#isMainnet = () =>
this.#networkAddress === ECAddress.BLOXBERG.MAINNET_ADDRESS ||
this.#networkAddress === ECAddress.POLYGON.MAINNET_ADDRESS;
#dispatchECEvent = (message, status = ECStatus.DEFAULT, type = ECEvent.TASK_PROGRESS) => {
// Create a new custom event with a custom event name, and pass any data as the event detail
const customEvent = new CustomEvent(type, { detail: { message, status } });
// Dispatch the custom event on the current instance of the class (or any DOM element)
this.dispatchEvent(customEvent);
};
async #getEnclaveDetails() {
const details = await this.#imageRegistryContract.getEnclaveDetailsV3(this.#runnerType, VERSION);
if (details) {
[this.#enclaveImageIPFSHash, this.#enclavePublicKey, this.#enclaveDockerComposeIPFSHash] = details;
this.#dispatchECEvent(`ENCLAVE_IMAGE_IPFS_HASH:${this.#enclaveImageIPFSHash}`);
this.#dispatchECEvent(`ENCLAVE_PUBLIC_KEY:${this.#enclavePublicKey}`);
this.#dispatchECEvent(`ENCLAVE_DOCKER_COMPOSE_IPFS_HASH:${this.#enclaveDockerComposeIPFSHash}`);
}
}
#getTokensFromFaucet = async () => {
const account = this.#tokenContract.getCurrentWallet();
const balance = await this.#tokenContract.getBalance(account);
if (parseInt(balance, 10) <= 100) {
const tx = await this.#tokenContract.getFaucetTokens(account);
const transactionHash = tx.hash;
const isProcessed = await this.#waitForTransactionToBeProcessed(this.#tokenContract, transactionHash);
if (!isProcessed) {
return { success: false, message: 'Unable to create request, please check connectivity with Bloxberg node.' };
}
return { success: true };
}
return { success: true };
};
// eslint-disable-next-line class-methods-use-this
async #getReason(contract, txHash) {
const tx = await contract.getProvider().getTransaction(txHash);
if (!tx) {
console.log('tx not found');
return 'Transaction hash not found';
}
delete tx.gasPrice;
const code = await contract.getProvider().call(tx, tx.blockNumber);
const reason = ethers.utils.toUtf8String(`0x${code.substring(138)}`);
console.log(reason);
return reason.trim();
}
// eslint-disable-next-line class-methods-use-this
async #waitForTransactionToBeProcessed(contract, transactionHash) {
await contract.getProvider().waitForTransaction(transactionHash);
const txReceipt = await contract.getProvider().getTransactionReceipt(transactionHash);
console.log(txReceipt);
return !(!txReceipt && txReceipt.status === 0);
}
async #handleMetaMaskConnection() {
try {
await this.#tokenContract.getProvider().send('eth_requestAccounts', []);
const walletAddress = this.#tokenContract.getCurrentWallet();
return walletAddress !== null && walletAddress !== undefined;
} catch (e) {
return false;
}
}
async #listenForAddDORequestEvent() {
let intervalRepeats = 0;
let _addDORequestEVPassed = false;
let _orderPlacedEVPassed = false;
let _orderClosedEVPassed = false;
const protocolContract = this.#protocolContract.getContract();
const messageInterval = () => {
if (intervalRepeats % 2 === 0) {
this.#dispatchECEvent(`\\ Waiting for the task to be processed by ${this.#nodeAddress} ...`);
} else {
this.#dispatchECEvent(`/ Waiting for the task to be processed by ${this.#nodeAddress} ...`);
}
// eslint-disable-next-line no-plusplus
intervalRepeats++;
};
const _orderApproved = async () => {
_orderPlacedEVPassed = true;
protocolContract.off('_orderPlacedEV', _orderPlacedEV);
// approve order in case we are not providing a node address as metadata4 parameter
if (!this.#nodeAddress) {
await this.#approveOrder(this.#orderNumber);
}
this.#dispatchECEvent(`Order ${this.#orderNumber} was placed and approved.`);
this.#dispatchECEvent(
`Order ${this.#orderNumber} was placed and approved.`,
ECStatus.DEFAULT,
ECEvent.TASK_ORDER_PLACED
);
this.#interval = setInterval(messageInterval, 1000);
};
const _addDORequestEV = async (_from, _doRequest) => {
const walletAddress = (await this.#tokenContract.getProvider().send('eth_requestAccounts', []))[0];
try {
if (walletAddress && _from.toLowerCase() === walletAddress.toLowerCase() && !_addDORequestEVPassed) {
this.#doRequest = _doRequest.toNumber();
this.#dispatchECEvent(`Task was picked up and DO Request ${this.#doRequest} was created.`);
_addDORequestEVPassed = true;
protocolContract.off('_addDORequestEV', _addDORequestEV);
this.#dispatchECEvent(
`Task was picked up and DO Request ${this.#doRequest} was created.`,
ECStatus.DEFAULT,
ECEvent.TASK_CREATED
);
// in case _orderPlacedEV was dispatched before _addDORequestEV we have to call the _orderApproved
if (this.#orderNumber !== -1 && this.#doRequest === this.#doRequestId) {
await _orderApproved();
}
const showErrorAfterTimeout = () => {
this.#dispatchECEvent(
'Task processing failed due to unavailability of nodes. The network is currently busy. Please consider increasing the task price.',
ECStatus.ERROR
);
this.#dispatchECEvent(
'Task processing failed due to unavailability of nodes. The network is currently busy. Please consider increasing the task price.',
ECStatus.ERROR,
ECEvent.TASK_NOT_PROCESSED
);
};
// checking if task was picked for approval and passed resource requirements
clearTimeout(this.#orderPlacedTimer);
this.#orderPlacedTimer = setTimeout(() => {
if (!this.#taskHasBeenPickedForApproval) {
showErrorAfterTimeout();
}
clearTimeout(this.#orderPlacedTimer);
}, 60 * 1000);
} else if (!walletAddress) {
this.#dispatchECEvent('Unable to retrieve current wallet address.', ECStatus.ERROR);
}
} catch (e) {
console.log(e);
this.#dispatchECEvent('Unable to retrieve current wallet address.', ECStatus.ERROR);
}
};
const _orderPlacedEV = async (orderNumber, doRequestId) => {
this.#taskHasBeenPickedForApproval = true;
// this means that addDPRequestEV was dispatched
if (doRequestId.toNumber() === this.#doRequest && !_orderPlacedEVPassed && this.#doRequest !== -1) {
this.#orderNumber = orderNumber.toNumber();
this.#doRequestId = doRequestId.toNumber();
await _orderApproved();
} else {
// otherwise keep track of the result of this event and call the function _orderApproved in addDPRequestEV
// eslint-disable-next-line no-lonely-if
if (this.#doRequest === -1) {
this.#orderNumber = orderNumber.toNumber();
this.#doRequestId = doRequestId.toNumber();
}
}
};
const _orderClosedEV = async (orderNumber) => {
if (this.#orderNumber === orderNumber.toNumber() && !_orderClosedEVPassed && this.#orderNumber !== -1) {
clearInterval(this.#interval);
_orderClosedEVPassed = true;
protocolContract.off('_orderClosedEV', _orderClosedEV);
// get processed result from the order and create a certificate
const parsedOrderResult = await this.#getResultFromOrder(orderNumber);
if (parsedOrderResult.success === false) {
this.#dispatchECEvent(parsedOrderResult.message, ECStatus.ERROR);
} else {
this.#dispatchECEvent(
{
result: parsedOrderResult.result,
resultHash: parsedOrderResult.resultHash,
resultValue: parsedOrderResult.resultValue,
resultValueTimestamp: parsedOrderResult.resultTimestamp,
resultTaskCode: parsedOrderResult.resultTaskCode
},
ECStatus.SUCCESS,
ECEvent.TASK_COMPLETED
);
}
}
};
protocolContract.on('_addDORequestEV', _addDORequestEV);
protocolContract.on('_orderPlacedEV', _orderPlacedEV);
protocolContract.on('_orderClosedEV', _orderClosedEV);
}
async #approveOrder(orderId) {
const tx = await this.#protocolContract.approveOrder(orderId);
const isProcessed = await this.#waitForTransactionToBeProcessed(this.#protocolContract, tx.hash);
if (!isProcessed) {
const reason = await this.#getReason(this.#protocolContract, tx.hash);
this.#dispatchECEvent(`Unable to approve order. ${reason}`, ECStatus.ERROR);
return;
}
this.#dispatchECEvent(`Order successfully approved!`, ECStatus.SUCCESS);
this.#dispatchECEvent(`TX hash: ${tx.hash}`);
this.#nodeAddress = (await this.#protocolContract.getOrder(orderId)).dproc;
}
async #getCurrentWalletPublicKey() {
const account = this.#tokenContract.getCurrentWallet();
const keyB64 = await window.ethereum.request({
method: 'eth_getEncryptionPublicKey',
params: [account]
});
return Buffer.from(keyB64, 'base64').toString('hex');
}
async #getV3ImageMetadata(challengeHash) {
// generating encrypted base64 hash of the challenge
const base64EncryptedChallenge = await encryptWithCertificate(challengeHash, this.#enclavePublicKey);
// uploading to IPFS the base64 encrypted challenge
const challengeIPFSHash = await ipfsClient.uploadToIPFS(base64EncryptedChallenge);
const publicKey = await this.#getCurrentWalletPublicKey();
// image metadata for v3 format v3:image_ipfs_hash:image_name:docker_compose_ipfs_hash:client_challenge_ipfs_hash:public_key
return `${VERSION}:${this.#enclaveImageIPFSHash}:${this.#runnerType}:${
this.#enclaveDockerComposeIPFSHash
}:${challengeIPFSHash}:${publicKey}`;
}
async #getV3CodeMetadata(code) {
// extracting code from all the code cells
let scriptChecksum = sha256(code);
// uploading all node js code to IPFS and received hash of transaction
const base64EncryptedScript = await encryptWithCertificate(code, this.#enclavePublicKey);
this.#scriptHash = await ipfsClient.uploadToIPFS(base64EncryptedScript);
scriptChecksum = await this.#tokenContract.signMessage(scriptChecksum);
// v3:code_ipfs_hash:code_checksum
return `${VERSION}:${this.#scriptHash}:${scriptChecksum}`;
}
async #getV3InputMedata() {
const fileSetChecksum = await this.#tokenContract.signMessage(ZERO_CHECKSUM);
// v3::filesetchecksum
return `${VERSION}::${fileSetChecksum}`;
}
async #createDORequest(imageMetadata, codeMetadata, inputMetadata, nodeAddress, gasLimit) {
try {
this.#dispatchECEvent(`Submitting transaction for DO request on ${formatDate()}.`);
// add here call to SC(smart contract)
const tx = await this.#protocolContract.addDORequest(
imageMetadata,
codeMetadata,
inputMetadata,
nodeAddress,
this.#resource,
gasLimit
);
const transactionHash = tx.hash;
this.#doHash = transactionHash;
this.#dispatchECEvent(`Waiting for transaction ${transactionHash} to be processed...`);
const isProcessed = await this.#waitForTransactionToBeProcessed(this.#protocolContract, transactionHash);
if (!isProcessed) {
const reason = await this.#getReason(this.#protocolContract, transactionHash);
this.#dispatchECEvent(`Unable to create DO request. ${reason}`);
return false;
}
this.#dispatchECEvent(`Transaction ${transactionHash} was confirmed.`);
return true;
} catch (e) {
console.log(e);
if (e.message.search('cannot estimate gas; transaction may fail or may require manual gas limit') !== -1) {
// eslint-disable-next-line no-return-await
return this.#createDORequest(imageMetadata, codeMetadata, inputMetadata, nodeAddress, 5000000);
}
return false;
}
}
// eslint-disable-next-line class-methods-use-this
#parseOrderResult = (result) => {
try {
const arr = result.split(':');
const tBytes = arr[1].startsWith('0x') ? arr[1] : `0x${arr[1]}`;
return {
version: arr[0],
transactionBytes: tBytes,
resultIPFSHash: arr[2]
};
} catch (e) {
throw new Error(ECError.PARSE_ERROR);
}
};
#parseTransactionBytes(bytes) {
try {
const result = parseTransactionBytes(this.#protocolAbi, bytes);
const arr = result.result.split(':');
return {
version: arr[0],
from: result.from,
taskCode: arr[1],
taskCodeString: ECOrderTaskStatus[arr[1]],
checksum: arr[2],
enclaveChallenge: arr[3]
};
} catch (e) {
throw new Error(ECError.PARSE_ERROR);
}
}
async #getResultFromOrder(orderId) {
try {
// get the result of the order using the `etnyContract` object
const orderResult = await this.#protocolContract.getResultFromOrder(orderId);
this.#dispatchECEvent(`Task with order number ${orderId} was successfully processed at ${formatDate()}.`);
// parse the order result
const parsedOrderResult = this.#parseOrderResult(orderResult);
this.#dispatchECEvent(`Result IPFS hash: ${parsedOrderResult.resultIPFSHash}`);
// parse the transaction bytes of the order result
const transactionResult = this.#parseTransactionBytes(parsedOrderResult.transactionBytes);
// generate a wallet address using the `challengeHash` and `transactionResult.enclaveChallenge`
const wallet = generateWallet(this.#challengeHash, transactionResult.enclaveChallenge);
// check if the generated wallet address matches the `transactionResult.from` address
if (!wallet || wallet !== transactionResult.from) {
return { success: false, message: 'Integrity check failed, signer wallet address is wrong.' };
}
// get the result value from IPFS using the `parsedOrderResult.resultIPFSHash`
const ipfsResult = await ipfsClient.getFromIPFS(parsedOrderResult.resultIPFSHash);
// decrypt data
const currentWalletAddress = this.#tokenContract.getCurrentWallet();
const decryptedData = await decryptWithPrivateKey(ipfsResult, currentWalletAddress);
if (!decryptedData.success) {
return { success: false, message: 'Could not decrypt the order result.' };
}
// update the loading message to show the result value
this.#dispatchECEvent(`Result value: ${decryptedData.data}`);
// calculate the SHA-256 checksum of the result value
const ipfsResultChecksum = sha256(decryptedData.data);
// check if the calculated checksum matches the `transactionResult.checksum`
if (ipfsResultChecksum !== transactionResult.checksum) {
return { success: false, message: 'Integrity check failed, checksum of the order result is wrong.' };
}
// get the original input transaction hash and the output transaction hash for the order
const transaction = await this.#protocolContract.getProvider().getTransaction(this.#doHash);
const block = await this.#protocolContract.getProvider().getBlock(transaction.blockNumber);
const blockTimestamp = block.timestamp;
const endBlockNumber = await this.#protocolContract.getProvider().getBlockNumber();
const startBlockNumber = endBlockNumber - LAST_BLOCKS;
//
let resultTransactionHash;
let resultBlockTimestamp;
// eslint-disable-next-line no-plusplus
for (let i = endBlockNumber; i >= startBlockNumber; i--) {
// eslint-disable-next-line no-await-in-loop
const block = await this.#protocolContract.getProvider().getBlockWithTransactions(i);
// eslint-disable-next-line no-continue
if (!block || !block.transactions) continue;
// eslint-disable-next-line no-restricted-syntax
for (const transaction of block.transactions) {
if (transaction.to === this.#protocolContract.contractAddress() && transaction.data) {
resultTransactionHash = transaction.hash;
resultBlockTimestamp = block.timestamp;
}
}
}
return {
success: true,
contractAddress: this.#tokenContract.contractAddress(),
inputTransactionHash: this.#doHash,
outputTransactionHash: resultTransactionHash,
orderId,
imageHash: `${this.#enclaveImageIPFSHash}:${this.#runnerType}`,
scriptHash: this.#scriptHash,
fileSetHash: this.#fileSetHash,
publicTimestamp: blockTimestamp,
resultHash: parsedOrderResult.resultIPFSHash,
resultTaskCode: transactionResult.taskCodeString,
resultValue: ipfsResult,
resultTimestamp: resultBlockTimestamp,
result: decryptedData.data
};
} catch (ex) {
console.log(ex);
if (ex.name === ECError.PARSE_ERROR) {
return { success: false, message: 'Ethernity parsing transaction error.' };
}
if (ex.name === ECError.IPFS_DOWNLOAD_ERROR) {
return { success: false, message: 'Ethernity IPFS download result error.' };
}
await delay(5000);
this.#getResultFromOrderRepeats += 1;
// eslint-disable-next-line no-return-await
return await this.#getResultFromOrder(orderId);
}
}
async #processTask(code) {
await this.#listenForAddDORequestEvent();
this.#challengeHash = generateRandomHexOfSize(20);
// getting image metadata
const imageMetadata = await this.#getV3ImageMetadata(this.#challengeHash);
// // getting script metadata
const codeMetadata = await this.#getV3CodeMetadata(code);
// // getting input metadata
const inputMetadata = await this.#getV3InputMedata();
// // create new DO Request
return this.#createDORequest(imageMetadata, codeMetadata, inputMetadata, this.#nodeAddress);
}
#reset = () => {
this.#orderNumber = -1;
this.#doHash = null;
this.#doRequestId = -1;
this.#doRequest = -1;
this.#scriptHash = '';
this.#fileSetHash = '';
this.#interval = null;
this.#getResultFromOrderRepeats = 1;
this.#taskHasBeenPickedForApproval = false;
};
#cleanup = async () => {
this.#reset();
const contract = this.#protocolContract.getContract();
contract.removeAllListeners();
};
#isNodeOperatorAddress = async (nodeAddress) => {
if (isNullOrEmpty(nodeAddress)) return true;
if (isAddress(nodeAddress)) {
const isNode = await this.#protocolContract.isNodeOperator(nodeAddress);
if (!isNode) {
this.#dispatchECEvent('Introduced address is not a valid node operator address.', ECStatus.ERROR);
return false;
}
return true;
}
this.#dispatchECEvent('Introduced address is not a valid wallet address.', ECStatus.ERROR);
return false;
};
// eslint-disable-next-line class-methods-use-this
initializeStorage(ipfsAddress, protocol, port, token) {
ipfsClient.initialize(ipfsAddress, protocol, port, token);
}
// use this in order to reset the instance and have a new runner
static resetInstance() {
EthernityCloudRunner.instance = null;
}
async #checkNetwork() {
try {
// checking network
const networkName = await this.#tokenContract.getNetworkName();
console.log(networkName);
// eslint-disable-next-line default-case
switch (this.#networkAddress) {
case ECAddress.BLOXBERG.MAINNET_ADDRESS:
case ECAddress.BLOXBERG.TESTNET_ADDRESS:
if (networkName !== ECNetworkName.BLOXBERG) {
this.#dispatchECEvent(`Please switch Metamask network and use Bloxberg!`, ECStatus.ERROR);
}
break;
case ECAddress.POLYGON.MAINNET_ADDRESS:
if (networkName !== ECNetworkName.POLYGON) {
this.#dispatchECEvent(`Please switch Metamask network and use Polygon!`, ECStatus.ERROR);
}
break;
case ECAddress.POLYGON.TESTNET_ADDRESS:
if (networkName !== ECNetworkName.MUMBAI) {
this.#dispatchECEvent(`Please switch Metamask network and use Mumbai!`, ECStatus.ERROR);
}
break;
}
const runnerNetworkName = ECNetworkNameDictionary[this.#networkAddress];
return networkName === runnerNetworkName;
} catch (e) {
this.#dispatchECEvent(
`Please switch Metamask network and use ${ECNetworkName1Dictionary[this.#networkAddress]}!`,
ECStatus.ERROR
);
return false;
}
}
async run(
runnerType,
code,
nodeAddress = '',
resources = { taskPrice: 10, cpu: 1, memory: 1, storage: 40, bandwidth: 1, duration: 1, validators: 1 }
) {
try {
// checking if the balance of the wallet is higher or equal than the price we agreed to pay for task execution
let balance = await this.#tokenContract.getBalance();
balance = parseInt(balance, 10);
if (balance < resources.taskPrice) {
this.#dispatchECEvent(
`Your wallet balance (${balance}/${resources.taskPrice}) is insufficient to cover the requested task price. The task has been declined.`,
ECStatus.ERROR
);
return;
}
this.#nodeAddress = nodeAddress;
this.#resource = resources;
const isNodeOperatorAddress = await this.#isNodeOperatorAddress(nodeAddress);
if (isNodeOperatorAddress) {
this.#runnerType = runnerType;
this.#imageRegistryContract = new ImageRegistryContract(this.#networkAddress, runnerType);
await this.#cleanup();
this.#dispatchECEvent('Started processing task...');
await this.#tokenContract.initialize();
const connected = await this.#handleMetaMaskConnection();
if (connected) {
await this.#getEnclaveDetails();
// if (!this.#isMainnet) {
// const faucetResult = await this.#getTokensFromFaucet();
// if (!faucetResult) {
// this.#dispatchECEvent('Unable to retrieve tETNY from the faucet.', ECStatus.ERROR);
// }
// }
if (
this.#networkAddress === ECAddress.POLYGON.MAINNET_ADDRESS ||
this.#networkAddress === ECAddress.POLYGON.TESTNET_ADDRESS
) {
this.#dispatchECEvent('Checking for the allowance on the current wallet...');
const passedAllowance = await this.#tokenContract.checkAndSetAllowance(
this.#protocolContract.contractAddress(),
'100',
resources.taskPrice.toString()
);
if (!passedAllowance) {
this.#dispatchECEvent('Unable to set allowance.', ECStatus.ERROR);
return;
}
this.#dispatchECEvent('Allowance checking completed.');
}
const canProcessTask = await this.#processTask(code);
if (!canProcessTask) {
this.#dispatchECEvent('Unable to proceed with the task; exiting.', ECStatus.ERROR);
}
} else {
this.#dispatchECEvent('Unable to connect to MetaMask', ECStatus.ERROR);
}
}
} catch (e) {
this.#dispatchECEvent(e.message, ECStatus.ERROR);
}
}
}
export default EthernityCloudRunner;