Skip to content

Commit 58f2688

Browse files
authored
Merge pull request #90 from iExecBlockchainComputing/feature/[email protected]@8.2.0
Feature/iexec commons [email protected] iexec [email protected]
2 parents 457b9f9 + 332847f commit 58f2688

File tree

8 files changed

+178
-40
lines changed

8 files changed

+178
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ All notable changes to this project will be documented in this file.
1414
- Use `testcontainers` in integration tests. (#89)
1515
### Dependency Upgrades
1616
- Upgrade to `feign` 11.10. (#80)
17-
- Upgrade to `iexec-common` 8.1.0-NEXT-SNAPSHOT. (#83 #85)
18-
- Upgrade to `iexec-commons-poco` 2.0.1. (#83 #85 #86)
17+
- Upgrade to `iexec-common` 8.2.0. (#83 #85 #90)
18+
- Upgrade to `iexec-commons-poco` 3.0.2. (#83 #85 #86 #90)
1919

2020
## [[8.0.1]](https://github.com/iExecBlockchainComputing/iexec-blockchain-adapter-api/releases/tag/v8.0.1) 2023-04-06
2121

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ To run properly, the iExec Blockchain Adapter API requires:
3232
| `IEXEC_BLOCKCHAIN_ADAPTER_API_IS_SIDECHAIN` | Define if iExec on-chain protocol is built on top of token (`false`) or native currency (`true`). | Boolean | `false` |
3333
| `IEXEC_BLOCKCHAIN_ADAPTER_API_GAS_PRICE_MULTIPLIER` | Transactions will be sent with `networkGasPrice * gasPriceMultiplier`. | Float | `1.0` |
3434
| `IEXEC_BLOCKCHAIN_ADAPTER_API_GAS_PRICE_CAP` | In Wei, will be used for transactions if `networkGasPrice * gasPriceMultiplier > gasPriceCap`. | Positive integer | `22000000000` |
35-
| `IEXEC_BLOCKCHAIN_ADAPTER_API_BROKER_URL` | URL of the broker to interact with when matching iExec orders. | URL | `http://localhost:3000` |
3635
| `IEXEC_BLOCKCHAIN_ADAPTER_API_WALLET_PATH` | Path to the wallet of the server. | String | `src/main/resources/wallet.json` |
3736
| `IEXEC_BLOCKCHAIN_ADAPTER_API_WALLET_PASSWORD` | Password to unlock the wallet of the server. | String | `whatever` |
3837

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version=8.0.1
2-
iexecCommonVersion=8.1.0-NEXT-SNAPSHOT
3-
iexecCommonsPocoVersion=2.0.1
2+
iexecCommonVersion=8.2.0
3+
iexecCommonsPocoVersion=3.0.2
44

55
nexusUser
66
nexusPassword

src/itest/resources/logback.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<configuration>
3+
<property name="LOG_LEVEL" value="${IEXEC_LOG_LEVEL:-INFO}"/>
4+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
5+
<logger name="org.springframework.web" level="${LOG_LEVEL}"/>
6+
7+
<logger name="org.testcontainers" level="INFO"/>
8+
</configuration>

src/main/java/com/iexec/blockchain/tool/IexecHubService.java

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.iexec.common.utils.EthAddress;
2020
import com.iexec.common.worker.result.ResultUtils;
2121
import com.iexec.commons.poco.chain.*;
22-
import com.iexec.commons.poco.contract.generated.IexecHubContract;
2322
import com.iexec.commons.poco.utils.BytesUtils;
2423
import org.apache.commons.lang3.StringUtils;
2524
import org.springframework.stereotype.Service;
@@ -35,22 +34,14 @@
3534
@Service
3635
public class IexecHubService extends IexecHubAbstractService {
3736

38-
public static final int ONE_SECOND = 1000;
39-
private final CredentialsService credentialsService;
40-
private final Web3jService web3jService;
41-
private final Integer blockTime;
42-
private final Integer chainId;
43-
4437
public IexecHubService(CredentialsService credentialsService,
4538
Web3jService web3jService,
4639
ChainConfig chainConfig) {
47-
super(credentialsService.getCredentials(),
40+
super(
41+
credentialsService.getCredentials(),
4842
web3jService,
49-
chainConfig.getHubAddress());
50-
this.credentialsService = credentialsService;
51-
this.web3jService = web3jService;
52-
blockTime = chainConfig.getBlockTime();
53-
chainId = chainConfig.getChainId();
43+
chainConfig.getHubAddress()
44+
);
5445
}
5546

5647
public static boolean isSignature(String hexString) {
@@ -68,9 +59,10 @@ public static boolean isAddress(String hexString) {
6859
}
6960

7061
public TransactionReceipt initializeTask(String chainDealId,
71-
int taskIndex) throws Exception {
72-
return getContract()
73-
.initialize(stringToBytes(chainDealId),
62+
int taskIndex) throws Exception {
63+
return iexecHubContract
64+
.initialize(
65+
stringToBytes(chainDealId),
7466
BigInteger.valueOf(taskIndex))
7567
.send();
7668
}
@@ -82,25 +74,29 @@ public TransactionReceipt contribute(String chainTaskId,
8274
String enclaveSignature) throws Exception {
8375
String resultHash = ResultUtils.computeResultHash(chainTaskId, resultDigest);
8476
String resultSeal =
85-
ResultUtils.computeResultSeal(credentialsService.getCredentials().getAddress(),
77+
ResultUtils.computeResultSeal(credentials.getAddress(),
8678
chainTaskId,
8779
resultDigest);
8880

89-
return getContract()
90-
.contribute(stringToBytes(chainTaskId),
81+
return iexecHubContract
82+
.contribute(
83+
stringToBytes(chainTaskId),
9184
stringToBytes(resultHash),
9285
stringToBytes(resultSeal),
9386
enclaveChallenge,
9487
stringToBytes(enclaveSignature),
95-
stringToBytes(workerpoolSignature)).send();
88+
stringToBytes(workerpoolSignature))
89+
.send();
9690
}
9791

9892

9993
public TransactionReceipt reveal(String chainTaskId,
10094
String resultDigest) throws Exception {
101-
return getContract()
102-
.reveal(stringToBytes(chainTaskId),
103-
stringToBytes(resultDigest)).send();
95+
return iexecHubContract
96+
.reveal(
97+
stringToBytes(chainTaskId),
98+
stringToBytes(resultDigest))
99+
.send();
104100
}
105101

106102
public TransactionReceipt finalizeTask(String chainTaskId,
@@ -111,22 +107,16 @@ public TransactionReceipt finalizeTask(String chainTaskId,
111107
byte[] resultsCallback = StringUtils.isNotEmpty(callbackData) ?
112108
stringToBytes(callbackData) : new byte[0];
113109

114-
return getContract()
115-
.finalize(stringToBytes(chainTaskId),
110+
return iexecHubContract
111+
.finalize(
112+
stringToBytes(chainTaskId),
116113
results,
117-
resultsCallback).send();
118-
}
119-
120-
private IexecHubContract getContract() {
121-
return getHubContract(web3jService.getWritingContractGasProvider(),
122-
chainId,
123-
blockTime * ONE_SECOND,
124-
50);
114+
resultsCallback)
115+
.send();
125116
}
126117

127-
128118
public boolean hasEnoughGas() {
129-
return hasEnoughGas(credentialsService.getCredentials().getAddress());
119+
return hasEnoughGas(credentials.getAddress());
130120
}
131121

132122
/**

src/main/java/com/iexec/blockchain/tool/Web3jService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
import com.iexec.commons.poco.chain.Web3jAbstractService;
2020
import org.springframework.stereotype.Service;
2121

22+
import java.time.Duration;
23+
2224
@Service
2325
public class Web3jService extends Web3jAbstractService {
2426

2527
public Web3jService(ChainConfig chainConfig) {
2628
super(
29+
chainConfig.getChainId(),
2730
chainConfig.getNodeAddress(),
31+
Duration.ofSeconds(chainConfig.getBlockTime()),
2832
chainConfig.getGasPriceMultiplier(),
2933
chainConfig.getGasPriceCap(),
3034
chainConfig.isSidechain()
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2023-2023 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.blockchain.tool;
18+
19+
import com.iexec.commons.poco.contract.generated.IexecHubContract;
20+
import lombok.SneakyThrows;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.mockito.Mock;
24+
import org.mockito.MockitoAnnotations;
25+
import org.springframework.test.util.ReflectionTestUtils;
26+
import org.web3j.crypto.Credentials;
27+
import org.web3j.crypto.ECKeyPair;
28+
import org.web3j.crypto.Keys;
29+
import org.web3j.protocol.core.RemoteFunctionCall;
30+
import org.web3j.protocol.core.methods.response.TransactionReceipt;
31+
32+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
33+
import static org.mockito.ArgumentMatchers.any;
34+
import static org.mockito.Mockito.when;
35+
36+
class IexecHubServiceTests {
37+
private final ChainConfig chainConfig = ChainConfig
38+
.builder()
39+
.hubAddress("0xC129e7917b7c7DeDfAa5Fff1FB18d5D7050fE8ca")
40+
.build();
41+
@Mock
42+
private CredentialsService credentialsService;
43+
@Mock
44+
private IexecHubContract iexecHubContract;
45+
@Mock
46+
private Web3jService web3jService;
47+
@Mock
48+
private RemoteFunctionCall<TransactionReceipt> remoteFunctionCall;
49+
private IexecHubService iexecHubService;
50+
51+
@BeforeEach
52+
void init() {
53+
MockitoAnnotations.openMocks(this);
54+
Credentials credentials = createEthereumCredentials();
55+
when(credentialsService.getCredentials()).thenReturn(credentials);
56+
iexecHubService = new IexecHubService(credentialsService, web3jService, chainConfig);
57+
ReflectionTestUtils.setField(iexecHubService, "iexecHubContract", iexecHubContract);
58+
}
59+
60+
@SneakyThrows
61+
private Credentials createEthereumCredentials() {
62+
ECKeyPair ecKeyPair = Keys.createEcKeyPair();
63+
return Credentials.create(ecKeyPair);
64+
}
65+
66+
@Test
67+
void shouldNotInitializeTask() throws Exception {
68+
when(iexecHubContract.initialize(any(), any())).thenReturn(remoteFunctionCall);
69+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
70+
assertThatThrownBy(() -> iexecHubService.initializeTask("chainTaskId", 0))
71+
.isInstanceOf(Exception.class);
72+
}
73+
74+
@Test
75+
void shouldNotContribute() throws Exception {
76+
when(iexecHubContract.contribute(any(), any(), any(), any(), any(), any())).thenReturn(remoteFunctionCall);
77+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
78+
assertThatThrownBy(() -> iexecHubService.contribute("chainTaskId", "resultDigest",
79+
"workerpoolSignature", "enclaveChallenge", "enclaveSignature"))
80+
.isInstanceOf(Exception.class);
81+
}
82+
83+
@Test
84+
void shouldNotReveal() throws Exception {
85+
when(iexecHubContract.reveal(any(), any())).thenReturn(remoteFunctionCall);
86+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
87+
assertThatThrownBy(() -> iexecHubService.reveal("chainTaskId", "resultDigest"))
88+
.isInstanceOf(Exception.class);
89+
}
90+
91+
@Test
92+
void shouldNotFinalizeTask() throws Exception {
93+
when(iexecHubContract.finalize(any(), any(), any())).thenReturn(remoteFunctionCall);
94+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
95+
assertThatThrownBy(() -> iexecHubService.finalizeTask("chainTaskId", "resultLink", "callbackData"))
96+
.isInstanceOf(Exception.class);
97+
}
98+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023-2023 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.blockchain.tool;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
class Web3jServiceTests {
24+
private final ChainConfig chainConfig = ChainConfig
25+
.builder()
26+
.chainId(134)
27+
.isSidechain(true)
28+
.nodeAddress("https://bellecour.iex.ec")
29+
.hubAddress("0x3eca1B216A7DF1C7689aEb259fFB83ADFB894E7f")
30+
.blockTime(5)
31+
.gasPriceMultiplier(1.0f)
32+
.gasPriceCap(22_000_000_000L)
33+
.build();
34+
35+
@Test
36+
void shouldCreateInstance() {
37+
assertThat(new Web3jService(chainConfig)).isNotNull();
38+
}
39+
}

0 commit comments

Comments
 (0)