diff --git a/src/main/java/com/iexec/core/replicate/ReplicatesService.java b/src/main/java/com/iexec/core/replicate/ReplicatesService.java index b3ff8231a..97f9c0b97 100644 --- a/src/main/java/com/iexec/core/replicate/ReplicatesService.java +++ b/src/main/java/com/iexec/core/replicate/ReplicatesService.java @@ -147,9 +147,7 @@ public int getNbOffChainReplicatesWithStatus(String chainTaskId, ReplicateStatus private int getNbReplicatesWithGivenStatusJustBeforeWorkerLost(String chainTaskId, ReplicateStatus status) { int nbReplicates = 0; for (Replicate replicate : getReplicates(chainTaskId)) { - int size = replicate.getStatusChangeList().size(); - if (size >= 2 && replicate.getStatusChangeList().get(size - 1).getStatus().equals(WORKER_LOST) - && replicate.getStatusChangeList().get(size - 2).getStatus().equals(status)) { + if (isStatusBeforeWorkerLostEqualsTo(replicate, status)) { nbReplicates++; } } @@ -171,7 +169,11 @@ public Optional getRandomReplicateWithRevealStatus(String chainTaskId public Optional getReplicateWithResultUploadedStatus(String chainTaskId) { for (Replicate replicate : getReplicates(chainTaskId)) { - if (replicate.getCurrentStatus().equals(RESULT_UPLOADED)) { + + boolean isStatusResultUploaded = replicate.getCurrentStatus().equals(RESULT_UPLOADED); + boolean isStatusResultUploadedBeforeWorkerLost = isStatusBeforeWorkerLostEqualsTo(replicate, RESULT_UPLOADED); + + if (isStatusResultUploaded || isStatusResultUploadedBeforeWorkerLost) { return Optional.of(replicate); } } @@ -179,6 +181,13 @@ public Optional getReplicateWithResultUploadedStatus(String chainTask return Optional.empty(); } + private boolean isStatusBeforeWorkerLostEqualsTo(Replicate replicate, ReplicateStatus status) { + int size = replicate.getStatusChangeList().size(); + return size >= 2 + && replicate.getStatusChangeList().get(size - 1).getStatus().equals(WORKER_LOST) + && replicate.getStatusChangeList().get(size - 2).getStatus().equals(status); + } + public boolean moreReplicatesNeeded(String chainTaskId, int nbWorkersNeeded, long maxExecutionTime) { int nbValidReplicates = 0; for (Replicate replicate : getReplicates(chainTaskId)) { @@ -360,18 +369,9 @@ private boolean isTaskStatusFailOnChain(String chainTaskId, String walletAddress } } - boolean isPublicResult(String chainTaskId, Integer chainId) { - Optional beneficiary = iexecHubService.getTaskBeneficiary(chainTaskId, chainId); - if (!beneficiary.isPresent()) { - log.error("Failed to get beneficiary for isPublicResult() method [chainTaskId:{}]", chainTaskId); - return false; - } - return beneficiary.get().equals(BytesUtils.EMPTY_ADDRESS); - } - public boolean hasResultBeenUploaded(String chainTaskId) { // currently no check in case of IPFS - if(isPublicResult(chainTaskId, 0)){ + if(iexecHubService.isPublicResult(chainTaskId, 0)){ return true; } diff --git a/src/main/java/com/iexec/core/result/ResultController.java b/src/main/java/com/iexec/core/result/ResultController.java index 9cb6de404..a6713426f 100644 --- a/src/main/java/com/iexec/core/result/ResultController.java +++ b/src/main/java/com/iexec/core/result/ResultController.java @@ -108,7 +108,7 @@ public ResponseEntity getResult(@PathVariable("chainTaskId") String chai @RequestParam(name = "chainId") Integer chainId) throws IOException { Authorization auth = authorizationService.getAuthorizationFromToken(token); - boolean isPublicResult = resultService.isPublicResult(chainTaskId, chainId); + boolean isPublicResult = resultService.isPublicResult(chainTaskId); boolean isAuthorizedOwnerOfResult = auth != null && resultService.isOwnerOfResult(chainId, chainTaskId, auth.getWalletAddress()) && authorizationService.isAuthorizationValid(auth); diff --git a/src/main/java/com/iexec/core/result/ResultService.java b/src/main/java/com/iexec/core/result/ResultService.java index cb684b2be..bde06b28a 100644 --- a/src/main/java/com/iexec/core/result/ResultService.java +++ b/src/main/java/com/iexec/core/result/ResultService.java @@ -70,7 +70,7 @@ boolean canUploadResult(String chainTaskId, String walletAddress, byte[] zip) { } boolean isResultInDatabase(String chainTaskId) { - if(isPublicResult(chainTaskId, 0)){ + if(isPublicResult(chainTaskId)){ return isResultInIpfs(chainTaskId); } return isResultInMongo(chainTaskId); @@ -91,7 +91,7 @@ String addResult(Result result, byte[] data) { return ""; } - if (isPublicResult(result.getChainTaskId())) { + if (iexecHubService.isPublicResult(result.getChainTaskId(), 0)) { return addResultToIPFS(result, data); } else { return addResultToMongo(result, data); @@ -109,6 +109,10 @@ private String addResultToIPFS(Result result, byte[] data) { return IPFS_ADDRESS_PREFIX + ipfsService.putContent(result.getChainTaskId(), data); } + public boolean isPublicResult(String chainTaskId) { + return iexecHubService.isPublicResult(chainTaskId, 0); + } + byte[] getResultByChainTaskId(String chainTaskId) throws IOException { String resultFileName = getResultFilename(chainTaskId); GridFsResource[] resources = gridOperations.getResources(resultFileName); @@ -138,17 +142,4 @@ boolean isOwnerOfResult(Integer chainId, String chainTaskId, String downloaderAd } return true; } - - boolean isPublicResult(String chainTaskId) { - return isPublicResult(chainTaskId, 0); - } - - boolean isPublicResult(String chainTaskId, Integer chainId) { - Optional beneficiary = iexecHubService.getTaskBeneficiary(chainTaskId, chainId); - if (!beneficiary.isPresent()) { - log.error("Failed to get beneficiary for isPublicResult() method [chainTaskId:{}]", chainTaskId); - return false; - } - return beneficiary.get().equals(BytesUtils.EMPTY_ADDRESS); - } } diff --git a/src/main/java/com/iexec/core/result/ipfs/IPFSService.java b/src/main/java/com/iexec/core/result/ipfs/IPFSService.java index 144a09c47..ed52e3a13 100644 --- a/src/main/java/com/iexec/core/result/ipfs/IPFSService.java +++ b/src/main/java/com/iexec/core/result/ipfs/IPFSService.java @@ -31,7 +31,7 @@ public Optional getContent(String ipfsHash) { return Optional.empty(); } - public boolean doesContentExists(String ipfsHash) { + public boolean doesContentExist(String ipfsHash) { return getContent(ipfsHash).isPresent(); } diff --git a/src/test/java/com/iexec/core/result/ResultServiceTest.java b/src/test/java/com/iexec/core/result/ResultServiceTest.java index 7773f5298..a642d09c3 100644 --- a/src/test/java/com/iexec/core/result/ResultServiceTest.java +++ b/src/test/java/com/iexec/core/result/ResultServiceTest.java @@ -214,15 +214,14 @@ public void isOwnerOfResult() { @Test public void isPublicResult() { - String beneficiary = BytesUtils.EMPTY_ADDRESS; - when(iexecHubService.getTaskBeneficiary(chainTaskId, chainId)).thenReturn(Optional.of(beneficiary)); - assertThat(resultService.isPublicResult(chainTaskId, chainId)).isTrue(); + when(iexecHubService.isPublicResult(chainTaskId, 0)).thenReturn(true); + assertThat(resultService.isPublicResult(chainTaskId)).isTrue(); } @Test public void isNotPublicResult() { String beneficiary = "0xb"; when(iexecHubService.getTaskBeneficiary(chainTaskId, chainId)).thenReturn(Optional.of(beneficiary)); - assertThat(resultService.isPublicResult(chainTaskId, chainId)).isFalse(); + assertThat(resultService.isPublicResult(chainTaskId)).isFalse(); } } \ No newline at end of file