From 63edb17e571887330c0289b1dd915d970e626cc9 Mon Sep 17 00:00:00 2001 From: Mustafa Uzun Date: Thu, 19 Dec 2024 20:52:37 +0200 Subject: [PATCH 01/18] refactor: consumer for system transactions Signed-off-by: Mustafa Uzun --- .../com/swirlds/demo/crypto/CryptocurrencyDemoState.java | 4 +--- .../com/swirlds/demo/hello/HelloSwirldDemoState.java | 4 +--- .../main/java/com/swirlds/demo/stats/StatsDemoState.java | 5 +---- .../demo/addressbook/AddressBookTestingToolState.java | 5 +---- .../demo/consistency/ConsistencyTestingToolState.java | 9 ++------- .../java/com/swirlds/demo/iss/ISSTestingToolState.java | 4 +--- .../demo/migration/MigrationTestingToolState.java | 4 +--- .../swirlds/demo/platform/PlatformTestingToolState.java | 8 ++------ .../demo/stats/signing/StatsSigningTestingToolState.java | 8 ++------ .../com/swirlds/demo/stress/StressTestingToolState.java | 9 ++------- .../eventhandling/DefaultTransactionPrehandler.java | 2 +- .../swirlds/platform/state/PlatformMerkleStateRoot.java | 9 ++------- .../com/swirlds/platform/state/SwirldStateManager.java | 2 +- .../java/com/swirlds/platform/system/SwirldState.java | 5 ++--- .../platform/turtle/runner/TurtleTestingToolState.java | 5 +---- .../test/fixtures/state/BlockingSwirldState.java | 5 +---- 16 files changed, 22 insertions(+), 66 deletions(-) diff --git a/platform-sdk/platform-apps/demos/CryptocurrencyDemo/src/main/java/com/swirlds/demo/crypto/CryptocurrencyDemoState.java b/platform-sdk/platform-apps/demos/CryptocurrencyDemo/src/main/java/com/swirlds/demo/crypto/CryptocurrencyDemoState.java index 624f1bc15dcf..417e42ef1bc8 100644 --- a/platform-sdk/platform-apps/demos/CryptocurrencyDemo/src/main/java/com/swirlds/demo/crypto/CryptocurrencyDemoState.java +++ b/platform-sdk/platform-apps/demos/CryptocurrencyDemo/src/main/java/com/swirlds/demo/crypto/CryptocurrencyDemoState.java @@ -211,9 +211,7 @@ public synchronized CryptocurrencyDemoState copy() { public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); round.forEachEventTransaction((event, transaction) -> handleTransaction(event.getCreatorId(), transaction)); } diff --git a/platform-sdk/platform-apps/demos/HelloSwirldDemo/src/main/java/com/swirlds/demo/hello/HelloSwirldDemoState.java b/platform-sdk/platform-apps/demos/HelloSwirldDemo/src/main/java/com/swirlds/demo/hello/HelloSwirldDemoState.java index c7d66c8090df..091f995c2cba 100644 --- a/platform-sdk/platform-apps/demos/HelloSwirldDemo/src/main/java/com/swirlds/demo/hello/HelloSwirldDemoState.java +++ b/platform-sdk/platform-apps/demos/HelloSwirldDemo/src/main/java/com/swirlds/demo/hello/HelloSwirldDemoState.java @@ -111,9 +111,7 @@ private HelloSwirldDemoState(final HelloSwirldDemoState sourceState) { public synchronized void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); round.forEachTransaction(this::handleTransaction); } diff --git a/platform-sdk/platform-apps/demos/StatsDemo/src/main/java/com/swirlds/demo/stats/StatsDemoState.java b/platform-sdk/platform-apps/demos/StatsDemo/src/main/java/com/swirlds/demo/stats/StatsDemoState.java index 64d9e5a976f5..95edc58169bd 100644 --- a/platform-sdk/platform-apps/demos/StatsDemo/src/main/java/com/swirlds/demo/stats/StatsDemoState.java +++ b/platform-sdk/platform-apps/demos/StatsDemo/src/main/java/com/swirlds/demo/stats/StatsDemoState.java @@ -36,7 +36,6 @@ import com.swirlds.platform.system.Round; import com.swirlds.platform.system.SoftwareVersion; import edu.umd.cs.findbugs.annotations.NonNull; -import java.util.List; import java.util.function.Consumer; import java.util.function.Function; @@ -83,9 +82,7 @@ private StatsDemoState(final StatsDemoState sourceState) { public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) {} + @NonNull final Consumer> stateSignatureTransactions) {} /** * {@inheritDoc} diff --git a/platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolState.java b/platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolState.java index 76f36aafdae0..fe1c74e122e0 100644 --- a/platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolState.java +++ b/platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolState.java @@ -71,7 +71,6 @@ import java.text.ParseException; import java.time.Duration; import java.util.Iterator; -import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -222,9 +221,7 @@ public void init( public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { Objects.requireNonNull(round, "the round cannot be null"); Objects.requireNonNull(platformState, "the platform state cannot be null"); throwIfImmutable(); diff --git a/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java b/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java index c0313c161976..4dba460083f5 100644 --- a/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java +++ b/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java @@ -44,7 +44,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; -import java.util.List; import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -246,9 +245,7 @@ private void applyTransactionToState(final @NonNull ConsensusTransaction transac @Override public void preHandle( @NonNull final Event event, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { event.forEachTransaction(transaction -> { if (transaction.isSystem()) { return; @@ -272,9 +269,7 @@ public void preHandle( public void handleConsensusRound( final @NonNull Round round, final @NonNull PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { Objects.requireNonNull(round); Objects.requireNonNull(platformState); diff --git a/platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java b/platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java index f78d3e6eebc4..746aee8c9353 100644 --- a/platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java +++ b/platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java @@ -243,9 +243,7 @@ void writeObjectByChildIndex(int index, List lis public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); final Iterator eventIterator = round.iterator(); diff --git a/platform-sdk/platform-apps/tests/MigrationTestingTool/src/main/java/com/swirlds/demo/migration/MigrationTestingToolState.java b/platform-sdk/platform-apps/tests/MigrationTestingTool/src/main/java/com/swirlds/demo/migration/MigrationTestingToolState.java index 504679ccff2e..c58c83cf8a72 100644 --- a/platform-sdk/platform-apps/tests/MigrationTestingTool/src/main/java/com/swirlds/demo/migration/MigrationTestingToolState.java +++ b/platform-sdk/platform-apps/tests/MigrationTestingTool/src/main/java/com/swirlds/demo/migration/MigrationTestingToolState.java @@ -285,9 +285,7 @@ public void init( public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); for (final Iterator eventIt = round.iterator(); eventIt.hasNext(); ) { final ConsensusEvent event = eventIt.next(); diff --git a/platform-sdk/platform-apps/tests/PlatformTestingTool/src/main/java/com/swirlds/demo/platform/PlatformTestingToolState.java b/platform-sdk/platform-apps/tests/PlatformTestingTool/src/main/java/com/swirlds/demo/platform/PlatformTestingToolState.java index 48a2b0ea9694..427cc93c6a41 100644 --- a/platform-sdk/platform-apps/tests/PlatformTestingTool/src/main/java/com/swirlds/demo/platform/PlatformTestingToolState.java +++ b/platform-sdk/platform-apps/tests/PlatformTestingTool/src/main/java/com/swirlds/demo/platform/PlatformTestingToolState.java @@ -1056,9 +1056,7 @@ protected void preHandleTransaction(final Transaction transaction) { public synchronized void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); if (!initialized.get()) { throw new IllegalStateException("handleConsensusRound() called before init()"); @@ -1662,9 +1660,7 @@ private static class ChildIndices { @Override public void preHandle( @NonNull final Event event, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { event.forEachTransaction(this::preHandleTransaction); } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index dabd0b8fff3c..429c4926fde6 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -115,9 +115,7 @@ public synchronized StatsSigningTestingToolState copy() { @Override public void preHandle( @NonNull final Event event, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { final SttTransactionPool sttTransactionPool = transactionPoolSupplier.get(); if (sttTransactionPool != null) { event.forEachTransaction(transaction -> { @@ -141,9 +139,7 @@ public void preHandle( public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); round.forEachTransaction(this::handleTransaction); } diff --git a/platform-sdk/platform-apps/tests/StressTestingTool/src/main/java/com/swirlds/demo/stress/StressTestingToolState.java b/platform-sdk/platform-apps/tests/StressTestingTool/src/main/java/com/swirlds/demo/stress/StressTestingToolState.java index 3225ecffe8f3..769543cf6beb 100644 --- a/platform-sdk/platform-apps/tests/StressTestingTool/src/main/java/com/swirlds/demo/stress/StressTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StressTestingTool/src/main/java/com/swirlds/demo/stress/StressTestingToolState.java @@ -43,7 +43,6 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import java.time.Duration; -import java.util.List; import java.util.function.Consumer; import java.util.function.Function; @@ -103,9 +102,7 @@ public void init( @Override public void preHandle( @NonNull final Event event, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { busyWait(config.preHandleTime()); } @@ -116,9 +113,7 @@ public void preHandle( public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); round.forEachTransaction(this::handleTransaction); } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/DefaultTransactionPrehandler.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/DefaultTransactionPrehandler.java index 6ddb39c0aade..884991c5eb80 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/DefaultTransactionPrehandler.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/DefaultTransactionPrehandler.java @@ -42,7 +42,7 @@ public class DefaultTransactionPrehandler implements TransactionPrehandler { private static final Logger logger = LogManager.getLogger(DefaultTransactionPrehandler.class); - public static final Consumer>> NO_OP_CONSUMER = + public static final Consumer> NO_OP_CONSUMER = systemTransactions -> {}; /** diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/PlatformMerkleStateRoot.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/PlatformMerkleStateRoot.java index b6c9b1c7fc37..ae21d3ce8d66 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/PlatformMerkleStateRoot.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/PlatformMerkleStateRoot.java @@ -48,7 +48,6 @@ import com.swirlds.state.spi.WritableStates; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import java.util.List; import java.util.function.Consumer; import java.util.function.Function; @@ -153,9 +152,7 @@ protected PlatformMerkleStateRoot copyingConstructor() { public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { throwIfImmutable(); lifecycles.onHandleConsensusRound(round, this); } @@ -173,9 +170,7 @@ public void sealConsensusRound(@NonNull final Round round) { @Override public void preHandle( @NonNull final Event event, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { lifecycles.onPreHandle(event, this); } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/SwirldStateManager.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/SwirldStateManager.java index ce99739dc690..06a9d89e1456 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/SwirldStateManager.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/SwirldStateManager.java @@ -123,7 +123,7 @@ public void setInitialState(@NonNull final MerkleRoot state) { /** * Handles the events in a consensus round. Implementations are responsible for invoking - * {@link SwirldState#handleConsensusRound(Round, PlatformStateModifier, Consumer>>)}. + * {@link SwirldState#handleConsensusRound(Round, PlatformStateModifier, Consumer>)}. * * @param round the round to handle */ diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldState.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldState.java index a6d0368bea77..7c09a13ecea8 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldState.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldState.java @@ -26,7 +26,6 @@ import com.swirlds.platform.system.transaction.Transaction; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import java.util.List; import java.util.Objects; import java.util.function.Consumer; @@ -77,7 +76,7 @@ default void init( */ default void preHandle( final Event event, - final Consumer>> stateSignatureTransactions) {} + final Consumer> stateSignatureTransactions) {} /** * This method should apply the transactions in the provided round to the state. Only called on mutable states. @@ -90,7 +89,7 @@ default void preHandle( void handleConsensusRound( final Round round, final PlatformStateModifier platformState, - final Consumer>> stateSignatureTransactions); + final Consumer> stateSignatureTransactions); /** * Called by the platform after it has made all its changes to this state for the given round. diff --git a/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/turtle/runner/TurtleTestingToolState.java b/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/turtle/runner/TurtleTestingToolState.java index e1d23cd50e89..4e59efa87143 100644 --- a/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/turtle/runner/TurtleTestingToolState.java +++ b/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/turtle/runner/TurtleTestingToolState.java @@ -25,7 +25,6 @@ import com.swirlds.platform.system.BasicSoftwareVersion; import com.swirlds.platform.system.Round; import edu.umd.cs.findbugs.annotations.NonNull; -import java.util.List; import java.util.function.Consumer; /** @@ -84,9 +83,7 @@ public int getVersion() { public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { state = NonCryptographicHashing.hash64( state, round.getRoundNum(), diff --git a/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java b/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java index e5537a738c89..2ee22c376ec7 100644 --- a/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java +++ b/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java @@ -36,7 +36,6 @@ import com.swirlds.state.merkle.singleton.StringLeaf; import edu.umd.cs.findbugs.annotations.NonNull; import java.io.IOException; -import java.util.List; import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; @@ -86,9 +85,7 @@ private BlockingSwirldState(final BlockingSwirldState that) { public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull - final Consumer>> - stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransactions) { // intentionally does nothing } From 50669f79a9517ab66b848f2011c96764f497f9f6 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 20 Dec 2024 12:36:37 +0200 Subject: [PATCH 02/18] refactor: adapt StatsSigningTestingTool to work with Bytes wrapper for transactions Signed-off-by: Ivan Kavaldzhiev --- .../StatsSigningTestingTool/build.gradle.kts | 6 + .../signing/StatsSigningTestingToolMain.java | 24 +- .../signing/StatsSigningTestingToolState.java | 66 +++-- .../StatsSigningTestingToolStateTest.java | 267 ++++++++++++++++++ .../swirlds/platform/system/SwirldMain.java | 29 +- 5 files changed, 341 insertions(+), 51 deletions(-) create mode 100644 platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts index 2a4f4cf630fa..e40a1c16da55 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts @@ -6,3 +6,9 @@ plugins { id("org.hiero.gradle.module.application") } tasks.withType().configureEach { options.compilerArgs.add("-Xlint:-cast") } application.mainClass = "com.swirlds.demo.stats.signing.StatsSigningTestingToolMain" + +testModuleInfo { + requires("org.assertj.core") + requires("org.junit.jupiter.api") + requires("org.mockito") +} diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java index 4e38c8ccdde7..44d1a7364ce1 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java @@ -1,19 +1,4 @@ -/* - * Copyright (C) 2022-2024 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. - * 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. - */ - +// SPDX-License-Identifier: Apache-2.0 package com.swirlds.demo.stats.signing; /* * This file is public domain. @@ -34,6 +19,8 @@ import static com.swirlds.platform.test.fixtures.state.FakeMerkleStateLifecycles.FAKE_MERKLE_STATE_LIFECYCLES; import static com.swirlds.platform.test.fixtures.state.FakeMerkleStateLifecycles.registerMerkleStateRootClassIds; +import com.hedera.hapi.platform.event.StateSignatureTransaction; +import com.hedera.pbj.runtime.io.buffer.Bytes; import com.swirlds.common.constructable.ClassConstructorPair; import com.swirlds.common.constructable.ConstructableRegistry; import com.swirlds.common.constructable.ConstructableRegistryException; @@ -313,4 +300,9 @@ public PlatformMerkleStateRoot newMerkleStateRoot() { public BasicSoftwareVersion getSoftwareVersion() { return softwareVersion; } + + @Override + public Bytes encodeSystemTransaction(@NonNull StateSignatureTransaction transaction) { + return StateSignatureTransaction.PROTOBUF.toBytes(transaction); + } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index dabd0b8fff3c..6f4bed5488f7 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -1,19 +1,4 @@ -/* - * Copyright (C) 2022-2024 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. - * 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. - */ - +// SPDX-License-Identifier: Apache-2.0 package com.swirlds.demo.stats.signing; /* * This file is public domain. @@ -46,6 +31,7 @@ import com.swirlds.platform.system.transaction.ConsensusTransaction; import com.swirlds.platform.system.transaction.Transaction; import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -118,12 +104,20 @@ public void preHandle( @NonNull final Consumer>> stateSignatureTransactions) { + + final var scopedSystemTransactions = new ArrayList>(); final SttTransactionPool sttTransactionPool = transactionPoolSupplier.get(); if (sttTransactionPool != null) { event.forEachTransaction(transaction -> { if (transaction.isSystem()) { return; } + + if (areTransactionBytesSystemOnes((ConsensusTransaction) transaction)) { + scopedSystemTransactions.add( + new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction)); + } + final TransactionSignature transactionSignature = sttTransactionPool.expandSignatures(transaction.getApplicationTransaction()); if (transactionSignature != null) { @@ -132,6 +126,8 @@ public void preHandle( } }); } + + stateSignatureTransactions.accept(scopedSystemTransactions); } /** @@ -145,13 +141,28 @@ public void handleConsensusRound( final Consumer>> stateSignatureTransactions) { throwIfImmutable(); - round.forEachTransaction(this::handleTransaction); + final var scopedSystemTransactions = new ArrayList>(); + + round.forEachEventTransaction((event, transaction) -> { + final var transactionWithSystemBytes = handleTransaction(transaction); + if (transactionWithSystemBytes != null) { + scopedSystemTransactions.add(new ScopedSystemTransaction( + event.getCreatorId(), event.getSoftwareVersion(), transactionWithSystemBytes)); + } + }); + + stateSignatureTransactions.accept(scopedSystemTransactions); } - private void handleTransaction(final ConsensusTransaction trans) { + private ConsensusTransaction handleTransaction(final ConsensusTransaction trans) { if (trans.isSystem()) { - return; + return null; } + + if (areTransactionBytesSystemOnes(trans)) { + return trans; + } + final TransactionSignature s = trans.getMetadata(); if (s != null && validateSignature(s, trans) && s.getSignatureStatus() != VerificationStatus.VALID) { @@ -177,6 +188,23 @@ private void handleTransaction(final ConsensusTransaction trans) { runningSum += TransactionCodec.txId(trans.getApplicationTransaction()); maybeDelay(); + + return null; + } + + /** + * Checks if the transaction bytes are system ones. + * + * @param transaction the consensus transaction to check + * @return true if the transaction bytes are system ones, false otherwise + */ + private boolean areTransactionBytesSystemOnes(final ConsensusTransaction transaction) { + // We have maximum allocation of 100 bytes for the transaction + 10 bytes for the preamble size in + // TransactionCodec + // + 64 bytes for the maximum public key + 64 bytes for the maximum signature size + 12 bytes for 3 integer + // bytes + final var maximumSignedEncodedTransactionSize = 100 + 10 + 64 + 64 + 12; + return transaction.getApplicationTransaction().length() > maximumSignedEncodedTransactionSize; } private void maybeDelay() { diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java new file mode 100644 index 000000000000..2d348576ceae --- /dev/null +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -0,0 +1,267 @@ +// SPDX-License-Identifier: Apache-2.0 +package com.swirlds.demo.stats.signing; + +import static com.hedera.hapi.platform.event.EventTransaction.TransactionOneOfType.APPLICATION_TRANSACTION; +import static com.hedera.hapi.platform.event.EventTransaction.TransactionOneOfType.STATE_SIGNATURE_TRANSACTION; +import static com.swirlds.common.utility.ByteUtils.longToByteArray; +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.hedera.hapi.node.base.SemanticVersion; +import com.hedera.hapi.node.base.Timestamp; +import com.hedera.hapi.node.state.roster.Roster; +import com.hedera.hapi.platform.event.EventCore; +import com.hedera.hapi.platform.event.EventTransaction; +import com.hedera.hapi.platform.event.GossipEvent; +import com.hedera.hapi.platform.event.StateSignatureTransaction; +import com.hedera.pbj.runtime.OneOf; +import com.hedera.pbj.runtime.io.buffer.Bytes; +import com.swirlds.common.platform.NodeId; +import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction; +import com.swirlds.platform.consensus.ConsensusSnapshot; +import com.swirlds.platform.consensus.EventWindow; +import com.swirlds.platform.event.AncientMode; +import com.swirlds.platform.event.PlatformEvent; +import com.swirlds.platform.gossip.shadowgraph.Generations; +import com.swirlds.platform.internal.ConsensusRound; +import com.swirlds.platform.state.MerkleStateLifecycles; +import com.swirlds.platform.state.PlatformStateModifier; +import com.swirlds.platform.system.Round; +import com.swirlds.platform.system.transaction.ConsensusTransaction; +import com.swirlds.platform.system.transaction.TransactionWrapper; +import java.time.Instant; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class StatsSigningTestingToolStateTest { + + private Random random; + private StatsSigningTestingToolState state; + private PlatformStateModifier platformStateModifier; + private Round round; + private PlatformEvent event; + private Consumer>> consumer; + private int consumerSize; + private ConsensusTransaction consensusTransaction; + private StateSignatureTransaction stateSignatureTransaction; + private Supplier transactionPoolSupplier; + private SttTransactionPool transactionPool; + + @BeforeEach + void setUp() { + transactionPool = mock(SttTransactionPool.class); + transactionPoolSupplier = mock(Supplier.class); + state = new StatsSigningTestingToolState( + mock(MerkleStateLifecycles.class), mock(Function.class), transactionPoolSupplier); + random = new Random(); + platformStateModifier = mock(PlatformStateModifier.class); + event = mock(PlatformEvent.class); + + final var eventWindow = new EventWindow(10, 5, 20, AncientMode.BIRTH_ROUND_THRESHOLD); + final var roster = new Roster(Collections.EMPTY_LIST); + when(event.transactionIterator()).thenReturn(Collections.emptyIterator()); + round = new ConsensusRound( + roster, + List.of(event), + event, + new Generations(), + eventWindow, + new ConsensusSnapshot(), + false, + Instant.now()); + + consumer = systemTransactions -> consumerSize = systemTransactions.size(); + consensusTransaction = mock(TransactionWrapper.class); + + final byte[] signature = new byte[384]; + random.nextBytes(signature); + final byte[] hash = new byte[48]; + random.nextBytes(hash); + stateSignatureTransaction = StateSignatureTransaction.newBuilder() + .signature(Bytes.wrap(signature)) + .hash(Bytes.wrap(hash)) + .round(round.getRoundNum()) + .build(); + + when(transactionPoolSupplier.get()).thenReturn(transactionPool); + } + + @Test + void handleConsensusRoundWithApplicationTransaction() { + // Given + givenRoundAndEvent(); + + // We need to pass a transaction bigger than 10 bytes because of {@link TransactionCodec#PREAMBLE_SIZE} + final var bytes = + Bytes.wrap(longToByteArray(random.nextLong())).append(Bytes.wrap(longToByteArray(random.nextLong()))); + when(consensusTransaction.getApplicationTransaction()).thenReturn(bytes); + + // When + state.handleConsensusRound(round, platformStateModifier, consumer); + + // Then + assertThat(consumerSize).isZero(); + } + + @Test + void handleConsensusRoundWithSystemTransaction() { + // Given + givenRoundAndEvent(); + + final var stateSignatureTransactionBytes = + StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + when(consensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); + + // When + state.handleConsensusRound(round, platformStateModifier, consumer); + + // Then + assertThat(consumerSize).isEqualTo(1); + } + + @Test + void handleConsensusRoundWithMultipleSystemTransaction() { + // Given + final var secondConsensusTransaction = mock(TransactionWrapper.class); + final var thirdConsensusTransaction = mock(TransactionWrapper.class); + when(event.getConsensusTimestamp()).thenReturn(Instant.now()); + when(event.consensusTransactionIterator()) + .thenReturn(List.of(consensusTransaction, secondConsensusTransaction, thirdConsensusTransaction) + .iterator()); + + final var stateSignatureTransactionBytes = + StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + when(consensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); + when(secondConsensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); + when(thirdConsensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); + + // When + state.handleConsensusRound(round, platformStateModifier, consumer); + + // Then + assertThat(consumerSize).isEqualTo(3); + } + + @Test + void handleConsensusRoundWithDeprecatedSystemTransaction() { + // Given + givenRoundAndEvent(); + + when(consensusTransaction.isSystem()).thenReturn(true); + + // When + state.handleConsensusRound(round, platformStateModifier, consumer); + + // Then + assertThat(consumerSize).isZero(); + } + + @Test + void preHandleConsensusRoundWithApplicationTransaction() { + // Given + givenRoundAndEvent(); + + // We need to pass a transaction bigger than 10 bytes because of {@link TransactionCodec#PREAMBLE_SIZE} + final var bytes = + Bytes.wrap(longToByteArray(random.nextLong())).append(Bytes.wrap(longToByteArray(random.nextLong()))); + + final var eventTransaction = new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, bytes)); + final var eventCore = mock(EventCore.class); + final var gossipEvent = new GossipEvent(eventCore, null, List.of(eventTransaction), Collections.emptyList()); + when(eventCore.timeCreated()).thenReturn(Timestamp.DEFAULT); + event = new PlatformEvent(gossipEvent); + + // When + state.preHandle(event, consumer); + + // Then + assertThat(consumerSize).isZero(); + } + + @Test + void preHandleConsensusRoundWithSystemTransaction() { + // Given + givenRoundAndEvent(); + + final var stateSignatureTransactionBytes = + StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + final var eventTransaction = + new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, stateSignatureTransactionBytes)); + final var eventCore = mock(EventCore.class); + final var gossipEvent = new GossipEvent(eventCore, null, List.of(eventTransaction), Collections.emptyList()); + when(eventCore.timeCreated()).thenReturn(Timestamp.DEFAULT); + event = new PlatformEvent(gossipEvent); + + // When + state.preHandle(event, consumer); + + // Then + assertThat(consumerSize).isEqualTo(1); + } + + @Test + void preHandleConsensusRoundWithMultipleSystemTransaction() { + // Given + when(event.getConsensusTimestamp()).thenReturn(Instant.now()); + + final var stateSignatureTransactionBytes = + StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + + final var eventTransaction = + new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, stateSignatureTransactionBytes)); + final var secondEventTransaction = + new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, stateSignatureTransactionBytes)); + final var thirdEventTransaction = + new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, stateSignatureTransactionBytes)); + final var eventCore = mock(EventCore.class); + final var gossipEvent = new GossipEvent( + eventCore, + null, + List.of(eventTransaction, secondEventTransaction, thirdEventTransaction), + Collections.emptyList()); + when(eventCore.timeCreated()).thenReturn(Timestamp.DEFAULT); + event = new PlatformEvent(gossipEvent); + + // When + state.preHandle(event, consumer); + + // Then + assertThat(consumerSize).isEqualTo(3); + } + + @Test + void preHandleConsensusRoundWithDeprecatedSystemTransaction() { + // Given + givenRoundAndEvent(); + + final var stateSignatureTransactionBytes = + StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + final var eventTransaction = + new EventTransaction(new OneOf<>(STATE_SIGNATURE_TRANSACTION, stateSignatureTransactionBytes)); + final var eventCore = mock(EventCore.class); + final var gossipEvent = new GossipEvent(eventCore, null, List.of(eventTransaction), Collections.emptyList()); + when(eventCore.timeCreated()).thenReturn(Timestamp.DEFAULT); + event = new PlatformEvent(gossipEvent); + + // When + state.preHandle(event, consumer); + + // Then + assertThat(consumerSize).isZero(); + } + + private void givenRoundAndEvent() { + when(event.getCreatorId()).thenReturn(new NodeId()); + when(event.getSoftwareVersion()).thenReturn(new SemanticVersion(1, 1, 1, "", "")); + when(event.getConsensusTimestamp()).thenReturn(Instant.now()); + when(event.consensusTransactionIterator()) + .thenReturn(Collections.singletonList(consensusTransaction).iterator()); + } +} diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java index 588a58b8e530..c496384813f5 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java @@ -1,21 +1,8 @@ -/* - * Copyright (C) 2016-2024 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. - * 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. - */ - +// SPDX-License-Identifier: Apache-2.0 package com.swirlds.platform.system; +import com.hedera.hapi.platform.event.StateSignatureTransaction; +import com.hedera.pbj.runtime.io.buffer.Bytes; import com.swirlds.common.platform.NodeId; import com.swirlds.platform.state.MerkleRoot; import edu.umd.cs.findbugs.annotations.NonNull; @@ -91,4 +78,14 @@ default List> getConfigDataTypes() { */ @NonNull SoftwareVersion getSoftwareVersion(); + + /** + * Encodes a system transaction to {@link Bytes} representation of a {@link com.hedera.hapi.node.base.Transaction}. + * + * @param transaction the {@link StateSignatureTransaction} to encode + * @return {@link Bytes} representation of the transaction + */ + default Bytes encodeSystemTransaction(@NonNull StateSignatureTransaction transaction) { + return Bytes.EMPTY; + } } From a7d6e22054d502651a1c50f389afee2839a1091f Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 20 Dec 2024 13:17:46 +0200 Subject: [PATCH 03/18] style: restore headers Signed-off-by: Ivan Kavaldzhiev --- .../StatsSigningTestingTool/build.gradle.kts | 17 ++++++++++++++++- .../signing/StatsSigningTestingToolMain.java | 17 ++++++++++++++++- .../signing/StatsSigningTestingToolState.java | 17 ++++++++++++++++- .../StatsSigningTestingToolStateTest.java | 17 ++++++++++++++++- .../com/swirlds/platform/system/SwirldMain.java | 17 ++++++++++++++++- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts index e40a1c16da55..dc8e2b5b99d4 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts @@ -1,4 +1,19 @@ -// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2024 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. + * 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. + */ + plugins { id("org.hiero.gradle.module.application") } // Remove the following line to enable all 'javac' lint checks that we have turned on by default diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java index 44d1a7364ce1..6b1902b606c3 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java @@ -1,4 +1,19 @@ -// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2024 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. + * 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.swirlds.demo.stats.signing; /* * This file is public domain. diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 6f4bed5488f7..3fda9fd2b58d 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -1,4 +1,19 @@ -// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2024 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. + * 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.swirlds.demo.stats.signing; /* * This file is public domain. diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index 2d348576ceae..f648357ea596 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -1,4 +1,19 @@ -// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2024 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. + * 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.swirlds.demo.stats.signing; import static com.hedera.hapi.platform.event.EventTransaction.TransactionOneOfType.APPLICATION_TRANSACTION; diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java index c496384813f5..5b122b90187a 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java @@ -1,4 +1,19 @@ -// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2024 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. + * 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.swirlds.platform.system; import com.hedera.hapi.platform.event.StateSignatureTransaction; From 2145caaad35b0f8b4c9027be08c6b1e579114fed Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 20 Dec 2024 16:44:36 +0200 Subject: [PATCH 04/18] nit: resolve PR comments Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolState.java | 23 ++++++++----------- .../StatsSigningTestingToolStateTest.java | 1 + 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index e50113e3a36d..6e174893fdb7 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -149,21 +149,18 @@ public void handleConsensusRound( throwIfImmutable(); round.forEachEventTransaction((event, transaction) -> { - final var transactionWithSystemBytes = handleTransaction(transaction); - if (transactionWithSystemBytes != null) { - stateSignatureTransaction.accept(new ScopedSystemTransaction( - event.getCreatorId(), event.getSoftwareVersion(), transactionWithSystemBytes)); + if (areTransactionBytesSystemOnes(transaction)) { + stateSignatureTransaction.accept( + new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction)); + } else { + handleTransaction(transaction); } }); } - private ConsensusTransaction handleTransaction(final ConsensusTransaction trans) { + private void handleTransaction(final ConsensusTransaction trans) { if (trans.isSystem()) { - return null; - } - - if (areTransactionBytesSystemOnes(trans)) { - return trans; + return; } final TransactionSignature s = trans.getMetadata(); @@ -191,8 +188,6 @@ private ConsensusTransaction handleTransaction(final ConsensusTransaction trans) runningSum += TransactionCodec.txId(trans.getApplicationTransaction()); maybeDelay(); - - return null; } /** @@ -204,8 +199,8 @@ private ConsensusTransaction handleTransaction(final ConsensusTransaction trans) private boolean areTransactionBytesSystemOnes(final ConsensusTransaction transaction) { // We have maximum allocation of 100 bytes for the transaction + 10 bytes for the preamble size in // TransactionCodec - // + 64 bytes for the maximum public key + 64 bytes for the maximum signature size + 12 bytes for 3 integer - // bytes + // + 64 bytes for the maximum public key size + 64 bytes for the maximum signature size + 12 bytes for 3 + // integers final var maximumSignedEncodedTransactionSize = 100 + 10 + 64 + 64 + 12; return transaction.getApplicationTransaction().length() > maximumSignedEncodedTransactionSize; } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index 13249404605b..41252f211142 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -171,6 +171,7 @@ void handleConsensusRoundWithDeprecatedSystemTransaction() { // Given givenRoundAndEvent(); + when(consensusTransaction.getApplicationTransaction()).thenReturn(Bytes.EMPTY); when(consensusTransaction.isSystem()).thenReturn(true); // When From 9315b1c102d263ae243ce1a9269a337213e8508f Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 20 Dec 2024 16:47:26 +0200 Subject: [PATCH 05/18] nit: resolve PR comments Signed-off-by: Ivan Kavaldzhiev --- .../swirlds/demo/stats/signing/StatsSigningTestingToolMain.java | 2 +- .../demo/stats/signing/StatsSigningTestingToolState.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java index 6b1902b606c3..5f3455e7350a 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java @@ -317,7 +317,7 @@ public BasicSoftwareVersion getSoftwareVersion() { } @Override - public Bytes encodeSystemTransaction(@NonNull StateSignatureTransaction transaction) { + public Bytes encodeSystemTransaction(@NonNull final StateSignatureTransaction transaction) { return StateSignatureTransaction.PROTOBUF.toBytes(transaction); } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 6e174893fdb7..88364e390b4d 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -126,6 +126,7 @@ public void preHandle( if (areTransactionBytesSystemOnes((ConsensusTransaction) transaction)) { stateSignatureTransaction.accept( new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction)); + return; } final TransactionSignature transactionSignature = From a3d2c8b02360f1f9c51240a6733710acef1fb120 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 27 Dec 2024 12:08:37 +0200 Subject: [PATCH 06/18] nit: resolve PR comment and change system transaction differentiation Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolState.java | 34 +++++++++---- .../stats/signing/SttTransactionPool.java | 4 ++ .../StatsSigningTestingToolStateTest.java | 49 ++++++++++++++----- 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 88364e390b4d..8d5344a775db 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -46,6 +46,7 @@ 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; @@ -123,7 +124,7 @@ public void preHandle( return; } - if (areTransactionBytesSystemOnes((ConsensusTransaction) transaction)) { + if (areTransactionBytesSystemOnes(transaction)) { stateSignatureTransaction.accept( new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction)); return; @@ -194,16 +195,31 @@ private void handleTransaction(final ConsensusTransaction trans) { /** * Checks if the transaction bytes are system ones. * - * @param transaction the consensus transaction to check + * @param transaction the transaction to check * @return true if the transaction bytes are system ones, false otherwise */ - private boolean areTransactionBytesSystemOnes(final ConsensusTransaction transaction) { - // We have maximum allocation of 100 bytes for the transaction + 10 bytes for the preamble size in - // TransactionCodec - // + 64 bytes for the maximum public key size + 64 bytes for the maximum signature size + 12 bytes for 3 - // integers - final var maximumSignedEncodedTransactionSize = 100 + 10 + 64 + 64 + 12; - return transaction.getApplicationTransaction().length() > maximumSignedEncodedTransactionSize; + private boolean areTransactionBytesSystemOnes(final Transaction transaction) { + final var transactionBytes = transaction.getApplicationTransaction(); + + if (transactionBytes.length() == 0) { + return false; + } + + final var transactionPool = transactionPoolSupplier.get(); + + // Check the data length size is the expected size based on the transactionSize property. + // If it is not, then the transaction is a system one. + if (TransactionCodec.txIsSigned(transactionBytes)) { + // Data length can be read directly from the extracted signature + final var signature = TransactionCodec.extractSignature(transactionBytes); + return signature.getMessageLength() != transactionPool.getTransactionSize(); + } else { + // Data length value can be read from the transaction bytes using an offset + final ByteBuffer wrapper = + ByteBuffer.wrap(transactionBytes.toByteArray()).position(21); + final byte dataLength = wrapper.get(); + return dataLength != transactionPool.getTransactionSize(); + } } private void maybeDelay() { diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java index 604a66727370..1458831ee14a 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java @@ -212,4 +212,8 @@ private void tryAcquirePrimitives() { } } } + + public int getTransactionSize() { + return transactionSize; + } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index 41252f211142..ecf9adf7de4c 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -18,7 +18,6 @@ import static com.hedera.hapi.platform.event.EventTransaction.TransactionOneOfType.APPLICATION_TRANSACTION; import static com.hedera.hapi.platform.event.EventTransaction.TransactionOneOfType.STATE_SIGNATURE_TRANSACTION; -import static com.swirlds.common.utility.ByteUtils.longToByteArray; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -33,6 +32,7 @@ import com.hedera.pbj.runtime.OneOf; import com.hedera.pbj.runtime.io.buffer.Bytes; import com.swirlds.common.platform.NodeId; +import com.swirlds.demo.stats.signing.algorithms.X25519SigningAlgorithm; import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction; import com.swirlds.platform.consensus.ConsensusSnapshot; import com.swirlds.platform.consensus.EventWindow; @@ -45,6 +45,7 @@ import com.swirlds.platform.system.Round; import com.swirlds.platform.system.transaction.ConsensusTransaction; import com.swirlds.platform.system.transaction.TransactionWrapper; +import java.security.SignatureException; import java.time.Instant; import java.util.ArrayList; import java.util.Collections; @@ -55,9 +56,12 @@ import java.util.function.Supplier; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; class StatsSigningTestingToolStateTest { + private static int transactionSize = 100; private Random random; private StatsSigningTestingToolState state; private PlatformStateModifier platformStateModifier; @@ -108,17 +112,19 @@ void setUp() { .build(); when(transactionPoolSupplier.get()).thenReturn(transactionPool); + when(transactionPool.getTransactionSize()).thenReturn(transactionSize); } - @Test - void handleConsensusRoundWithApplicationTransaction() { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void handleConsensusRoundWithApplicationTransaction(final boolean signedTransaction) throws SignatureException { // Given givenRoundAndEvent(); - // We need to pass a transaction bigger than 10 bytes because of {@link TransactionCodec#PREAMBLE_SIZE} - final var bytes = - Bytes.wrap(longToByteArray(random.nextLong())).append(Bytes.wrap(longToByteArray(random.nextLong()))); - when(consensusTransaction.getApplicationTransaction()).thenReturn(bytes); + final var transactionBytes = + signedTransaction ? getSignedApplicationTransaction() : getUnsignedApplicationTransaction(); + + when(consensusTransaction.getApplicationTransaction()).thenReturn(transactionBytes); // When state.handleConsensusRound(round, platformStateModifier, consumer); @@ -181,14 +187,13 @@ void handleConsensusRoundWithDeprecatedSystemTransaction() { assertThat(consumedSystemTransactions.size()).isZero(); } - @Test - void preHandleConsensusRoundWithApplicationTransaction() { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void preHandleConsensusRoundWithApplicationTransaction(final boolean signedTransaction) throws SignatureException { // Given givenRoundAndEvent(); - // We need to pass a transaction bigger than 10 bytes because of {@link TransactionCodec#PREAMBLE_SIZE} - final var bytes = - Bytes.wrap(longToByteArray(random.nextLong())).append(Bytes.wrap(longToByteArray(random.nextLong()))); + final var bytes = signedTransaction ? getSignedApplicationTransaction() : getUnsignedApplicationTransaction(); final var eventTransaction = new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, bytes)); final var eventCore = mock(EventCore.class); @@ -282,4 +287,24 @@ private void givenRoundAndEvent() { when(event.consensusTransactionIterator()) .thenReturn(Collections.singletonList(consensusTransaction).iterator()); } + + private Bytes getSignedApplicationTransaction() throws SignatureException { + final byte[] data = new byte[transactionSize]; + random.nextBytes(data); + + final var alg = new X25519SigningAlgorithm(); + alg.tryAcquirePrimitives(); + final var exSig = alg.signEx(data, 0, data.length); + final var sig = exSig.getSignature(); + final var transactionId = 80_000L; + return Bytes.wrap(TransactionCodec.encode(alg, transactionId, sig, data)); + } + + private Bytes getUnsignedApplicationTransaction() { + final byte[] data = new byte[transactionSize]; + random.nextBytes(data); + + final var transactionId = 80_000L; + return Bytes.wrap(TransactionCodec.encode(null, transactionId, null, data)); + } } From 7520540973bc470204fd5f825cda8fbdb4eeec1a Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 27 Dec 2024 12:16:36 +0200 Subject: [PATCH 07/18] nit: fix build Signed-off-by: Ivan Kavaldzhiev --- .../platform-apps/tests/StatsSigningTestingTool/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts index dc8e2b5b99d4..8742196fdf89 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts @@ -26,4 +26,5 @@ testModuleInfo { requires("org.assertj.core") requires("org.junit.jupiter.api") requires("org.mockito") + requires("org.junit.jupiter.params") } From c82d849411c0e0ecae2a2fcdebb20d15b6397a6b Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 27 Dec 2024 15:41:20 +0200 Subject: [PATCH 08/18] nit: resolve PR comment Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolState.java | 56 +++++++++++++++---- .../StatsSigningTestingToolStateTest.java | 2 +- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 8d5344a775db..01e2390cb107 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -205,21 +205,55 @@ private boolean areTransactionBytesSystemOnes(final Transaction transaction) { return false; } - final var transactionPool = transactionPoolSupplier.get(); + final ByteBuffer wrapper = ByteBuffer.wrap(transactionBytes.toByteArray()); + // Advance wrapper for TransactionID + wrapper.getLong(); + + // Get flag for determining signed or unsigned transaction + final var signed = wrapper.get(); + + if (signed != 0 && signed != 1) { + return true; + } + + final var signatureAlgorithmId = wrapper.get(); + + // Currently we support 1 for ED25519 and 2 for ECSecP256K1 and -1 for no signature + if (signatureAlgorithmId != -1 && signatureAlgorithmId != 1 && signatureAlgorithmId != 2) { + return true; + } - // Check the data length size is the expected size based on the transactionSize property. - // If it is not, then the transaction is a system one. + final var pkLength = wrapper.getInt(); + + int sigLength; + int dataLength; if (TransactionCodec.txIsSigned(transactionBytes)) { - // Data length can be read directly from the extracted signature - final var signature = TransactionCodec.extractSignature(transactionBytes); - return signature.getMessageLength() != transactionPool.getTransactionSize(); + if (pkLength == 0) { + return true; + } + final var pkBytes = new byte[pkLength]; + wrapper.get(pkBytes); + + sigLength = wrapper.getInt(); + + if (sigLength == 0) { + return true; + } + + final var sigBytes = new byte[sigLength]; + wrapper.get(sigBytes); } else { - // Data length value can be read from the transaction bytes using an offset - final ByteBuffer wrapper = - ByteBuffer.wrap(transactionBytes.toByteArray()).position(21); - final byte dataLength = wrapper.get(); - return dataLength != transactionPool.getTransactionSize(); + // Advance wrapper for sigLength + wrapper.getInt(); + // Skipping pkBytes and sigBytes loading since they are not present } + + dataLength = wrapper.getInt(); + final var data = new byte[dataLength]; + wrapper.get(data); + + final var transactionPool = transactionPoolSupplier.get(); + return wrapper.hasRemaining() && dataLength != transactionPool.getTransactionSize(); } private void maybeDelay() { diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index ecf9adf7de4c..7bf15fade27c 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -61,7 +61,7 @@ class StatsSigningTestingToolStateTest { - private static int transactionSize = 100; + private static final int transactionSize = 100; private Random random; private StatsSigningTestingToolState state; private PlatformStateModifier platformStateModifier; From 09bba4d74fd323617dea42b60eed13120fb9a0f1 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Tue, 7 Jan 2025 16:26:07 +0200 Subject: [PATCH 09/18] refactor: consume converted system transaction in callback instead of the transaction itself Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolState.java | 31 ++++++++++++------- .../StatsSigningTestingToolStateTest.java | 10 +++--- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 9dbf0ea5fd4f..9251825346b9 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -32,6 +32,7 @@ import com.hedera.hapi.node.base.SemanticVersion; import com.hedera.hapi.platform.event.StateSignatureTransaction; +import com.hedera.pbj.runtime.ParseException; import com.swirlds.common.constructable.ConstructableIgnored; import com.swirlds.common.crypto.CryptographyHolder; import com.swirlds.common.crypto.TransactionSignature; @@ -116,7 +117,7 @@ public synchronized StatsSigningTestingToolState copy() { @Override public void preHandle( @NonNull final Event event, - @NonNull final Consumer> stateSignatureTransaction) { + @NonNull final Consumer> stateSignatureTransactionCallback) { final SttTransactionPool sttTransactionPool = transactionPoolSupplier.get(); if (sttTransactionPool != null) { event.forEachTransaction(transaction -> { @@ -124,9 +125,8 @@ public void preHandle( return; } - if (areTransactionBytesSystemOnes(transaction)) { - stateSignatureTransaction.accept( - new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction)); + if (!transaction.isSystem() && areTransactionBytesSystemOnes(transaction)) { + consumeSystemTransaction(transaction, event, stateSignatureTransactionCallback); return; } @@ -147,13 +147,16 @@ public void preHandle( public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull final Consumer> stateSignatureTransaction) { + @NonNull final Consumer> stateSignatureTransactionCallback) { throwIfImmutable(); round.forEachEventTransaction((event, transaction) -> { + if (transaction.isSystem()) { + return; + } + if (areTransactionBytesSystemOnes(transaction)) { - stateSignatureTransaction.accept( - new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction)); + consumeSystemTransaction(transaction, event, stateSignatureTransactionCallback); } else { handleTransaction(transaction); } @@ -161,10 +164,6 @@ public void handleConsensusRound( } private void handleTransaction(final ConsensusTransaction trans) { - if (trans.isSystem()) { - return; - } - final TransactionSignature s = trans.getMetadata(); if (s != null && validateSignature(s, trans) && s.getSignatureStatus() != VerificationStatus.VALID) { @@ -256,6 +255,16 @@ private boolean areTransactionBytesSystemOnes(final Transaction transaction) { return wrapper.hasRemaining() && dataLength != transactionPool.getTransactionSize(); } + private void consumeSystemTransaction(final Transaction transaction, final Event event, final Consumer> stateSignatureTransactionCallback) { + try { + final var stateSignatureTransaction = StateSignatureTransaction.PROTOBUF.parse(transaction.getApplicationTransaction()); + stateSignatureTransactionCallback.accept( + new ScopedSystemTransaction<>(event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction)); + } catch (ParseException e) { + logger.error("Failed to parse StateSignatureTransaction", e); + } + } + private void maybeDelay() { if (SYNTHETIC_HANDLE_TIME) { final long start = System.nanoTime(); diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index 7bf15fade27c..7b969675ddb1 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -40,8 +40,8 @@ import com.swirlds.platform.event.PlatformEvent; import com.swirlds.platform.gossip.shadowgraph.Generations; import com.swirlds.platform.internal.ConsensusRound; -import com.swirlds.platform.state.MerkleStateLifecycles; import com.swirlds.platform.state.PlatformStateModifier; +import com.swirlds.platform.state.StateLifecycles; import com.swirlds.platform.system.Round; import com.swirlds.platform.system.transaction.ConsensusTransaction; import com.swirlds.platform.system.transaction.TransactionWrapper; @@ -71,15 +71,13 @@ class StatsSigningTestingToolStateTest { private List> consumedSystemTransactions; private ConsensusTransaction consensusTransaction; private StateSignatureTransaction stateSignatureTransaction; - private Supplier transactionPoolSupplier; - private SttTransactionPool transactionPool; @BeforeEach void setUp() { - transactionPool = mock(SttTransactionPool.class); - transactionPoolSupplier = mock(Supplier.class); + SttTransactionPool transactionPool = mock(SttTransactionPool.class); + Supplier transactionPoolSupplier = mock(Supplier.class); state = new StatsSigningTestingToolState( - mock(MerkleStateLifecycles.class), mock(Function.class), transactionPoolSupplier); + mock(StateLifecycles.class), mock(Function.class), transactionPoolSupplier); random = new Random(); platformStateModifier = mock(PlatformStateModifier.class); event = mock(PlatformEvent.class); From 90f12bcc009e8f4d6a248bd0371343e46d49c9e9 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Tue, 7 Jan 2025 17:52:27 +0200 Subject: [PATCH 10/18] style: spotless apply and remove unnecessary check Signed-off-by: Ivan Kavaldzhiev --- .../StatsSigningTestingTool/build.gradle.kts | 2 +- .../signing/StatsSigningTestingToolState.java | 22 +++++++++++++------ .../stats/signing/SttTransactionPool.java | 2 +- .../StatsSigningTestingToolStateTest.java | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts index 8742196fdf89..c960b7ec7229 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/build.gradle.kts @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Hedera Hashgraph, LLC + * Copyright (C) 2024-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. diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 9251825346b9..20feca3b8402 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -117,7 +117,9 @@ public synchronized StatsSigningTestingToolState copy() { @Override public void preHandle( @NonNull final Event event, - @NonNull final Consumer> stateSignatureTransactionCallback) { + @NonNull + final Consumer> + stateSignatureTransactionCallback) { final SttTransactionPool sttTransactionPool = transactionPoolSupplier.get(); if (sttTransactionPool != null) { event.forEachTransaction(transaction -> { @@ -125,7 +127,7 @@ public void preHandle( return; } - if (!transaction.isSystem() && areTransactionBytesSystemOnes(transaction)) { + if (areTransactionBytesSystemOnes(transaction)) { consumeSystemTransaction(transaction, event, stateSignatureTransactionCallback); return; } @@ -147,7 +149,9 @@ public void preHandle( public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull final Consumer> stateSignatureTransactionCallback) { + @NonNull + final Consumer> + stateSignatureTransactionCallback) { throwIfImmutable(); round.forEachEventTransaction((event, transaction) -> { @@ -255,11 +259,15 @@ private boolean areTransactionBytesSystemOnes(final Transaction transaction) { return wrapper.hasRemaining() && dataLength != transactionPool.getTransactionSize(); } - private void consumeSystemTransaction(final Transaction transaction, final Event event, final Consumer> stateSignatureTransactionCallback) { + private void consumeSystemTransaction( + final Transaction transaction, + final Event event, + final Consumer> stateSignatureTransactionCallback) { try { - final var stateSignatureTransaction = StateSignatureTransaction.PROTOBUF.parse(transaction.getApplicationTransaction()); - stateSignatureTransactionCallback.accept( - new ScopedSystemTransaction<>(event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction)); + final var stateSignatureTransaction = + StateSignatureTransaction.PROTOBUF.parse(transaction.getApplicationTransaction()); + stateSignatureTransactionCallback.accept(new ScopedSystemTransaction<>( + event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction)); } catch (ParseException e) { logger.error("Failed to parse StateSignatureTransaction", e); } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java index 1458831ee14a..94c3131fe085 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java @@ -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. diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index 7b969675ddb1..b1dd58d8b0c9 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Hedera Hashgraph, LLC + * Copyright (C) 2024-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. From c91aa49d98a720aae88a40d0197772db096f107d Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Tue, 7 Jan 2025 18:03:53 +0200 Subject: [PATCH 11/18] style: spotless apply Signed-off-by: Ivan Kavaldzhiev --- .../src/main/java/com/swirlds/platform/system/SwirldMain.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java index 34c9313afadd..8b3b11ecd4cb 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Hedera Hashgraph, LLC + * Copyright (C) 2024-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. From e8e5eae1fb0dbe61d8d06c516a2ba747a684e598 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Wed, 8 Jan 2025 14:24:05 +0200 Subject: [PATCH 12/18] nit: add final declarations and comments for elaborating business logic Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolState.java | 18 +++++++++++++++++- .../swirlds/platform/system/SwirldMain.java | 3 ++- .../fixtures/state/BlockingSwirldState.java | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 20feca3b8402..55ead2ed8a44 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -123,10 +123,18 @@ public void preHandle( final SttTransactionPool sttTransactionPool = transactionPoolSupplier.get(); if (sttTransactionPool != null) { event.forEachTransaction(transaction -> { + // We are not interested in pre-handling any system transactions, as they are + // specific for the platform only.We also don't want to consume deprecated + // EventTransaction.STATE_SIGNATURE_TRANSACTION system transactions in the + // callback,since it's intended to be used only for the new form of encoded system + // transactions in Bytes.Thus, we can directly skip the current + // iteration, if it processes a deprecated system transaction with the + // EventTransaction.STATE_SIGNATURE_TRANSACTION type. if (transaction.isSystem()) { return; } + // We should consume in the callback the new form of system transactions in Bytes if (areTransactionBytesSystemOnes(transaction)) { consumeSystemTransaction(transaction, event, stateSignatureTransactionCallback); return; @@ -155,10 +163,18 @@ public void handleConsensusRound( throwIfImmutable(); round.forEachEventTransaction((event, transaction) -> { + // We are not interested in handling any system transactions, as they are + // specific for the platform only.We also don't want to consume deprecated + // EventTransaction.STATE_SIGNATURE_TRANSACTION system transactions in the + // callback,since it's intended to be used only for the new form of encoded system + // transactions in Bytes.Thus, we can directly skip the current + // iteration, if it processes a deprecated system transaction with the + // EventTransaction.STATE_SIGNATURE_TRANSACTION type. if (transaction.isSystem()) { return; } + // We should consume in the callback the new form of system transactions in Bytes if (areTransactionBytesSystemOnes(transaction)) { consumeSystemTransaction(transaction, event, stateSignatureTransactionCallback); } else { @@ -268,7 +284,7 @@ private void consumeSystemTransaction( StateSignatureTransaction.PROTOBUF.parse(transaction.getApplicationTransaction()); stateSignatureTransactionCallback.accept(new ScopedSystemTransaction<>( event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction)); - } catch (ParseException e) { + } catch (final ParseException e) { logger.error("Failed to parse StateSignatureTransaction", e); } } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java index 8b3b11ecd4cb..e6a0fdbe4ca8 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java @@ -100,7 +100,8 @@ default List> getConfigDataTypes() { * @param transaction the {@link StateSignatureTransaction} to encode * @return {@link Bytes} representation of the transaction */ - default Bytes encodeSystemTransaction(@NonNull StateSignatureTransaction transaction) { + @NonNull + default Bytes encodeSystemTransaction(@NonNull final StateSignatureTransaction transaction) { return Bytes.EMPTY; } } diff --git a/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java b/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java index ab0586031fd8..fffe529c16ff 100644 --- a/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java +++ b/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/BlockingSwirldState.java @@ -84,7 +84,7 @@ private BlockingSwirldState(final BlockingSwirldState that) { public void handleConsensusRound( @NonNull final Round round, @NonNull final PlatformStateModifier platformState, - @NonNull final Consumer> stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransaction) { // intentionally does nothing } From 8a809cfbf35b95f74a5418fae94841a9cd4ed905 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 10 Jan 2025 14:24:00 +0200 Subject: [PATCH 13/18] refactor: simplify logic for distinguishing between system and application transactions Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolMain.java | 31 ++++++-- .../signing/StatsSigningTestingToolState.java | 73 +++++++------------ .../StatsSigningTestingToolStateTest.java | 19 +++-- 3 files changed, 57 insertions(+), 66 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java index 55fa20f3fe94..cd766d629b38 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java @@ -54,6 +54,7 @@ import com.swirlds.platform.system.Platform; import com.swirlds.platform.system.SwirldMain; import edu.umd.cs.findbugs.annotations.NonNull; +import java.nio.ByteBuffer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -65,23 +66,25 @@ 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 { try { logger.info(STARTUP.getMarker(), "Registering StatsSigningTestingToolState with ConstructableRegistry"); - ConstructableRegistry constructableRegistry = ConstructableRegistry.getInstance(); + final ConstructableRegistry constructableRegistry = ConstructableRegistry.getInstance(); constructableRegistry.registerConstructable( new ClassConstructorPair(StatsSigningTestingToolState.class, () -> { - StatsSigningTestingToolState statsSigningTestingToolState = new StatsSigningTestingToolState( - FAKE_MERKLE_STATE_LIFECYCLES, - version -> new BasicSoftwareVersion(version.major()), - () -> null); + final StatsSigningTestingToolState statsSigningTestingToolState = + new StatsSigningTestingToolState( + FAKE_MERKLE_STATE_LIFECYCLES, + version -> new BasicSoftwareVersion(version.major()), + () -> null); return statsSigningTestingToolState; })); registerMerkleStateRootClassIds(); logger.info(STARTUP.getMarker(), "StatsSigningTestingToolState is registered with ConstructableRegistry"); - } catch (ConstructableRegistryException e) { + } catch (final ConstructableRegistryException e) { logger.error(STARTUP.getMarker(), "Failed to register StatsSigningTestingToolState", e); throw new RuntimeException(e); } @@ -259,7 +262,7 @@ private synchronized void generateTransactions() { if (transPerSecToCreate > -1) { // if not unlimited (-1 means unlimited) // ramp up the TPS to the expected value - long elapsedTime = now / MILLISECONDS_TO_NANOSECONDS - rampUpStartTimeMilliSeconds; + final long elapsedTime = now / MILLISECONDS_TO_NANOSECONDS - rampUpStartTimeMilliSeconds; double rampUpTPS = 0; if (elapsedTime < TPS_RAMP_UP_WINDOW_MILLISECONDS) { rampUpTPS = expectedTPS * elapsedTime / ((double) (TPS_RAMP_UP_WINDOW_MILLISECONDS)); @@ -317,7 +320,19 @@ public BasicSoftwareVersion getSoftwareVersion() { } @Override + @NonNull public Bytes encodeSystemTransaction(@NonNull final StateSignatureTransaction transaction) { - return StateSignatureTransaction.PROTOBUF.toBytes(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()); } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 55ead2ed8a44..31d02ca728e4 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -33,6 +33,7 @@ import com.hedera.hapi.node.base.SemanticVersion; import com.hedera.hapi.platform.event.StateSignatureTransaction; import com.hedera.pbj.runtime.ParseException; +import com.hedera.pbj.runtime.io.buffer.Bytes; import com.swirlds.common.constructable.ConstructableIgnored; import com.swirlds.common.crypto.CryptographyHolder; import com.swirlds.common.crypto.TransactionSignature; @@ -225,54 +226,12 @@ private boolean areTransactionBytesSystemOnes(final Transaction transaction) { } final ByteBuffer wrapper = ByteBuffer.wrap(transactionBytes.toByteArray()); - // Advance wrapper for TransactionID - wrapper.getLong(); + final var markerSize = wrapper.getInt(); - // Get flag for determining signed or unsigned transaction - final var signed = wrapper.get(); + final var marker = new byte[markerSize]; + wrapper.get(marker); - if (signed != 0 && signed != 1) { - return true; - } - - final var signatureAlgorithmId = wrapper.get(); - - // Currently we support 1 for ED25519 and 2 for ECSecP256K1 and -1 for no signature - if (signatureAlgorithmId != -1 && signatureAlgorithmId != 1 && signatureAlgorithmId != 2) { - return true; - } - - final var pkLength = wrapper.getInt(); - - int sigLength; - int dataLength; - if (TransactionCodec.txIsSigned(transactionBytes)) { - if (pkLength == 0) { - return true; - } - final var pkBytes = new byte[pkLength]; - wrapper.get(pkBytes); - - sigLength = wrapper.getInt(); - - if (sigLength == 0) { - return true; - } - - final var sigBytes = new byte[sigLength]; - wrapper.get(sigBytes); - } else { - // Advance wrapper for sigLength - wrapper.getInt(); - // Skipping pkBytes and sigBytes loading since they are not present - } - - dataLength = wrapper.getInt(); - final var data = new byte[dataLength]; - wrapper.get(data); - - final var transactionPool = transactionPoolSupplier.get(); - return wrapper.hasRemaining() && dataLength != transactionPool.getTransactionSize(); + return StatsSigningTestingToolMain.STATE_SIGNATURE_MARKER.equals(new String(marker)); } private void consumeSystemTransaction( @@ -280,8 +239,8 @@ private void consumeSystemTransaction( final Event event, final Consumer> stateSignatureTransactionCallback) { try { - final var stateSignatureTransaction = - StateSignatureTransaction.PROTOBUF.parse(transaction.getApplicationTransaction()); + final var stateSignatureTransaction = StateSignatureTransaction.PROTOBUF.parse( + stripSystemTransactionBytes(transaction.getApplicationTransaction())); stateSignatureTransactionCallback.accept(new ScopedSystemTransaction<>( event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction)); } catch (final ParseException e) { @@ -289,6 +248,24 @@ private void consumeSystemTransaction( } } + 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(); diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index b1dd58d8b0c9..f4938ae0f732 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -64,6 +64,7 @@ class StatsSigningTestingToolStateTest { private static final int transactionSize = 100; private Random random; private StatsSigningTestingToolState state; + private StatsSigningTestingToolMain main; private PlatformStateModifier platformStateModifier; private Round round; private PlatformEvent event; @@ -74,10 +75,11 @@ class StatsSigningTestingToolStateTest { @BeforeEach void setUp() { - SttTransactionPool transactionPool = mock(SttTransactionPool.class); - Supplier transactionPoolSupplier = mock(Supplier.class); + final SttTransactionPool transactionPool = mock(SttTransactionPool.class); + final Supplier transactionPoolSupplier = mock(Supplier.class); state = new StatsSigningTestingToolState( mock(StateLifecycles.class), mock(Function.class), transactionPoolSupplier); + main = new StatsSigningTestingToolMain(); random = new Random(); platformStateModifier = mock(PlatformStateModifier.class); event = mock(PlatformEvent.class); @@ -136,8 +138,7 @@ void handleConsensusRoundWithSystemTransaction() { // Given givenRoundAndEvent(); - final var stateSignatureTransactionBytes = - StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + final var stateSignatureTransactionBytes = main.encodeSystemTransaction(stateSignatureTransaction); when(consensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); // When @@ -157,8 +158,8 @@ void handleConsensusRoundWithMultipleSystemTransaction() { .thenReturn(List.of(consensusTransaction, secondConsensusTransaction, thirdConsensusTransaction) .iterator()); - final var stateSignatureTransactionBytes = - StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + final var stateSignatureTransactionBytes = main.encodeSystemTransaction(stateSignatureTransaction); + when(consensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); when(secondConsensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); when(thirdConsensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes); @@ -211,8 +212,7 @@ void preHandleConsensusRoundWithSystemTransaction() { // Given givenRoundAndEvent(); - final var stateSignatureTransactionBytes = - StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + final var stateSignatureTransactionBytes = main.encodeSystemTransaction(stateSignatureTransaction); final var eventTransaction = new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, stateSignatureTransactionBytes)); final var eventCore = mock(EventCore.class); @@ -232,8 +232,7 @@ void preHandleConsensusRoundWithMultipleSystemTransaction() { // Given when(event.getConsensusTimestamp()).thenReturn(Instant.now()); - final var stateSignatureTransactionBytes = - StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction); + final var stateSignatureTransactionBytes = main.encodeSystemTransaction(stateSignatureTransaction); final var eventTransaction = new EventTransaction(new OneOf<>(APPLICATION_TRANSACTION, stateSignatureTransactionBytes)); From 2ab240e1a903ac0b5e3b082484f109ca8fb3065d Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 10 Jan 2025 14:26:04 +0200 Subject: [PATCH 14/18] nit: resolve PR comment Signed-off-by: Ivan Kavaldzhiev --- .../swirlds/demo/consistency/ConsistencyTestingToolState.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java b/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java index 6b9686311302..455daa66c925 100644 --- a/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java +++ b/platform-sdk/platform-apps/tests/ConsistencyTestingTool/src/main/java/com/swirlds/demo/consistency/ConsistencyTestingToolState.java @@ -245,7 +245,7 @@ private void applyTransactionToState(final @NonNull ConsensusTransaction transac @Override public void preHandle( @NonNull final Event event, - @NonNull final Consumer> stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransaction) { event.forEachTransaction(transaction -> { if (transaction.isSystem()) { return; @@ -269,7 +269,7 @@ public void preHandle( public void handleConsensusRound( final @NonNull Round round, final @NonNull PlatformStateModifier platformState, - @NonNull final Consumer> stateSignatureTransactions) { + @NonNull final Consumer> stateSignatureTransaction) { Objects.requireNonNull(round); Objects.requireNonNull(platformState); From 289fe7c7817f945120c0c9cd25c67893ab4dea0c Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 10 Jan 2025 14:33:39 +0200 Subject: [PATCH 15/18] nit: fix PR comment Signed-off-by: Ivan Kavaldzhiev --- .../stats/signing/StatsSigningTestingToolState.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 31d02ca728e4..d3b072a7b932 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -218,7 +218,7 @@ private void handleTransaction(final ConsensusTransaction trans) { * @param transaction the transaction to check * @return true if the transaction bytes are system ones, false otherwise */ - private boolean areTransactionBytesSystemOnes(final Transaction transaction) { + private boolean areTransactionBytesSystemOnes(@NonNull final Transaction transaction) { final var transactionBytes = transaction.getApplicationTransaction(); if (transactionBytes.length() == 0) { @@ -235,9 +235,11 @@ private boolean areTransactionBytesSystemOnes(final Transaction transaction) { } private void consumeSystemTransaction( - final Transaction transaction, - final Event event, - final Consumer> stateSignatureTransactionCallback) { + @NonNull final Transaction transaction, + @NonNull final Event event, + @NonNull + final Consumer> + stateSignatureTransactionCallback) { try { final var stateSignatureTransaction = StateSignatureTransaction.PROTOBUF.parse( stripSystemTransactionBytes(transaction.getApplicationTransaction())); From 32c9803f4ed5cb923eb1619428d2f92d2d1ed5c1 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Fri, 10 Jan 2025 16:03:51 +0200 Subject: [PATCH 16/18] nit: resolve PR comment Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolMain.java | 31 +++++++++-------- .../signing/StatsSigningTestingToolState.java | 33 +++---------------- .../stats/signing/SttTransactionPool.java | 8 ++--- .../demo/stats/signing/TransactionCodec.java | 21 +++++++----- .../StatsSigningTestingToolStateTest.java | 1 - 5 files changed, 36 insertions(+), 58 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java index cd766d629b38..bfea838bd911 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java @@ -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; @@ -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; @@ -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 { @@ -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; + } } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index d3b072a7b932..00711c25bdeb 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -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; @@ -225,13 +224,7 @@ private boolean areTransactionBytesSystemOnes(@NonNull final Transaction transac return false; } - 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( @@ -241,8 +234,10 @@ private void consumeSystemTransaction( final Consumer> 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) { @@ -250,24 +245,6 @@ private void consumeSystemTransaction( } } - 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(); diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java index 94c3131fe085..51dee62964d7 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/SttTransactionPool.java @@ -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) { // If we are unable to sign the transaction then log the failure and create an unsigned transaction logger.error( EXCEPTION.getMarker(), @@ -202,7 +202,7 @@ private void tryAcquirePrimitives() { if (algorithm.isAvailable()) { activeAlgorithms.put(algorithm.getId(), algorithm); } - } catch (Exception ex) { + } catch (final Exception ex) { logger.error( EXCEPTION.getMarker(), "Failed to Activate Signing Algorithm [ id = {}, class = {} ]", @@ -212,8 +212,4 @@ private void tryAcquirePrimitives() { } } } - - public int getTransactionSize() { - return transactionSize; - } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java index f7d27a361477..f9aaff4a210d 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java @@ -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. @@ -30,11 +30,11 @@ * The core transaction encoder and decoder implementation. See below for the binary transaction format specification. *

* 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 { @@ -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); diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index f4938ae0f732..651b304ecb30 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -112,7 +112,6 @@ void setUp() { .build(); when(transactionPoolSupplier.get()).thenReturn(transactionPool); - when(transactionPool.getTransactionSize()).thenReturn(transactionSize); } @ParameterizedTest From dfa1545185c12f51d8a266476c13fb45227d7fc1 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Mon, 13 Jan 2025 17:45:55 +0200 Subject: [PATCH 17/18] nit: resolve PR comments Signed-off-by: Ivan Kavaldzhiev --- .../demo/stats/signing/StatsSigningTestingToolMain.java | 9 ++++----- .../demo/stats/signing/StatsSigningTestingToolState.java | 3 ++- .../com/swirlds/demo/stats/signing/TransactionCodec.java | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java index bfea838bd911..995b320eba67 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolMain.java @@ -68,6 +68,7 @@ public class StatsSigningTestingToolMain implements SwirldMain { // the first four come from the parameters in the config.txt file + public static final byte SYSTEM_TRANSACTION_MARKER = 0; private static final Logger logger = LogManager.getLogger(StatsSigningTestingToolMain.class); static { @@ -326,16 +327,14 @@ public Bytes encodeSystemTransaction(@NonNull final StateSignatureTransaction tr 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 + // Add 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); + out.writeByte(SYSTEM_TRANSACTION_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; + throw new IllegalStateException("Failed to encode a system transaction.", e); } } } diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java index 00711c25bdeb..bbba9776a3b6 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolState.java @@ -27,6 +27,7 @@ */ import static com.swirlds.common.utility.CommonUtils.hex; +import static com.swirlds.demo.stats.signing.StatsSigningTestingToolMain.SYSTEM_TRANSACTION_MARKER; import static com.swirlds.logging.legacy.LogMarker.EXCEPTION; import static com.swirlds.logging.legacy.LogMarker.TESTING_EXCEPTIONS_ACCEPTABLE_RECONNECT; @@ -224,7 +225,7 @@ private boolean areTransactionBytesSystemOnes(@NonNull final Transaction transac return false; } - return transactionBytes.getByte(0) == (byte) 1; + return transactionBytes.getByte(0) == SYSTEM_TRANSACTION_MARKER; } private void consumeSystemTransaction( diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java index f9aaff4a210d..abbc068a47c1 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/TransactionCodec.java @@ -38,6 +38,7 @@ */ final class TransactionCodec { + public static final byte APPLICATION_TRANSACTION_MARKER = 1; public static final byte NO_ALGORITHM_PRESENT = -1; private static final int PREAMBLE_SIZE = Long.BYTES + (Byte.BYTES * 2); @@ -81,10 +82,10 @@ public static byte[] encode( 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; - // Add a 0 byte as a marker to indicate the start of an application transaction. This is used + + // Add a marker byte in the very beginning 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) + buffer.put(APPLICATION_TRANSACTION_MARKER) .putLong(transactionId) .put((signed) ? (byte) 1 : 0) .put((signed) ? algorithm.getId() : NO_ALGORITHM_PRESENT) From 3a1cead1dbeceda41cad6a49014b8301fbdb5098 Mon Sep 17 00:00:00 2001 From: Ivan Kavaldzhiev Date: Wed, 15 Jan 2025 13:57:40 +0200 Subject: [PATCH 18/18] style: spotless apply Signed-off-by: Ivan Kavaldzhiev --- .../signing/StatsSigningTestingToolStateLifecycles.java | 4 ++-- .../demo/stats/signing/StatsSigningTestingToolStateTest.java | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateLifecycles.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateLifecycles.java index 98cd4f076236..8bccbc435d42 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateLifecycles.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/main/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateLifecycles.java @@ -187,8 +187,8 @@ private void consumeSystemTransaction( @NonNull final Transaction transaction, @NonNull final Event event, @NonNull - final Consumer> - stateSignatureTransactionCallback) { + final Consumer> + stateSignatureTransactionCallback) { try { final Bytes transactionBytes = transaction.getApplicationTransaction(); final Bytes strippedSystemTransactionBytes = transactionBytes.slice(1, transactionBytes.length() - 1); diff --git a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java index 83b6f5d95138..705d431da603 100644 --- a/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java +++ b/platform-sdk/platform-apps/tests/StatsSigningTestingTool/src/test/java/com/swirlds/demo/stats/signing/StatsSigningTestingToolStateTest.java @@ -40,8 +40,6 @@ import com.swirlds.platform.event.PlatformEvent; import com.swirlds.platform.gossip.shadowgraph.Generations; import com.swirlds.platform.internal.ConsensusRound; -import com.swirlds.platform.state.PlatformStateModifier; -import com.swirlds.platform.state.StateLifecycles; import com.swirlds.platform.system.Round; import com.swirlds.platform.system.SoftwareVersion; import com.swirlds.platform.system.transaction.ConsensusTransaction; @@ -79,8 +77,7 @@ void setUp() { final SttTransactionPool transactionPool = mock(SttTransactionPool.class); final Supplier transactionPoolSupplier = mock(Supplier.class); final Function versionFactory = mock(Function.class); - state = new StatsSigningTestingToolState( - versionFactory); + state = new StatsSigningTestingToolState(versionFactory); stateLifecycles = new StatsSigningTestingToolStateLifecycles(transactionPoolSupplier); main = new StatsSigningTestingToolMain(); random = new Random();