Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: adapt AddressBookTestingTool to support transaction Bytes #17197

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 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.
* 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") }

application.mainClass = "com.swirlds.demo.addressbook.AddressBookTestingToolMain"

testModuleInfo {
requires("org.assertj.core")
requires("org.junit.jupiter.api")
requires("org.mockito")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static com.swirlds.platform.test.fixtures.state.FakeStateLifecycles.FAKE_MERKLE_STATE_LIFECYCLES;
import static com.swirlds.platform.test.fixtures.state.FakeStateLifecycles.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;
Expand Down Expand Up @@ -163,4 +165,9 @@
logger.info(STARTUP.getMarker(), "returning software version {}", softwareVersion);
return softwareVersion;
}

@Override
public Bytes encodeSystemTransaction(@NonNull final StateSignatureTransaction transaction) {
return StateSignatureTransaction.PROTOBUF.toBytes(transaction);

Check warning on line 171 in platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolMain.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolMain.java#L171

Added line #L171 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
import com.swirlds.platform.system.address.AddressBook;
import com.swirlds.platform.system.address.AddressBookUtils;
import com.swirlds.platform.system.events.ConsensusEvent;
import com.swirlds.platform.system.events.Event;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.platform.system.transaction.Transaction;
import com.swirlds.state.merkle.singleton.StringLeaf;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand All @@ -70,7 +72,6 @@
import java.nio.file.Path;
import java.text.ParseException;
import java.time.Duration;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -214,6 +215,17 @@
logger.info(STARTUP.getMarker(), "Registered PlatformService and RosterService states.");
}

@Override
public void preHandle(
@NonNull Event event,
@NonNull Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
event.transactionIterator().forEachRemaining(transaction -> {
if (!transaction.isSystem() && areTransactionBytesSystemOnes(transaction)) {
poulok marked this conversation as resolved.
Show resolved Hide resolved
consumeSystemTransaction(transaction, event, stateSignatureTransaction);
}
});
}

/**
* {@inheritDoc}
*/
Expand All @@ -238,11 +250,18 @@
roundsHandled++;
setChild(ROUND_HANDLED_INDEX, new StringLeaf(Long.toString(roundsHandled)));

final Iterator<ConsensusEvent> eventIterator = round.iterator();
for (ConsensusEvent event : round) {
poulok marked this conversation as resolved.
Show resolved Hide resolved
event.consensusTransactionIterator().forEachRemaining(transaction -> {
if (transaction.isSystem()) {
return;
}

while (eventIterator.hasNext()) {
final ConsensusEvent event = eventIterator.next();
event.consensusTransactionIterator().forEachRemaining(this::handleTransaction);
if (areTransactionBytesSystemOnes(transaction)) {
consumeSystemTransaction(transaction, event, stateSignatureTransaction);
} else {
handleTransaction(transaction);
}
});
}

if (!validationPerformed.getAndSet(true)) {
Expand All @@ -255,15 +274,44 @@
}
}

/**
* Checks if the transaction bytes are system ones. The test creates application transactions with max length of 4.
* System transactions will be always bigger than that.
*
* @param transaction the consensus transaction to check
* @return true if the transaction bytes are system ones, false otherwise
*/
private boolean areTransactionBytesSystemOnes(final Transaction transaction) {
return transaction.getApplicationTransaction().length() > 4;
}

/**
* Converts a transaction to a {@link StateSignatureTransaction} and then consumes it into a callback.
*
* @param transaction the transaction to consume
* @param event the event that contains the transaction
* @param stateSignatureTransactionCallback the callback to call with the system transaction
*/
private void consumeSystemTransaction(
final Transaction transaction,
final Event event,
final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransactionCallback) {
try {
final var stateSignatureTransaction =
StateSignatureTransaction.PROTOBUF.parse(transaction.getApplicationTransaction());
stateSignatureTransactionCallback.accept(new ScopedSystemTransaction<>(
event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction));
} catch (com.hedera.pbj.runtime.ParseException e) {
poulok marked this conversation as resolved.
Show resolved Hide resolved
logger.error("Failed to parse StateSignatureTransaction", e);

Check warning on line 305 in platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/AddressBookTestingTool/src/main/java/com/swirlds/demo/addressbook/AddressBookTestingToolState.java#L304-L305

Added lines #L304 - L305 were not covered by tests
}
}

/**
* Apply a transaction to the state.
*
* @param transaction the transaction to apply
*/
private void handleTransaction(@NonNull final ConsensusTransaction transaction) {
if (transaction.isSystem()) {
return;
}
final int delta =
ByteUtils.byteArrayToInt(transaction.getApplicationTransaction().toByteArray(), 0);
runningSum += delta;
Expand Down
Loading