11package com .iexec .worker .executor ;
22
3+ import com .fasterxml .jackson .annotation .JsonIgnore ;
34import com .iexec .common .chain .ChainReceipt ;
45import com .iexec .common .chain .ContributionAuthorization ;
6+ import com .iexec .common .contribution .Contribution ;
57import com .iexec .common .notification .TaskNotificationExtra ;
68import com .iexec .common .replicate .ReplicateActionResponse ;
79import com .iexec .common .replicate .ReplicateStatusCause ;
10+ import com .iexec .common .result .ComputedFile ;
811import com .iexec .common .security .Signature ;
912import com .iexec .common .task .TaskDescription ;
1013import com .iexec .common .tee .TeeEnclaveChallengeSignature ;
11- import com .iexec .common .utils .SignatureUtils ;
14+ import com .iexec .common .utils .BytesUtils ;
15+ import com .iexec .common .worker .result .ResultUtils ;
1216import com .iexec .worker .chain .ContributionService ;
1317import com .iexec .worker .chain .IexecHubService ;
1418import com .iexec .worker .chain .RevealService ;
@@ -232,24 +236,22 @@ ReplicateActionResponse contribute(String chainTaskId) {
232236 // System.exit(0);
233237 }
234238
235- String determinismHash = resultService .getTaskDeterminismHash (chainTaskId );
236- if (determinismHash . isEmpty ()) {
237- log .error ("Cannot contribute, determinism hash not found [chainTaskId:{}]" , chainTaskId );
239+ ComputedFile computedFile = resultService .getComputedFile (chainTaskId );
240+ if (computedFile == null ) {
241+ log .error ("Cannot contribute, getComputedFile [chainTaskId:{}]" , chainTaskId );
238242 return ReplicateActionResponse .failure (DETERMINISM_HASH_NOT_FOUND );
239243 }
240244
241245 ContributionAuthorization contributionAuthorization =
242246 contributionService .getContributionAuthorization (chainTaskId );
243247
244- String enclaveChallenge = contributionAuthorization .getEnclaveChallenge ();
245- Optional <Signature > enclaveSignature = getVerifiedEnclaveChallengeSignature (chainTaskId , enclaveChallenge );
246- if (enclaveSignature .isEmpty ()) {//could be 0x0
247- log .error ("Cannot contribute enclave signature not found [chainTaskId:{}]" , chainTaskId );
248- return ReplicateActionResponse .failure (ENCLAVE_SIGNATURE_NOT_FOUND );
248+ Contribution contribution = contributionService .getContribution (computedFile , contributionAuthorization );
249+ if (contribution == null ){
250+ log .error ("Failed to getContribution [chainTaskId:{}]" , chainTaskId );
251+ return ReplicateActionResponse .failure (ENCLAVE_SIGNATURE_NOT_FOUND );//TODO update status
249252 }
250253
251- Optional <ChainReceipt > oChainReceipt =
252- contributionService .contribute (contributionAuthorization , determinismHash , enclaveSignature .get ());
254+ Optional <ChainReceipt > oChainReceipt = contributionService .contribute (contribution );
253255
254256 if (!isValidChainReceipt (chainTaskId , oChainReceipt )) {
255257 return ReplicateActionResponse .failure (CHAIN_RECEIPT_NOT_VALID );
@@ -261,9 +263,11 @@ ReplicateActionResponse contribute(String chainTaskId) {
261263 ReplicateActionResponse reveal (String chainTaskId , TaskNotificationExtra extra ) {
262264 unsetTaskUsingCpu (chainTaskId );
263265
264- String determinismHash = resultService .getTaskDeterminismHash (chainTaskId );
265- if (determinismHash .isEmpty ()) {
266- log .error ("Cannot reveal, determinism hash not found [chainTaskId:{}]" , chainTaskId );
266+ ComputedFile computedFile = resultService .getComputedFile (chainTaskId );
267+ String resultDigest = computedFile != null ? computedFile .getResultDigest (): "" ;
268+
269+ if (resultDigest .isEmpty ()) {
270+ log .error ("Cannot reveal, resultDigest not found [chainTaskId:{}]" , chainTaskId );
267271 return ReplicateActionResponse .failure (DETERMINISM_HASH_NOT_FOUND );
268272 }
269273
@@ -279,7 +283,7 @@ ReplicateActionResponse reveal(String chainTaskId, TaskNotificationExtra extra)
279283 return ReplicateActionResponse .failure (BLOCK_NOT_REACHED );
280284 }
281285
282- boolean canReveal = revealService .repeatCanReveal (chainTaskId , determinismHash );
286+ boolean canReveal = revealService .repeatCanReveal (chainTaskId , resultDigest );
283287
284288 if (!canReveal ) {
285289 log .error ("Cannot reveal, one or more conditions are not satisfied [chainTaskId:{}]" , chainTaskId );
@@ -292,7 +296,7 @@ ReplicateActionResponse reveal(String chainTaskId, TaskNotificationExtra extra)
292296 System .exit (0 );
293297 }
294298
295- Optional <ChainReceipt > oChainReceipt = revealService .reveal (chainTaskId , determinismHash );
299+ Optional <ChainReceipt > oChainReceipt = revealService .reveal (chainTaskId , resultDigest );
296300 if (!isValidChainReceipt (chainTaskId , oChainReceipt )) {
297301 return ReplicateActionResponse .failure (CHAIN_RECEIPT_NOT_VALID );
298302 }
@@ -321,7 +325,8 @@ ReplicateActionResponse uploadResult(String chainTaskId) {
321325 return ReplicateActionResponse .failure (RESULT_LINK_MISSING );
322326 }
323327
324- String callbackData = resultService .getCallbackDataFromFile (chainTaskId );
328+ ComputedFile computedFile = resultService .getComputedFile (chainTaskId );
329+ String callbackData = computedFile != null ? computedFile .getCallbackData (): "" ;
325330
326331 log .info ("Result uploaded [chainTaskId:{}, resultLink:{}, callbackData:{}]" ,
327332 chainTaskId , resultLink , callbackData );
@@ -343,38 +348,6 @@ boolean abort(String chainTaskId) {
343348 return resultService .removeResult (chainTaskId );
344349 }
345350
346-
347- //TODO Move that to result service
348- Optional <Signature > getVerifiedEnclaveChallengeSignature (String chainTaskId , String expectedSigner ) {
349- boolean isTeeTask = iexecHubService .isTeeTask (chainTaskId );
350- if (!isTeeTask ) {
351- return Optional .of (SignatureUtils .emptySignature ());
352- }
353-
354- Optional <TeeEnclaveChallengeSignature > optionalTeeEnclaveChallengeSignature =
355- resultService .readTeeEnclaveChallengeSignatureFile (chainTaskId );
356- if (optionalTeeEnclaveChallengeSignature .isEmpty ()) {
357- log .error ("Error reading and parsing enclaveSig.iexec file [chainTaskId:{}]" , chainTaskId );
358- return Optional .empty ();
359- }
360- TeeEnclaveChallengeSignature enclaveChallengeSignature = optionalTeeEnclaveChallengeSignature .get ();
361-
362- String messageHash = TeeEnclaveChallengeSignature .getMessageHash (
363- enclaveChallengeSignature .getResultHash (),
364- enclaveChallengeSignature .getResultSeal ());
365-
366- boolean isExpectedSigner = resultService .isExpectedSignerOnSignature (messageHash ,
367- enclaveChallengeSignature .getSignature (),
368- expectedSigner );
369-
370- if (!isExpectedSigner ) {
371- log .error ("Scone enclave signature is not valid [chainTaskId:{}]" , chainTaskId );
372- return Optional .empty ();
373- }
374-
375- return Optional .of (enclaveChallengeSignature .getSignature ());
376- }
377-
378351 boolean checkGasBalance (String chainTaskId ) {
379352 if (iexecHubService .hasEnoughGas ()) {
380353 return true ;
0 commit comments