Skip to content

Commit f753d6c

Browse files
authored
Use new FileHashUtils API (#622)
1 parent 68ec17d commit f753d6c

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
99
- Add workerpool address in configuration instead of reading from Scheduler `/workers/config` endpoint. (#607)
1010
- Set `0x0` as default value for Workerpool address and prevents startup if incorrectly configured. (#608)
1111
- Implement `Purgeable` on `SubscriptionService`. (#620)
12+
- Use new `FileHashUtils` API. (#622)
1213

1314
### Bug fixes
1415

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version=8.5.0
2-
iexecCommonVersion=8.5.0
2+
iexecCommonVersion=8.5.0-NEXT-SNAPSHOT
33
iexecCommonsContainersVersion=1.2.2
44
iexecCommonsPocoVersion=4.1.0-NEXT-SNAPSHOT
55
iexecResultVersion=8.5.0

src/main/java/com/iexec/worker/dataset/DataService.java

Lines changed: 27 additions & 30 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.
@@ -30,7 +30,6 @@
3030

3131
import javax.annotation.Nonnull;
3232
import java.io.File;
33-
import java.nio.file.Paths;
3433
import java.util.List;
3534

3635

@@ -47,20 +46,20 @@ public DataService(WorkerConfigurationService workerConfigurationService) {
4746
/**
4847
* Download dataset file for the given standard task and save
4948
* it in {@link IexecFileHelper#SLASH_IEXEC_IN}.
50-
*
49+
*
5150
* @param taskDescription Task description containing dataset related parameters
5251
* @return downloaded dataset file path
5352
* @throws WorkflowException if download fails or bad checksum.
5453
*/
5554
public String downloadStandardDataset(@Nonnull TaskDescription taskDescription)
5655
throws WorkflowException {
57-
String chainTaskId = taskDescription.getChainTaskId();
58-
String uri = taskDescription.getDatasetUri();
59-
String filename = taskDescription.getDatasetAddress();
60-
String parentDirectoryPath = workerConfigurationService.getTaskInputDir(chainTaskId);
56+
final String chainTaskId = taskDescription.getChainTaskId();
57+
final String uri = taskDescription.getDatasetUri();
58+
final String filename = taskDescription.getDatasetAddress();
59+
final String parentDirectoryPath = workerConfigurationService.getTaskInputDir(chainTaskId);
6160
String datasetLocalFilePath = "";
6261
if (MultiAddressHelper.isMultiAddress(uri)) {
63-
for (String ipfsGateway : MultiAddressHelper.IPFS_GATEWAYS) {
62+
for (final String ipfsGateway : MultiAddressHelper.IPFS_GATEWAYS) {
6463
log.debug("Try to download dataset from {}", ipfsGateway);
6564
datasetLocalFilePath =
6665
downloadFile(chainTaskId, ipfsGateway + uri, parentDirectoryPath, filename);
@@ -75,17 +74,16 @@ public String downloadStandardDataset(@Nonnull TaskDescription taskDescription)
7574
if (datasetLocalFilePath.isEmpty()) {
7675
throw new WorkflowException(ReplicateStatusCause.DATASET_FILE_DOWNLOAD_FAILED);
7776
}
78-
String expectedSha256 = taskDescription.getDatasetChecksum();
77+
final String expectedSha256 = taskDescription.getDatasetChecksum();
7978
if (StringUtils.isEmpty(expectedSha256)) {
80-
log.warn("INSECURE! Cannot check empty on-chain dataset checksum " +
81-
"[chainTaskId:{}]", chainTaskId);
79+
log.warn("INSECURE! Cannot check empty on-chain dataset checksum [chainTaskId:{}]",
80+
chainTaskId);
8281
return datasetLocalFilePath;
8382
}
84-
String actualSha256 = FileHashUtils.sha256(new File(datasetLocalFilePath));
83+
final String actualSha256 = FileHashUtils.sha256(new File(datasetLocalFilePath));
8584
if (!expectedSha256.equals(actualSha256)) {
86-
log.error("Dataset checksum mismatch [chainTaskId:{}, " +
87-
"expected:{}, actual:{}]", chainTaskId, expectedSha256,
88-
actualSha256);
85+
log.error("Dataset checksum mismatch [chainTaskId:{}, expected:{}, actual:{}]",
86+
chainTaskId, expectedSha256, actualSha256);
8987
throw new WorkflowException(ReplicateStatusCause.DATASET_FILE_BAD_CHECKSUM);
9088
}
9189
return datasetLocalFilePath;
@@ -94,18 +92,17 @@ public String downloadStandardDataset(@Nonnull TaskDescription taskDescription)
9492
/**
9593
* Download input files for the given standard task and save them
9694
* in the input folder.
97-
*
95+
*
9896
* @param chainTaskId Task ID used to create input files download folder
99-
* @param uriList List of input files to download
97+
* @param uriList List of input files to download
10098
* @throws WorkflowException if download fails.
10199
*/
102-
public void downloadStandardInputFiles(String chainTaskId, @Nonnull List<String> uriList)
100+
public void downloadStandardInputFiles(final String chainTaskId, @Nonnull final List<String> uriList)
103101
throws WorkflowException {
104-
for (String uri: uriList) {
105-
String filename = !StringUtils.isEmpty(uri)
106-
? Paths.get(uri).getFileName().toString()
107-
: "";
108-
String parenDirectoryPath = workerConfigurationService.getTaskInputDir(chainTaskId);
102+
for (final String uri : uriList) {
103+
final String filename = FileHashUtils.createFileNameFromUri(uri);
104+
final String parenDirectoryPath = workerConfigurationService.getTaskInputDir(chainTaskId);
105+
log.debug("Download file [uri:{}, fileName:{}]", uri, filename);
109106
if (downloadFile(chainTaskId, uri, parenDirectoryPath, filename).isEmpty()) {
110107
throw new WorkflowException(ReplicateStatusCause.INPUT_FILES_DOWNLOAD_FAILED);
111108
}
@@ -115,21 +112,21 @@ public void downloadStandardInputFiles(String chainTaskId, @Nonnull List<String>
115112
/**
116113
* Download a file from a URI in the provided parent
117114
* directory and save it with the provided filename.
118-
*
119-
* @param chainTaskId Task ID, for logging purpose
120-
* @param uri URI of single file to download
115+
*
116+
* @param chainTaskId Task ID, for logging purpose
117+
* @param uri URI of single file to download
121118
* @param parentDirectoryPath Destination folder on worker host
122-
* @param filename Name of downloaded file in destination folder
119+
* @param filename Name of downloaded file in destination folder
123120
* @return absolute path of the saved file on worker host
124121
*/
125-
String downloadFile(String chainTaskId, String uri,
126-
String parentDirectoryPath, String filename) {
122+
String downloadFile(final String chainTaskId, final String uri,
123+
final String parentDirectoryPath, final String filename) {
127124
if (StringUtils.isEmpty(chainTaskId) ||
128125
StringUtils.isEmpty(uri) ||
129126
StringUtils.isEmpty(parentDirectoryPath) ||
130127
StringUtils.isEmpty(filename)) {
131128
log.error("Failed to download, args shouldn't be empty " +
132-
"[chainTaskId:{}, datasetUri:{}, parentDir:{}, filename:{}]",
129+
"[chainTaskId:{}, datasetUri:{}, parentDir:{}, filename:{}]",
133130
chainTaskId, uri, parentDirectoryPath, filename);
134131
return StringUtils.EMPTY;
135132
}

src/test/java/com/iexec/worker/dataset/DataServiceTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.iexec.worker.dataset;
1818

1919
import com.iexec.common.replicate.ReplicateStatusCause;
20+
import com.iexec.common.utils.FileHashUtils;
2021
import com.iexec.commons.poco.task.TaskDescription;
2122
import com.iexec.worker.config.WorkerConfigurationService;
2223
import com.iexec.worker.utils.WorkflowException;
@@ -85,7 +86,7 @@ void beforeEach() {
8586
@Test
8687
void shouldDownloadStandardTaskDataset() throws Exception {
8788
final TaskDescription taskDescription = getTaskDescriptionBuilder().build();
88-
String filepath = dataService.downloadStandardDataset(taskDescription);
89+
final String filepath = dataService.downloadStandardDataset(taskDescription);
8990
assertThat(filepath).isEqualTo(iexecIn + "/" + DATASET_ADDRESS);
9091
}
9192

@@ -178,7 +179,7 @@ void shouldNotDownloadDatasetSinceEmptyUri() {
178179
final TaskDescription taskDescription = getTaskDescriptionBuilder()
179180
.datasetUri("")
180181
.build();
181-
WorkflowException e = assertThrows(
182+
final WorkflowException e = assertThrows(
182183
WorkflowException.class,
183184
() -> dataService.downloadStandardDataset(taskDescription));
184185
assertThat(e.getReplicateStatusCause())
@@ -190,7 +191,7 @@ void shouldNotDownloadDatasetSinceEmptyDatasetAddress() {
190191
final TaskDescription taskDescription = getTaskDescriptionBuilder()
191192
.datasetAddress("")
192193
.build();
193-
WorkflowException e = assertThrows(
194+
final WorkflowException e = assertThrows(
194195
WorkflowException.class,
195196
() -> dataService.downloadStandardDataset(taskDescription));
196197
assertThat(e.getReplicateStatusCause())
@@ -201,7 +202,7 @@ void shouldNotDownloadDatasetSinceEmptyDatasetAddress() {
201202
void shouldNotDownloadDatasetSinceEmptyParentDirectory() {
202203
final TaskDescription taskDescription = getTaskDescriptionBuilder().build();
203204
when(workerConfigurationService.getTaskInputDir(CHAIN_TASK_ID)).thenReturn("");
204-
WorkflowException e = assertThrows(
205+
final WorkflowException e = assertThrows(
205206
WorkflowException.class,
206207
() -> dataService.downloadStandardDataset(taskDescription));
207208
assertThat(e.getReplicateStatusCause())
@@ -213,7 +214,7 @@ void shouldNotDownloadDatasetSinceBadChecksum() {
213214
final TaskDescription taskDescription = getTaskDescriptionBuilder()
214215
.datasetChecksum("badChecksum")
215216
.build();
216-
WorkflowException e = assertThrows(
217+
final WorkflowException e = assertThrows(
217218
WorkflowException.class,
218219
() -> dataService.downloadStandardDataset(taskDescription));
219220
assertThat(e.getReplicateStatusCause())
@@ -231,9 +232,9 @@ void shouldDownloadDatasetSinceEmptyOnchainChecksum() throws Exception {
231232

232233
@Test
233234
void shouldDownloadInputFiles() throws Exception {
234-
List<String> uris = List.of(HTTP_URI);
235+
final List<String> uris = List.of(HTTP_URI);
235236
dataService.downloadStandardInputFiles(CHAIN_TASK_ID, uris);
236-
File inputFile = new File(iexecIn, "iExec-RLC-RLC-icon.png");
237+
final File inputFile = new File(iexecIn, FileHashUtils.createFileNameFromUri(HTTP_URI));
237238
assertThat(inputFile).exists();
238239
}
239240
}

0 commit comments

Comments
 (0)