Skip to content

refactor: adapt AddressBookTestingTool to support transaction Bytes #17197

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

Merged
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ public BasicSoftwareVersion getSoftwareVersion() {
logger.info(STARTUP.getMarker(), "returning software version {}", softwareVersion);
return softwareVersion;
}

@Override
public Bytes encodeSystemTransaction(@NonNull final StateSignatureTransaction transaction) {
return StateSignatureTransaction.PROTOBUF.toBytes(transaction);
}
}
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,18 @@ public void init(
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)) {
stateSignatureTransaction.accept(
new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction));
}
});
}

/**
* {@inheritDoc}
*/
Expand All @@ -238,11 +251,15 @@ public void handleConsensusRound(
roundsHandled++;
setChild(ROUND_HANDLED_INDEX, new StringLeaf(Long.toString(roundsHandled)));

final Iterator<ConsensusEvent> eventIterator = round.iterator();

while (eventIterator.hasNext()) {
final ConsensusEvent event = eventIterator.next();
event.consensusTransactionIterator().forEachRemaining(this::handleTransaction);
for (ConsensusEvent event : round) {
event.consensusTransactionIterator().forEachRemaining(transaction -> {
if (areTransactionBytesSystemOnes(transaction)) {
stateSignatureTransaction.accept(
new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction));
} else {
handleTransaction(transaction);
}
});
}

if (!validationPerformed.getAndSet(true)) {
Expand All @@ -255,6 +272,17 @@ public void handleConsensusRound(
}
}

/**
* 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;
}

/**
* Apply a transaction to the state.
*
Expand Down
Loading
Loading