@@ -191,35 +191,8 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
191191 }
192192
193193 try {
194- final Optional <ReplicatesList > oReplicatesList = replicatesService .getReplicatesList (chainTaskId );
195- // Check is only here to prevent
196- // "`Optional.get()` without `isPresent()` warning".
197- // This case should not happen.
198- if (oReplicatesList .isEmpty ()) {
199- return false ;
200- }
201-
202- final ReplicatesList replicatesList = oReplicatesList .get ();
203-
204- final boolean hasWorkerAlreadyParticipated =
205- replicatesList .hasWorkerAlreadyParticipated (walletAddress );
206- if (hasWorkerAlreadyParticipated ) {
207- return false ;
208- }
209-
210- final boolean taskNeedsMoreContributions = ConsensusHelper .doesTaskNeedMoreContributionsForConsensus (
211- chainTaskId ,
212- replicatesList .getReplicates (),
213- task .getTrust (),
214- task .getMaxExecutionTime ());
215-
216- if (!taskNeedsMoreContributions
217- || taskService .isConsensusReached (replicatesList )) {
218- return false ;
219- }
220-
221- return workerService .addChainTaskIdToWorker (chainTaskId , walletAddress )
222- .map (worker -> replicatesService .addNewReplicate (replicatesList , walletAddress ))
194+ return replicatesService .getReplicatesList (chainTaskId )
195+ .map (replicatesList -> acceptOrRejectTask (task , walletAddress , replicatesList ))
223196 .orElse (false );
224197 } finally {
225198 // We should always unlock the task
@@ -229,6 +202,41 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
229202 }
230203 }
231204
205+ /**
206+ * Given a {@link Task}, a {@code walletAddress} of a worker and a {@link ReplicatesList},
207+ * tries to accept the task - i.e. create a new {@link Replicate}
208+ * for that task on that worker.
209+ *
210+ * @param task {@link Task} needing at least one new {@link Replicate}.
211+ * @param walletAddress Wallet address of a worker looking for new {@link Task}.
212+ * @param replicatesList Replicates of given {@link Task}.
213+ * @return {@literal true} if the task has been accepted,
214+ * {@literal false} otherwise.
215+ */
216+ boolean acceptOrRejectTask (Task task , String walletAddress , ReplicatesList replicatesList ) {
217+ final boolean hasWorkerAlreadyParticipated =
218+ replicatesList .hasWorkerAlreadyParticipated (walletAddress );
219+ if (hasWorkerAlreadyParticipated ) {
220+ return false ;
221+ }
222+
223+ final String chainTaskId = replicatesList .getChainTaskId ();
224+ final boolean taskNeedsMoreContributions = ConsensusHelper .doesTaskNeedMoreContributionsForConsensus (
225+ chainTaskId ,
226+ replicatesList .getReplicates (),
227+ task .getTrust (),
228+ task .getMaxExecutionTime ());
229+
230+ if (!taskNeedsMoreContributions
231+ || taskService .isConsensusReached (replicatesList )) {
232+ return false ;
233+ }
234+
235+ return workerService .addChainTaskIdToWorker (chainTaskId , walletAddress )
236+ .map (worker -> replicatesService .addNewReplicate (replicatesList , walletAddress ))
237+ .orElse (false );
238+ }
239+
232240 /**
233241 * Get notifications missed by the worker during the time it was absent.
234242 *
0 commit comments