@@ -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 }
@@ -104,11 +99,16 @@ Optional<ReplicateTaskSummary> getAvailableReplicateTaskSummary(long workerLastB
10499
105100 // TODO : Remove this, the optional can never be empty
106101 // This is covered in workerService.canAcceptMoreWorks
107- Optional <Worker > optional = workerService .getWorker (walletAddress );
102+ final Optional <Worker > optional = workerService .getWorker (walletAddress );
108103 if (optional .isEmpty ()) {
109104 return Optional .empty ();
110105 }
111- Worker worker = optional .get ();
106+ final Worker worker = optional .get ();
107+
108+ // return empty if max computing task is reached or if the worker is not found
109+ if (!workerService .canAcceptMoreWorks (worker )) {
110+ return Optional .empty ();
111+ }
112112
113113 return getReplicateTaskSummaryForAnyAvailableTask (
114114 walletAddress ,
@@ -161,8 +161,8 @@ private Optional<ReplicateTaskSummary> getReplicateTaskSummary(Task task, String
161161 chainTaskId ,
162162 task .getEnclaveChallenge ());
163163 ReplicateTaskSummaryBuilder replicateTaskSummary = ReplicateTaskSummary .builder ()
164- .workerpoolAuthorization (authorization );
165- if (task .isTeeTask ()){
164+ .workerpoolAuthorization (authorization );
165+ if (task .isTeeTask ()) {
166166 replicateTaskSummary .smsUrl (task .getSmsUrl ());
167167 }
168168 return Optional .of (replicateTaskSummary .build ());
@@ -173,7 +173,7 @@ private Optional<ReplicateTaskSummary> getReplicateTaskSummary(Task task, String
173173 * tries to accept the task - i.e. create a new {@link Replicate}
174174 * for that task on that worker.
175175 *
176- * @param task {@link Task} needing at least one new {@link Replicate}.
176+ * @param task {@link Task} needing at least one new {@link Replicate}.
177177 * @param walletAddress Wallet address of a worker looking for new {@link Task}.
178178 * @return {@literal true} if the task has been accepted,
179179 * {@literal false} otherwise.
@@ -220,8 +220,8 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
220220 return false ;
221221 }
222222
223- replicatesService . addNewReplicate (chainTaskId , walletAddress );
224- workerService . addChainTaskIdToWorker ( chainTaskId , walletAddress );
223+ workerService . addChainTaskIdToWorker (chainTaskId , walletAddress )
224+ . ifPresent ( worker -> replicatesService . addNewReplicate ( chainTaskId , walletAddress ) );
225225 } finally {
226226 // We should always unlock the task
227227 // so that it could be taken by another replicate
@@ -234,8 +234,8 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
234234
235235 /**
236236 * Get notifications missed by the worker during the time it was absent.
237- *
238- * @param blockNumber last seen blocknumber by the worker
237+ *
238+ * @param blockNumber last seen blocknumber by the worker
239239 * @param walletAddress of the worker
240240 * @return list of missed notifications. Can be empty if no notification is found
241241 */
@@ -264,7 +264,7 @@ public List<TaskNotification> getMissedTaskNotifications(long blockNumber, Strin
264264 continue ;
265265 }
266266 TaskNotificationExtra taskNotificationExtra =
267- getTaskNotificationExtra (task , taskNotificationType .get (), walletAddress , enclaveChallenge );
267+ getTaskNotificationExtra (task , taskNotificationType .get (), walletAddress , enclaveChallenge );
268268
269269 TaskNotification taskNotification = TaskNotification .builder ()
270270 .chainTaskId (chainTaskId )
@@ -286,7 +286,7 @@ public List<TaskNotification> getMissedTaskNotifications(long blockNumber, Strin
286286 private TaskNotificationExtra getTaskNotificationExtra (Task task , TaskNotificationType taskNotificationType , String walletAddress , String enclaveChallenge ) {
287287 TaskNotificationExtra taskNotificationExtra = TaskNotificationExtra .builder ().build ();
288288
289- switch (taskNotificationType ){
289+ switch (taskNotificationType ) {
290290 case PLEASE_CONTRIBUTE :
291291 WorkerpoolAuthorization authorization = signatureService .createAuthorization (
292292 walletAddress , task .getChainTaskId (), enclaveChallenge );
@@ -312,7 +312,7 @@ public Optional<TaskNotificationType> getTaskNotificationType(Task task, Replica
312312 // CONTRIBUTION_TIMEOUT or CONSENSUS_REACHED without contribution
313313 if (task .getCurrentStatus ().equals (TaskStatus .CONTRIBUTION_TIMEOUT )
314314 || (task .getCurrentStatus ().equals (TaskStatus .CONSENSUS_REACHED )
315- && !replicate .containsContributedStatus ())) {
315+ && !replicate .containsContributedStatus ())) {
316316 return Optional .of (TaskNotificationType .PLEASE_ABORT );
317317 }
318318
0 commit comments