66import com .iexec .common .security .Signature ;
77import com .iexec .common .task .TaskDescription ;
88import com .iexec .common .utils .BytesUtils ;
9+ import com .iexec .common .utils .WaitUtils ;
910import com .iexec .worker .config .PublicConfigurationService ;
1011import lombok .extern .slf4j .Slf4j ;
1112import org .springframework .beans .factory .annotation .Autowired ;
1213import org .springframework .stereotype .Service ;
1314import org .web3j .protocol .core .RemoteCall ;
15+ import org .web3j .protocol .core .methods .response .BaseEventResponse ;
1416import org .web3j .protocol .core .methods .response .TransactionReceipt ;
1517
1618import java .util .HashMap ;
@@ -97,8 +99,7 @@ private IexecHubABILegacy.TaskContributeEventResponse sendContributeTransaction(
9799 contributeEvent = contributeEvents .get (0 );
98100 }
99101
100- if (contributeEvent != null && contributeEvent .log != null &&
101- isStatusValidOnChainAfterPendingReceipt (chainTaskId , CONTRIBUTED , this ::isContributionStatusValidOnChain )) {
102+ if (isSuccessTx (chainTaskId , contributeEvent , CONTRIBUTED )) {
102103 log .info ("Contributed [chainTaskId:{}, resultHash:{}, gasUsed:{}, log:{}]" ,
103104 chainTaskId , resultHash , contributeReceipt .getGasUsed (), contributeEvent .log );
104105 return contributeEvent ;
@@ -108,6 +109,18 @@ private IexecHubABILegacy.TaskContributeEventResponse sendContributeTransaction(
108109 return null ;
109110 }
110111
112+ private boolean isSuccessTx (String chainTaskId , BaseEventResponse txEvent , ChainContributionStatus pretendedStatus ) {
113+ if (txEvent == null || txEvent .log == null ) {
114+ return false ;
115+ }
116+
117+ if (txEvent .log .getType () == null || txEvent .log .getType ().equals (PENDING_RECEIPT_STATUS )) {
118+ return isStatusValidOnChainAfterPendingReceipt (chainTaskId , pretendedStatus , this ::isContributionStatusValidOnChain );
119+ }
120+
121+ return true ;
122+ }
123+
111124 IexecHubABILegacy .TaskRevealEventResponse reveal (String chainTaskId , String resultDigest ) {
112125 try {
113126 return CompletableFuture .supplyAsync (() -> {
@@ -142,8 +155,7 @@ private IexecHubABILegacy.TaskRevealEventResponse sendRevealTransaction(String c
142155 revealEvent = revealEvents .get (0 );
143156 }
144157
145- if (revealEvent != null && revealEvent .log != null &&
146- isStatusValidOnChainAfterPendingReceipt (chainTaskId , REVEALED , this ::isContributionStatusValidOnChain )) {
158+ if (isSuccessTx (chainTaskId , revealEvent , REVEALED )) {
147159 log .info ("Revealed [chainTaskId:{}, resultDigest:{}, gasUsed:{}, log:{}]" ,
148160 chainTaskId , resultDigest , revealReceipt .getGasUsed (), revealEvent .log );
149161 return revealEvent ;
@@ -189,7 +201,7 @@ private boolean isBlockchainReadTrueWhenNodeNotSync(String chainTaskId, Function
189201 long maxWaitingTime = web3jService .getMaxWaitingTimeWhenPendingReceipt ();
190202 long startTime = System .currentTimeMillis ();
191203
192- for (long duration = 0L ; duration < maxWaitingTime ; duration = System .currentTimeMillis () - startTime ) {
204+ for (long duration = 0L ; duration < maxWaitingTime ; duration = System .currentTimeMillis () - startTime ) {
193205 try {
194206 if (booleanBlockchainReadFunction .apply (chainTaskId )) {
195207 return true ;
@@ -205,16 +217,16 @@ private boolean isBlockchainReadTrueWhenNodeNotSync(String chainTaskId, Function
205217 return false ;
206218 }
207219
208- Boolean isChainTaskActive (String chainTaskId ){
220+ Boolean isChainTaskActive (String chainTaskId ) {
209221 Optional <ChainTask > chainTask = getChainTask (chainTaskId );
210- if (chainTask .isPresent ()){
211- switch (chainTask .get ().getStatus ()){
222+ if (chainTask .isPresent ()) {
223+ switch (chainTask .get ().getStatus ()) {
212224 case UNSET :
213225 break ;//Could happen if node not synchronized. Should wait.
214226 case ACTIVE :
215227 return true ;
216228 case REVEALING :
217- return false ;
229+ return false ;
218230 case COMPLETED :
219231 return false ;
220232 case FAILLED :
@@ -224,16 +236,16 @@ Boolean isChainTaskActive(String chainTaskId){
224236 return false ;
225237 }
226238
227- public Boolean isChainTaskRevealing (String chainTaskId ){
239+ public Boolean isChainTaskRevealing (String chainTaskId ) {
228240 Optional <ChainTask > chainTask = getChainTask (chainTaskId );
229- if (chainTask .isPresent ()){
230- switch (chainTask .get ().getStatus ()){
241+ if (chainTask .isPresent ()) {
242+ switch (chainTask .get ().getStatus ()) {
231243 case UNSET :
232244 break ;//Should not happen
233245 case ACTIVE :
234246 break ;//Could happen if node not synchronized. Should wait.
235247 case REVEALING :
236- return true ;
248+ return true ;
237249 case COMPLETED :
238250 return false ;
239251 case FAILLED :
@@ -248,15 +260,15 @@ public Boolean isChainTaskRevealing(String chainTaskId){
248260 *
249261 */
250262 public void putTaskDescription (TaskDescription taskDescription ) {
251- if (taskDescription != null && taskDescription .getChainTaskId () != null ){
263+ if (taskDescription != null && taskDescription .getChainTaskId () != null ) {
252264 taskDescriptions .putIfAbsent (taskDescription .getChainTaskId (), taskDescription );
253265 return ;
254266 }
255267 log .error ("Cant putTaskDescription [taskDescription:{}]" , taskDescription );
256268 }
257269
258270 public TaskDescription getTaskDescription (String chainTaskId ) {
259- if (taskDescriptions .get (chainTaskId ) == null ){
271+ if (taskDescriptions .get (chainTaskId ) == null ) {
260272 Optional <TaskDescription > taskDescriptionFromChain = this .getTaskDescriptionFromChain (chainTaskId );
261273 taskDescriptionFromChain .ifPresent (this ::putTaskDescription );
262274 }
0 commit comments