Skip to content

Commit b5cfc1d

Browse files
committed
Add more tests
1 parent 829d6a0 commit b5cfc1d

File tree

1 file changed

+111
-185
lines changed

1 file changed

+111
-185
lines changed

test/byContract/IexecPoco/IexecPoco2-contribute-and-finalize.test.ts

Lines changed: 111 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
IexecInterfaceNative__factory,
1212
TestClient__factory,
1313
} from '../../../typechain';
14-
import { NULL } from '../../../utils/constants';
1514
import { IexecOrders, OrdersAssets, OrdersPrices, buildOrders } from '../../../utils/createOrders';
1615
import {
1716
ContributionStatusEnum,
@@ -37,7 +36,8 @@ const volume = 1;
3736
const teeDealTag = '0x0000000000000000000000000000000000000000000000000000000000000001';
3837
const standardDealTag = HashZero;
3938
const emptyEnclaveAddress = AddressZero;
40-
const emptyEnclaveSignature = NULL.SIGNATURE;
39+
const emptyEnclaveSignature = '0x';
40+
const { results, resultDigest } = buildUtf8ResultAndDigest('result');
4141

4242
let proxyAddress: string;
4343
let [iexecPoco, iexecPocoAsWorker]: IexecInterfaceNative[] = [];
@@ -116,7 +116,7 @@ describe('IexecPoco2#contributeAndFinalize', () => {
116116
// Save frozens.
117117
const accounts = [requester, appProvider, datasetProvider, scheduler, worker];
118118
const accountsInitialFrozens = await iexecWrapper.getInitialFrozens(accounts);
119-
// ContributeAndFinalize task.
119+
// Run contributeAndFinalize.
120120
const { resultsCallback, callbackResultDigest: resultsCallbackDigest } =
121121
buildResultCallbackAndDigest(123);
122122
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
@@ -247,20 +247,19 @@ describe('IexecPoco2#contributeAndFinalize', () => {
247247
}).toArray(),
248248
);
249249
await iexecPoco.initialize(dealId, taskIndex).then((tx) => tx.wait());
250-
// ContributeAndFinalize task.
251-
const { results, resultDigest } = buildUtf8ResultAndDigest('result');
250+
// Run contributeAndFinalize.
252251
const { resultHash } = buildResultHashAndResultSeal(taskId, resultDigest, worker);
253252
const contributeAndFinalizeTx = await iexecPoco.connect(worker).contributeAndFinalize(
254253
taskId,
255254
resultDigest,
256255
results,
257-
'0x',
258-
AddressZero, // enclave,
259-
'0x',
256+
'0x', // callback data
257+
emptyEnclaveAddress,
258+
emptyEnclaveSignature,
260259
await buildAndSignContributionAuthorizationMessage(
261260
worker.address,
262261
taskId,
263-
AddressZero, // enclave
262+
emptyEnclaveAddress,
264263
scheduler,
265264
),
266265
);
@@ -286,230 +285,157 @@ describe('IexecPoco2#contributeAndFinalize', () => {
286285
});
287286

288287
it('Should not contributeAndFinalize when task is not active', async () => {
289-
const { taskId } = await iexecWrapper.signAndMatchOrders(...defaultOrders.toArray());
290-
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
291-
taskId,
292-
resultDigest,
293-
worker,
288+
// Create deal and task.
289+
const { taskId } = await iexecWrapper.signAndMatchOrders(
290+
...buildOrders({
291+
assets: ordersAssets,
292+
requester: requester.address,
293+
prices: ordersPrices,
294+
volume,
295+
trust,
296+
tag: standardDealTag,
297+
}).toArray(),
294298
);
299+
// No initialize.
295300
await expect(
296-
iexecPocoAsWorker.contribute(
297-
taskId,
298-
resultHash,
299-
resultSeal,
300-
emptyEnclaveAddress,
301-
emptyEnclaveSignature,
302-
await buildAndSignContributionAuthorizationMessage(
303-
worker.address,
301+
iexecPoco
302+
.connect(worker)
303+
.contributeAndFinalize(
304304
taskId,
305+
resultDigest,
306+
results,
307+
'0x',
305308
emptyEnclaveAddress,
306-
scheduler,
309+
'0x',
310+
await buildAndSignContributionAuthorizationMessage(
311+
worker.address,
312+
taskId,
313+
emptyEnclaveSignature,
314+
scheduler,
315+
),
307316
),
308-
),
309317
).to.be.revertedWithoutReason(); // require#1
310318
});
311319

312320
it('Should not contributeAndFinalize after deadline', async () => {
313-
const { dealId, taskIndex, taskId } = await iexecWrapper.signAndMatchOrders(
314-
...defaultOrders.toArray(),
315-
);
316-
await iexecPoco.initialize(dealId, taskIndex).then((tx) => tx.wait());
317-
expect((await iexecPoco.viewTask(taskId)).status).to.equal(TaskStatusEnum.ACTIVE);
318-
const task = await iexecPoco.viewTask(taskId);
319-
await time.setNextBlockTimestamp(task.contributionDeadline);
320-
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
321-
taskId,
322-
resultDigest,
323-
worker,
324-
);
325-
// active task
326-
// but after deadline
327-
await expect(
328-
iexecPocoAsWorker.contribute(
329-
taskId,
330-
resultHash,
331-
resultSeal,
332-
emptyEnclaveAddress,
333-
emptyEnclaveSignature,
334-
await buildAndSignContributionAuthorizationMessage(
335-
worker.address,
336-
taskId,
337-
emptyEnclaveAddress,
338-
scheduler,
339-
),
340-
),
341-
).to.be.revertedWithoutReason(); // require#2
342-
});
343-
344-
it('Should not contributeAndFinalize when someone else has already contributed', async () => {
321+
// Create deal and task.
345322
const { dealId, taskIndex, taskId } = await iexecWrapper.signAndMatchOrders(
346323
...buildOrders({
347324
assets: ordersAssets,
348325
requester: requester.address,
349326
prices: ordersPrices,
350327
volume,
351-
trust: 3, // so consensus is not yet reached on first contribution
328+
trust,
352329
tag: standardDealTag,
353330
}).toArray(),
354331
);
355332
await iexecPoco.initialize(dealId, taskIndex).then((tx) => tx.wait());
356-
const workerTaskStake = await iexecPoco
357-
.viewDeal(dealId)
358-
.then((deal) => deal.workerStake.toNumber());
359-
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
360-
taskId,
361-
resultDigest,
362-
worker,
363-
);
364-
await iexecWrapper.depositInIexecAccount(worker, workerTaskStake);
365-
await iexecPocoAsWorker
366-
.contribute(
367-
taskId,
368-
resultHash,
369-
resultSeal,
370-
emptyEnclaveAddress,
371-
emptyEnclaveSignature,
372-
await buildAndSignContributionAuthorizationMessage(
373-
worker.address,
374-
taskId,
375-
emptyEnclaveAddress,
376-
scheduler,
377-
),
378-
)
379-
.then((tx) => tx.wait());
380-
expect((await iexecPoco.viewContribution(taskId, worker.address)).status).to.equal(
381-
ContributionStatusEnum.CONTRIBUTED,
382-
);
383-
// active task, before deadline
384-
// but already contributed
333+
const task = await iexecPoco.viewTask(taskId);
334+
await time.setNextBlockTimestamp(task.contributionDeadline);
335+
// Task is active but after deadline.
385336
await expect(
386-
iexecPocoAsWorker.contribute(
387-
taskId,
388-
resultHash,
389-
resultSeal,
390-
emptyEnclaveAddress,
391-
emptyEnclaveSignature,
392-
await buildAndSignContributionAuthorizationMessage(
393-
worker.address,
337+
iexecPoco
338+
.connect(worker)
339+
.contributeAndFinalize(
394340
taskId,
341+
resultDigest,
342+
results,
343+
'0x',
395344
emptyEnclaveAddress,
396-
scheduler,
345+
'0x',
346+
await buildAndSignContributionAuthorizationMessage(
347+
worker.address,
348+
taskId,
349+
emptyEnclaveSignature,
350+
scheduler,
351+
),
397352
),
398-
),
399-
).to.be.revertedWithoutReason(); // require#3
353+
).to.be.revertedWithoutReason(); // require#2
400354
});
401355

402-
it('Should not contributeAndFinalize when trust != 1', async () => {
403-
// trust == 0
404-
// trust > 1
356+
it('Should not contributeAndFinalize when someone else has already contributed', async () => {
357+
// TODO require#3
405358
});
406359

407-
it('Should not contributeAndFinalize when callback data is missing', async () => {});
408-
409-
it('Should not contributeAndFinalize when enclave challenge for TEE task is missing', async () => {
410-
const { dealId, taskIndex, taskId } = await iexecWrapper.signAndMatchOrders(
360+
it('Should not contributeAndFinalize when trust is not 1', async () => {
361+
// Trust == 0
362+
const {
363+
dealId: dealId1,
364+
taskIndex: taskIndex1,
365+
taskId: taskId1,
366+
} = await iexecWrapper.signAndMatchOrders(
411367
...buildOrders({
412368
assets: ordersAssets,
413369
requester: requester.address,
414370
prices: ordersPrices,
415371
volume,
416372
trust: 0,
417-
tag: teeDealTag,
373+
tag: standardDealTag,
418374
}).toArray(),
419375
);
420-
await iexecPoco.initialize(dealId, taskIndex).then((tx) => tx.wait());
421-
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
422-
taskId,
423-
resultDigest,
424-
worker,
425-
);
426-
// active task, before deadline, not contributed
427-
// but no TEE enclave challenge
376+
await iexecPoco.initialize(dealId1, taskIndex1).then((tx) => tx.wait());
377+
// Task active, before deadline, but bad trust.
428378
await expect(
429-
iexecPocoAsWorker.contribute(
430-
taskId,
431-
resultHash,
432-
resultSeal,
433-
emptyEnclaveAddress,
434-
emptyEnclaveSignature,
435-
await buildAndSignContributionAuthorizationMessage(
436-
worker.address,
437-
taskId,
379+
iexecPoco
380+
.connect(worker)
381+
.contributeAndFinalize(
382+
taskId1,
383+
resultDigest,
384+
results,
385+
'0x',
438386
emptyEnclaveAddress,
439-
scheduler,
387+
'0x',
388+
await buildAndSignContributionAuthorizationMessage(
389+
worker.address,
390+
taskId1,
391+
emptyEnclaveSignature,
392+
scheduler,
393+
),
440394
),
441-
),
442395
).to.be.revertedWithoutReason(); // require#4
443-
});
444-
445-
it('Should not contributeAndFinalize when scheduler signature is invalid', async () => {
446-
const { dealId, taskIndex, taskId } = await iexecWrapper.signAndMatchOrders(
447-
...defaultOrders.toArray(),
448-
);
449-
await iexecPoco.initialize(dealId, taskIndex).then((tx) => tx.wait());
450-
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
451-
taskId,
452-
resultDigest,
453-
worker,
454-
);
455-
// active task, before deadline, not contributed, enclave challenge not required
456-
// but invalid scheduler signature
457-
await expect(
458-
iexecPocoAsWorker.contribute(
459-
taskId,
460-
resultHash,
461-
resultSeal,
462-
emptyEnclaveAddress,
463-
emptyEnclaveSignature,
464-
await buildAndSignContributionAuthorizationMessage(
465-
worker.address,
466-
taskId,
467-
emptyEnclaveAddress,
468-
anyone, // authorization not signed by scheduler
469-
),
470-
),
471-
).to.be.revertedWithoutReason(); // require#5
472-
});
473-
474-
it('Should not contributeAndFinalize when enclave signature is invalid', async () => {
475-
const { dealId, taskIndex, taskId } = await iexecWrapper.signAndMatchOrders(
396+
// Trust > 1
397+
const {
398+
dealId: dealId2,
399+
taskIndex: taskIndex2,
400+
taskId: taskId2,
401+
} = await iexecWrapper.signAndMatchOrders(
476402
...buildOrders({
477403
assets: ordersAssets,
478404
requester: requester.address,
479405
prices: ordersPrices,
480-
volume,
406+
volume: 3, // to create new orders.
481407
trust: 0,
482-
tag: teeDealTag,
408+
tag: standardDealTag,
483409
}).toArray(),
484410
);
485-
await iexecPoco.initialize(dealId, taskIndex).then((tx) => tx.wait());
486-
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
487-
taskId,
488-
resultDigest,
489-
worker,
490-
);
491-
// active task, before deadline, not contributed, enclave challenge set,
492-
// valid scheduler signature
493-
// but invalid enclave signature
411+
await iexecPoco.initialize(dealId2, taskIndex2).then((tx) => tx.wait());
412+
// Task active, before deadline, but bad trust.
494413
await expect(
495-
iexecPocoAsWorker.contribute(
496-
taskId,
497-
resultHash,
498-
resultSeal,
499-
enclave.address,
500-
await buildAndSignPocoClassicEnclaveMessage(
501-
resultHash,
502-
resultSeal,
503-
anyone, // enclave message signed by someone else
504-
),
505-
await buildAndSignContributionAuthorizationMessage(
506-
worker.address,
507-
taskId,
508-
enclave.address,
509-
scheduler,
414+
iexecPoco
415+
.connect(worker)
416+
.contributeAndFinalize(
417+
taskId2,
418+
resultDigest,
419+
results,
420+
'0x',
421+
emptyEnclaveAddress,
422+
'0x',
423+
await buildAndSignContributionAuthorizationMessage(
424+
worker.address,
425+
taskId2,
426+
emptyEnclaveSignature,
427+
scheduler,
428+
),
510429
),
511-
),
512-
).to.be.revertedWithoutReason(); // require#6
430+
).to.be.revertedWithoutReason(); // require#4
513431
});
432+
433+
it('Should not contributeAndFinalize when callback data is missing', async () => {});
434+
435+
it('Should not contributeAndFinalize when enclave challenge for TEE task is missing', async () => {});
436+
437+
it('Should not contributeAndFinalize when scheduler signature is invalid', async () => {});
438+
439+
it('Should not contributeAndFinalize when enclave signature is invalid', async () => {});
514440
});
515441
});

0 commit comments

Comments
 (0)