Skip to content

Commit 292d629

Browse files
committed
Remove duplicated call to blockchain in ProxyService
1 parent c7898e8 commit 292d629

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
## [[NEXT]](https://github.com/iExecBlockchainComputing/iexec-result-proxy/releases/tag/vNEXT) 2023
66

7+
### Bug Fixes
8+
9+
- Remove duplicated call to blockchain in `ProxyService`. (#110)
10+
711
### Quality
812

913
- Add and use a non-root user in the dockerfile. (#106)

src/main/java/com/iexec/resultproxy/proxy/ProxyService.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.iexec.common.utils.FileHelper;
2222
import com.iexec.common.worker.result.ResultUtils;
2323
import com.iexec.commons.poco.chain.ChainContribution;
24-
import com.iexec.commons.poco.chain.ChainContributionStatus;
2524
import com.iexec.commons.poco.chain.ChainTask;
2625
import com.iexec.commons.poco.chain.ChainTaskStatus;
2726
import com.iexec.commons.poco.task.TaskDescription;
@@ -39,6 +38,7 @@
3938

4039
import static com.iexec.common.utils.IexecFileHelper.SLASH_IEXEC_OUT;
4140
import static com.iexec.common.utils.IexecFileHelper.readComputedFile;
41+
import static com.iexec.commons.poco.chain.ChainContributionStatus.REVEALED;
4242

4343
/**
4444
* Service class to manage all the results. If the result is public, it will be stored on IPFS. If there is a dedicated
@@ -59,17 +59,18 @@ public ProxyService(IexecHubService iexecHubService,
5959

6060

6161
boolean canUploadResult(String chainTaskId, String walletAddress, byte[] zip) {
62-
if (iexecHubService.isTeeTask(chainTaskId)){
63-
Optional<ChainTask> chainTask = iexecHubService.getChainTask(chainTaskId);//TODO Add requester field to getChainTask
64-
if (chainTask.isEmpty()){
62+
if (iexecHubService.isTeeTask(chainTaskId)) {
63+
//TODO Add requester field to getChainTask
64+
Optional<ChainTask> chainTask = iexecHubService.getChainTask(chainTaskId);
65+
if (chainTask.isEmpty()) {
6566
log.error("Trying to upload result for TEE but getChainTask failed [chainTaskId:{}, uploader:{}]",
6667
chainTaskId, walletAddress);
6768
return false;
6869
}
69-
boolean isActive = chainTask.get().getStatus().equals(ChainTaskStatus.ACTIVE);
70+
boolean isActive = chainTask.get().getStatus() == ChainTaskStatus.ACTIVE;
7071

7172
Optional<TaskDescription> taskDescription = iexecHubService.getTaskDescriptionFromChain(chainTaskId);
72-
if (taskDescription.isEmpty()){
73+
if (taskDescription.isEmpty()) {
7374
log.error("Trying to upload result for TEE but getTaskDescription failed [chainTaskId:{}, uploader:{}]",
7475
chainTaskId, walletAddress);
7576
return false;
@@ -85,15 +86,6 @@ boolean canUploadResult(String chainTaskId, String walletAddress, byte[] zip) {
8586
return false;
8687
}
8788

88-
// ContributionStatus of chainTask should be REVEALED
89-
boolean isChainContributionStatusSetToRevealed = iexecHubService.isStatusTrueOnChain(chainTaskId,
90-
walletAddress, ChainContributionStatus.REVEALED);
91-
if (!isChainContributionStatusSetToRevealed) {
92-
log.error("Trying to upload result even though ChainContributionStatus is not REVEALED [chainTaskId:{}, uploadRequester:{}]",
93-
chainTaskId, walletAddress);
94-
return false;
95-
}
96-
9789
return isResultValid(chainTaskId, walletAddress, zip);
9890
}
9991
}
@@ -102,7 +94,8 @@ boolean canUploadResult(String chainTaskId, String walletAddress, byte[] zip) {
10294
* A result for a standard task is considered as valid if:
10395
* <ul>
10496
* <li>It has an associated on-chain contribution
105-
* <li>The on-chain contribution result hash is the one we compute here again.
97+
* <li>The associated on-chain contribution status is {@code REVEALED}
98+
* <li>The on-chain contribution result hash is the one we compute here again
10699
* </ul>
107100
*
108101
* @param chainTaskId ID of the task
@@ -116,8 +109,13 @@ boolean isResultValid(String chainTaskId, String walletAddress, byte[] zip) {
116109
final String zipDestinationPath = resultFolderPath + SLASH_IEXEC_OUT;
117110
try {
118111
final Optional<ChainContribution> oChainContribution = iexecHubService.getChainContribution(chainTaskId, walletAddress);
119-
if (oChainContribution.isEmpty()) {
120-
log.error("Trying to upload result but no on-chain contribution [chainTaskId:{}, uploader:{}]",
112+
// ContributionStatus of chainTask should be REVEALED
113+
boolean isChainContributionStatusSetToRevealed = oChainContribution
114+
.map(ChainContribution::getStatus)
115+
.filter(chainStatus -> chainStatus == REVEALED)
116+
.isPresent();
117+
if (!isChainContributionStatusSetToRevealed) {
118+
log.error("Trying to upload result even though ChainContributionStatus is not REVEALED [chainTaskId:{}, uploadRequester:{}]",
121119
chainTaskId, walletAddress);
122120
return false;
123121
}
@@ -129,7 +127,7 @@ boolean isResultValid(String chainTaskId, String walletAddress, byte[] zip) {
129127
log.error("Can't write result file [chainTaskId:{}, uploader:{}]", chainTaskId, walletAddress);
130128
return false;
131129
}
132-
FileHelper.unZipFile(resultZipPath, zipDestinationPath);
130+
FileHelper.unZipFile(resultZipPath, zipDestinationPath);
133131

134132
final ComputedFile computedFile = readComputedFile(chainTaskId, zipDestinationPath);
135133
final String resultDigest = ResultUtils.computeWeb2ResultDigest(computedFile, resultFolderPath);

src/test/java/com/iexec/resultproxy/proxy/ProxyServiceTest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
import java.util.Base64;
1616
import java.util.Optional;
1717

18+
import static com.iexec.commons.poco.chain.ChainContributionStatus.CONTRIBUTED;
1819
import static com.iexec.commons.poco.chain.ChainContributionStatus.REVEALED;
1920
import static org.assertj.core.api.Assertions.assertThat;
2021
import static org.mockito.Mockito.*;
2122

2223
class ProxyServiceTest {
2324
private static final String CHAIN_TASK_ID = "0x59d9b6c36d6db89bae058ff55de6e4d6a6f6e0da3f9ea02297fc8d6d5f5cedf1";
25+
private static final String RESULT_HASH = "0x865e1ebff87de7928040a42383b46690a12a988b278eb880e0e641f5da3cc9d1";
2426
private static final String WALLET_ADDRESS = "0x123abc";
2527
/**
2628
* Contains a valid result zip, with the following files:
@@ -36,7 +38,8 @@ class ProxyServiceTest {
3638
* including a fixed ChainTask ID: {@literal 0x59d9b6c36d6db89bae058ff55de6e4d6a6f6e0da3f9ea02297fc8d6d5f5cedf1}.
3739
*/
3840
private static final ChainContribution CHAIN_CONTRIBUTION = ChainContribution.builder()
39-
.resultHash("0x865e1ebff87de7928040a42383b46690a12a988b278eb880e0e641f5da3cc9d1")
41+
.status(REVEALED)
42+
.resultHash(RESULT_HASH)
4043
.build();
4144

4245
@TempDir
@@ -62,46 +65,40 @@ void init() {
6265
void isNotAbleToUploadSinceNoChainContribution() {
6366
when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false);
6467
when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false);
65-
when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true);
6668
when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.empty());
6769

6870
assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isFalse();
6971

7072
verify(iexecHubService).isTeeTask(CHAIN_TASK_ID);
7173
verify(proxyService).isResultFound(CHAIN_TASK_ID);
72-
verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED);
7374
verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS);
7475
}
7576

7677
@Test
7778
void isNotAbleToUploadSinceCannotWriteZip() {
7879
when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false);
7980
when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false);
80-
when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true);
8181
when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION));
8282
when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn("/this/path/does/not/exist");
8383

8484
assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isFalse();
8585

8686
verify(iexecHubService).isTeeTask(CHAIN_TASK_ID);
8787
verify(proxyService).isResultFound(CHAIN_TASK_ID);
88-
verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED);
8988
verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS);
9089
}
9190

9291
@Test
9392
void isNotAbleToUploadSinceWrongHash() {
9493
when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false);
9594
when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false);
96-
when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true);
9795
when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION));
9896
when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn(tmpFolder.getAbsolutePath());
9997

100-
assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, new byte[] {})).isFalse();
98+
assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, new byte[]{})).isFalse();
10199

102100
verify(iexecHubService).isTeeTask(CHAIN_TASK_ID);
103101
verify(proxyService).isResultFound(CHAIN_TASK_ID);
104-
verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED);
105102
verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS);
106103
}
107104

@@ -116,31 +113,28 @@ void isNotAbleToUploadSinceResultAlreadyExistsWithIpfs() {
116113

117114
verify(iexecHubService).isTeeTask(CHAIN_TASK_ID);
118115
verify(proxyService).isResultFound(CHAIN_TASK_ID);
119-
verify(iexecHubService, never()).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED);
120116
verify(iexecHubService, never()).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS);
121117
}
122118

123119
@Test
124120
void isNotAbleToUploadSinceChainStatusIsNotRevealedWithIpfs() {
121+
ChainContribution chainContribution = ChainContribution.builder().status(CONTRIBUTED).resultHash(RESULT_HASH).build();
125122
when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false);
126123
when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(true);
127-
when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(false);
128-
when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION));
124+
when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(chainContribution));
129125
when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn(tmpFolder.getAbsolutePath());
130126

131127
assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isFalse();
132128

133129
verify(iexecHubService).isTeeTask(CHAIN_TASK_ID);
134130
verify(proxyService).isResultFound(CHAIN_TASK_ID);
135-
verify(iexecHubService, never()).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED);
136131
verify(iexecHubService, never()).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS);
137132
}
138133

139134
@Test
140135
void isAbleToUploadWithIpfs() {
141136
when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false);
142137
when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false);
143-
when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true);
144138
when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION));
145139
when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn(tmpFolder.getAbsolutePath());
146140

@@ -149,6 +143,5 @@ void isAbleToUploadWithIpfs() {
149143
verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS);
150144
verify(iexecHubService).isTeeTask(CHAIN_TASK_ID);
151145
verify(proxyService).isResultFound(CHAIN_TASK_ID);
152-
verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED);
153146
}
154147
}

0 commit comments

Comments
 (0)