Skip to content

Commit

Permalink
fix: refactored static method to a more general toPbj()
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Aug 23, 2024
1 parent 262347b commit ba5be60
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.hedera.block.server.Constants.SERVER_STREAMING_METHOD_NAME;
import static com.hedera.block.server.Constants.SERVICE_NAME;
import static com.hedera.block.server.Constants.SINGLE_BLOCK_METHOD_NAME;
import static com.hedera.block.server.Translator.toPbj;
import static com.hedera.block.server.Translator.toProtocSingleBlockResponse;
import static com.hedera.block.server.Translator.toProtocSubscribeStreamResponse;
import static java.lang.System.Logger;
Expand All @@ -44,7 +45,6 @@
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.grpc.stub.StreamObserver;
import io.helidon.webserver.grpc.GrpcService;
Expand Down Expand Up @@ -175,7 +175,7 @@ void protocSingleBlock(

try {
final SingleBlockRequest pbjSingleBlockRequest =
toPbjSingleBlockRequest(singleBlockRequest);
toPbj(SingleBlockRequest.PROTOBUF, singleBlockRequest.toByteArray());
singleBlock(pbjSingleBlockRequest, singleBlockResponseStreamObserver);
} catch (ParseException e) {
LOGGER.log(ERROR, "Error parsing protoc SingleBlockRequest: {0}", singleBlockRequest);
Expand Down Expand Up @@ -255,12 +255,4 @@ static com.hedera.hapi.block.protoc.SingleBlockResponse buildSingleBlockNotFound

return toProtocSingleBlockResponse(response);
}

@NonNull
private static com.hedera.hapi.block.SingleBlockRequest toPbjSingleBlockRequest(
@NonNull final com.hedera.hapi.block.protoc.SingleBlockRequest singleBlockRequest)
throws ParseException {

return SingleBlockRequest.PROTOBUF.parse(Bytes.wrap(singleBlockRequest.toByteArray()));
}
}
24 changes: 12 additions & 12 deletions server/src/main/java/com/hedera/block/server/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.hedera.hapi.block.SubscribeStreamRequest;
import com.hedera.hapi.block.SubscribeStreamResponse;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.Codec;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.pbj.runtime.io.buffer.Bytes;
Expand All @@ -38,8 +37,9 @@
import java.io.IOException;

/**
* TODO: Remove this class once the Helidon PBJ gRPC work is integrated. Translator class to convert
* between PBJ and google protoc objects.
* Translator class to convert between PBJ and google protoc objects.
*
* <p>TODO: Remove this class once the Helidon PBJ gRPC work is integrated.
*/
public final class Translator {
private static final Logger LOGGER = System.getLogger(Translator.class.getName());
Expand Down Expand Up @@ -188,18 +188,18 @@ public static com.hedera.hapi.block.protoc.PublishStreamRequest toProtocPublishS
}

/**
* Converts a {@link com.hedera.hapi.block.stream.protoc.BlockItem} to a {@link
* com.hedera.hapi.block.stream.BlockItem}.
* Converts protoc bytes to a PBJ record of the same type.
*
* @param blockItem the {@link com.hedera.hapi.block.stream.protoc.BlockItem} to convert
* @return the converted {@link com.hedera.hapi.block.stream.BlockItem}
* @throws ParseException if the conversion between the protoc and PBJ objects fails
* @param <T> the type of PBJ record to convert to
* @param codec the record codec to convert the bytes to a PBJ record
* @param bytes the protoc bytes to convert to a PBJ record
* @return the converted PBJ record
* @throws ParseException if the conversion between the protoc bytes and PBJ objects fails
*/
@NonNull
public static BlockItem toPbjBlockItem(
@NonNull final com.hedera.hapi.block.stream.protoc.BlockItem blockItem)
throws ParseException {
return BlockItem.PROTOBUF.parse(Bytes.wrap(blockItem.toByteArray()));
public static <T extends Record> T toPbj(
@NonNull final Codec<T> codec, @NonNull final byte[] bytes) throws ParseException {
return codec.parse(Bytes.wrap(bytes));
}

private static <T extends Record> byte[] asBytes(@NonNull Codec<T> codec, @NonNull T tx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.hedera.block.server.producer;

import static com.hedera.block.server.Translator.toPbjBlockItem;
import static com.hedera.block.server.Translator.toPbj;
import static com.hedera.block.server.Translator.toProtocPublishStreamResponse;
import static com.hedera.block.server.producer.Util.getFakeHash;
import static java.lang.System.Logger;
Expand Down Expand Up @@ -92,7 +92,8 @@ public void onNext(

try {

final BlockItem blockItem = toPbjBlockItem(publishStreamRequest.getBlockItem());
final BlockItem blockItem =
toPbj(BlockItem.PROTOBUF, publishStreamRequest.getBlockItem().toByteArray());

// Publish the block to all the subscribers unless
// there's an issue with the StreamMediator.
Expand Down

0 comments on commit ba5be60

Please sign in to comment.