Skip to content

Commit

Permalink
refactor: enhance consistency testing tool (#17136)
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Uzun <mustafa.uzun@limechain.tech>
  • Loading branch information
mustafauzunn authored Jan 13, 2025
1 parent 04f8074 commit 4f9e972
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
// 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.consistency.ConsistencyTestingToolMain"
Expand All @@ -7,6 +22,7 @@ mainModuleInfo { annotationProcessor("com.swirlds.config.processor") }

testModuleInfo {
requires("com.swirlds.common.test.fixtures")
requires("org.assertj.core")
requires("org.junit.jupiter.api")
requires("org.mockito")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -139,4 +141,9 @@ public SoftwareVersion getSoftwareVersion() {
public List<Class<? extends Record>> getConfigDataTypes() {
return List.of(ConsistencyTestingToolConfig.class);
}

@Override
public Bytes encodeSystemTransaction(final @NonNull StateSignatureTransaction transaction) {
return StateSignatureTransaction.PROTOBUF.toBytes(transaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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.config.StateCommonConfig;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.common.utility.NonCryptographicHashing;
Expand All @@ -36,6 +37,7 @@
import com.swirlds.platform.system.SoftwareVersion;
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 Down Expand Up @@ -250,6 +252,11 @@ public void preHandle(
if (transaction.isSystem()) {
return;
}

if (isSystemTransaction(transaction)) {
consumeSystemTransaction(transaction, event, stateSignatureTransaction);
return;
}
final long transactionContents =
byteArrayToLong(transaction.getApplicationTransaction().toByteArray(), 0);

Expand Down Expand Up @@ -284,12 +291,43 @@ public void handleConsensusRound(

roundsHandled++;

round.forEachTransaction(this::applyTransactionToState);
round.forEachEventTransaction((ev, tx) -> {
if (isSystemTransaction(tx)) {
consumeSystemTransaction(tx, ev, stateSignatureTransaction);
} else {
applyTransactionToState(tx);
}
});
stateLong = NonCryptographicHashing.hash64(stateLong, round.getRoundNum());

transactionHandlingHistory.processRound(ConsistencyTestingToolRound.fromRound(round, stateLong));

setChild(ROUND_HANDLED_INDEX, new StringLeaf(Long.toString(roundsHandled)));
setChild(STATE_LONG_INDEX, new StringLeaf(Long.toString(stateLong)));
}

/**
* Determines if the given transaction is a system transaction for this app.
*
* @param transaction the transaction to check
* @return true if the transaction is a system transaction, false otherwise
*/
private boolean isSystemTransaction(final @NonNull Transaction transaction) {
return transaction.getApplicationTransaction().length() > 8;
}

private void consumeSystemTransaction(
final @NonNull Transaction transaction,
final @NonNull Event event,
final @NonNull Consumer<ScopedSystemTransaction<StateSignatureTransaction>>
stateSignatureTransactionCallback) {
try {
final var stateSignatureTransaction =
StateSignatureTransaction.PROTOBUF.parse(transaction.getApplicationTransaction());
stateSignatureTransactionCallback.accept(new ScopedSystemTransaction<>(
event.getCreatorId(), event.getSoftwareVersion(), stateSignatureTransaction));
} catch (final ParseException e) {
logger.error("Failed to parse StateSignatureTransaction", e);
}
}
}
Loading

0 comments on commit 4f9e972

Please sign in to comment.