Skip to content

Commit

Permalink
Merge branch '11616-fix-custom-fee-translator' into mod-run-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tinker-michaelj committed Feb 20, 2024
2 parents d4d841e + b5bd25c commit 6e38f99
Showing 1 changed file with 24 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1326,12 +1323,29 @@ public static <T extends Record, R extends GeneratedMessageV3> R pbjToProto(
}
}

private interface ProtoParser<R extends GeneratedMessageV3> {
R parseFrom(byte[] bytes) throws InvalidProtocolBufferException;
}

private static <T extends Record, R extends GeneratedMessageV3> R explicitPbjToProto(
@NonNull final T pbj, @NonNull final Codec<T> pbjCodec, @NonNull final ProtoParser<R> 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 <T extends GeneratedMessageV3, R extends Record> @NonNull R protoToPbj(
@NonNull final T proto, @NonNull final Class<R> 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<R>) codecField.get(null);
return codec.parse(BufferedData.wrap(bytes));
} catch (NoSuchFieldException | IllegalAccessException | ParseException e) {
Expand All @@ -1350,7 +1364,7 @@ public static <T extends Record, R extends GeneratedMessageV3> 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 6e38f99

Please sign in to comment.