Skip to content
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 @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
- Create `ConfigServerClient` instance and use it. (#700)
- Allow up to 32 task updates at a given time. (#703)
- Index `currentStatus` field in `task` collection. (#707)
- Replace `CredentialsService` with `SignerService`. (#708)

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version=8.4.1
iexecCommonVersion=8.4.0-NEXT-SNAPSHOT
iexecCommonsPocoVersion=3.2.0
iexecCommonsPocoVersion=4.0.0-NEXT-SNAPSHOT
iexecBlockchainAdapterVersion=8.4.0
iexecResultVersion=8.4.0
iexecSmsVersion=8.5.1
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/iexec/core/chain/ChainConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@ public class ChainConfig {
@Value("#{publicChainConfig.chainId}")
private int chainId;

@Value("#{publicChainConfig.isSidechain()}")
@Value("#{publicChainConfig.sidechain}")
private boolean isSidechain;

@Value("#{publicChainConfig.iexecHubContractAddress}")
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/com/iexec/core/chain/CredentialsService.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/com/iexec/core/chain/DealWatcherService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -188,7 +188,7 @@ private void onDealEvent(DealEvent dealEvent, String watcher) {
*/
private void handleDeal(DealEvent dealEvent) {
String chainDealId = dealEvent.getChainDealId();
Optional<ChainDeal> oChainDeal = iexecHubService.getChainDeal(chainDealId);
Optional<ChainDeal> oChainDeal = iexecHubService.getChainDealWithDetails(chainDealId);
if (oChainDeal.isEmpty()) {
log.error("Could not get chain deal [chainDealId:{}]", chainDealId);
return;
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/iexec/core/chain/IexecHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@
public class IexecHubService extends IexecHubAbstractService implements Purgeable {

private final ThreadPoolExecutor executor;
private final CredentialsService credentialsService;
private final SignerService signerService;
private final Web3jService web3jService;

public IexecHubService(CredentialsService credentialsService,
public IexecHubService(SignerService signerService,
Web3jService web3jService,
ChainConfig chainConfig) {
super(
credentialsService.getCredentials(),
signerService.getCredentials(),
web3jService,
chainConfig.getHubAddress());
this.credentialsService = credentialsService;
this.signerService = signerService;
this.web3jService = web3jService;
this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
if (!hasEnoughGas()) {
Expand Down Expand Up @@ -209,7 +209,9 @@ public Optional<ChainReceipt> reOpen(String chainTaskId) {
log.info("Requested reopen [chainTaskId:{}, waitingTxCount:{}]", chainTaskId, getWaitingTransactionCount());
try {
return CompletableFuture.supplyAsync(() -> sendReopenTransaction(chainTaskId), executor).get();
} catch (InterruptedException | ExecutionException e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
log.error("reOpen asynchronous execution did not complete", e);
}
return Optional.empty();
Expand Down Expand Up @@ -245,7 +247,7 @@ Flowable<IexecHubContract.SchedulerNoticeEventResponse> getDealEventObservable(E
}

public boolean hasEnoughGas() {
final boolean hasEnoughGas = hasEnoughGas(credentialsService.getCredentials().getAddress());
final boolean hasEnoughGas = hasEnoughGas(signerService.getAddress());
log.debug("Gas status [hasEnoughGas:{}]", hasEnoughGas);
return hasEnoughGas;
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/iexec/core/chain/SignatureService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,31 @@

package com.iexec.core.chain;

import com.iexec.commons.poco.chain.SignerService;
import com.iexec.commons.poco.chain.WorkerpoolAuthorization;
import com.iexec.commons.poco.security.Signature;
import com.iexec.commons.poco.utils.BytesUtils;
import com.iexec.commons.poco.utils.HashUtils;
import org.springframework.stereotype.Service;
import org.web3j.crypto.Sign;

@Service
public class SignatureService {

private final CredentialsService credentialsService;
private final SignerService signerService;

public SignatureService(CredentialsService credentialsService) {
this.credentialsService = credentialsService;
public SignatureService(SignerService signerService) {
this.signerService = signerService;
}

public String getAddress() {
return credentialsService.getCredentials().getAddress();
return signerService.getAddress();
}

public Signature sign(String hash) {
return new Signature(Sign.signPrefixedMessage(
BytesUtils.stringToBytes(hash), credentialsService.getCredentials().getEcKeyPair()));
return signerService.signMessageHash(hash);
}

public WorkerpoolAuthorization createAuthorization(String workerWallet, String chainTaskId, String enclaveChallenge) {
String hash = HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge);
final String hash = HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge);
return WorkerpoolAuthorization.builder()
.workerWallet(workerWallet)
.chainTaskId(chainTaskId)
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/iexec/core/chain/WalletConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.iexec.core.chain;

import com.iexec.common.config.PublicChainConfig;
import com.iexec.commons.poco.chain.SignerService;
import lombok.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.annotation.Bean;

@Value
@ConstructorBinding
@ConfigurationProperties(prefix = "wallet")
public class WalletConfiguration {
String encryptedFilePath;
String password;

@Bean
SignerService signerService(Web3jService web3jService, PublicChainConfig publicChainConfig) throws Exception {
return new SignerService(web3jService.getWeb3j(), publicChainConfig.getChainId(), password, encryptedFilePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.iexec.core.configuration;

import com.iexec.commons.poco.chain.SignerService;
import com.iexec.core.chain.ChainConfig;
import com.iexec.core.chain.CredentialsService;
import com.iexec.core.config.PublicConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -33,7 +33,7 @@
@Service
public class PublicConfigurationService {
private final ChainConfig chainConfig;
private final CredentialsService credentialsService;
private final SignerService signerService;
private final WorkerConfiguration workerConfiguration;
private final ResultRepositoryConfiguration resultRepoConfig;
private final ConfigServerClientConfig configServerClientConfig;
Expand All @@ -47,12 +47,12 @@ public class PublicConfigurationService {
private String publicConfigurationHash = null;

public PublicConfigurationService(ChainConfig chainConfig,
CredentialsService credentialsService,
SignerService signerService,
WorkerConfiguration workerConfiguration,
ResultRepositoryConfiguration resultRepoConfig,
ConfigServerClientConfig configServerClientConfig) {
this.chainConfig = chainConfig;
this.credentialsService = credentialsService;
this.signerService = signerService;
this.workerConfiguration = workerConfiguration;
this.resultRepoConfig = resultRepoConfig;
this.configServerClientConfig = configServerClientConfig;
Expand All @@ -64,7 +64,7 @@ void buildPublicConfiguration() {
.workerPoolAddress(chainConfig.getPoolAddress())
.blockchainAdapterUrl(configServerClientConfig.getUrl())
.configServerUrl(configServerClientConfig.getUrl())
.schedulerPublicAddress(credentialsService.getCredentials().getAddress())
.schedulerPublicAddress(signerService.getAddress())
.resultRepositoryURL(resultRepoConfig.getResultRepositoryURL())
.askForReplicatePeriod(workerConfiguration.getAskForReplicatePeriod())
.requiredWorkerVersion(workerConfiguration.getRequiredWorkerVersion())
Expand Down
30 changes: 19 additions & 11 deletions src/test/java/com/iexec/core/chain/DealWatcherServiceTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,15 +33,15 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.util.ReflectionTestUtils;
import org.web3j.protocol.core.methods.response.Log;
Expand All @@ -56,6 +56,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class DealWatcherServiceTests {

private static final String OUT_OF_SERVICE_FIELD_NAME = "outOfService";
Expand Down Expand Up @@ -91,9 +92,7 @@ static void initRegistry() {
Metrics.globalRegistry.add(new SimpleMeterRegistry());
}

@BeforeEach
void init() {
MockitoAnnotations.openMocks(this);
void initMocks() {
when(chainConfig.getHubAddress()).thenReturn("hubAddress");
when(chainConfig.getPoolAddress()).thenReturn("0x1");
}
Expand Down Expand Up @@ -124,6 +123,7 @@ void shouldReturnZeroForAllCountersWhereNothingHasAppended() {
@Test
void shouldRunAndStop() {
BigInteger blockNumber = BigInteger.TEN;
initMocks();
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(blockNumber);
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.empty());
dealWatcherService.run();
Expand All @@ -142,6 +142,7 @@ void shouldUpdateLastSeenBlockWhenOneDeal() {
BigInteger blockOfDeal = BigInteger.valueOf(3);
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice(blockOfDeal);

initMocks();
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));

Expand Down Expand Up @@ -182,8 +183,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndCreateTask() {

Task task = new Task();

initMocks();
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(iexecHubService.isBeforeContributionDeadline(chainDeal)).thenReturn(true);
when(taskService.addTask(any(), anyInt(), anyLong(), any(), any(), anyInt(), anyLong(), any(), any(), any()))
.thenReturn(Optional.of(task));
Expand Down Expand Up @@ -214,8 +216,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndNotCreateTaskSinceDealIsExpired() {
BigInteger blockOfDeal = BigInteger.valueOf(3);
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice(blockOfDeal);

initMocks();
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(iexecHubService.isBeforeContributionDeadline(chainDeal)).thenReturn(false);
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);

Expand All @@ -236,8 +239,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndNotCreateTaskSinceBotSizeIsZero() {
.botSize(BigInteger.valueOf(0))
.build();

initMocks();
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);

dealWatcherService.subscribeToDealEventFromOneBlockToLatest(from);
Expand All @@ -257,8 +261,9 @@ void shouldUpdateLastSeenBlockWhenOneDealButNotCreateTaskSinceExceptionThrown()
.botSize(BigInteger.valueOf(1))
.build();

initMocks();
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);

dealWatcherService.subscribeToDealEventFromOneBlockToLatest(from);
Expand All @@ -275,6 +280,7 @@ void shouldUpdateLastSeenBlockTwiceWhenTwoDeals() {
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice1 = createSchedulerNotice(blockOfDeal1);
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice2 = createSchedulerNotice(blockOfDeal2);

initMocks();
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice1, schedulerNotice2));

Expand All @@ -290,6 +296,7 @@ void shouldNotUpdateLastSeenBlockWhenReceivingOldMissedDeal() {
BigInteger blockOfDeal = BigInteger.valueOf(3);
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice(blockOfDeal);

initMocks();
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));

Expand All @@ -306,13 +313,14 @@ void shouldReplayAllEventInRange() {
BigInteger blockOfDeal = BigInteger.valueOf(3);
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice(blockOfDeal);

initMocks();
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(BigInteger.TEN);
when(configurationService.getFromReplay()).thenReturn(BigInteger.ZERO);
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));

dealWatcherService.replayDealEvent();

verify(iexecHubService).getChainDeal(any());
verify(iexecHubService).getChainDealWithDetails(any());
Counter lastBlockCounter = Metrics.globalRegistry.find(DealWatcherService.METRIC_DEALS_LAST_BLOCK).counter();
Counter dealsReplayCounter = Metrics.globalRegistry.find(DealWatcherService.METRIC_DEALS_REPLAY_COUNT).counter();
Assertions.assertThat(lastBlockCounter).isNotNull();
Expand Down
Loading