Skip to content

Commit

Permalink
Merge pull request #572 from iExecBlockchainComputing/bugfix/clean-ie…
Browse files Browse the repository at this point in the history
…xec-hub-service

Remove unused methods in `IexecHubService`
  • Loading branch information
jbern0rd authored Apr 18, 2023
2 parents 0c2cfb3 + 1ce18a7 commit 3363222
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 147 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
### Bug Fixes
- Prevent race condition on replicate update. (#568)
### Quality
- Remove unused methods in `IexecHubService`. (#572)
### Dependency Upgrades
- Upgrade to `iexec-common` 8.0.0. (#571)
- Add new `iexec-commons-poco` 1.0.2 dependency. (#571)
Expand Down
147 changes: 0 additions & 147 deletions src/main/java/com/iexec/core/chain/IexecHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@

import com.iexec.commons.poco.chain.*;
import com.iexec.commons.poco.contract.generated.IexecHubContract;
import com.iexec.commons.poco.utils.BytesUtils;
import io.reactivex.Flowable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.BaseEventResponse;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Optional;
Expand All @@ -40,8 +33,6 @@
import java.util.concurrent.ThreadPoolExecutor;

import static com.iexec.common.utils.DateTimeUtils.now;
import static com.iexec.commons.poco.chain.ChainTaskStatus.ACTIVE;
import static com.iexec.commons.poco.chain.ChainTaskStatus.COMPLETED;
import static com.iexec.commons.poco.utils.BytesUtils.stringToBytes;

@Slf4j
Expand All @@ -52,7 +43,6 @@ public class IexecHubService extends IexecHubAbstractService {
private final CredentialsService credentialsService;
private final Web3jService web3jService;

@Autowired
public IexecHubService(CredentialsService credentialsService,
Web3jService web3jService,
ChainConfig chainConfig) {
Expand All @@ -68,22 +58,6 @@ public IexecHubService(CredentialsService credentialsService,
}
}

/**
* Check if a deal's task can be initialized.
* An initializable task should have the status
* UNSET onchain and have a contribution deadline
* that is still in the future.
*
* @param chainDealId
* @param taskIndex
* @return true if the task is initializable, false otherwise.
*/
public boolean isInitializableOnchain(String chainDealId, int taskIndex) {
boolean isTaskUnsetOnChain = isTaskInUnsetStatusOnChain(chainDealId, taskIndex);
boolean isBeforeContributionDeadline = isBeforeContributionDeadline(chainDealId);
return isBeforeContributionDeadline && isTaskUnsetOnChain;
}

/**
* Check if the task is defined onchain and
* has the status {@link ChainTaskStatus#UNSET}.
Expand Down Expand Up @@ -167,52 +141,6 @@ public Date getChainDealFinalDeadline(ChainDeal chainDeal) {
return new Date(startTime + maxTime * 10);
}

@Deprecated
public Optional<Pair<String, ChainReceipt>> initialize(String chainDealId, int taskIndex) {
log.info("Requested initialize [chainDealId:{}, taskIndex:{}, waitingTxCount:{}]", chainDealId, taskIndex, getWaitingTransactionCount());
try {
return CompletableFuture.supplyAsync(() -> sendInitializeTransaction(chainDealId, taskIndex), executor).get();
} catch (InterruptedException | ExecutionException e) {
log.error("initialize asynchronous execution did not complete", e);
}
return Optional.empty();
}

private Optional<Pair<String, ChainReceipt>> sendInitializeTransaction(String chainDealId, int taskIndex) {
byte[] chainDealIdBytes = stringToBytes(chainDealId);
String computedChainTaskId = ChainUtils.generateChainTaskId(chainDealId, taskIndex);
RemoteCall<TransactionReceipt> initializeCall =
getHubContract(web3jService.getWritingContractGasProvider())
.initialize(chainDealIdBytes, BigInteger.valueOf(taskIndex));

TransactionReceipt initializeReceipt;
try {
initializeReceipt = initializeCall.send();
} catch (Exception e) {
log.error("Failed to send initialize [chainDealId:{}, taskIndex:{}]", chainDealId, taskIndex, e);
return Optional.empty();
}

List<IexecHubContract.TaskInitializeEventResponse> initializeEvents = getHubContract().getTaskInitializeEvents(initializeReceipt);

IexecHubContract.TaskInitializeEventResponse initializeEvent = null;
if (initializeEvents != null && !initializeEvents.isEmpty()) {
initializeEvent = initializeEvents.get(0);
}

if (isSuccessTx(computedChainTaskId, initializeEvent, ACTIVE)) {
String chainTaskId = BytesUtils.bytesToString(initializeEvent.taskid);
ChainReceipt chainReceipt = buildChainReceipt(initializeReceipt);
log.info("Initialized [chainTaskId:{}, chainDealId:{}, taskIndex:{}, gasUsed:{}, block:{}]",
computedChainTaskId, chainDealId, taskIndex,
initializeReceipt.getGasUsed(), chainReceipt.getBlockNumber());
return Optional.of(Pair.of(chainTaskId, chainReceipt));
}

log.error("Failed to initialize [chainDealId:{}, taskIndex:{}]", chainDealId, taskIndex);
return Optional.empty();
}

public boolean canFinalize(String chainTaskId) {
Optional<ChainTask> optional = getChainTask(chainTaskId);
if (optional.isEmpty()) {
Expand All @@ -236,73 +164,6 @@ public boolean canFinalize(String chainTaskId) {
return ret;
}

public Optional<ChainReceipt> finalizeTask(String chainTaskId, String resultLink, String callbackData) {
log.info("Requested finalize [chainTaskId:{}, waitingTxCount:{}]", chainTaskId, getWaitingTransactionCount());
try {
return CompletableFuture.supplyAsync(() -> sendFinalizeTransaction(chainTaskId, resultLink, callbackData), executor).get();
} catch (InterruptedException | ExecutionException e) {
log.error("finalizeTask asynchronous execution did not complete", e);
}
return Optional.empty();
}

private Optional<ChainReceipt> sendFinalizeTransaction(String chainTaskId, String resultLink, String callbackData) {
byte[] chainTaskIdBytes = stringToBytes(chainTaskId);
byte[] results = new byte[0];
byte[] resultsCallback = new byte[0];

boolean shouldSendCallback = callbackData != null && !callbackData.isEmpty();
if (!shouldSendCallback) {
results = resultLink.getBytes(StandardCharsets.UTF_8);
} else {
resultsCallback = stringToBytes(callbackData);
}

TransactionReceipt finalizeReceipt;

RemoteCall<TransactionReceipt> finalizeCall = getHubContract(web3jService.getWritingContractGasProvider())
.finalize(chainTaskIdBytes, results, resultsCallback);

try {
finalizeReceipt = finalizeCall.send();
} catch (Exception e) {
log.error("Failed to send finalize [chainTaskId:{}, resultLink:{}, callbackData:{}, shouldSendCallback:{}]",
chainTaskId, resultLink, callbackData, shouldSendCallback, e);
return Optional.empty();
}

List<IexecHubContract.TaskFinalizeEventResponse> finalizeEvents = getHubContract().getTaskFinalizeEvents(finalizeReceipt);

IexecHubContract.TaskFinalizeEventResponse finalizeEvent = null;
if (finalizeEvents != null && !finalizeEvents.isEmpty()) {
finalizeEvent = finalizeEvents.get(0);
}

if (isSuccessTx(chainTaskId, finalizeEvent, COMPLETED)) {
ChainReceipt chainReceipt = buildChainReceipt(finalizeReceipt);
log.info("Finalized [chainTaskId:{}, resultLink:{}, callbackData:{}, " +
"shouldSendCallback:{}, gasUsed:{}, block:{}]",
chainTaskId, resultLink, callbackData, shouldSendCallback,
finalizeReceipt.getGasUsed(), chainReceipt.getBlockNumber());
return Optional.of(chainReceipt);
}

log.error("Failed to finalize [chainTaskId:{}]", chainTaskId);
return Optional.empty();
}

private boolean isSuccessTx(String chainTaskId, BaseEventResponse txEvent, ChainTaskStatus pretendedStatus) {
if (txEvent == null || txEvent.log == null) {
return false;
}

if (txEvent.log.getType() == null || txEvent.log.getType().equals(PENDING_RECEIPT_STATUS)) {
return isStatusValidOnChainAfterPendingReceipt(chainTaskId, pretendedStatus, this::isTaskStatusValidOnChain);
}

return true;
}

public boolean canReopen(String chainTaskId) {
Optional<ChainTask> optional = getChainTask(chainTaskId);
if (optional.isEmpty()) {
Expand Down Expand Up @@ -371,14 +232,6 @@ public boolean hasEnoughGas() {
return hasEnoughGas(credentialsService.getCredentials().getAddress());
}

private Boolean isTaskStatusValidOnChain(String chainTaskId, ChainStatus chainTaskStatus) {
if (chainTaskStatus instanceof ChainTaskStatus) {
Optional<ChainTask> optionalChainTask = getChainTask(chainTaskId);
return optionalChainTask.isPresent() && optionalChainTask.get().getStatus().equals(chainTaskStatus);
}
return false;
}

private ChainReceipt buildChainReceipt(TransactionReceipt receipt) {
return ChainReceipt.builder()
.txHash(receipt.getTransactionHash())
Expand Down

0 comments on commit 3363222

Please sign in to comment.