Skip to content

Commit

Permalink
Use functional style to improve reading of method
Browse files Browse the repository at this point in the history
  • Loading branch information
mcornaton committed Dec 14, 2023
1 parent caff7a7 commit 5ddcf26
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions src/main/java/com/iexec/core/replicate/ReplicateSupplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,35 +191,8 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
}

try {
final Optional<ReplicatesList> oReplicatesList = replicatesService.getReplicatesList(chainTaskId);
// Check is only here to prevent
// "`Optional.get()` without `isPresent()` warning".
// This case should not happen.
if (oReplicatesList.isEmpty()) {
return false;
}

final ReplicatesList replicatesList = oReplicatesList.get();

final boolean hasWorkerAlreadyParticipated =
replicatesList.hasWorkerAlreadyParticipated(walletAddress);
if (hasWorkerAlreadyParticipated) {
return false;
}

final boolean taskNeedsMoreContributions = ConsensusHelper.doesTaskNeedMoreContributionsForConsensus(
chainTaskId,
replicatesList.getReplicates(),
task.getTrust(),
task.getMaxExecutionTime());

if (!taskNeedsMoreContributions
|| taskService.isConsensusReached(replicatesList)) {
return false;
}

return workerService.addChainTaskIdToWorker(chainTaskId, walletAddress)
.map(worker -> replicatesService.addNewReplicate(replicatesList, walletAddress))
return replicatesService.getReplicatesList(chainTaskId)
.map(replicatesList -> acceptOrRejectTask(task, walletAddress, replicatesList))
.orElse(false);
} finally {
// We should always unlock the task
Expand All @@ -229,6 +202,41 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
}
}

/**
* Given a {@link Task}, a {@code walletAddress} of a worker and a {@link ReplicatesList},
* tries to accept the task - i.e. create a new {@link Replicate}
* for that task on that worker.
*
* @param task {@link Task} needing at least one new {@link Replicate}.
* @param walletAddress Wallet address of a worker looking for new {@link Task}.
* @param replicatesList Replicates of given {@link Task}.
* @return {@literal true} if the task has been accepted,
* {@literal false} otherwise.
*/
boolean acceptOrRejectTask(Task task, String walletAddress, ReplicatesList replicatesList) {
final boolean hasWorkerAlreadyParticipated =
replicatesList.hasWorkerAlreadyParticipated(walletAddress);
if (hasWorkerAlreadyParticipated) {
return false;
}

final String chainTaskId = replicatesList.getChainTaskId();
final boolean taskNeedsMoreContributions = ConsensusHelper.doesTaskNeedMoreContributionsForConsensus(
chainTaskId,
replicatesList.getReplicates(),
task.getTrust(),
task.getMaxExecutionTime());

if (!taskNeedsMoreContributions
|| taskService.isConsensusReached(replicatesList)) {
return false;
}

return workerService.addChainTaskIdToWorker(chainTaskId, walletAddress)
.map(worker -> replicatesService.addNewReplicate(replicatesList, walletAddress))
.orElse(false);
}

/**
* Get notifications missed by the worker during the time it was absent.
*
Expand Down

0 comments on commit 5ddcf26

Please sign in to comment.