From f6d30f11d575c000aaf04e722206bbf96e6a185f Mon Sep 17 00:00:00 2001 From: Maxence Cornaton Date: Fri, 12 May 2023 09:42:43 +0200 Subject: [PATCH 1/2] Refactor unnotified detectors --- CHANGELOG.md | 1 + .../ContributionUnnotifiedDetector.java | 75 ++----------- .../replicate/RevealUnnotifiedDetector.java | 71 ++---------- .../replicate/UnnotifiedAbstractDetector.java | 105 ++++++++++++------ .../task/listener/ReplicateListeners.java | 2 +- .../ContributionUnnotifiedDetectorTests.java | 12 +- .../RevealUnnotifiedDetectorTests.java | 12 +- .../replicate/ReplicateListenersTests.java | 9 +- 8 files changed, 112 insertions(+), 175 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5210e618..73de6f092 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file. - Remove unused methods in `IexecHubService`. (#572) - Clean unused Replicate methods and update tests. (#573) - Clean unused `ReplicateStatus#RESULT_UPLOAD_REQUEST_FAILED`. (#575) +- Refactor unnotified detectors to avoid code duplication. (#580) ### Dependency Upgrades - Upgrade to `iexec-common` 8.0.0-NEXT-SNAPSHOT. (#571 #575) - Add new `iexec-commons-poco` 1.0.2-NEXT-SNAPSHOT dependency. (#571 #574) diff --git a/src/main/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetector.java b/src/main/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetector.java index af2731af6..19c0f9884 100644 --- a/src/main/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetector.java +++ b/src/main/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetector.java @@ -19,7 +19,6 @@ import com.iexec.common.replicate.ReplicateStatus; import com.iexec.commons.poco.chain.ChainContributionStatus; import com.iexec.core.chain.IexecHubService; -import com.iexec.core.chain.Web3jService; import com.iexec.core.configuration.CronConfiguration; import com.iexec.core.replicate.ReplicatesService; import com.iexec.core.task.TaskService; @@ -28,80 +27,28 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import java.util.List; - @Slf4j @Service public class ContributionUnnotifiedDetector extends UnnotifiedAbstractDetector { - private static final int LESS_OFTEN_DETECTOR_FREQUENCY = 10; - private final List dectectWhenOffchainTaskStatuses; - private final ReplicateStatus offchainCompleting; - private final ReplicateStatus offchainCompleted; - private final ChainContributionStatus onchainCompleted; - private final int detectorRate; - - private int detectorOccurrence; - public ContributionUnnotifiedDetector(TaskService taskService, ReplicatesService replicatesService, IexecHubService iexecHubService, - Web3jService web3jService, CronConfiguration cronConfiguration) { - super(taskService, replicatesService, iexecHubService, web3jService); - dectectWhenOffchainTaskStatuses = TaskStatus.getWaitingContributionStatuses(); - offchainCompleting = ReplicateStatus.CONTRIBUTING; - offchainCompleted = ReplicateStatus.CONTRIBUTED; - onchainCompleted = ChainContributionStatus.CONTRIBUTED; - this.detectorRate = cronConfiguration.getContribute(); + super( + taskService, + replicatesService, + iexecHubService, + TaskStatus.getWaitingContributionStatuses(), + ReplicateStatus.CONTRIBUTING, + ReplicateStatus.CONTRIBUTED, + ChainContributionStatus.CONTRIBUTED, + cronConfiguration.getContribute()); } - /** - * Detects onchain CONTRIBUTED only if replicates are offchain CONTRIBUTING and - * onchain CONTRIBUTED if replicates are not CONTRIBUTED. - * The second detection is not always ran, depending on the detector run occurrences. - */ + @Override @Scheduled(fixedRateString = "#{@cronConfiguration.getContribute()}") public void detectOnChainChanges() { - detectOnchainContributedWhenOffchainContributing(); - - detectorOccurrence++; - if (detectorOccurrence % LESS_OFTEN_DETECTOR_FREQUENCY == 0) { - detectOnchainContributed(); - } + super.detectOnChainChanges(); } - - /* - * Detecting onchain CONTRIBUTED only if replicates are offchain CONTRIBUTING - * (worker didn't notify last offchain CONTRIBUTED) - * We want to detect them very often since it's highly probable - */ - public void detectOnchainContributedWhenOffchainContributing() { - log.debug("Detect onchain Contributed (when offchain Contributing) [retryIn:{}]", - this.detectorRate); - dectectOnchainCompletedWhenOffchainCompleting( - dectectWhenOffchainTaskStatuses, - offchainCompleting, - offchainCompleted, - onchainCompleted - ); - } - - /* - * Detecting onchain CONTRIBUTED if replicates are not CONTRIBUTED - * (worker didn't notify any status) - * We want to detect them: - * - Frequently but no so often since it's eth node resource consuming and less probable - * - When we receive a CANT_CONTRIBUTE_SINCE_TASK_NOT_ACTIVE - */ - public void detectOnchainContributed() { - log.debug("Detect onchain Contributed [retryIn:{}]", this.detectorRate * LESS_OFTEN_DETECTOR_FREQUENCY); - dectectOnchainCompleted( - dectectWhenOffchainTaskStatuses, - offchainCompleting, - offchainCompleted, - onchainCompleted - ); - } - } diff --git a/src/main/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetector.java b/src/main/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetector.java index 82743f8ab..cfd8cc3bd 100644 --- a/src/main/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetector.java +++ b/src/main/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetector.java @@ -19,7 +19,6 @@ import com.iexec.common.replicate.ReplicateStatus; import com.iexec.commons.poco.chain.ChainContributionStatus; import com.iexec.core.chain.IexecHubService; -import com.iexec.core.chain.Web3jService; import com.iexec.core.configuration.CronConfiguration; import com.iexec.core.replicate.ReplicatesService; import com.iexec.core.task.TaskService; @@ -28,76 +27,28 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import java.util.List; - @Slf4j @Service public class RevealUnnotifiedDetector extends UnnotifiedAbstractDetector { - private static final int LESS_OFTEN_DETECTOR_FREQUENCY = 10; - private final List dectectWhenTaskStatuses; - private final ReplicateStatus offchainCompleting; - private final ReplicateStatus offchainCompleted; - private final ChainContributionStatus onchainCompleted; - private final CronConfiguration cronConfiguration; - - private int detectorOccurrence; - public RevealUnnotifiedDetector(TaskService taskService, ReplicatesService replicatesService, IexecHubService iexecHubService, - Web3jService web3jService, CronConfiguration cronConfiguration) { - super(taskService, replicatesService, iexecHubService, web3jService); - this.cronConfiguration = cronConfiguration; - dectectWhenTaskStatuses = TaskStatus.getWaitingRevealStatuses(); - offchainCompleting = ReplicateStatus.REVEALING; - offchainCompleted = ReplicateStatus.REVEALED; - onchainCompleted = ChainContributionStatus.REVEALED; + super( + taskService, + replicatesService, + iexecHubService, + TaskStatus.getWaitingRevealStatuses(), + ReplicateStatus.REVEALING, + ReplicateStatus.REVEALED, + ChainContributionStatus.REVEALED, + cronConfiguration.getReveal()); } + @Override @Scheduled(fixedRateString = "#{@cronConfiguration.getReveal()}") public void detectOnChainChanges() { - detectOnchainRevealedWhenOffchainRevealed(); - - detectorOccurrence++; - if (detectorOccurrence % LESS_OFTEN_DETECTOR_FREQUENCY == 0) { - detectOnchainRevealed(); - } + super.detectOnChainChanges(); } - - /* - * Detecting onchain REVEALED only if replicates are offchain REVEALING - * (worker didn't notify last offchain REVEALED) - * We want to detect them very often since it's highly probable - */ - public void detectOnchainRevealedWhenOffchainRevealed() { - log.debug("Detect onchain Revealed (when offchain Revealing) [retryIn:{}]", - cronConfiguration.getReveal()); - dectectOnchainCompletedWhenOffchainCompleting( - dectectWhenTaskStatuses, - offchainCompleting, - offchainCompleted, - onchainCompleted - ); - } - - /* - * Detecting onchain REVEALED if replicates are not REVEALED - * (worker didn't notify any status) - * We want to detect them: - * - Frequently but no so often since it's eth node resource consuming and less probable - * - When we receive a CANT_REVEAL - */ - public void detectOnchainRevealed() { - log.debug("Detect onchain Revealed [retryIn:{}]", - cronConfiguration.getReveal() * LESS_OFTEN_DETECTOR_FREQUENCY); - dectectOnchainCompleted( - dectectWhenTaskStatuses, - offchainCompleting, - offchainCompleted, - onchainCompleted - ); - } - } diff --git a/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java b/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java index d8c4bdc66..5a59a454b 100644 --- a/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java +++ b/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java @@ -21,7 +21,6 @@ import com.iexec.commons.poco.chain.ChainContributionStatus; import com.iexec.commons.poco.chain.ChainReceipt; import com.iexec.core.chain.IexecHubService; -import com.iexec.core.chain.Web3jService; import com.iexec.core.replicate.Replicate; import com.iexec.core.replicate.ReplicatesService; import com.iexec.core.task.Task; @@ -36,27 +35,65 @@ @Slf4j public abstract class UnnotifiedAbstractDetector { - - - protected TaskService taskService; - protected ReplicatesService replicatesService; - protected IexecHubService iexecHubService; - protected Web3jService web3jService; - - public UnnotifiedAbstractDetector(TaskService taskService, - ReplicatesService replicatesService, - IexecHubService iexecHubService, - Web3jService web3jService) { + private static final int LESS_OFTEN_DETECTOR_FREQUENCY = 10; + + private final TaskService taskService; + private final ReplicatesService replicatesService; + private final IexecHubService iexecHubService; + + private final List detectWhenOffChainTaskStatuses; + private final ReplicateStatus offchainCompleting; + private final ReplicateStatus offchainCompleted; + private final ChainContributionStatus onchainCompleted; + private final int detectorRate; + + private int detectorOccurrence = 0; + + protected UnnotifiedAbstractDetector(TaskService taskService, + ReplicatesService replicatesService, + IexecHubService iexecHubService, + List detectWhenOffChainTaskStatuses, + ReplicateStatus offchainCompleting, + ReplicateStatus offchainCompleted, + ChainContributionStatus onchainCompleted, + int detectorRate) { this.taskService = taskService; this.replicatesService = replicatesService; this.iexecHubService = iexecHubService; - this.web3jService = web3jService; + + this.detectWhenOffChainTaskStatuses = detectWhenOffChainTaskStatuses; + this.offchainCompleting = offchainCompleting; + this.offchainCompleted = offchainCompleted; + this.onchainCompleted = onchainCompleted; + this.detectorRate = detectorRate; + } + + /** + * Detects the following issues: + *
    + *
  • `onchainCompleted` status only if replicates are in `offchainCompleting` status;
  • + *
  • `onchainCompleted` if replicates are not in `offchainCompleted` status.
  • + *
+ * The second detection is not always ran, depending on the detector run occurrences. + */ + void detectOnChainChanges() { + detectOnchainCompletedWhenOffchainCompleting(); + + detectorOccurrence++; + if (detectorOccurrence % LESS_OFTEN_DETECTOR_FREQUENCY == 0) { + detectOnchainCompleted(); + } } - void dectectOnchainCompletedWhenOffchainCompleting(List detectWhenOffChainTaskStatuses, - ReplicateStatus offchainCompleting, - ReplicateStatus offchainCompleted, - ChainContributionStatus onchainCompleted) { + /** + * Detecting onchain `offchainCompleted` status only if replicates are offchain `offchainCompleting` + * (worker didn't notify last offchain `offchainCompleted` status) + * We want to detect them very often since it's highly probable + */ + void detectOnchainCompletedWhenOffchainCompleting() { + log.debug("Detect onchain {} (when offchain {}) [retryIn:{}]", + this.offchainCompleted, this.offchainCompleting, this.detectorRate); + for (Task task : taskService.findByCurrentStatus(detectWhenOffChainTaskStatuses)) { for (Replicate replicate : replicatesService.getReplicates(task.getChainTaskId())) { ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus(); @@ -69,16 +106,21 @@ void dectectOnchainCompletedWhenOffchainCompleting(List detectWhenOf if (statusTrueOnChain) { log.info("Detected confirmed missing update (replicate) [is:{}, should:{}, taskId:{}]", lastRelevantStatus, onchainCompleted, task.getChainTaskId()); - updateReplicateStatuses(task, replicate, offchainCompleted); + updateReplicateStatuses(task, replicate); } } } } - void dectectOnchainCompleted(List detectWhenOffChainTaskStatuses, - ReplicateStatus offchainCompleting, - ReplicateStatus offchainCompleted, - ChainContributionStatus onchainCompleted) { + /** + * Detecting `onchainCompleted` if replicates are not in `offchainCompleted` status + * (worker didn't notify any status) + * We want to detect them: + * - Frequently but no so often since it's eth node resource consuming and less probable + * - When we receive a "can't do " relative to the `onchainCompleted` status (e.g.: `CANNOT_REVEAL`) + */ + public void detectOnchainCompleted() { + log.debug("Detect onchain {} [retryIn:{}]", onchainCompleted, this.detectorRate * LESS_OFTEN_DETECTOR_FREQUENCY); for (Task task : taskService.findByCurrentStatus(detectWhenOffChainTaskStatuses)) { for (Replicate replicate : replicatesService.getReplicates(task.getChainTaskId())) { ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus(); @@ -92,7 +134,7 @@ void dectectOnchainCompleted(List detectWhenOffChainTaskStatuses, if (statusTrueOnChain) { log.info("Detected confirmed missing update (replicate) [is:{}, should:{}, taskId:{}]", lastRelevantStatus, onchainCompleted, task.getChainTaskId()); - updateReplicateStatuses(task, replicate, offchainCompleted); + updateReplicateStatuses(task, replicate); } } } @@ -106,17 +148,16 @@ void dectectOnchainCompleted(List detectWhenOffChainTaskStatuses, * we couldn't get the metadata (block number) of the tx. * In this case we put 0 as default block number. */ - private void updateReplicateStatuses(Task task, Replicate replicate, ReplicateStatus offchainCompleted) { - String chainTaskId = task.getChainTaskId(); + private void updateReplicateStatuses(Task task, Replicate replicate) { + final String chainTaskId = task.getChainTaskId(); long initBlockNumber = task.getInitializationBlockNumber(); - List statusesToUpdate; - if (replicate.getCurrentStatus().equals(WORKER_LOST)) { - statusesToUpdate = getMissingStatuses(replicate.getLastButOneStatus(), offchainCompleted); - } else { - statusesToUpdate = getMissingStatuses(replicate.getCurrentStatus(), offchainCompleted); - } - String wallet = replicate.getWalletAddress(); + final ReplicateStatus retrieveFrom = replicate.getCurrentStatus().equals(WORKER_LOST) + ? replicate.getLastButOneStatus() + : replicate.getCurrentStatus(); + final List statusesToUpdate = getMissingStatuses(retrieveFrom, offchainCompleted); + + final String wallet = replicate.getWalletAddress(); for (ReplicateStatus statusToUpdate : statusesToUpdate) { // add details to the update if needed diff --git a/src/main/java/com/iexec/core/task/listener/ReplicateListeners.java b/src/main/java/com/iexec/core/task/listener/ReplicateListeners.java index 1f06240b9..47449f9da 100644 --- a/src/main/java/com/iexec/core/task/listener/ReplicateListeners.java +++ b/src/main/java/com/iexec/core/task/listener/ReplicateListeners.java @@ -78,7 +78,7 @@ public void onReplicateUpdatedEvent(ReplicateUpdatedEvent event) { * task to consensus_reached */ if (cause != null && cause.equals(TASK_NOT_ACTIVE)) { - contributionUnnotifiedDetector.detectOnchainContributed(); + contributionUnnotifiedDetector.detectOnchainCompleted(); } /* diff --git a/src/test/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetectorTests.java b/src/test/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetectorTests.java index bad0aee23..d8305c65f 100644 --- a/src/test/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetectorTests.java +++ b/src/test/java/com/iexec/core/detector/replicate/ContributionUnnotifiedDetectorTests.java @@ -114,7 +114,7 @@ void shouldDetectUnNotifiedContributedAfterContributing() { when(iexecHubService.getContributionBlock(anyString(), anyString(), anyLong())) .thenReturn(ChainReceipt.builder().blockNumber(10L).txHash("0xabcef").build()); - contributionDetector.detectOnchainContributedWhenOffchainContributing(); + contributionDetector.detectOnchainCompletedWhenOffchainCompleting(); Mockito.verify(replicatesService, Mockito.times(1)) // Missed CONTRIBUTED .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -133,7 +133,7 @@ void shouldDetectUnNotifiedContributedAfterContributingSinceBeforeContributing() when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate)); when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(true); - contributionDetector.detectOnchainContributedWhenOffchainContributing(); + contributionDetector.detectOnchainCompletedWhenOffchainCompleting(); Mockito.verify(replicatesService, Mockito.times(0)) .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -151,7 +151,7 @@ void shouldNotDetectUnNotifiedContributedAfterContributingSinceNotContributedOnC // when(cronConfiguration.getContribute()).thenReturn(DETECTOR_PERIOD); when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate)); when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(false); - contributionDetector.detectOnchainContributedWhenOffchainContributing(); + contributionDetector.detectOnchainCompletedWhenOffchainCompleting(); Mockito.verify(replicatesService, Mockito.times(0)) .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -178,7 +178,7 @@ void shouldDetectUnNotifiedContributed1() { .txHash("0xabcef") .build()); - contributionDetector.detectOnchainContributed(); + contributionDetector.detectOnchainCompleted(); Mockito.verify(replicatesService, Mockito.times(1))//Missed CONTRIBUTED .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -202,7 +202,7 @@ void shouldDetectUnNotifiedContributed2() { .txHash("0xabcef") .build()); - contributionDetector.detectOnchainContributed(); + contributionDetector.detectOnchainCompleted(); Mockito.verify(replicatesService, Mockito.times(1))//Missed CONTRIBUTING & CONTRIBUTED .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -220,7 +220,7 @@ void shouldNotDetectUnNotifiedContributedSinceContributed() { when(cronConfiguration.getContribute()).thenReturn(DETECTOR_PERIOD); when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate)); when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(true); - contributionDetector.detectOnchainContributed(); + contributionDetector.detectOnchainCompleted(); Mockito.verify(replicatesService, Mockito.times(0)) .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); diff --git a/src/test/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetectorTests.java b/src/test/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetectorTests.java index 936c16f9b..99b8b8266 100644 --- a/src/test/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetectorTests.java +++ b/src/test/java/com/iexec/core/detector/replicate/RevealUnnotifiedDetectorTests.java @@ -115,7 +115,7 @@ void shouldDetectUnNotifiedRevealedAfterRevealing() { .txHash("0xabcef") .build()); - revealDetector.detectOnchainRevealedWhenOffchainRevealed(); + revealDetector.detectOnchainCompletedWhenOffchainCompleting(); Mockito.verify(replicatesService, Mockito.times(1))//Missed REVEALED .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -133,7 +133,7 @@ void shouldDetectUnNotifiedRevealedAfterRevealingSinceBeforeRevealing() { when(cronConfiguration.getReveal()).thenReturn(DETECTOR_PERIOD); when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate)); when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(true); - revealDetector.detectOnchainRevealedWhenOffchainRevealed(); + revealDetector.detectOnchainCompletedWhenOffchainCompleting(); Mockito.verify(replicatesService, Mockito.times(0)) .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -150,7 +150,7 @@ void shouldNotDetectUnNotifiedRevealedAfterRevealingSinceNotRevealedOnChain() { when(cronConfiguration.getReveal()).thenReturn(DETECTOR_PERIOD); when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate)); when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(false); - revealDetector.detectOnchainRevealedWhenOffchainRevealed(); + revealDetector.detectOnchainCompletedWhenOffchainCompleting(); Mockito.verify(replicatesService, Mockito.times(0)) .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -177,7 +177,7 @@ void shouldDetectUnNotifiedRevealed1() { .txHash("0xabcef") .build()); - revealDetector.detectOnchainRevealed(); + revealDetector.detectOnchainCompleted(); Mockito.verify(replicatesService, Mockito.times(1))//Missed REVEALED .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -206,7 +206,7 @@ void shouldDetectUnNotifiedRevealed2() { .txHash("0xabcef") .build()); - revealDetector.detectOnchainRevealed(); + revealDetector.detectOnchainCompleted(); Mockito.verify(replicatesService, Mockito.times(1))//Missed REVEALING & REVEALED .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); @@ -224,7 +224,7 @@ void shouldNotDetectUnNotifiedRevealedSinceRevealed() { when(cronConfiguration.getReveal()).thenReturn(DETECTOR_PERIOD); when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate)); when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(true); - revealDetector.detectOnchainRevealed(); + revealDetector.detectOnchainCompleted(); Mockito.verify(replicatesService, Mockito.times(0)) .updateReplicateStatus(any(), any(), any(), any(ReplicateStatusDetails.class)); diff --git a/src/test/java/com/iexec/core/replicate/ReplicateListenersTests.java b/src/test/java/com/iexec/core/replicate/ReplicateListenersTests.java index 73486eafb..c250bc7c4 100644 --- a/src/test/java/com/iexec/core/replicate/ReplicateListenersTests.java +++ b/src/test/java/com/iexec/core/replicate/ReplicateListenersTests.java @@ -19,7 +19,6 @@ import com.iexec.common.replicate.ReplicateStatus; import com.iexec.common.replicate.ReplicateStatusUpdate; import com.iexec.core.detector.replicate.ContributionUnnotifiedDetector; -import com.iexec.core.task.TaskService; import com.iexec.core.task.listener.ReplicateListeners; import com.iexec.core.task.update.TaskUpdateRequestManager; import com.iexec.core.worker.WorkerService; @@ -46,8 +45,6 @@ class ReplicateListenersTests { private final static String CHAIN_TASK_ID = "chainTaskId"; private final static String WORKER_WALLET = "0xwallet1"; - @Mock - private TaskService taskService; @Mock private WorkerService workerService; @Mock @@ -121,7 +118,7 @@ void shouldTriggerDetectOnchainContributedSinceTaskNotActive() { replicateListeners.onReplicateUpdatedEvent(replicateUpdatedEvent); - Mockito.verify(contributionUnnotifiedDetector, Mockito.times(1)).detectOnchainContributed(); + Mockito.verify(contributionUnnotifiedDetector, Mockito.times(1)).detectOnchainCompleted(); } @Test @@ -135,7 +132,7 @@ void shouldNotTriggerDetectOnchain() { replicateListeners.onReplicateUpdatedEvent(replicateUpdatedEvent); } - Mockito.verify(contributionUnnotifiedDetector, Mockito.times(0)).detectOnchainContributed(); + Mockito.verify(contributionUnnotifiedDetector, Mockito.times(0)).detectOnchainCompleted(); } @Test @@ -148,7 +145,7 @@ void shouldNotTriggerDetectOnchainContributedSinceCauseIsNull() { replicateListeners.onReplicateUpdatedEvent(replicateUpdatedEvent); - Mockito.verify(contributionUnnotifiedDetector, Mockito.times(0)).detectOnchainContributed(); + Mockito.verify(contributionUnnotifiedDetector, Mockito.times(0)).detectOnchainCompleted(); } static Stream getUncompletableStatuses() { From b6428d0210efd10652ee728c38bb8cb173b1d51e Mon Sep 17 00:00:00 2001 From: Maxence Cornaton Date: Fri, 12 May 2023 16:35:34 +0200 Subject: [PATCH 2/2] Make variables final --- .../replicate/UnnotifiedAbstractDetector.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java b/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java index 5a59a454b..0b5b5da68 100644 --- a/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java +++ b/src/main/java/com/iexec/core/detector/replicate/UnnotifiedAbstractDetector.java @@ -96,12 +96,16 @@ void detectOnchainCompletedWhenOffchainCompleting() { for (Task task : taskService.findByCurrentStatus(detectWhenOffChainTaskStatuses)) { for (Replicate replicate : replicatesService.getReplicates(task.getChainTaskId())) { - ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus(); + final ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus(); if (lastRelevantStatus != offchainCompleting) { continue; } - boolean statusTrueOnChain = iexecHubService.isStatusTrueOnChain(task.getChainTaskId(), replicate.getWalletAddress(), onchainCompleted); + final boolean statusTrueOnChain = iexecHubService.isStatusTrueOnChain( + task.getChainTaskId(), + replicate.getWalletAddress(), + onchainCompleted + ); if (statusTrueOnChain) { log.info("Detected confirmed missing update (replicate) [is:{}, should:{}, taskId:{}]", @@ -123,13 +127,17 @@ public void detectOnchainCompleted() { log.debug("Detect onchain {} [retryIn:{}]", onchainCompleted, this.detectorRate * LESS_OFTEN_DETECTOR_FREQUENCY); for (Task task : taskService.findByCurrentStatus(detectWhenOffChainTaskStatuses)) { for (Replicate replicate : replicatesService.getReplicates(task.getChainTaskId())) { - ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus(); + final ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus(); if (lastRelevantStatus == offchainCompleted) { continue; } - boolean statusTrueOnChain = iexecHubService.isStatusTrueOnChain(task.getChainTaskId(), replicate.getWalletAddress(), onchainCompleted); + final boolean statusTrueOnChain = iexecHubService.isStatusTrueOnChain( + task.getChainTaskId(), + replicate.getWalletAddress(), + onchainCompleted + ); if (statusTrueOnChain) { log.info("Detected confirmed missing update (replicate) [is:{}, should:{}, taskId:{}]", @@ -150,7 +158,7 @@ public void detectOnchainCompleted() { */ private void updateReplicateStatuses(Task task, Replicate replicate) { final String chainTaskId = task.getChainTaskId(); - long initBlockNumber = task.getInitializationBlockNumber(); + final long initBlockNumber = task.getInitializationBlockNumber(); final ReplicateStatus retrieveFrom = replicate.getCurrentStatus().equals(WORKER_LOST) ? replicate.getLastButOneStatus() @@ -164,17 +172,17 @@ private void updateReplicateStatuses(Task task, Replicate replicate) { switch (statusToUpdate) { case CONTRIBUTED: // retrieve the contribution block for that wallet - ChainReceipt contributedBlock = iexecHubService.getContributionBlock(chainTaskId, + final ChainReceipt contributedBlock = iexecHubService.getContributionBlock(chainTaskId, wallet, initBlockNumber); - long contributedBlockNumber = contributedBlock != null ? contributedBlock.getBlockNumber() : 0; + final long contributedBlockNumber = contributedBlock != null ? contributedBlock.getBlockNumber() : 0; replicatesService.updateReplicateStatus(chainTaskId, wallet, statusToUpdate, new ReplicateStatusDetails(contributedBlockNumber)); break; case REVEALED: // retrieve the reveal block for that wallet - ChainReceipt revealedBlock = iexecHubService.getRevealBlock(chainTaskId, wallet, + final ChainReceipt revealedBlock = iexecHubService.getRevealBlock(chainTaskId, wallet, initBlockNumber); - long revealedBlockNumber = revealedBlock != null ? revealedBlock.getBlockNumber() : 0; + final long revealedBlockNumber = revealedBlock != null ? revealedBlock.getBlockNumber() : 0; replicatesService.updateReplicateStatus(chainTaskId, wallet, statusToUpdate, new ReplicateStatusDetails(revealedBlockNumber)); break;