Skip to content

Commit

Permalink
nit: resolve PR comment
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Kavaldzhiev <[email protected]>
  • Loading branch information
IvanKavaldzhiev committed Jan 10, 2025
1 parent 289fe7c commit 32c9803
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.hedera.pbj.runtime.io.stream.WritableStreamingData;
import com.swirlds.common.constructable.ClassConstructorPair;
import com.swirlds.common.constructable.ConstructableRegistry;
import com.swirlds.common.constructable.ConstructableRegistryException;
Expand All @@ -54,7 +55,8 @@
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.SwirldMain;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.ByteBuffer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -66,7 +68,6 @@
public class StatsSigningTestingToolMain implements SwirldMain {
// the first four come from the parameters in the config.txt file

public static final String STATE_SIGNATURE_MARKER = "STATE_SIGNATURE_MARKER";
private static final Logger logger = LogManager.getLogger(StatsSigningTestingToolMain.class);

static {
Expand Down Expand Up @@ -322,17 +323,19 @@ public BasicSoftwareVersion getSoftwareVersion() {
@Override
@NonNull
public Bytes encodeSystemTransaction(@NonNull final StateSignatureTransaction transaction) {
final String stateSignatureMarker = STATE_SIGNATURE_MARKER;
final int markerSize = stateSignatureMarker.length();
final byte[] parsedStateSignatureTransaction =
StateSignatureTransaction.PROTOBUF.toBytes(transaction).toByteArray();
final int bufferCapacity = Integer.BYTES + markerSize + parsedStateSignatureTransaction.length;

final ByteBuffer buffer = ByteBuffer.allocate(bufferCapacity);
buffer.putInt(markerSize);
buffer.put(stateSignatureMarker.getBytes());
buffer.put(parsedStateSignatureTransaction);

return Bytes.wrap(buffer.array());
final var bytes = new ByteArrayOutputStream();
final var out = new WritableStreamingData(bytes);

// Add a 1 byte as a marker to indicate the start of a system transaction. This is used
// to later differentiate between application transactions and system transactions.
final byte marker = 1;
out.writeByte(marker);
try {
StateSignatureTransaction.PROTOBUF.write(transaction, out);
return Bytes.wrap(bytes.toByteArray());
} catch (final IOException e) {
logger.error("Failed to write StateSignatureTransaction to output stream", e);
return Bytes.EMPTY;

Check warning on line 338 in platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java#L336-L338

Added lines #L336 - L338 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.platform.system.transaction.Transaction;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -225,13 +224,7 @@ private boolean areTransactionBytesSystemOnes(@NonNull final Transaction transac
return false;

Check warning on line 224 in platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java#L224

Added line #L224 was not covered by tests
}

final ByteBuffer wrapper = ByteBuffer.wrap(transactionBytes.toByteArray());
final var markerSize = wrapper.getInt();

final var marker = new byte[markerSize];
wrapper.get(marker);

return StatsSigningTestingToolMain.STATE_SIGNATURE_MARKER.equals(new String(marker));
return transactionBytes.getByte(0) == (byte) 1;
}

private void consumeSystemTransaction(
Expand All @@ -241,33 +234,17 @@ private void consumeSystemTransaction(
final Consumer<ScopedSystemTransaction<StateSignatureTransaction>>
stateSignatureTransactionCallback) {
try {
final var stateSignatureTransaction = StateSignatureTransaction.PROTOBUF.parse(
stripSystemTransactionBytes(transaction.getApplicationTransaction()));
final Bytes transactionBytes = transaction.getApplicationTransaction();
final Bytes strippedSystemTransactionBytes = transactionBytes.slice(1, transactionBytes.length() - 1);
final StateSignatureTransaction stateSignatureTransaction =
StateSignatureTransaction.PROTOBUF.parse(strippedSystemTransactionBytes);
stateSignatureTransactionCallback.accept(new ScopedSystemTransaction<>(
event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction));
} catch (final ParseException e) {
logger.error("Failed to parse StateSignatureTransaction", e);

Check warning on line 244 in platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java#L243-L244

Added lines #L243 - L244 were not covered by tests
}
}

private Bytes stripSystemTransactionBytes(final Bytes transactionBytes) {
final byte[] transactionBytesArray = transactionBytes.toByteArray();
final ByteBuffer wrapper = ByteBuffer.wrap(transactionBytesArray);
final int transactionSize = transactionBytesArray.length;

// Get the size of the marker string
final int markerSize = wrapper.getInt();

// Get the marker itself
final var marker = new byte[markerSize];
wrapper.get(marker);

// Get the StateSignatureTransaction we are interested in
final var stateSignatureTransaction = new byte[transactionSize - (markerSize + Integer.BYTES)];
wrapper.get(stateSignatureTransaction);
return Bytes.wrap(stateSignatureTransaction);
}

private void maybeDelay() {
if (SYNTHETIC_HANDLE_TIME) {
final long start = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void init() {
final byte[] sig = exSig.getSignature();

transactions[i] = TransactionCodec.encode(alg, transactionId, sig, data);
} catch (SignatureException e) {
} catch (final SignatureException e) {

Check warning on line 168 in platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java#L168

Added line #L168 was not covered by tests
// If we are unable to sign the transaction then log the failure and create an unsigned transaction
logger.error(
EXCEPTION.getMarker(),
Expand Down Expand Up @@ -202,7 +202,7 @@ private void tryAcquirePrimitives() {
if (algorithm.isAvailable()) {
activeAlgorithms.put(algorithm.getId(), algorithm);
}
} catch (Exception ex) {
} catch (final Exception ex) {

Check warning on line 205 in platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java#L205

Added line #L205 was not covered by tests
logger.error(
EXCEPTION.getMarker(),
"Failed to Activate Signing Algorithm [ id = {}, class = {} ]",
Expand All @@ -212,8 +212,4 @@ private void tryAcquirePrimitives() {
}
}
}

public int getTransactionSize() {
return transactionSize;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2022-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,11 +30,11 @@
* The core transaction encoder and decoder implementation. See below for the binary transaction format specification.
* <p>
* Transaction Structure:
* ------------------------------------------------------------------------------------------------------------
* | 8 bytes | 1 byte | 1 byte | 4 bytes | pklen bytes | 4 bytes | siglen bytes | 4 bytes | datalen bytes |
* |---------|--------|----------|-----------|-------------|---------|--------------|---------|---------------|
* | id | signed | sigAlgId | pklen | pk | siglen | sig | datalen | data |
* ------------------------------------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------------------------------------------
* | 1 byte | 8 bytes | 1 byte | 1 byte | 4 bytes | pklen bytes | 4 bytes | siglen bytes | 4 bytes | datalen bytes |
* |--------|---------|--------|----------|-----------|-------------|---------|--------------|---------|---------------|
* | marker | id | signed | sigAlgId | pklen | pk | siglen | sig | datalen | data |
* ---------------------------------------------------------------------------------------------------------------------
*/
final class TransactionCodec {

Expand Down Expand Up @@ -78,11 +78,14 @@ public static int overheadSize(final SigningAlgorithm algorithm) {

public static byte[] encode(
final SigningAlgorithm algorithm, final long transactionId, final byte[] signature, final byte[] data) {
final ByteBuffer buffer = ByteBuffer.allocate(bufferSize(algorithm, (data != null) ? data.length : 0));
final ByteBuffer buffer = ByteBuffer.allocate(1 + bufferSize(algorithm, (data != null) ? data.length : 0));
final boolean signed =
algorithm != null && algorithm.isAvailable() && signature != null && signature.length > 0;

buffer.putLong(transactionId)
// Add a 0 byte as a marker to indicate the start of an application transaction. This is used
// to later differentiate between application transactions and system transactions.
final byte marker = 0;
buffer.put(marker)
.putLong(transactionId)
.put((signed) ? (byte) 1 : 0)
.put((signed) ? algorithm.getId() : NO_ALGORITHM_PRESENT)
.putInt((signed) ? algorithm.getPublicKeyLength() : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ void setUp() {
.build();

when(transactionPoolSupplier.get()).thenReturn(transactionPool);
when(transactionPool.getTransactionSize()).thenReturn(transactionSize);
}

@ParameterizedTest
Expand Down

0 comments on commit 32c9803

Please sign in to comment.