From 8b8654f33daa142f18d85c2c9fcb9c847bb8fe0a Mon Sep 17 00:00:00 2001 From: Dmitry Timofeev Date: Thu, 23 May 2019 20:57:28 +0300 Subject: [PATCH 1/3] Fix default Transaction#info [ECR-3193]: Empty string caused panic in the native code, and, subsequently, a lost request. Made #info return an empty object, though, one might argue it will be more difficult to understand why `transactions` returns nothing. That, however, is to be addressed *after* Dynamic Services. --- .../serialization/json/JsonSerializer.java | 8 ++-- .../binding/transaction/Transaction.java | 18 +++++++- .../binding/transaction/TransactionTest.java | 42 +++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 exonum-java-binding/core/src/test/java/com/exonum/binding/transaction/TransactionTest.java diff --git a/exonum-java-binding/common/src/main/java/com/exonum/binding/common/serialization/json/JsonSerializer.java b/exonum-java-binding/common/src/main/java/com/exonum/binding/common/serialization/json/JsonSerializer.java index 32fcab6962..6660943002 100644 --- a/exonum-java-binding/common/src/main/java/com/exonum/binding/common/serialization/json/JsonSerializer.java +++ b/exonum-java-binding/common/src/main/java/com/exonum/binding/common/serialization/json/JsonSerializer.java @@ -28,7 +28,9 @@ /** * Provides {@link Gson} serializer for converting Java objects to Json and vice versa. * It is configured to serialize Exonum objects in a format, compatible with the core framework - * and light clients (e.g., {@link HashCode} as a hex string). + * and light clients (e.g., {@link HashCode} as a hex string). If needed, a new serializer + * with adapters for service-specific types can be {@linkplain #builder() created}, with + * Exonum types support already included. */ public final class JsonSerializer { @@ -50,8 +52,8 @@ public static GsonBuilder builder() { } /** - * Returns preconfigured {@link Gson} instance. Helpful in cases when no additional configuration - * of the Json serializer is required. + * Returns preconfigured {@link Gson} instance. Helpful in cases when no additional + * {@linkplain #builder() configuration} of the Json serializer is required. */ public static Gson json() { return INSTANCE; diff --git a/exonum-java-binding/core/src/main/java/com/exonum/binding/transaction/Transaction.java b/exonum-java-binding/core/src/main/java/com/exonum/binding/transaction/Transaction.java index 112a2cec49..c0b6bdefa7 100644 --- a/exonum-java-binding/core/src/main/java/com/exonum/binding/transaction/Transaction.java +++ b/exonum-java-binding/core/src/main/java/com/exonum/binding/transaction/Transaction.java @@ -41,10 +41,24 @@ public interface Transaction { void execute(TransactionContext context) throws TransactionExecutionException; /** - * Returns some information about this transaction in JSON format. + * Returns the information about this transaction in JSON format. + * For example, it is included in the blockchain explorer response to + * a + * transaction request. + * + *

By default, no information is provided. If needed, it can be easily implemented + * with {@linkplain com.exonum.binding.common.serialization.json.JsonSerializer Gson}: + * + *


+   *   @Override
+   *   public String info() {
+   *     return JsonSerializer.json().toJson(this);
+   *   }
+   * 
*/ default String info() { - return ""; + // Empty object by default + return "{}"; } } diff --git a/exonum-java-binding/core/src/test/java/com/exonum/binding/transaction/TransactionTest.java b/exonum-java-binding/core/src/test/java/com/exonum/binding/transaction/TransactionTest.java new file mode 100644 index 0000000000..f46f4e052b --- /dev/null +++ b/exonum-java-binding/core/src/test/java/com/exonum/binding/transaction/TransactionTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2019 The Exonum Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exonum.binding.transaction; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import java.util.Map; +import org.junit.jupiter.api.Test; + +class TransactionTest { + + @Test + void infoIsEmptyByDefault() { + Transaction tx = context -> { + // no-op + }; + + String info = tx.info(); + + // Check it is correctly deserialized to an empty object + Gson gson = new Gson(); + Map deserialized = gson.fromJson(info, new TypeToken>() { + }.getType()); + assertThat(deserialized).isEmpty(); + } +} From dceeb6d6155455bad16bb711a0e7c3b97c58e503 Mon Sep 17 00:00:00 2001 From: Dmitry Timofeev Date: Fri, 24 May 2019 10:37:57 +0300 Subject: [PATCH 2/3] Add changelog entry --- exonum-java-binding/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/exonum-java-binding/CHANGELOG.md b/exonum-java-binding/CHANGELOG.md index c5b1de9fbf..6a5b9bad51 100644 --- a/exonum-java-binding/CHANGELOG.md +++ b/exonum-java-binding/CHANGELOG.md @@ -28,6 +28,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. format instead of the whole message in binary form. - RocksDB library is no longer required to be installed on Mac or Linux to run the Exonum Java application. (#902) + +### Fixed +- The default [`Transaction#info`][tx-info-07] implementation causing an error on `transaction` +request. It is modified to return an empty object (no info) by default. (#904) + +[tx-info-07]: https://exonum.com/doc/api/java-binding-core/0.7.0/com/exonum/binding/transaction/Transaction.html#info() ## [0.6.0]- 2019-05-08 From 19b91f895ee8a66272714ab17cd57c68bbf50d08 Mon Sep 17 00:00:00 2001 From: Dmitry Timofeev Date: Fri, 24 May 2019 10:38:23 +0300 Subject: [PATCH 3/3] space --- exonum-java-binding/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exonum-java-binding/CHANGELOG.md b/exonum-java-binding/CHANGELOG.md index 6a5b9bad51..ba5afad272 100644 --- a/exonum-java-binding/CHANGELOG.md +++ b/exonum-java-binding/CHANGELOG.md @@ -35,7 +35,7 @@ request. It is modified to return an empty object (no info) by default. (#904) [tx-info-07]: https://exonum.com/doc/api/java-binding-core/0.7.0/com/exonum/binding/transaction/Transaction.html#info() -## [0.6.0]- 2019-05-08 +## [0.6.0] - 2019-05-08 **If you are upgrading an existing Java service, consult the [migration guide](https://github.com/exonum/exonum-java-binding/blob/ejb/v0.6.0/exonum-java-binding/doc/Migration_guide_0.6.md).**