11/*
2- * Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+ * Copyright 2020-2025 IEXEC BLOCKCHAIN TECH
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package com .iexec .worker .chain ;
1818
19- import com .iexec .common .contribution .Contribution ;
2019import com .iexec .common .replicate .ReplicateStatusCause ;
2120import com .iexec .common .result .ComputedFile ;
22- import com .iexec .common .worker .result .ResultUtils ;
2321import com .iexec .commons .poco .chain .*;
2422import com .iexec .commons .poco .contract .generated .IexecHubContract ;
23+ import com .iexec .commons .poco .task .TaskDescription ;
2524import com .iexec .commons .poco .utils .BytesUtils ;
25+ import com .iexec .commons .poco .utils .HashUtils ;
2626import lombok .extern .slf4j .Slf4j ;
2727import org .springframework .stereotype .Service ;
2828
2929import java .math .BigInteger ;
30- import java .util .Date ;
3130import java .util .Optional ;
3231
3332import static com .iexec .common .replicate .ReplicateStatusCause .*;
3433
35-
3634@ Slf4j
3735@ Service
3836public class ContributionService {
@@ -56,53 +54,49 @@ public boolean isChainTaskInitialized(String chainTaskId) {
5654 return iexecHubService .getTaskDescription (chainTaskId ) != null ;
5755 }
5856
59- public Optional <ReplicateStatusCause > getCannotContributeStatusCause (String chainTaskId ) {
60- Optional <ChainTask > optionalChainTask = iexecHubService .getChainTask (chainTaskId );
61- if (optionalChainTask .isEmpty ()) {
62- return Optional .of (CHAIN_UNREACHABLE );
57+ public Optional <ReplicateStatusCause > getCannotContributeStatusCause (final String chainTaskId ) {
58+ if (!isWorkerpoolAuthorizationPresent (chainTaskId )) {
59+ return Optional .of (WORKERPOOL_AUTHORIZATION_NOT_FOUND );
6360 }
6461
65- ChainTask chainTask = optionalChainTask .get ();
62+ final ChainTask chainTask = iexecHubService .getChainTask (chainTaskId ).orElse (null );
63+ if (chainTask == null ) {
64+ return Optional .of (CHAIN_UNREACHABLE );
65+ }
6666
6767 if (!hasEnoughStakeToContribute (chainTask )) {
6868 return Optional .of (STAKE_TOO_LOW );
6969 }
7070
71- if (! isTaskActiveToContribute ( chainTask ) ) {
71+ if (chainTask . getStatus () != ChainTaskStatus . ACTIVE ) {
7272 return Optional .of (TASK_NOT_ACTIVE );
7373 }
7474
75- if (! isBeforeContributionDeadlineToContribute ( chainTask )) {
75+ if (chainTask . isContributionDeadlineReached ( )) {
7676 return Optional .of (CONTRIBUTION_TIMEOUT );
7777 }
7878
79- if (! isContributionUnsetToContribute ( chainTask )) {
79+ if (chainTask . hasContributionFrom ( workerWalletAddress )) {
8080 return Optional .of (CONTRIBUTION_ALREADY_SET );
8181 }
8282
83- if (!isWorkerpoolAuthorizationPresent (chainTaskId )) {
84- return Optional .of (WORKERPOOL_AUTHORIZATION_NOT_FOUND );
85- }
86-
8783 return Optional .empty ();
8884 }
8985
90- // TODO: trust could become part of TaskDescription to avoid fetching deal on-chain
91- public Optional <ReplicateStatusCause > getCannotContributeAndFinalizeStatusCause (String chainTaskId ) {
92- Optional <ChainTask > optionalChainTask = iexecHubService .getChainTask (chainTaskId );
93- if (optionalChainTask .isEmpty ()) {
94- return Optional .of (CHAIN_UNREACHABLE );
95- }
96- ChainTask chainTask = optionalChainTask .get ();
97-
86+ public Optional <ReplicateStatusCause > getCannotContributeAndFinalizeStatusCause (final String chainTaskId ) {
9887 // check TRUST is 1
99- Optional < ChainDeal > oChainDeal = iexecHubService .getChainDeal ( chainTask . getDealid () );
100- if (oChainDeal . isEmpty () || !BigInteger .ONE .equals (oChainDeal . get () .getTrust ())) {
88+ final TaskDescription taskDescription = iexecHubService .getTaskDescription ( chainTaskId );
89+ if (taskDescription == null || !BigInteger .ONE .equals (taskDescription .getTrust ())) {
10190 return Optional .of (TRUST_NOT_1 );
10291 }
10392
93+ final ChainTask chainTask = iexecHubService .getChainTask (chainTaskId ).orElse (null );
94+ if (chainTask == null ) {
95+ return Optional .of (CHAIN_UNREACHABLE );
96+ }
97+
10498 // check TASK_ALREADY_CONTRIBUTED
105- if (! chainTask .getContributors (). isEmpty ()) {
99+ if (chainTask .hasContributions ()) {
106100 return Optional .of (TASK_ALREADY_CONTRIBUTED );
107101 }
108102
@@ -128,39 +122,17 @@ private boolean hasEnoughStakeToContribute(ChainTask chainTask) {
128122 return optionalChainAccount .get ().getDeposit () >= optionalChainDeal .get ().getWorkerStake ().longValue ();
129123 }
130124
131- private boolean isTaskActiveToContribute (ChainTask chainTask ) {
132- return iexecHubService .isChainTaskActive (chainTask .getChainTaskId ());
133- }
134-
135- private boolean isBeforeContributionDeadlineToContribute (ChainTask chainTask ) {
136- return new Date ().getTime () < chainTask .getContributionDeadline ();
137- }
138-
139- private boolean isContributionUnsetToContribute (ChainTask chainTask ) {
140- Optional <ChainContribution > optionalContribution = iexecHubService .getChainContribution (chainTask .getChainTaskId ());
141- if (optionalContribution .isEmpty ()) return false ;
142-
143- ChainContribution chainContribution = optionalContribution .get ();
144- return chainContribution .getStatus () == ChainContributionStatus .UNSET ;
145- }
146-
147- public boolean isContributionDeadlineReached (String chainTaskId ) {
148- Optional <ChainTask > oTask = iexecHubService .getChainTask (chainTaskId );
149-
150- return oTask .isEmpty () || !isBeforeContributionDeadlineToContribute (oTask .get ());
151- }
152-
153125 // returns ChainReceipt of the contribution if successful, null otherwise
154- public Optional <ChainReceipt > contribute (Contribution contribution ) {
126+ public Optional <ChainReceipt > contribute (final Contribution contribution ) {
155127
156128 IexecHubContract .TaskContributeEventResponse contributeResponse = iexecHubService .contribute (contribution );
157129
158130 if (contributeResponse == null ) {
159- log .error ("ContributeTransactionReceipt received but was null [chainTaskId:{}]" , contribution .getChainTaskId ());
131+ log .error ("ContributeTransactionReceipt received but was null [chainTaskId:{}]" , contribution .chainTaskId ());
160132 return Optional .empty ();
161133 }
162134
163- ChainReceipt chainReceipt = ChainUtils .buildChainReceipt (contributeResponse .log , contribution .getChainTaskId (),
135+ final ChainReceipt chainReceipt = ChainUtils .buildChainReceipt (contributeResponse .log , contribution .chainTaskId (),
164136 iexecHubService .getLatestBlockNumber ());
165137
166138 return Optional .of (chainReceipt );
@@ -179,8 +151,8 @@ public Contribution getContribution(ComputedFile computedFile) {
179151 }
180152
181153 String resultDigest = computedFile .getResultDigest ();
182- String resultHash = ResultUtils . computeResultHash (chainTaskId , resultDigest );
183- String resultSeal = ResultUtils . computeResultSeal (workerWalletAddress , chainTaskId , resultDigest );
154+ String resultHash = HashUtils . concatenateAndHash (chainTaskId , resultDigest );
155+ String resultSeal = HashUtils . concatenateAndHash (workerWalletAddress , chainTaskId , resultDigest );
184156 String workerpoolSignature = workerpoolAuthorization .getSignature ().getValue ();
185157 String enclaveChallenge = workerpoolAuthorization .getEnclaveChallenge ();
186158 String enclaveSignature = computedFile .getEnclaveSignature ();
0 commit comments