This repository was archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplate.10_execute_calculator.js
140 lines (126 loc) · 4.85 KB
/
template.10_execute_calculator.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
/* eslint-disable require-jsdoc */
import fs from 'fs';
import os from 'os';
import path from 'path';
import Web3 from 'web3';
import Enigma from '../../src/Enigma';
import utils from '../../src/enigma-utils';
import * as eeConstants from '../../src/emitterConstants';
import {EnigmaContract, EnigmaTokenContract} from './contractLoader';
import * as constants from './testConstants';
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
describe('Enigma tests', () => {
let accounts;
let web3;
let enigma;
let epochSize;
it('initializes', () => {
const provider = new Web3.providers.HttpProvider('http://localhost:9545');
web3 = new Web3(provider);
return web3.eth.getAccounts().then((result) => {
accounts = result;
enigma = new Enigma(
web3,
EnigmaContract.networks['4447'].address,
EnigmaTokenContract.networks['4447'].address,
'http://localhost:3346',
{
gas: 4712388,
gasPrice: 100000000000,
from: accounts[0],
},
);
enigma.admin();
expect(Enigma.version()).toEqual('0.0.1');
});
});
const homedir = os.homedir();
it('should generate and save key/pair', () => {
enigma.setTaskKeyPair('cupcake');
});
const calculatorAddr = fs.readFileSync(path.join(homedir, '.enigma', 'addr-calculator.txt'), 'utf-8');
let task1;
it('should execute compute task', async () => {
let taskFn = 'sub(uint256,uint256)';
let taskArgs = [
[76, 'uint256'],
[17, 'uint256'],
];
let taskGasLimit = 100000;
let taskGasPx = utils.toGrains(1);
task1 = await new Promise((resolve, reject) => {
enigma.computeTask(taskFn, taskArgs, taskGasLimit, taskGasPx, accounts[0], calculatorAddr)
.on(eeConstants.SEND_TASK_INPUT_RESULT, (result) => resolve(result))
.on(eeConstants.ERROR, (error) => reject(error));
});
}, constants.TIMEOUT_COMPUTE);
it('should get the pending task', async () => {
task1 = await enigma.getTaskRecordStatus(task1);
expect(task1.ethStatus).toEqual(1);
});
it('should get the confirmed task', async () => {
do {
await sleep(1000);
task1 = await enigma.getTaskRecordStatus(task1);
process.stdout.write('Waiting. Current Task Status is '+task1.ethStatus+'\r');
} while (task1.ethStatus != 2);
expect(task1.ethStatus).toEqual(2);
process.stdout.write('Completed. Final Task Status is '+task1.ethStatus+'\n');
}, constants.TIMEOUT_COMPUTE);
it('should get the result and verify the computation is correct', async () => {
task1 = await new Promise((resolve, reject) => {
enigma.getTaskResult(task1)
.on(eeConstants.GET_TASK_RESULT_RESULT, (result) => resolve(result))
.on(eeConstants.ERROR, (error) => reject(error));
});
expect(task1.engStatus).toEqual('SUCCESS');
expect(task1.encryptedAbiEncodedOutputs).toBeTruthy();
task1 = await enigma.decryptTaskResult(task1);
expect(task1.usedGas).toBeTruthy();
expect(task1.workerTaskSig).toBeTruthy();
expect(parseInt(task1.decryptedOutput, 16)).toEqual(76-17);
}, constants.TIMEOUT_COMPUTE);
let task2;
it('should execute compute task', async () => {
let taskFn = 'mul(uint256,uint256)';
let taskArgs = [
[76, 'uint256'],
[17, 'uint256'],
];
let taskGasLimit = 100000;
let taskGasPx = utils.toGrains(1);
task2 = await new Promise((resolve, reject) => {
enigma.computeTask(taskFn, taskArgs, taskGasLimit, taskGasPx, accounts[0], calculatorAddr)
.on(eeConstants.SEND_TASK_INPUT_RESULT, (result) => resolve(result))
.on(eeConstants.ERROR, (error) => reject(error));
});
}, constants.TIMEOUT_COMPUTE);
it('should get the pending task', async () => {
task2 = await enigma.getTaskRecordStatus(task2);
expect(task2.ethStatus).toEqual(1);
});
it('should get the confirmed task', async () => {
do {
await sleep(1000);
task2 = await enigma.getTaskRecordStatus(task2);
process.stdout.write('Waiting. Current Task Status is '+task2.ethStatus+'\r');
} while (task2.ethStatus != 2);
expect(task2.ethStatus).toEqual(2);
process.stdout.write('Completed. Final Task Status is '+task2.ethStatus+'\n');
}, constants.TIMEOUT_COMPUTE);
it('should get and validate the result', async () => {
task2 = await new Promise((resolve, reject) => {
enigma.getTaskResult(task2)
.on(eeConstants.GET_TASK_RESULT_RESULT, (result) => resolve(result))
.on(eeConstants.ERROR, (error) => reject(error));
});
expect(task2.engStatus).toEqual('SUCCESS');
expect(task2.encryptedAbiEncodedOutputs).toBeTruthy();
task2 = await enigma.decryptTaskResult(task2);
expect(task2.usedGas).toBeTruthy();
expect(task2.workerTaskSig).toBeTruthy();
expect(parseInt(task2.decryptedOutput, 16)).toEqual(76*17);
}, constants.TIMEOUT_COMPUTE);
});