Skip to content

Commit bd5b362

Browse files
MakarovSdmitry-timofeev
authored andcommitted
Update LC with DS changes [ECR-3860] (#1244)
1 parent a8c55dc commit bd5b362

File tree

13 files changed

+159
-174
lines changed

13 files changed

+159
-174
lines changed

exonum-light-client/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515

1616
## Unreleased
1717

18+
### Changed
19+
- `TransactionResponse#getExecutionResult` now returns `ExecutionStatus`
20+
instead of `TransactionResult`. (#1244)
21+
1822
## 0.4.0 — 2019-10-09
1923

2024
The new release of the light client brings support for Exonum 0.12
@@ -28,7 +32,7 @@ and Exonum Java 0.8.
2832
- `ExonumClient#getBlockByHeight` and `#getBlocks` to throw
2933
`IllegalArgumentException` when blocks with heights exceeding
3034
the current blockchain height are requested (#1137)
31-
- `Block` JSON representation to be compatible with the one used
35+
- `Block` JSON representation to be compatible with the one used
3236
for blocks by the core. Applied `@SerializedName` annotation
3337
to all fields. (#1137)
3438
- Updated project dependencies to the newest versions.

exonum-light-client/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ The following example shows how to create the transaction message.
8282
In addition please read about [transaction message structure][exonum-tx-message-builder].
8383
```java
8484
TransactionMessage txMessage = TransactionMessage.builder()
85-
.serviceId((short) 1)
86-
.transactionId((short) 2)
85+
.serviceId(1)
86+
.transactionId(2)
8787
.payload(data)
88-
.sign(keys, CryptoFunctions.ed25519());
88+
.sign(keys);
8989
```
9090
* `data` is a bytes array which contains transactional information/parameters
9191
in a service-defined format.

exonum-light-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
<properties>
6363
<!-- Project configuration -->
64-
<ejb.version>0.8.0</ejb.version>
64+
<ejb.version>0.9.0-SNAPSHOT</ejb.version>
6565
<java.compiler.source>8</java.compiler.source>
6666
<java.compiler.target>8</java.compiler.target>
6767
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

exonum-light-client/src/main/java/com/exonum/client/ExonumClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
/**
3838
* Main interface for Exonum Light client.
3939
* Provides a convenient way for interaction with Exonum framework APIs.
40-
* All the methods of the interface work in a blocking way
41-
* i.e. invoke underlying request immediately, and block until the response can be processed
40+
* All the methods of the interface work in a blocking way,
41+
* i.e., invoke underlying request immediately, and block until the response can be processed
4242
* or an error occurs. In case the thread is interrupted, the blocked methods will complete
4343
* exceptionally.
4444
*

exonum-light-client/src/main/java/com/exonum/client/ExplorerApiHelper.java

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818
package com.exonum.client;
1919

2020
import static com.exonum.client.ExonumApi.JSON;
21+
import static com.google.common.base.Preconditions.checkArgument;
2122
import static java.util.stream.Collectors.toList;
2223

24+
import com.exonum.binding.common.blockchain.ExecutionStatuses;
2325
import com.exonum.binding.common.blockchain.TransactionLocation;
24-
import com.exonum.binding.common.blockchain.TransactionResult;
2526
import com.exonum.binding.common.hash.HashCode;
2627
import com.exonum.binding.common.message.TransactionMessage;
2728
import com.exonum.client.response.Block;
2829
import com.exonum.client.response.BlockResponse;
2930
import com.exonum.client.response.BlocksResponse;
3031
import com.exonum.client.response.TransactionResponse;
3132
import com.exonum.client.response.TransactionStatus;
33+
import com.exonum.core.messages.Runtime.ErrorKind;
34+
import com.exonum.core.messages.Runtime.ExecutionError;
35+
import com.exonum.core.messages.Runtime.ExecutionStatus;
3236
import com.google.common.annotations.VisibleForTesting;
3337
import com.google.common.collect.ImmutableList;
3438
import com.google.gson.JsonArray;
@@ -58,11 +62,11 @@ static HashCode parseSubmitTxResponse(String json) {
5862

5963
static TransactionResponse parseGetTxResponse(String json) {
6064
GetTxResponse response = JSON.fromJson(json, GetTxResponse.class);
61-
TransactionResult executionResult = getTransactionResult(response.getStatus());
65+
ExecutionStatus executionResult = getExecutionStatus(response.getStatus());
6266

6367
return new TransactionResponse(
6468
response.getType(),
65-
response.getContent().getMessage(),
69+
response.getContent(),
6670
executionResult,
6771
response.getLocation()
6872
);
@@ -87,25 +91,48 @@ static BlocksResponse parseGetBlocksResponse(String json) {
8791
);
8892
}
8993

90-
private static TransactionResult getTransactionResult(
94+
private static ExecutionStatus getExecutionStatus(
9195
GetTxResponseExecutionResult executionStatus) {
9296
if (executionStatus == null) {
9397
return null;
9498
}
9599
switch (executionStatus.getType()) {
96100
case SUCCESS:
97-
return TransactionResult.successful();
98-
case ERROR:
99-
return TransactionResult.error(executionStatus.getCode(),
100-
executionStatus.getDescription());
101+
return ExecutionStatuses.success();
101102
case PANIC:
102-
return TransactionResult.unexpectedError(executionStatus.getDescription());
103+
return buildPanicExecutionStatus(executionStatus.getDescription());
104+
case DISPATCHER_ERROR:
105+
return buildExecutionStatus(ErrorKind.DISPATCHER, executionStatus.getCode(),
106+
executionStatus.getDescription());
107+
case RUNTIME_ERROR:
108+
return buildExecutionStatus(ErrorKind.RUNTIME, executionStatus.getCode(),
109+
executionStatus.getDescription());
110+
case SERVICE_ERROR:
111+
return ExecutionStatuses.serviceError(executionStatus.getCode(),
112+
executionStatus.getDescription());
103113
default:
104114
throw new IllegalStateException("Unexpected transaction execution status: "
105115
+ executionStatus.getType());
106116
}
107117
}
108118

119+
@VisibleForTesting
120+
static ExecutionStatus buildPanicExecutionStatus(String description) {
121+
return buildExecutionStatus(ErrorKind.PANIC, 0, description);
122+
}
123+
124+
@VisibleForTesting
125+
static ExecutionStatus buildExecutionStatus(ErrorKind errorKind, int code,
126+
String description) {
127+
checkArgument(0 <= code, "Error code (%s) must be non-negative", code);
128+
return ExecutionStatus.newBuilder()
129+
.setError(ExecutionError.newBuilder()
130+
.setKind(errorKind)
131+
.setCode(code)
132+
.setDescription(description))
133+
.build();
134+
}
135+
109136
/**
110137
* Json object wrapper for submit transaction request.
111138
*/
@@ -130,25 +157,14 @@ private static class GetTxResponse {
130157
@NonNull
131158
TransactionStatus type;
132159
@NonNull
133-
GetTxResponseContent content;
160+
TransactionMessage content;
134161
TransactionLocation location;
135162
JsonObject locationProof; //TODO: in scope of LC P3
136163
GetTxResponseExecutionResult status;
137164
}
138165

139166
/**
140-
* Json object wrapper for get transaction response content i.e.
141-
* {@code "$.content"}.
142-
*/
143-
@Value
144-
private static class GetTxResponseContent {
145-
JsonObject debug; // contains executable tx in json. currently not supported
146-
@NonNull
147-
TransactionMessage message;
148-
}
149-
150-
/**
151-
* Json object wrapper for transaction execution result i.e.
167+
* Json object wrapper for transaction execution result, i.e.,
152168
* {@code "$.status"}.
153169
*/
154170
@Value
@@ -159,16 +175,20 @@ private static class GetTxResponseExecutionResult {
159175
}
160176

161177
/**
162-
* Json object wrapper for transaction execution status i.e.
178+
* Json object wrapper for transaction execution status, i.e.,
163179
* {@code "$.status.type"}.
164180
*/
165181
private enum GetTxResponseExecutionStatus {
166182
@SerializedName("success")
167183
SUCCESS,
168-
@SerializedName("error")
169-
ERROR,
170184
@SerializedName("panic")
171-
PANIC
185+
PANIC,
186+
@SerializedName("dispatcher_error")
187+
DISPATCHER_ERROR,
188+
@SerializedName("runtime_error")
189+
RUNTIME_ERROR,
190+
@SerializedName("service_error")
191+
SERVICE_ERROR
172192
}
173193

174194
@Value

exonum-light-client/src/main/java/com/exonum/client/response/ConsensusStatus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
*/
2323
public enum ConsensusStatus {
2424
/**
25-
* Shows that consensus is active
26-
* i.e. it is enabled and the node has enough connected peers.
25+
* Shows that consensus is active,
26+
* i.e., it is enabled and the node has enough connected peers.
2727
*/
2828
ACTIVE,
2929
/**

exonum-light-client/src/main/java/com/exonum/client/response/TransactionStatus.java

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

1717
package com.exonum.client.response;
1818

19-
import com.exonum.binding.common.blockchain.TransactionResult;
19+
import com.exonum.core.messages.Runtime.ExecutionStatus;
2020
import com.google.gson.annotations.SerializedName;
2121

2222
/**
@@ -31,7 +31,7 @@ public enum TransactionStatus {
3131
/**
3232
* Shows that transaction is committed to the blockchain.
3333
* Please note that a committed transaction has not necessarily completed
34-
* successfully — use the {@linkplain TransactionResult execution result}
34+
* successfully — use the {@linkplain ExecutionStatus execution result}
3535
* to check that.
3636
*/
3737
@SerializedName("committed")

exonum-light-client/src/main/lombok/com/exonum/client/response/Block.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public Optional<ZonedDateTime> getCommitTime() {
100100
return Optional.ofNullable(commitTime);
101101
}
102102

103-
104103
/**
105104
* Returns true if this block is empty:
106105
* contains no {@linkplain #getNumTransactions() transactions}.

exonum-light-client/src/main/lombok/com/exonum/client/response/TransactionResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import static com.google.common.base.Preconditions.checkState;
2121

2222
import com.exonum.binding.common.blockchain.TransactionLocation;
23-
import com.exonum.binding.common.blockchain.TransactionResult;
2423
import com.exonum.binding.common.message.TransactionMessage;
24+
import com.exonum.core.messages.Runtime.ExecutionStatus;
2525
import com.google.common.base.Objects;
2626
import lombok.Value;
2727

@@ -39,7 +39,7 @@ public class TransactionResponse {
3939
* Transaction execution result.
4040
* Not available unless the transaction is {@linkplain #isCommitted committed} to the blockchain.
4141
*/
42-
TransactionResult executionResult;
42+
ExecutionStatus executionResult;
4343
/**
4444
* Transaction location in the blockchain.
4545
* Not available unless the transaction is {@linkplain #isCommitted committed} to the blockchain.
@@ -50,7 +50,7 @@ public class TransactionResponse {
5050
* Returns transaction execution result.
5151
* @throws IllegalStateException if the transaction is not committed yet
5252
*/
53-
public TransactionResult getExecutionResult() {
53+
public ExecutionStatus getExecutionResult() {
5454
checkState(status == COMMITTED,
5555
"Transaction result is available for committed transactions only");
5656
return executionResult;

exonum-light-client/src/test/java/com/exonum/client/ExonumHttpClientIntegrationTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ void submitTransactionTest() throws InterruptedException {
7070
// Create request
7171
KeyPair keys = ed25519().generateKeyPair();
7272
TransactionMessage txMessage = TransactionMessage.builder()
73-
.serviceId((short) 1)
74-
.transactionId((short) 2)
73+
.serviceId(1)
74+
.transactionId(2)
7575
.payload(new byte[]{0x00, 0x01, 0x02})
76-
.sign(keys, ed25519());
76+
.sign(keys);
7777
// Mock response
7878
String hash = "f128c720e04b8243";
7979
String mockResponse = "{\"tx_hash\":\"" + hash + "\"}";
@@ -160,16 +160,7 @@ void getTransaction() throws InterruptedException {
160160
TransactionMessage expectedMessage = createTransactionMessage();
161161
String mockResponse = "{\n"
162162
+ " 'type': 'in-pool',\n"
163-
+ " 'content': {\n"
164-
+ " 'debug': {\n"
165-
+ " 'to': {\n"
166-
+ " 'data': []\n"
167-
+ " },\n"
168-
+ " 'amount': 10,\n"
169-
+ " 'seed': 9587307158524814255\n"
170-
+ " },\n"
171-
+ " 'message': '" + toHex(expectedMessage) + "'\n"
172-
+ " }\n"
163+
+ " 'content': '" + toHex(expectedMessage) + "'\n"
173164
+ "}";
174165
server.enqueue(new MockResponse().setBody(mockResponse));
175166

0 commit comments

Comments
 (0)