Skip to content

Commit 6387120

Browse files
authored
Replace CredentialsService with SignerService (#708)
1 parent 33519e8 commit 6387120

15 files changed

+177
-110
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
1616
- Create `ConfigServerClient` instance and use it. (#700)
1717
- Allow up to 32 task updates at a given time. (#703)
1818
- Index `currentStatus` field in `task` collection. (#707)
19+
- Replace `CredentialsService` with `SignerService`. (#708)
1920

2021
### Bug Fixes
2122

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version=8.4.1
22
iexecCommonVersion=8.4.0-NEXT-SNAPSHOT
3-
iexecCommonsPocoVersion=3.2.0
3+
iexecCommonsPocoVersion=4.0.0-NEXT-SNAPSHOT
44
iexecBlockchainAdapterVersion=8.4.0
55
iexecResultVersion=8.4.0
66
iexecSmsVersion=8.5.1

src/main/java/com/iexec/core/chain/ChainConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2024 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.
@@ -33,7 +33,7 @@ public class ChainConfig {
3333
@Value("#{publicChainConfig.chainId}")
3434
private int chainId;
3535

36-
@Value("#{publicChainConfig.isSidechain()}")
36+
@Value("#{publicChainConfig.sidechain}")
3737
private boolean isSidechain;
3838

3939
@Value("#{publicChainConfig.iexecHubContractAddress}")

src/main/java/com/iexec/core/chain/CredentialsService.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/main/java/com/iexec/core/chain/DealWatcherService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2024 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.
@@ -188,7 +188,7 @@ private void onDealEvent(DealEvent dealEvent, String watcher) {
188188
*/
189189
private void handleDeal(DealEvent dealEvent) {
190190
String chainDealId = dealEvent.getChainDealId();
191-
Optional<ChainDeal> oChainDeal = iexecHubService.getChainDeal(chainDealId);
191+
Optional<ChainDeal> oChainDeal = iexecHubService.getChainDealWithDetails(chainDealId);
192192
if (oChainDeal.isEmpty()) {
193193
log.error("Could not get chain deal [chainDealId:{}]", chainDealId);
194194
return;

src/main/java/com/iexec/core/chain/IexecHubService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@
4949
public class IexecHubService extends IexecHubAbstractService implements Purgeable {
5050

5151
private final ThreadPoolExecutor executor;
52-
private final CredentialsService credentialsService;
52+
private final SignerService signerService;
5353
private final Web3jService web3jService;
5454

55-
public IexecHubService(CredentialsService credentialsService,
55+
public IexecHubService(SignerService signerService,
5656
Web3jService web3jService,
5757
ChainConfig chainConfig) {
5858
super(
59-
credentialsService.getCredentials(),
59+
signerService.getCredentials(),
6060
web3jService,
6161
chainConfig.getHubAddress());
62-
this.credentialsService = credentialsService;
62+
this.signerService = signerService;
6363
this.web3jService = web3jService;
6464
this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
6565
if (!hasEnoughGas()) {
@@ -209,7 +209,9 @@ public Optional<ChainReceipt> reOpen(String chainTaskId) {
209209
log.info("Requested reopen [chainTaskId:{}, waitingTxCount:{}]", chainTaskId, getWaitingTransactionCount());
210210
try {
211211
return CompletableFuture.supplyAsync(() -> sendReopenTransaction(chainTaskId), executor).get();
212-
} catch (InterruptedException | ExecutionException e) {
212+
} catch (InterruptedException e) {
213+
Thread.currentThread().interrupt();
214+
} catch (ExecutionException e) {
213215
log.error("reOpen asynchronous execution did not complete", e);
214216
}
215217
return Optional.empty();
@@ -245,7 +247,7 @@ Flowable<IexecHubContract.SchedulerNoticeEventResponse> getDealEventObservable(E
245247
}
246248

247249
public boolean hasEnoughGas() {
248-
final boolean hasEnoughGas = hasEnoughGas(credentialsService.getCredentials().getAddress());
250+
final boolean hasEnoughGas = hasEnoughGas(signerService.getAddress());
249251
log.debug("Gas status [hasEnoughGas:{}]", hasEnoughGas);
250252
return hasEnoughGas;
251253
}

src/main/java/com/iexec/core/chain/SignatureService.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,31 @@
1616

1717
package com.iexec.core.chain;
1818

19+
import com.iexec.commons.poco.chain.SignerService;
1920
import com.iexec.commons.poco.chain.WorkerpoolAuthorization;
2021
import com.iexec.commons.poco.security.Signature;
21-
import com.iexec.commons.poco.utils.BytesUtils;
2222
import com.iexec.commons.poco.utils.HashUtils;
2323
import org.springframework.stereotype.Service;
24-
import org.web3j.crypto.Sign;
2524

2625
@Service
2726
public class SignatureService {
2827

29-
private final CredentialsService credentialsService;
28+
private final SignerService signerService;
3029

31-
public SignatureService(CredentialsService credentialsService) {
32-
this.credentialsService = credentialsService;
30+
public SignatureService(SignerService signerService) {
31+
this.signerService = signerService;
3332
}
3433

3534
public String getAddress() {
36-
return credentialsService.getCredentials().getAddress();
35+
return signerService.getAddress();
3736
}
3837

3938
public Signature sign(String hash) {
40-
return new Signature(Sign.signPrefixedMessage(
41-
BytesUtils.stringToBytes(hash), credentialsService.getCredentials().getEcKeyPair()));
39+
return signerService.signMessageHash(hash);
4240
}
4341

4442
public WorkerpoolAuthorization createAuthorization(String workerWallet, String chainTaskId, String enclaveChallenge) {
45-
String hash = HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge);
43+
final String hash = HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge);
4644
return WorkerpoolAuthorization.builder()
4745
.workerWallet(workerWallet)
4846
.chainTaskId(chainTaskId)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 IEXEC BLOCKCHAIN TECH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.iexec.core.chain;
18+
19+
import com.iexec.common.config.PublicChainConfig;
20+
import com.iexec.commons.poco.chain.SignerService;
21+
import lombok.Value;
22+
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.boot.context.properties.ConstructorBinding;
24+
import org.springframework.context.annotation.Bean;
25+
26+
@Value
27+
@ConstructorBinding
28+
@ConfigurationProperties(prefix = "wallet")
29+
public class WalletConfiguration {
30+
String encryptedFilePath;
31+
String password;
32+
33+
@Bean
34+
SignerService signerService(Web3jService web3jService, PublicChainConfig publicChainConfig) throws Exception {
35+
return new SignerService(web3jService.getWeb3j(), publicChainConfig.getChainId(), password, encryptedFilePath);
36+
}
37+
}

src/main/java/com/iexec/core/configuration/PublicConfigurationService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.iexec.core.configuration;
1818

19+
import com.iexec.commons.poco.chain.SignerService;
1920
import com.iexec.core.chain.ChainConfig;
20-
import com.iexec.core.chain.CredentialsService;
2121
import com.iexec.core.config.PublicConfiguration;
2222
import lombok.extern.slf4j.Slf4j;
2323
import org.springframework.stereotype.Service;
@@ -33,7 +33,7 @@
3333
@Service
3434
public class PublicConfigurationService {
3535
private final ChainConfig chainConfig;
36-
private final CredentialsService credentialsService;
36+
private final SignerService signerService;
3737
private final WorkerConfiguration workerConfiguration;
3838
private final ResultRepositoryConfiguration resultRepoConfig;
3939
private final ConfigServerClientConfig configServerClientConfig;
@@ -47,12 +47,12 @@ public class PublicConfigurationService {
4747
private String publicConfigurationHash = null;
4848

4949
public PublicConfigurationService(ChainConfig chainConfig,
50-
CredentialsService credentialsService,
50+
SignerService signerService,
5151
WorkerConfiguration workerConfiguration,
5252
ResultRepositoryConfiguration resultRepoConfig,
5353
ConfigServerClientConfig configServerClientConfig) {
5454
this.chainConfig = chainConfig;
55-
this.credentialsService = credentialsService;
55+
this.signerService = signerService;
5656
this.workerConfiguration = workerConfiguration;
5757
this.resultRepoConfig = resultRepoConfig;
5858
this.configServerClientConfig = configServerClientConfig;
@@ -64,7 +64,7 @@ void buildPublicConfiguration() {
6464
.workerPoolAddress(chainConfig.getPoolAddress())
6565
.blockchainAdapterUrl(configServerClientConfig.getUrl())
6666
.configServerUrl(configServerClientConfig.getUrl())
67-
.schedulerPublicAddress(credentialsService.getCredentials().getAddress())
67+
.schedulerPublicAddress(signerService.getAddress())
6868
.resultRepositoryURL(resultRepoConfig.getResultRepositoryURL())
6969
.askForReplicatePeriod(workerConfiguration.getAskForReplicatePeriod())
7070
.requiredWorkerVersion(workerConfiguration.getRequiredWorkerVersion())

src/test/java/com/iexec/core/chain/DealWatcherServiceTests.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2024 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.
@@ -33,15 +33,15 @@
3333
import org.assertj.core.api.Assertions;
3434
import org.junit.jupiter.api.AfterEach;
3535
import org.junit.jupiter.api.BeforeAll;
36-
import org.junit.jupiter.api.BeforeEach;
3736
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.api.extension.ExtendWith;
3838
import org.junit.jupiter.params.ParameterizedTest;
3939
import org.junit.jupiter.params.provider.Arguments;
4040
import org.junit.jupiter.params.provider.MethodSource;
4141
import org.mockito.ArgumentCaptor;
4242
import org.mockito.InjectMocks;
4343
import org.mockito.Mock;
44-
import org.mockito.MockitoAnnotations;
44+
import org.mockito.junit.jupiter.MockitoExtension;
4545
import org.springframework.context.ApplicationEventPublisher;
4646
import org.springframework.test.util.ReflectionTestUtils;
4747
import org.web3j.protocol.core.methods.response.Log;
@@ -56,6 +56,7 @@
5656
import static org.assertj.core.api.Assertions.assertThat;
5757
import static org.mockito.Mockito.*;
5858

59+
@ExtendWith(MockitoExtension.class)
5960
class DealWatcherServiceTests {
6061

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

94-
@BeforeEach
95-
void init() {
96-
MockitoAnnotations.openMocks(this);
95+
void initMocks() {
9796
when(chainConfig.getHubAddress()).thenReturn("hubAddress");
9897
when(chainConfig.getPoolAddress()).thenReturn("0x1");
9998
}
@@ -124,6 +123,7 @@ void shouldReturnZeroForAllCountersWhereNothingHasAppended() {
124123
@Test
125124
void shouldRunAndStop() {
126125
BigInteger blockNumber = BigInteger.TEN;
126+
initMocks();
127127
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(blockNumber);
128128
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.empty());
129129
dealWatcherService.run();
@@ -142,6 +142,7 @@ void shouldUpdateLastSeenBlockWhenOneDeal() {
142142
BigInteger blockOfDeal = BigInteger.valueOf(3);
143143
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice(blockOfDeal);
144144

145+
initMocks();
145146
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
146147
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
147148

@@ -182,8 +183,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndCreateTask() {
182183

183184
Task task = new Task();
184185

186+
initMocks();
185187
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
186-
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
188+
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
187189
when(iexecHubService.isBeforeContributionDeadline(chainDeal)).thenReturn(true);
188190
when(taskService.addTask(any(), anyInt(), anyLong(), any(), any(), anyInt(), anyLong(), any(), any(), any()))
189191
.thenReturn(Optional.of(task));
@@ -214,8 +216,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndNotCreateTaskSinceDealIsExpired() {
214216
BigInteger blockOfDeal = BigInteger.valueOf(3);
215217
IexecHubContract.SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice(blockOfDeal);
216218

219+
initMocks();
217220
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
218-
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
221+
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
219222
when(iexecHubService.isBeforeContributionDeadline(chainDeal)).thenReturn(false);
220223
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
221224

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

242+
initMocks();
239243
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
240-
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
244+
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
241245
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
242246

243247
dealWatcherService.subscribeToDealEventFromOneBlockToLatest(from);
@@ -257,8 +261,9 @@ void shouldUpdateLastSeenBlockWhenOneDealButNotCreateTaskSinceExceptionThrown()
257261
.botSize(BigInteger.valueOf(1))
258262
.build();
259263

264+
initMocks();
260265
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
261-
when(iexecHubService.getChainDeal(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
266+
when(iexecHubService.getChainDealWithDetails(BytesUtils.bytesToString(schedulerNotice.dealid))).thenReturn(Optional.of(chainDeal));
262267
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
263268

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

283+
initMocks();
278284
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
279285
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice1, schedulerNotice2));
280286

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

299+
initMocks();
293300
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(from);
294301
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
295302

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

316+
initMocks();
309317
when(configurationService.getLastSeenBlockWithDeal()).thenReturn(BigInteger.TEN);
310318
when(configurationService.getFromReplay()).thenReturn(BigInteger.ZERO);
311319
when(iexecHubService.getDealEventObservable(any())).thenReturn(Flowable.just(schedulerNotice));
312320

313321
dealWatcherService.replayDealEvent();
314322

315-
verify(iexecHubService).getChainDeal(any());
323+
verify(iexecHubService).getChainDealWithDetails(any());
316324
Counter lastBlockCounter = Metrics.globalRegistry.find(DealWatcherService.METRIC_DEALS_LAST_BLOCK).counter();
317325
Counter dealsReplayCounter = Metrics.globalRegistry.find(DealWatcherService.METRIC_DEALS_REPLAY_COUNT).counter();
318326
Assertions.assertThat(lastBlockCounter).isNotNull();

0 commit comments

Comments
 (0)