Skip to content

Commit d2dfb3f

Browse files
Updated how transaction result is represented [ECR-3693] (#1174)
Use the protobuf message to represent the transaction execution result. Update the exonum revision and the proto messages.
1 parent 32116ee commit d2dfb3f

File tree

39 files changed

+370
-612
lines changed

39 files changed

+370
-612
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2019 The Exonum Team
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.exonum.binding.common.blockchain;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
21+
import com.exonum.core.messages.Runtime.ErrorKind;
22+
import com.exonum.core.messages.Runtime.ExecutionError;
23+
import com.exonum.core.messages.Runtime.ExecutionStatus;
24+
import com.google.protobuf.Empty;
25+
26+
/**
27+
* Provides factory methods for creating some execution statuses, which represent a result
28+
* of the runtime operation execution (most often — service transaction execution).
29+
*
30+
* <p>Only the factory methods for the most common statuses are provided;
31+
* consider using the {@linkplain ExecutionStatus#newBuilder()} for other error kinds.
32+
*
33+
* @see ExecutionStatus
34+
*/
35+
public final class ExecutionStatuses {
36+
37+
private static final ExecutionStatus SUCCESS = ExecutionStatus.newBuilder()
38+
.setOk(Empty.getDefaultInstance())
39+
.build();
40+
41+
/**
42+
* Creates a successful execution status.
43+
*/
44+
public static ExecutionStatus success() {
45+
return SUCCESS;
46+
}
47+
48+
/**
49+
* Creates an execution status corresponding to a service-defined error with an empty description.
50+
* The status will have the <em>kind</em> field equal to {@link ErrorKind#SERVICE}.
51+
*
52+
* @param code a service-defined error code; must be non-negative
53+
*/
54+
public static ExecutionStatus serviceError(int code) {
55+
return serviceError(code, "");
56+
}
57+
58+
/**
59+
* Creates an execution status corresponding to a service-defined error.
60+
* The status will have the <em>kind</em> field equal to {@link ErrorKind#SERVICE}.
61+
*
62+
* @param code a service-defined error code; must be non-negative
63+
* @param description an error description; may be empty
64+
*/
65+
public static ExecutionStatus serviceError(int code, String description) {
66+
checkArgument(0 <= code, "Error code (%s) must be non-negative", code);
67+
return ExecutionStatus.newBuilder()
68+
.setError(ExecutionError.newBuilder()
69+
.setKind(ErrorKind.SERVICE)
70+
.setCode(code)
71+
.setDescription(description))
72+
.build();
73+
}
74+
75+
private ExecutionStatuses() {}
76+
}

exonum-java-binding/common/src/main/java/com/exonum/binding/common/blockchain/TransactionResult.java

Lines changed: 0 additions & 141 deletions
This file was deleted.

exonum-java-binding/common/src/main/java/com/exonum/binding/common/message/ParsedTransactionMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
import com.exonum.binding.common.crypto.PublicKey;
2222
import com.exonum.binding.common.hash.HashCode;
23-
import com.exonum.binding.messages.Consensus;
24-
import com.exonum.binding.messages.Consensus.ExonumMessage;
25-
import com.exonum.binding.messages.Runtime.AnyTx;
23+
import com.exonum.core.messages.Consensus;
24+
import com.exonum.core.messages.Consensus.ExonumMessage;
25+
import com.exonum.core.messages.Runtime.AnyTx;
2626
import com.google.common.base.MoreObjects;
2727
import com.google.protobuf.ByteString;
2828
import com.google.protobuf.InvalidProtocolBufferException;

exonum-java-binding/common/src/main/java/com/exonum/binding/common/message/SignedMessage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import com.exonum.binding.common.crypto.PublicKey;
2020
import com.exonum.binding.common.hash.HashCode;
2121
import com.exonum.binding.common.hash.Hashing;
22-
import com.exonum.binding.messages.Consensus;
23-
import com.exonum.binding.messages.Consensus.ExonumMessage;
22+
import com.exonum.core.messages.Consensus;
23+
import com.exonum.core.messages.Consensus.ExonumMessage;
2424
import com.google.protobuf.ByteString;
2525
import com.google.protobuf.InvalidProtocolBufferException;
2626

exonum-java-binding/common/src/main/java/com/exonum/binding/common/message/TransactionMessage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import com.exonum.binding.common.crypto.KeyPair;
2626
import com.exonum.binding.common.crypto.PublicKey;
2727
import com.exonum.binding.common.hash.HashCode;
28-
import com.exonum.binding.messages.Consensus;
29-
import com.exonum.binding.messages.Consensus.ExonumMessage;
30-
import com.exonum.binding.messages.Runtime.AnyTx;
31-
import com.exonum.binding.messages.Runtime.CallInfo;
32-
import com.exonum.binding.messages.Types;
28+
import com.exonum.core.messages.Consensus;
29+
import com.exonum.core.messages.Consensus.ExonumMessage;
30+
import com.exonum.core.messages.Runtime.AnyTx;
31+
import com.exonum.core.messages.Runtime.CallInfo;
32+
import com.exonum.core.messages.Types;
3333
import com.google.protobuf.ByteString;
3434
import com.google.protobuf.InvalidProtocolBufferException;
3535
import com.google.protobuf.MessageLite;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2019 The Exonum Team
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.exonum.binding.common.blockchain;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
import static org.junit.jupiter.params.provider.Arguments.arguments;
23+
24+
import com.exonum.core.messages.Runtime.ErrorKind;
25+
import com.exonum.core.messages.Runtime.ExecutionError;
26+
import com.exonum.core.messages.Runtime.ExecutionStatus;
27+
import com.google.common.collect.ImmutableList;
28+
import java.util.List;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.Arguments;
32+
import org.junit.jupiter.params.provider.MethodSource;
33+
import org.junit.jupiter.params.provider.ValueSource;
34+
35+
class ExecutionStatusesTest {
36+
37+
@Test
38+
void success() {
39+
ExecutionStatus s = ExecutionStatuses.success();
40+
assertTrue(s.hasOk());
41+
}
42+
43+
@ParameterizedTest
44+
@MethodSource("errors")
45+
void serviceError(int code, String description) {
46+
ExecutionStatus s = ExecutionStatuses.serviceError(code, description);
47+
48+
assertTrue(s.hasError());
49+
ExecutionError error = s.getError();
50+
assertThat(error.getKind()).isEqualTo(ErrorKind.SERVICE);
51+
assertThat(error.getCode()).isEqualTo(code);
52+
assertThat(error.getDescription()).isEqualTo(description);
53+
}
54+
55+
private static List<Arguments> errors() {
56+
return ImmutableList.of(
57+
arguments(0, "Min error code"),
58+
arguments(1, ""),
59+
arguments(Integer.MAX_VALUE, "Max error code")
60+
);
61+
}
62+
63+
@ParameterizedTest
64+
@ValueSource(ints = {-1, -2, Integer.MIN_VALUE})
65+
void serviceErrorRejectsNegative(int code) {
66+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
67+
() -> ExecutionStatuses.serviceError(code, ""));
68+
69+
assertThat(e).hasMessageContaining(String.valueOf(code));
70+
}
71+
}

0 commit comments

Comments
 (0)