Skip to content

Commit 2358967

Browse files
authored
feat: create WorkerpoolAuthorization instances with on-chain deal ID and task index (#761)
1 parent 7da7812 commit 2358967

File tree

15 files changed

+195
-167
lines changed

15 files changed

+195
-167
lines changed

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,19 @@ private void handleDeal(DealEvent dealEvent) {
198198
int startBag = chainDeal.getBotFirst().intValue();
199199
int endBag = chainDeal.getBotFirst().intValue() + chainDeal.getBotSize().intValue();
200200
for (int taskIndex = startBag; taskIndex < endBag; taskIndex++) {
201-
Optional<Task> optional = taskService.addTask(
202-
chainDealId,
203-
taskIndex,
204-
dealEvent.getBlockNumber().longValue(),
205-
chainDeal.getChainApp().getMultiaddr(),
206-
chainDeal.getParams().getIexecArgs(),
207-
chainDeal.getTrust().intValue(),
208-
chainDeal.getChainCategory().getMaxExecutionTime(),
209-
chainDeal.getTag(),
210-
iexecHubService.getChainDealContributionDeadline(chainDeal),
211-
iexecHubService.getChainDealFinalDeadline(chainDeal));
212-
optional.ifPresent(task -> applicationEventPublisher
213-
.publishEvent(new TaskCreatedEvent(task.getChainTaskId())));
201+
taskService
202+
.addTask(
203+
chainDealId,
204+
taskIndex,
205+
dealEvent.getBlockNumber().longValue(),
206+
chainDeal.getChainApp().getMultiaddr(),
207+
chainDeal.getParams().getIexecArgs(),
208+
chainDeal.getTrust().intValue(),
209+
chainDeal.getChainCategory().getMaxExecutionTime(),
210+
chainDeal.getTag(),
211+
iexecHubService.getChainDealContributionDeadline(chainDeal),
212+
iexecHubService.getChainDealFinalDeadline(chainDeal))
213+
.ifPresent(this::publishTaskCreatedEvent);
214214
}
215215
}
216216

@@ -248,6 +248,17 @@ boolean shouldProcessDeal(ChainDeal chainDeal) {
248248
return true;
249249
}
250250

251+
/**
252+
* Publishes a {@code TaskCreatedEvent} for a newly created task.
253+
*
254+
* @param task Newly created task for which the event will be published
255+
*/
256+
private void publishTaskCreatedEvent(final Task task) {
257+
final TaskCreatedEvent event = new TaskCreatedEvent(
258+
this, task.getChainTaskId(), task.getChainDealId(), task.getTaskIndex());
259+
applicationEventPublisher.publishEvent(event);
260+
}
261+
251262
/*
252263
* Some deal events are sometimes missed by #schedulerNoticeEventObservable method
253264
* so we decide to replay events from times to times (already saved events will be ignored)

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2025 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.
@@ -27,23 +27,29 @@ public class SignatureService {
2727

2828
private final SignerService signerService;
2929

30-
public SignatureService(SignerService signerService) {
30+
public SignatureService(final SignerService signerService) {
3131
this.signerService = signerService;
3232
}
3333

3434
public String getAddress() {
3535
return signerService.getAddress();
3636
}
3737

38-
public Signature sign(String hash) {
38+
public Signature sign(final String hash) {
3939
return signerService.signMessageHash(hash);
4040
}
4141

42-
public WorkerpoolAuthorization createAuthorization(String workerWallet, String chainTaskId, String enclaveChallenge) {
42+
public WorkerpoolAuthorization createAuthorization(final String workerWallet,
43+
final String chainTaskId,
44+
final String dealId,
45+
final int taskIndex,
46+
final String enclaveChallenge) {
4347
final String hash = HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge);
4448
return WorkerpoolAuthorization.builder()
4549
.workerWallet(workerWallet)
4650
.chainTaskId(chainTaskId)
51+
.dealId(dealId)
52+
.taskIndex(taskIndex)
4753
.enclaveChallenge(enclaveChallenge)
4854
.signature(sign(hash))
4955
.build();

src/main/java/com/iexec/core/replicate/ReplicateSupplyService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ private Optional<ReplicateTaskSummary> getReplicateTaskSummary(Task task, String
158158
final WorkerpoolAuthorization authorization = signatureService.createAuthorization(
159159
walletAddress,
160160
chainTaskId,
161+
task.getChainDealId(),
162+
task.getTaskIndex(),
161163
task.getEnclaveChallenge());
162164
ReplicateTaskSummaryBuilder replicateTaskSummary = ReplicateTaskSummary.builder()
163165
.workerpoolAuthorization(authorization);
@@ -290,7 +292,7 @@ public List<TaskNotification> getMissedTaskNotifications(long blockNumber, Strin
290292

291293
private TaskNotificationExtra getTaskNotificationExtra(Task task, TaskNotificationType taskNotificationType, String walletAddress) {
292294
final WorkerpoolAuthorization authorization = signatureService.createAuthorization(
293-
walletAddress, task.getChainTaskId(), task.getEnclaveChallenge());
295+
walletAddress, task.getChainTaskId(), task.getChainDealId(), task.getTaskIndex(), task.getEnclaveChallenge());
294296
final TaskNotificationExtra.TaskNotificationExtraBuilder taskNotificationExtra =
295297
TaskNotificationExtra.builder().workerpoolAuthorization(authorization);
296298

src/main/java/com/iexec/core/result/ResultService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public class ResultService {
3939
private final SignatureService signatureService;
4040
private final TaskService taskService;
4141

42-
public ResultService(final ResultRepositoryConfiguration resultRepositoryConfiguration, final SignatureService signatureService, final TaskService taskService) {
42+
public ResultService(final ResultRepositoryConfiguration resultRepositoryConfiguration,
43+
final SignatureService signatureService,
44+
final TaskService taskService) {
4345
this.resultRepositoryConfiguration = resultRepositoryConfiguration;
4446
this.signatureService = signatureService;
4547
this.taskService = taskService;
@@ -50,7 +52,8 @@ public boolean isResultUploaded(final TaskDescription taskDescription) {
5052
final String chainTaskId = taskDescription.getChainTaskId();
5153
final ResultProxyClient resultProxyClient = resultRepositoryConfiguration.createResultProxyClientFromURL(taskDescription.getDealParams().getIexecResultStorageProxy());
5254
final String enclaveChallenge = taskService.getTaskByChainTaskId(chainTaskId).map(Task::getEnclaveChallenge).orElse(EMPTY_ADDRESS);
53-
final WorkerpoolAuthorization workerpoolAuthorization = signatureService.createAuthorization(signatureService.getAddress(), chainTaskId, enclaveChallenge);
55+
final WorkerpoolAuthorization workerpoolAuthorization = signatureService.createAuthorization(
56+
signatureService.getAddress(), chainTaskId, taskDescription.getChainDealId(), taskDescription.getBotIndex(), enclaveChallenge);
5457
final String resultProxyToken = resultProxyClient.getJwt(workerpoolAuthorization.getSignature().getValue(), workerpoolAuthorization);
5558
if (resultProxyToken.isEmpty()) {
5659
log.error("isResultUploaded failed (getResultProxyToken) [chainTaskId:{}]", chainTaskId);

src/main/java/com/iexec/core/sms/SmsService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,27 @@ private boolean checkSmsTeeFramework(SmsClient smsClient,
138138
return true;
139139
}
140140

141-
public Optional<String> getEnclaveChallenge(String chainTaskId, String smsUrl) {
141+
public Optional<String> getEnclaveChallenge(final String chainTaskId,
142+
final String dealId,
143+
final int taskIndex,
144+
final String smsUrl) {
142145
return StringUtils.isEmpty(smsUrl)
143146
? Optional.of(BytesUtils.EMPTY_ADDRESS)
144-
: generateEnclaveChallenge(chainTaskId, smsUrl);
147+
: generateEnclaveChallenge(chainTaskId, dealId, taskIndex, smsUrl);
145148
}
146149

147-
Optional<String> generateEnclaveChallenge(String chainTaskId, String smsUrl) {
150+
Optional<String> generateEnclaveChallenge(final String chainTaskId,
151+
final String dealId,
152+
final int taskIndex,
153+
final String smsUrl) {
148154
// SMS client should already have been created once before.
149155
// If it couldn't be created, then the task would have been aborted.
150156
// So the following won't throw an exception.
151157
final SmsClient smsClient = smsClientProvider.getSmsClient(smsUrl);
152158

153159
try {
154160
final String teeChallengePublicKey = smsClient.generateTeeChallenge(
155-
signatureService.createAuthorization("", chainTaskId, "").getSignature().getValue(),
161+
signatureService.createAuthorization("", chainTaskId, dealId, taskIndex, "").getSignature().getValue(),
156162
chainTaskId);
157163

158164
if (StringUtils.isEmpty(teeChallengePublicKey)) {

src/main/java/com/iexec/core/task/event/TaskCreatedEvent.java

Lines changed: 14 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-2025 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.
@@ -16,9 +16,21 @@
1616

1717
package com.iexec.core.task.event;
1818

19+
import lombok.EqualsAndHashCode;
1920
import lombok.Value;
21+
import org.springframework.context.ApplicationEvent;
2022

2123
@Value
22-
public class TaskCreatedEvent {
24+
@EqualsAndHashCode(callSuper = true)
25+
public class TaskCreatedEvent extends ApplicationEvent {
2326
String chainTaskId;
27+
String chainDealId;
28+
int taskIndex;
29+
30+
public TaskCreatedEvent(final Object source, final String chainTaskId, final String chainDealId, final int taskIndex) {
31+
super(source);
32+
this.chainTaskId = chainTaskId;
33+
this.chainDealId = chainDealId;
34+
this.taskIndex = taskIndex;
35+
}
2436
}

src/main/java/com/iexec/core/task/update/TaskUpdateManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ void initializing2Initialized(final Task task) {
246246
update.set("initializationBlockNumber", task.getDealBlockNumber());
247247

248248
// Create enclave challenge after task has been initialized on-chain
249-
final Optional<String> enclaveChallenge = smsService.getEnclaveChallenge(task.getChainTaskId(), task.getSmsUrl());
249+
final Optional<String> enclaveChallenge = smsService.getEnclaveChallenge(
250+
task.getChainTaskId(), task.getChainDealId(), task.getTaskIndex(), task.getSmsUrl());
250251
if (enclaveChallenge.isEmpty()) {
251252
log.error("Can't initialize task, enclave challenge is empty [chainTaskId:{}]", task.getChainTaskId());
252253
toFailed(task, INITIALIZE_FAILED);

src/test/java/com/iexec/core/TestUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
public class TestUtils {
3030
public static final String CHAIN_DEAL_ID = "0xd82223e5feff6720792ffed1665e980da95e5d32b177332013eaba8edc07f31c";
3131
public static final String CHAIN_TASK_ID = "0x65bc5e94ed1486b940bd6cc0013c418efad58a0a52a3d08cee89faaa21970426";
32+
public static final int TASK_INDEX = 0;
3233

3334
public static final String WORKER_ADDRESS = "0x87ae2b87b5db23830572988fb1f51242fbc471ce";
3435
public static final String WALLET_WORKER_1 = "0x1a69b2eb604db8eba185df03ea4f5288dcbbd248";

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ void shouldUpdateLastSeenBlockWhenOneDealAndCreateTask() {
198198
verify(configurationService).setLastSeenBlockWithDeal(blockOfDeal);
199199
verify(applicationEventPublisher).publishEvent(any(TaskCreatedEvent.class));
200200
verify(applicationEventPublisher).publishEvent(argumentCaptor.capture());
201-
assertThat(argumentCaptor.getValue()).isEqualTo(new TaskCreatedEvent(task.getChainTaskId()));
201+
assertThat(argumentCaptor.getValue())
202+
.usingRecursiveComparison()
203+
.ignoringFields("timestamp")
204+
.isEqualTo(new TaskCreatedEvent(dealWatcherService, task.getChainTaskId(), task.getChainDealId(), task.getTaskIndex()));
202205
}
203206

204207
@Test

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2025 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.
@@ -42,36 +42,34 @@ class SignatureServiceTests {
4242
@InjectMocks
4343
private SignatureService signatureService;
4444

45+
private final String workerWallet = "0x748e091bf16048cb5103E0E10F9D5a8b7fBDd860";
46+
private final String chainTaskId = "0xd94b63fc2d3ec4b96daf84b403bbafdc8c8517e8e2addd51fec0fa4e67801be8";
47+
private final String enclaveChallenge = "0x9a43BB008b7A657e1936ebf5d8e28e5c5E021596";
48+
4549
@Test
4650
void shouldAuthorizationHashBeValid() {
47-
String workerWallet = "0x748e091bf16048cb5103E0E10F9D5a8b7fBDd860";
48-
String chainTaskid = "0xd94b63fc2d3ec4b96daf84b403bbafdc8c8517e8e2addd51fec0fa4e67801be8";
49-
String enclaveWallet = "0x9a43BB008b7A657e1936ebf5d8e28e5c5E021596";
50-
51-
String expected = "0x54a76d209e8167e1ffa3bde8e3e7b30068423ca9554e1d605d8ee8fd0f165562";
52-
53-
assertThat(HashUtils.concatenateAndHash(workerWallet, chainTaskid, enclaveWallet)).isEqualTo(expected);
51+
final String expectedHash = "0x54a76d209e8167e1ffa3bde8e3e7b30068423ca9554e1d605d8ee8fd0f165562";
52+
assertThat(HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge)).isEqualTo(expectedHash);
5453
}
5554

5655
@Test
5756
void shouldCreateCorrectAuthorization() {
58-
// input
59-
String workerWallet = "0x748e091bf16048cb5103E0E10F9D5a8b7fBDd860";
60-
String chainTaskId = "0xd94b63fc2d3ec4b96daf84b403bbafdc8c8517e8e2addd51fec0fa4e67801be8";
61-
String enclaveChallenge = "0x9a43BB008b7A657e1936ebf5d8e28e5c5E021596";
62-
String privateKey = "0x2a46e8c1535792f6689b10d5c882c9363910c30751ec193ae71ec71630077909";
63-
String hash = HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge);
57+
final String privateKey = "0x2a46e8c1535792f6689b10d5c882c9363910c30751ec193ae71ec71630077909";
58+
final String hash = HashUtils.concatenateAndHash(workerWallet, chainTaskId, enclaveChallenge);
6459

6560
ReflectionTestUtils.setField(signerService, "credentials", Credentials.create(privateKey));
6661
when(signerService.signMessageHash(hash)).thenCallRealMethod();
6762

6863
// creation
69-
WorkerpoolAuthorization authorization = signatureService.createAuthorization(workerWallet, chainTaskId, enclaveChallenge);
64+
final WorkerpoolAuthorization authorization =
65+
signatureService.createAuthorization(workerWallet, chainTaskId, "", 0, enclaveChallenge);
7066

7167
// check
72-
WorkerpoolAuthorization expected = WorkerpoolAuthorization.builder()
68+
final WorkerpoolAuthorization expected = WorkerpoolAuthorization.builder()
7369
.workerWallet(workerWallet)
7470
.chainTaskId(chainTaskId)
71+
.dealId("")
72+
.taskIndex(0)
7573
.enclaveChallenge(enclaveChallenge)
7674
.signature(new Signature(
7775
BytesUtils.stringToBytes("0x63f2c959ed7dfc11619e1e0b5ba8a4bf56f81ce81d0b6e6e9cdeca538cb85d97"),

0 commit comments

Comments
 (0)