Skip to content

Commit

Permalink
Merge branch 'master' into reopen
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugo Plouviez committed Jun 14, 2019
2 parents ef1cf43 + 46dc2e7 commit b51dbd3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.iexec.common.chain.ChainContributionStatus;
import com.iexec.common.replicate.ReplicateStatus;
import com.iexec.core.chain.IexecHubService;
import com.iexec.core.chain.Web3jService;
import com.iexec.core.configuration.CoreConfigurationService;
import com.iexec.core.replicate.ReplicatesService;
import com.iexec.core.task.TaskService;
Expand All @@ -27,8 +28,9 @@ public class ContributionUnnotifiedDetector extends UnnotifiedAbstractDetector {
public ContributionUnnotifiedDetector(TaskService taskService,
ReplicatesService replicatesService,
IexecHubService iexecHubService,
CoreConfigurationService coreConfigurationService) {
super(taskService, replicatesService, iexecHubService);
CoreConfigurationService coreConfigurationService,
Web3jService web3jService) {
super(taskService, replicatesService, iexecHubService, web3jService);
this.coreConfigurationService = coreConfigurationService;
dectectWhenOffchainTaskStatuses = TaskStatus.getWaitingContributionStatuses();
offchainCompleting = ReplicateStatus.CONTRIBUTING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.iexec.common.chain.ChainContributionStatus;
import com.iexec.common.replicate.ReplicateStatus;
import com.iexec.core.chain.IexecHubService;
import com.iexec.core.chain.Web3jService;
import com.iexec.core.configuration.CoreConfigurationService;
import com.iexec.core.replicate.ReplicatesService;
import com.iexec.core.task.TaskService;
Expand All @@ -27,8 +28,9 @@ public class RevealUnnotifiedDetector extends UnnotifiedAbstractDetector {
public RevealUnnotifiedDetector(TaskService taskService,
ReplicatesService replicatesService,
IexecHubService iexecHubService,
CoreConfigurationService coreConfigurationService) {
super(taskService, replicatesService, iexecHubService);
CoreConfigurationService coreConfigurationService,
Web3jService web3jService) {
super(taskService, replicatesService, iexecHubService, web3jService);
this.coreConfigurationService = coreConfigurationService;
dectectWhenTaskStatuses = TaskStatus.getWaitingContributionStatuses();
offchainCompleting = ReplicateStatus.REVEALING;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.iexec.core.detector.replicate;

import com.iexec.common.chain.ChainContributionStatus;
import com.iexec.common.replicate.ReplicateDetails;
import com.iexec.common.replicate.ReplicateStatus;
import com.iexec.common.replicate.ReplicateStatusModifier;
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;
Expand All @@ -26,13 +28,16 @@ public abstract class UnnotifiedAbstractDetector {
protected TaskService taskService;
protected ReplicatesService replicatesService;
protected IexecHubService iexecHubService;
protected Web3jService web3jService;

public UnnotifiedAbstractDetector(TaskService taskService,
ReplicatesService replicatesService,
IexecHubService iexecHubService) {
IexecHubService iexecHubService,
Web3jService web3jService) {
this.taskService = taskService;
this.replicatesService = replicatesService;
this.iexecHubService = iexecHubService;
this.web3jService = web3jService;
}

void dectectOnchainCompletedWhenOffchainCompleting(List<TaskStatus> dectectWhenOffchainTaskStatuses,
Expand Down Expand Up @@ -77,19 +82,39 @@ void dectectOnchainCompleted(List<TaskStatus> dectectWhenOffchainTaskStatuses,
}
}

void updateReplicateStatuses(String chainTaskId, Replicate replicate, ReplicateStatus offchainCompleted) {
private void updateReplicateStatuses(String chainTaskId, Replicate replicate, ReplicateStatus offchainCompleted) {
List<ReplicateStatus> statusesToUpdate;
if (replicate.getCurrentStatus().equals(WORKER_LOST)) {
statusesToUpdate = getMissingStatuses(replicate.getLastButOneStatus(), offchainCompleted);
} else {
statusesToUpdate = getMissingStatuses(replicate.getCurrentStatus(), offchainCompleted);
}

String wallet = replicate.getWalletAddress();

for (ReplicateStatus statusToUpdate : statusesToUpdate) {
replicatesService.updateReplicateStatus(chainTaskId, replicate.getWalletAddress(),
statusToUpdate, ReplicateStatusModifier.POOL_MANAGER);
}
}
// add details to the update if needed
switch (statusToUpdate) {
case CONTRIBUTED:
// retrieve the contribution block for that wallet
long contributedBlock = iexecHubService.getContributionBlockNumber(chainTaskId, wallet, web3jService.getLatestBlockNumber());
replicatesService.updateReplicateStatus(chainTaskId, wallet,
statusToUpdate, ReplicateStatusModifier.POOL_MANAGER, new ReplicateDetails(contributedBlock));
break;
case REVEALED:
// retrieve the reveal block for that wallet
long revealedBlock = iexecHubService.getRevealBlockNumber(chainTaskId, wallet, web3jService.getLatestBlockNumber());
replicatesService.updateReplicateStatus(chainTaskId, wallet,
statusToUpdate, ReplicateStatusModifier.POOL_MANAGER, new ReplicateDetails(revealedBlock));
break;
default:
// by default, no need to retrieve anything
replicatesService.updateReplicateStatus(chainTaskId, wallet,
statusToUpdate, ReplicateStatusModifier.POOL_MANAGER);

}


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.iexec.common.replicate.ReplicateStatusChange;
import com.iexec.common.replicate.ReplicateStatusModifier;
import com.iexec.core.chain.IexecHubService;
import com.iexec.core.chain.Web3jService;
import com.iexec.core.configuration.CoreConfigurationService;
import com.iexec.core.replicate.Replicate;
import com.iexec.core.replicate.ReplicatesService;
Expand All @@ -17,7 +18,7 @@
import java.util.Arrays;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -39,6 +40,9 @@ public class ContributionUnnotifiedDetectorTests {
@Mock
private CoreConfigurationService coreConfigurationService;

@Mock
private Web3jService web3jService;

@Spy
@InjectMocks
private ContributionUnnotifiedDetector contributionDetector;
Expand All @@ -63,10 +67,13 @@ public void shouldDetectUnNotifiedContributedAfterContributing() {
when(coreConfigurationService.getUnnotifiedContributionDetectorPeriod()).thenReturn(DETECTOR_PERIOD);
when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate));
when(iexecHubService.doesWishedStatusMatchesOnChainStatus(any(), any(), any())).thenReturn(true);
when(web3jService.getLatestBlockNumber()).thenReturn(11L);
when(iexecHubService.getContributionBlockNumber(anyString(), anyString(), anyLong())).thenReturn(10L);

contributionDetector.detectOnchainContributedWhenOffchainContributing();

Mockito.verify(replicatesService, Mockito.times(1))//Missed CONTRIBUTED
.updateReplicateStatus(any(), any(), any(), any());
.updateReplicateStatus(any(), any(), any(), any(), any());
}

@Test
Expand Down Expand Up @@ -120,10 +127,13 @@ public void shouldDetectUnNotifiedContributed1() {
when(coreConfigurationService.getUnnotifiedContributionDetectorPeriod()).thenReturn(DETECTOR_PERIOD);
when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate));
when(iexecHubService.doesWishedStatusMatchesOnChainStatus(any(), any(), any())).thenReturn(true);
when(web3jService.getLatestBlockNumber()).thenReturn(11L);
when(iexecHubService.getContributionBlockNumber(anyString(), anyString(), anyLong())).thenReturn(10L);

contributionDetector.detectOnchainContributed();

Mockito.verify(replicatesService, Mockito.times(1))//Missed CONTRIBUTED
.updateReplicateStatus(any(), any(), any(), any());
.updateReplicateStatus(any(), any(), any(), any(), any());
}

@Test
Expand All @@ -138,10 +148,13 @@ public void shouldDetectUnNotifiedContributed2() {
when(coreConfigurationService.getUnnotifiedContributionDetectorPeriod()).thenReturn(DETECTOR_PERIOD);
when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate));
when(iexecHubService.doesWishedStatusMatchesOnChainStatus(any(), any(), any())).thenReturn(true);
when(web3jService.getLatestBlockNumber()).thenReturn(11L);
when(iexecHubService.getContributionBlockNumber(anyString(), anyString(), anyLong())).thenReturn(10L);

contributionDetector.detectOnchainContributed();

Mockito.verify(replicatesService, Mockito.times(2))//Missed CONTRIBUTING & CONTRIBUTED
.updateReplicateStatus(any(), any(), any(), any());
Mockito.verify(replicatesService, Mockito.times(1))//Missed CONTRIBUTING & CONTRIBUTED
.updateReplicateStatus(any(), any(), any(), any(), any());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.iexec.common.replicate.ReplicateStatusChange;
import com.iexec.common.replicate.ReplicateStatusModifier;
import com.iexec.core.chain.IexecHubService;
import com.iexec.core.chain.Web3jService;
import com.iexec.core.configuration.CoreConfigurationService;
import com.iexec.core.replicate.Replicate;
import com.iexec.core.replicate.ReplicatesService;
Expand All @@ -16,7 +17,7 @@

import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -38,6 +39,9 @@ public class RevealUnnotifiedDetectorTests {
@Mock
private CoreConfigurationService coreConfigurationService;

@Mock
private Web3jService web3jService;

@Spy
@InjectMocks
private RevealUnnotifiedDetector revealDetector;
Expand All @@ -62,10 +66,13 @@ public void shouldDetectUnNotifiedRevealedAfterRevealing() {
when(coreConfigurationService.getUnnotifiedRevealDetectorPeriod()).thenReturn(DETECTOR_PERIOD);
when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate));
when(iexecHubService.doesWishedStatusMatchesOnChainStatus(any(), any(), any())).thenReturn(true);
when(web3jService.getLatestBlockNumber()).thenReturn(11L);
when(iexecHubService.getRevealBlockNumber(anyString(), anyString(), anyLong())).thenReturn(10L);

revealDetector.detectOnchainRevealedWhenOffchainRevealed();

Mockito.verify(replicatesService, Mockito.times(1))//Missed REVEALED
.updateReplicateStatus(any(), any(), any(), any());
.updateReplicateStatus(any(), any(), any(), any(), any());
}

@Test
Expand Down Expand Up @@ -119,10 +126,13 @@ public void shouldDetectUnNotifiedRevealed1() {
when(coreConfigurationService.getUnnotifiedRevealDetectorPeriod()).thenReturn(DETECTOR_PERIOD);
when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate));
when(iexecHubService.doesWishedStatusMatchesOnChainStatus(any(), any(), any())).thenReturn(true);
when(web3jService.getLatestBlockNumber()).thenReturn(11L);
when(iexecHubService.getRevealBlockNumber(anyString(), anyString(), anyLong())).thenReturn(10L);

revealDetector.detectOnchainRevealed();

Mockito.verify(replicatesService, Mockito.times(1))//Missed REVEALED
.updateReplicateStatus(any(), any(), any(), any());
.updateReplicateStatus(any(), any(), any(), any(), any());
}

@Test
Expand All @@ -137,10 +147,13 @@ public void shouldDetectUnNotifiedRevealed2() {
when(coreConfigurationService.getUnnotifiedRevealDetectorPeriod()).thenReturn(DETECTOR_PERIOD);
when(replicatesService.getReplicates(any())).thenReturn(Collections.singletonList(replicate));
when(iexecHubService.doesWishedStatusMatchesOnChainStatus(any(), any(), any())).thenReturn(true);
when(web3jService.getLatestBlockNumber()).thenReturn(11L);
when(iexecHubService.getRevealBlockNumber(anyString(), anyString(), anyLong())).thenReturn(10L);

revealDetector.detectOnchainRevealed();

Mockito.verify(replicatesService, Mockito.times(2))//Missed REVEALING & REVEALED
.updateReplicateStatus(any(), any(), any(), any());
Mockito.verify(replicatesService, Mockito.times(1))//Missed REVEALING & REVEALED
.updateReplicateStatus(any(), any(), any(), any(), any());
}

@Test
Expand Down

0 comments on commit b51dbd3

Please sign in to comment.