@@ -86,14 +86,9 @@ public ReplicateSupplyService(ReplicatesService replicatesService,
8686 */
8787 @ Retryable (value = {OptimisticLockingFailureException .class }, maxAttempts = 5 )
8888 Optional <ReplicateTaskSummary > getAvailableReplicateTaskSummary (long workerLastBlock , String walletAddress ) {
89- // return empty if max computing task is reached or if the worker is not found
90- if (!workerService .canAcceptMoreWorks (walletAddress )) {
91- return Optional .empty ();
92- }
93-
9489 // return empty if the worker is not sync
9590 //TODO Check if worker node is sync
96- boolean isWorkerLastBlockAvailable = workerLastBlock > 0 ;
91+ final boolean isWorkerLastBlockAvailable = workerLastBlock > 0 ;
9792 if (!isWorkerLastBlockAvailable ) {
9893 return Optional .empty ();
9994 }
@@ -102,13 +97,16 @@ Optional<ReplicateTaskSummary> getAvailableReplicateTaskSummary(long workerLastB
10297 return Optional .empty ();
10398 }
10499
105- // TODO : Remove this, the optional can never be empty
106- // This is covered in workerService.canAcceptMoreWorks
107- Optional <Worker > optional = workerService .getWorker (walletAddress );
100+ final Optional <Worker > optional = workerService .getWorker (walletAddress );
108101 if (optional .isEmpty ()) {
109102 return Optional .empty ();
110103 }
111- Worker worker = optional .get ();
104+ final Worker worker = optional .get ();
105+
106+ // return empty if max computing task is reached or if the worker is not found
107+ if (!workerService .canAcceptMoreWorks (worker )) {
108+ return Optional .empty ();
109+ }
112110
113111 return getReplicateTaskSummaryForAnyAvailableTask (
114112 walletAddress ,
@@ -161,8 +159,8 @@ private Optional<ReplicateTaskSummary> getReplicateTaskSummary(Task task, String
161159 chainTaskId ,
162160 task .getEnclaveChallenge ());
163161 ReplicateTaskSummaryBuilder replicateTaskSummary = ReplicateTaskSummary .builder ()
164- .workerpoolAuthorization (authorization );
165- if (task .isTeeTask ()){
162+ .workerpoolAuthorization (authorization );
163+ if (task .isTeeTask ()) {
166164 replicateTaskSummary .smsUrl (task .getSmsUrl ());
167165 }
168166 return Optional .of (replicateTaskSummary .build ());
@@ -173,7 +171,7 @@ private Optional<ReplicateTaskSummary> getReplicateTaskSummary(Task task, String
173171 * tries to accept the task - i.e. create a new {@link Replicate}
174172 * for that task on that worker.
175173 *
176- * @param task {@link Task} needing at least one new {@link Replicate}.
174+ * @param task {@link Task} needing at least one new {@link Replicate}.
177175 * @param walletAddress Wallet address of a worker looking for new {@link Task}.
178176 * @return {@literal true} if the task has been accepted,
179177 * {@literal false} otherwise.
@@ -184,22 +182,6 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
184182 }
185183
186184 final String chainTaskId = task .getChainTaskId ();
187- final Optional <ReplicatesList > oReplicatesList = replicatesService .getReplicatesList (chainTaskId );
188- // Check is only here to prevent
189- // "`Optional.get()` without `isPresent()` warning".
190- // This case should not happen.
191- if (oReplicatesList .isEmpty ()) {
192- return false ;
193- }
194-
195- final ReplicatesList replicatesList = oReplicatesList .get ();
196-
197- final boolean hasWorkerAlreadyParticipated =
198- replicatesList .hasWorkerAlreadyParticipated (walletAddress );
199- if (hasWorkerAlreadyParticipated ) {
200- return false ;
201- }
202-
203185 final Lock lock = taskAccessForNewReplicateLocks
204186 .computeIfAbsent (chainTaskId , k -> new ReentrantLock ());
205187 if (!lock .tryLock ()) {
@@ -209,33 +191,56 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
209191 }
210192
211193 try {
212- final boolean taskNeedsMoreContributions = ConsensusHelper .doesTaskNeedMoreContributionsForConsensus (
213- chainTaskId ,
214- replicatesList .getReplicates (),
215- task .getTrust (),
216- task .getMaxExecutionTime ());
217-
218- if (!taskNeedsMoreContributions
219- || taskService .isConsensusReached (replicatesList )) {
220- return false ;
221- }
222-
223- replicatesService .addNewReplicate (chainTaskId , walletAddress );
224- workerService .addChainTaskIdToWorker (chainTaskId , walletAddress );
194+ return replicatesService .getReplicatesList (chainTaskId )
195+ .map (replicatesList -> acceptOrRejectTask (task , walletAddress , replicatesList ))
196+ .orElse (false );
225197 } finally {
226198 // We should always unlock the task
227199 // so that it could be taken by another replicate
228200 // if there's any issue.
229201 lock .unlock ();
230202 }
203+ }
231204
232- return true ;
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 );
233238 }
234239
235240 /**
236241 * Get notifications missed by the worker during the time it was absent.
237- *
238- * @param blockNumber last seen blocknumber by the worker
242+ *
243+ * @param blockNumber last seen blocknumber by the worker
239244 * @param walletAddress of the worker
240245 * @return list of missed notifications. Can be empty if no notification is found
241246 */
@@ -264,7 +269,7 @@ public List<TaskNotification> getMissedTaskNotifications(long blockNumber, Strin
264269 continue ;
265270 }
266271 TaskNotificationExtra taskNotificationExtra =
267- getTaskNotificationExtra (task , taskNotificationType .get (), walletAddress , enclaveChallenge );
272+ getTaskNotificationExtra (task , taskNotificationType .get (), walletAddress , enclaveChallenge );
268273
269274 TaskNotification taskNotification = TaskNotification .builder ()
270275 .chainTaskId (chainTaskId )
@@ -286,7 +291,7 @@ public List<TaskNotification> getMissedTaskNotifications(long blockNumber, Strin
286291 private TaskNotificationExtra getTaskNotificationExtra (Task task , TaskNotificationType taskNotificationType , String walletAddress , String enclaveChallenge ) {
287292 TaskNotificationExtra taskNotificationExtra = TaskNotificationExtra .builder ().build ();
288293
289- switch (taskNotificationType ){
294+ switch (taskNotificationType ) {
290295 case PLEASE_CONTRIBUTE :
291296 WorkerpoolAuthorization authorization = signatureService .createAuthorization (
292297 walletAddress , task .getChainTaskId (), enclaveChallenge );
@@ -312,7 +317,7 @@ public Optional<TaskNotificationType> getTaskNotificationType(Task task, Replica
312317 // CONTRIBUTION_TIMEOUT or CONSENSUS_REACHED without contribution
313318 if (task .getCurrentStatus ().equals (TaskStatus .CONTRIBUTION_TIMEOUT )
314319 || (task .getCurrentStatus ().equals (TaskStatus .CONSENSUS_REACHED )
315- && !replicate .containsContributedStatus ())) {
320+ && !replicate .containsContributedStatus ())) {
316321 return Optional .of (TaskNotificationType .PLEASE_ABORT );
317322 }
318323
0 commit comments