2727import com .iexec .common .utils .FileHelper ;
2828import com .iexec .common .utils .IexecFileHelper ;
2929import com .iexec .common .worker .result .ResultUtils ;
30+ import com .iexec .commons .poco .chain .ChainDeal ;
3031import com .iexec .commons .poco .chain .ChainTask ;
3132import com .iexec .commons .poco .chain .ChainTaskStatus ;
3233import com .iexec .commons .poco .chain .WorkerpoolAuthorization ;
3334import com .iexec .commons .poco .task .TaskDescription ;
35+ import com .iexec .commons .poco .tee .TeeUtils ;
3436import com .iexec .commons .poco .utils .BytesUtils ;
3537import com .iexec .resultproxy .api .ResultProxyClient ;
3638import com .iexec .worker .chain .CredentialsService ;
5456@ Service
5557public class ResultService implements Purgeable {
5658 public static final String ERROR_FILENAME = "error.txt" ;
57- public static final String WRITE_COMPUTED_FILE_LOG_ARGS = " [chainTaskId:{}, computedFile:{}]" ;
5859
5960 private final WorkerConfigurationService workerConfigService ;
6061 private final CredentialsService credentialsService ;
6162 private final IexecHubService iexecHubService ;
6263 private final ResultProxyClient resultProxyClient ;
6364 private final Map <String , ResultInfo > resultInfoMap = ExpiringTaskMapFactory .getExpiringTaskMap ();
65+ private final ObjectMapper mapper = new ObjectMapper ();
6466
6567 public ResultService (
6668 WorkerConfigurationService workerConfigService ,
@@ -305,34 +307,30 @@ public ComputedFile getComputedFile(String chainTaskId) {
305307 }
306308
307309 /**
308- * Write computed file. Most likely used by tee-post-compute .
310+ * Writes computed file submitted to the worker through a REST call .
309311 * TODO: check compute stage is successful
310312 *
311313 * @param computedFile computed file to be written
312- * @return true is computed file is successfully written to disk
314+ * @return {@literal true} is computed file is successfully written to disk, {@literal false} otherwise
313315 */
314316 public boolean writeComputedFile (ComputedFile computedFile ) {
315317 if (computedFile == null || StringUtils .isEmpty (computedFile .getTaskId ())) {
316318 log .error ("Cannot write computed file [computedFile:{}]" , computedFile );
317319 return false ;
318320 }
319- String chainTaskId = computedFile .getTaskId ();
320- ChainTaskStatus chainTaskStatus =
321- iexecHubService .getChainTask (chainTaskId )
322- .map (ChainTask ::getStatus )
323- .orElse (null );
321+ final String chainTaskId = computedFile .getTaskId ();
322+ log .debug ("Received computed file [chainTaskId:{}, computedFile:{}]" , chainTaskId , computedFile );
323+ final ChainTask chainTask = iexecHubService .getChainTask (chainTaskId ).orElse (null );
324+ final ChainTaskStatus chainTaskStatus = chainTask != null ? chainTask .getStatus () : null ;
324325 if (chainTaskStatus != ChainTaskStatus .ACTIVE ) {
325- log .error ("Cannot write computed file if task is not active " +
326- "[chainTaskId:{}, computedFile:{}, chainTaskStatus:{}]" ,
326+ log .error ("Cannot write computed file if task is not active [chainTaskId:{}, computedFile:{}, chainTaskStatus:{}]" ,
327327 chainTaskId , computedFile , chainTaskStatus );
328328 return false ;
329329 }
330- String computedFilePath =
331- workerConfigService .getTaskOutputDir (chainTaskId )
332- + IexecFileHelper .SLASH_COMPUTED_JSON ;
330+ final String computedFilePath = workerConfigService .getTaskOutputDir (chainTaskId )
331+ + IexecFileHelper .SLASH_COMPUTED_JSON ;
333332 if (new File (computedFilePath ).exists ()) {
334- log .error ("Cannot write computed file if already written" +
335- WRITE_COMPUTED_FILE_LOG_ARGS ,
333+ log .error ("Cannot write computed file if already written [chainTaskId:{}, computedFile:{}]" ,
336334 chainTaskId , computedFile );
337335 return false ;
338336 }
@@ -341,15 +339,20 @@ public boolean writeComputedFile(ComputedFile computedFile) {
341339 chainTaskId , computedFile );
342340 return false ;
343341 }
344- boolean isSignatureRequired = iexecHubService .isTeeTask (chainTaskId );
345- if (isSignatureRequired &&
346- (StringUtils .isEmpty (computedFile .getEnclaveSignature ())
347- || stringToBytes (computedFile .getEnclaveSignature ()).length != 65 )) {
342+ // TODO replace with fast getChainDeal access, only 1 on-chain read instead of 4
343+ final ChainDeal chainDeal = iexecHubService .getChainDeal (chainTask .getDealid ()).orElse (null );
344+ if (chainDeal == null || !TeeUtils .isTeeTag (chainDeal .getTag ())) {
345+ log .error ("Cannot write computed file if task is not of TEE type [chainTaskId:{}, computedFile:{}]" ,
346+ chainTaskId , computedFile );
347+ return false ;
348+ }
349+ // should always be TEE with a valid signature
350+ if (StringUtils .isEmpty (computedFile .getEnclaveSignature ())
351+ || stringToBytes (computedFile .getEnclaveSignature ()).length != 65 ) {
348352 log .error ("Cannot write computed file if TEE signature is invalid [chainTaskId:{}, computedFile:{}]" ,
349353 chainTaskId , computedFile );
350354 return false ;
351355 }
352- ObjectMapper mapper = new ObjectMapper ();
353356 try {
354357 String json = mapper .writeValueAsString (computedFile );
355358 Files .write (Paths .get (computedFilePath ), json .getBytes ());
0 commit comments