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_voting.js
245 lines (221 loc) · 9.07 KB
/
template.10_execute_voting.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
/* 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 VotingETHContract from '../../../build/contracts/VotingETH';
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 votingETHContract;
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');
});
});
it('should generate and save key/pair', () => {
enigma.setTaskKeyPair('cupcake');
});
it('initializes VotingETH contract', async () => {
votingETHContract = new enigma.web3.eth.Contract(VotingETHContract['abi'],
VotingETHContract.networks['4447'].address);
expect(votingETHContract.options.address).toBeTruthy();
});
const homedir = os.homedir();
const votingAddr = fs.readFileSync(path.join(homedir, '.enigma', 'addr-voting.txt'), 'utf-8');
it('creates a new poll on ETH', async () => {
const initialPollsLength = (await votingETHContract.methods.getPolls().call()).length;
await votingETHContract.methods.createPoll(50, "Is privacy important?", 30).send({
gas: 4712388,
gasPrice: 100000000000,
from: accounts[0],
});
const finalPollsLength = (await votingETHContract.methods.getPolls().call()).length;
expect(finalPollsLength - initialPollsLength).toEqual(1);
});
let task1;
const addr1 = '0x0000000000000000000000000000000000000000000000000000000000000001';
it('should execute compute task: voter 1 casting vote', async () => {
const pollId = (await votingETHContract.methods.getPolls().call()).length - 1;
let taskFn = 'cast_vote(uint256,bytes32,uint256)';
let taskArgs = [
[pollId, 'uint256'],
[addr1, 'bytes32'],
[1, 'uint256'],
];
let taskGasLimit = 1000000;
let taskGasPx = utils.toGrains(1);
task1 = await new Promise((resolve, reject) => {
enigma.computeTask(taskFn, taskArgs, taskGasLimit, taskGasPx, accounts[0], votingAddr)
.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 success', 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);
let task2;
const addr2 = '0x0000000000000000000000000000000000000000000000000000000000000002';
it('should fail to execute compute task: voter 1 casting vote to poll again', async () => {
const pollId = (await votingETHContract.methods.getPolls().call()).length - 1;
let taskFn = 'cast_vote(uint256,bytes32,uint256)';
let taskArgs = [
[pollId, 'uint256'],
[addr1, 'bytes32'],
[0, 'uint256'],
];
let taskGasLimit = 1000000;
let taskGasPx = utils.toGrains(1);
task2 = await new Promise((resolve, reject) => {
enigma.computeTask(taskFn, taskArgs, taskGasLimit, taskGasPx, accounts[0], votingAddr)
.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 success', async () => {
do {
await sleep(1000);
task2 = await enigma.getTaskRecordStatus(task2);
process.stdout.write('Waiting. Current Task Status is '+task2.ethStatus+'\r');
} while (task2.ethStatus !== 3);
expect(task2.ethStatus).toEqual(3);
process.stdout.write('Completed. Final Task Status is '+task2.ethStatus+'\n');
}, constants.TIMEOUT_COMPUTE);
it('should execute compute task: voter 2 casting vote', async () => {
const pollId = (await votingETHContract.methods.getPolls().call()).length - 1;
let taskFn = 'cast_vote(uint256,bytes32,uint256)';
let taskArgs = [
[pollId, 'uint256'],
[addr2, 'bytes32'],
[0, 'uint256'],
];
let taskGasLimit = 1000000;
let taskGasPx = utils.toGrains(1);
task2 = await new Promise((resolve, reject) => {
enigma.computeTask(taskFn, taskArgs, taskGasLimit, taskGasPx, accounts[0], votingAddr)
.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 success', 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);
let task3;
it('should fail to execute compute task to tally poll when poll has not expired', async () => {
const pollId = (await votingETHContract.methods.getPolls().call()).length - 1;
let taskFn = 'tally_poll(uint256)';
let taskArgs = [
[pollId, 'uint256'],
];
let taskGasLimit = 1000000;
let taskGasPx = utils.toGrains(1);
task3 = await new Promise((resolve, reject) => {
enigma.computeTask(taskFn, taskArgs, taskGasLimit, taskGasPx, accounts[0], votingAddr)
.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 () => {
task3 = await enigma.getTaskRecordStatus(task3);
expect(task3.ethStatus).toEqual(1);
});
it('should get the confirmed task success', async () => {
do {
await sleep(1000);
task3 = await enigma.getTaskRecordStatus(task3);
process.stdout.write('Waiting. Current Task Status is '+task3.ethStatus+'\r');
} while (task3.ethStatus !== 4);
expect(task3.ethStatus).toEqual(4);
process.stdout.write('Completed. Final Task Status is '+task3.ethStatus+'\n');
}, constants.TIMEOUT_COMPUTE);
it('checks poll is still pending on ETH', async () => {
const polls = await votingETHContract.methods.getPolls().call();
const pollId = polls.length - 1;
expect(polls[pollId].status).toEqual('1');
});
let task4;
it('should execute compute task: tally poll', async () => {
const polls = await votingETHContract.methods.getPolls().call();
const pollId = polls.length - 1;
expect(polls[pollId].status).toEqual('1');
let taskFn = 'tally_poll(uint256)';
let taskArgs = [
[pollId, 'uint256'],
];
let taskGasLimit = 1000000;
let taskGasPx = utils.toGrains(1);
await sleep(30000);
task4 = await new Promise((resolve, reject) => {
enigma.computeTask(taskFn, taskArgs, taskGasLimit, taskGasPx, accounts[0], votingAddr)
.on(eeConstants.SEND_TASK_INPUT_RESULT, (result) => resolve(result))
.on(eeConstants.ERROR, (error) => reject(error));
});
}, constants.TIMEOUT_COMPUTE_LONG);
it('should get the pending task', async () => {
task4 = await enigma.getTaskRecordStatus(task4);
expect(task4.ethStatus).toEqual(1);
});
it('should get the confirmed task', async () => {
do {
await sleep(1000);
task4 = await enigma.getTaskRecordStatus(task4);
process.stdout.write('Waiting. Current Task Status is '+task4.ethStatus+'\r');
} while (task4.ethStatus !== 2);
expect(task4.ethStatus).toEqual(2);
process.stdout.write('Completed. Final Task Status is '+task4.ethStatus+'\n');
}, constants.TIMEOUT_COMPUTE);
it('checks poll has registered as passed on ETH', async () => {
const polls = await votingETHContract.methods.getPolls().call();
const pollId = polls.length - 1;
expect(polls[pollId].status).toEqual('2');
});
});