Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch results on-chain when updating a replicate status #659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Bug Fixes

- Filter out `CONTRIBUTE_AND_FINALIZE` tasks when detecting missed `REVEALED` status update. (#658)
- Fetch `results` on-chain when updating a replicate status. (#659)

## [[8.3.0]](https://github.com/iExecBlockchainComputing/iexec-core/releases/tag/v8.3.0) 2024-01-11

Expand Down
33 changes: 15 additions & 18 deletions src/main/java/com/iexec/core/replicate/ReplicatesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.iexec.common.replicate.*;
import com.iexec.common.utils.ContextualLockRunner;
import com.iexec.commons.poco.chain.ChainContribution;
import com.iexec.commons.poco.chain.ChainTask;
import com.iexec.commons.poco.notification.TaskNotificationType;
import com.iexec.commons.poco.task.TaskDescription;
import com.iexec.core.chain.IexecHubService;
Expand All @@ -34,6 +35,7 @@
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.web3j.utils.Numeric;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -101,26 +103,16 @@ public Optional<ReplicatesList> getReplicatesList(String chainTaskId) {
}

public List<Replicate> getReplicates(String chainTaskId) {
Optional<ReplicatesList> optionalList = getReplicatesList(chainTaskId);
if (optionalList.isEmpty()) {
return Collections.emptyList();
}
return optionalList.get().getReplicates();
return getReplicatesList(chainTaskId)
.map(ReplicatesList::getReplicates)
.orElse(Collections.emptyList());
}

public Optional<Replicate> getReplicate(String chainTaskId, String walletAddress) {
Optional<ReplicatesList> optional = getReplicatesList(chainTaskId);
if (optional.isEmpty()) {
return Optional.empty();
}

for (Replicate replicate : optional.get().getReplicates()) {
if (replicate.getWalletAddress().equals(walletAddress)) {
return Optional.of(replicate);
}
}

return Optional.empty();
return getReplicates(chainTaskId)
.stream()
.filter(replicate -> replicate.getWalletAddress().equals(walletAddress))
.findFirst();
}

public int getNbReplicatesWithCurrentStatus(String chainTaskId, ReplicateStatus... listStatus) {
Expand Down Expand Up @@ -244,10 +236,15 @@ public UpdateReplicateStatusArgs computeUpdateReplicateStatusArgs(String chainTa

if (statusUpdate.getStatus() == RESULT_UPLOADED || statusUpdate.getStatus() == CONTRIBUTE_AND_FINALIZE_DONE) {
final ReplicateStatusDetails details = statusUpdate.getDetails();
// TODO fetch chainCallbackData from chainTask when using latest ABI
if (details != null) {
resultLink = details.getResultLink();
chainCallbackData = details.getChainCallbackData();
}
resultLink = iexecHubService.getChainTask(chainTaskId)
.map(ChainTask::getResults)
.map(Numeric::hexStringToByteArray)
.map(String::new)
.orElse(null);
}

return UpdateReplicateStatusArgs.builder()
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.iexec.common.replicate.*;
import com.iexec.commons.poco.chain.ChainContribution;
import com.iexec.commons.poco.chain.ChainTask;
import com.iexec.commons.poco.notification.TaskNotificationType;
import com.iexec.commons.poco.task.TaskDescription;
import com.iexec.commons.poco.utils.BytesUtils;
Expand All @@ -31,7 +32,9 @@
import org.junit.jupiter.api.Test;
import org.mockito.*;
import org.springframework.context.ApplicationEventPublisher;
import org.web3j.utils.Numeric;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -1373,9 +1376,14 @@ void computeUpdateReplicateStatusArgsResultUploaded() {
.status(RESULT_UPLOADED)
.details(details)
.build();
final ChainTask chainTask = ChainTask.builder()
.chainTaskId(CHAIN_TASK_ID)
.results(Numeric.toHexString(expectedResultLink.getBytes(StandardCharsets.UTF_8)))
.build();

when(iexecHubService.getTaskDescription(CHAIN_TASK_ID))
.thenReturn(expectedTaskDescription);
when(iexecHubService.getChainTask(CHAIN_TASK_ID)).thenReturn(Optional.of(chainTask));

final UpdateReplicateStatusArgs actualResult =
replicatesService.computeUpdateReplicateStatusArgs(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate);
Expand Down Expand Up @@ -1407,11 +1415,16 @@ void computeUpdateReplicateStatusArgsContributeAndFinalizeDone() {
.status(CONTRIBUTE_AND_FINALIZE_DONE)
.details(details)
.build();
final ChainTask chainTask = ChainTask.builder()
.chainTaskId(CHAIN_TASK_ID)
.results(Numeric.toHexString(expectedResultLink.getBytes(StandardCharsets.UTF_8)))
.build();

when(iexecHubService.getTaskDescription(CHAIN_TASK_ID)).thenReturn(expectedTaskDescription);
when(iexecHubService.getWorkerWeight(WALLET_WORKER_1)).thenReturn(expectedWorkerWeight);
when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_WORKER_1))
.thenReturn(Optional.of(expectedChainContribution));
when(iexecHubService.getChainTask(CHAIN_TASK_ID)).thenReturn(Optional.of(chainTask));

assertThat(replicatesService.computeUpdateReplicateStatusArgs(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate))
.isEqualTo(UpdateReplicateStatusArgs.builder()
Expand Down