From b5bd25c27dd007f6304ddcb8439b5edc39dd4ecb Mon Sep 17 00:00:00 2001 From: Michael Tinker Date: Tue, 20 Feb 2024 10:37:47 -0600 Subject: [PATCH] Use safest fromPbj translation approach for PBJ CustomFee objects Signed-off-by: Michael Tinker --- .../app/service/mono/pbj/PbjConverter.java | 75 ++++++------------- 1 file changed, 24 insertions(+), 51 deletions(-) diff --git a/hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/pbj/PbjConverter.java b/hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/pbj/PbjConverter.java index e8d599bd3630..5394e68dcb34 100644 --- a/hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/pbj/PbjConverter.java +++ b/hedera-node/hedera-mono-service/src/main/java/com/hedera/node/app/service/mono/pbj/PbjConverter.java @@ -50,10 +50,7 @@ import com.hedera.hapi.node.state.throttles.ThrottleUsageSnapshot; import com.hedera.hapi.node.transaction.CustomFee; import com.hedera.hapi.node.transaction.ExchangeRate; -import com.hedera.hapi.node.transaction.FixedFee; -import com.hedera.hapi.node.transaction.FractionalFee; import com.hedera.hapi.node.transaction.Query; -import com.hedera.hapi.node.transaction.RoyaltyFee; import com.hedera.hapi.node.transaction.TransactionBody; import com.hedera.hapi.node.transaction.TransactionRecord; import com.hedera.node.app.hapi.utils.throttles.DeterministicThrottle; @@ -802,7 +799,7 @@ public static SemanticVersion toPbj(@NonNull com.hederahashgraph.api.proto.java. } public static @NonNull Transaction toPbj(final @NonNull com.hederahashgraph.api.proto.java.Transaction t) { - return protoToPbj(Objects.requireNonNull(t), Transaction.class); + return protoToPbj(requireNonNull(t), Transaction.class); } public static Timestamp toPbj(@NonNull com.hederahashgraph.api.proto.java.Timestamp t) { @@ -1326,12 +1323,29 @@ public static R pbjToProto( } } + private interface ProtoParser { + R parseFrom(byte[] bytes) throws InvalidProtocolBufferException; + } + + private static R explicitPbjToProto( + @NonNull final T pbj, @NonNull final Codec pbjCodec, @NonNull final ProtoParser protoParser) { + requireNonNull(pbj); + requireNonNull(pbjCodec); + requireNonNull(protoParser); + try { + return protoParser.parseFrom(asBytes(pbjCodec, pbj)); + } catch (InvalidProtocolBufferException e) { + // Should be impossible + throw new IllegalStateException("Serialization failure for " + pbj, e); + } + } + @SuppressWarnings("unchecked") public static @NonNull R protoToPbj( @NonNull final T proto, @NonNull final Class pbjClass) { try { - final var bytes = Objects.requireNonNull(proto).toByteArray(); - final var codecField = Objects.requireNonNull(pbjClass).getDeclaredField("PROTOBUF"); + final var bytes = requireNonNull(proto).toByteArray(); + final var codecField = requireNonNull(pbjClass).getDeclaredField("PROTOBUF"); final var codec = (Codec) codecField.get(null); return codec.parse(BufferedData.wrap(bytes)); } catch (NoSuchFieldException | IllegalAccessException | ParseException e) { @@ -1350,7 +1364,7 @@ public static R pbjToProto( */ public static @NonNull Key fromGrpcKey(@NonNull final com.hederahashgraph.api.proto.java.Key grpcKey) { try (final var bais = - new ByteArrayInputStream(Objects.requireNonNull(grpcKey).toByteArray())) { + new ByteArrayInputStream(requireNonNull(grpcKey).toByteArray())) { final var stream = new ReadableStreamingData(bais); stream.limit(bais.available()); return Key.PROTOBUF.parse(stream); @@ -1451,7 +1465,7 @@ public static Key asPbjKey(@NonNull final JKey jKey) { public static @NonNull CustomFee fromFcCustomFee(@Nullable final FcCustomFee fcFee) { try (final var bais = - new ByteArrayInputStream(Objects.requireNonNull(fcFee).asGrpc().toByteArray())) { + new ByteArrayInputStream(requireNonNull(fcFee).asGrpc().toByteArray())) { final var stream = new ReadableStreamingData(bais); stream.limit(bais.available()); return CustomFee.PROTOBUF.parse(stream); @@ -1495,27 +1509,8 @@ public static com.hederahashgraph.api.proto.java.FileID fromPbj(final FileID som @NonNull public static com.hederahashgraph.api.proto.java.CustomFee fromPbj(@NonNull final CustomFee customFee) { - var builder = com.hederahashgraph.api.proto.java.CustomFee.newBuilder(); - if (customFee.hasFixedFee()) { - builder.setFixedFee(fromPbj(customFee.fixedFee())); - } else if (customFee.hasFractionalFee()) { - builder.setFractionalFee(fromPbj(customFee.fractionalFee())); - } else if (customFee.hasRoyaltyFee()) { - builder.setRoyaltyFee(fromPbj(customFee.royaltyFee())); - } - - builder.setFeeCollectorAccountId(fromPbj(customFee.feeCollectorAccountId())); - builder.setAllCollectorsAreExempt(customFee.allCollectorsAreExempt()); - - return builder.build(); - } - - @NonNull - public static com.hederahashgraph.api.proto.java.RoyaltyFee fromPbj(@NonNull final RoyaltyFee royaltyFee) { - var builder = com.hederahashgraph.api.proto.java.RoyaltyFee.newBuilder(); - builder.setExchangeValueFraction(fromPbj(royaltyFee.exchangeValueFraction())); - if (royaltyFee.hasFallbackFee()) builder.setFallbackFee(fromPbj(royaltyFee.fallbackFee())); - return builder.build(); + return explicitPbjToProto( + customFee, CustomFee.PROTOBUF, com.hederahashgraph.api.proto.java.CustomFee::parseFrom); } @NonNull @@ -1526,28 +1521,6 @@ public static com.hederahashgraph.api.proto.java.Fraction fromPbj(@NonNull final return builder.build(); } - @NonNull - public static com.hederahashgraph.api.proto.java.FractionalFee fromPbj(@NonNull final FractionalFee fractionalFee) { - var builder = com.hederahashgraph.api.proto.java.FractionalFee.newBuilder(); - builder.setFractionalAmount(fromPbj(fractionalFee.fractionalAmount())); - builder.setMinimumAmount(fractionalFee.minimumAmount()); - builder.setMaximumAmount(fractionalFee.maximumAmount()); - builder.setNetOfTransfers(fractionalFee.netOfTransfers()); - return builder.build(); - } - - @NonNull - public static com.hederahashgraph.api.proto.java.FixedFee fromPbj(@Nullable FixedFee fixedFee) { - var builder = com.hederahashgraph.api.proto.java.FixedFee.newBuilder(); - if (fixedFee != null) { - builder.setAmount(fixedFee.amount()); - if (fixedFee.hasDenominatingTokenId()) { - builder.setDenominatingTokenId(fromPbj(fixedFee.denominatingTokenId())); - } - } - return builder.build(); - } - @NonNull public static com.hederahashgraph.api.proto.java.File fromPbj(@Nullable File file) { var builder = com.hederahashgraph.api.proto.java.File.newBuilder();