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: enhance migration testing tool #17246

Merged
merged 15 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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,9 +1,26 @@
// 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.migration.MigrationTestingToolMain"

testModuleInfo {
requires("org.junit.jupiter.api")
requires("org.assertj.core")
requires("org.junit.jupiter.params")
requires("org.mockito")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static com.swirlds.platform.test.fixtures.state.FakeStateLifecycles.registerMerkleStateRootClassIds;

import com.hedera.hapi.node.state.roster.RosterEntry;
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 @@ -207,4 +209,9 @@
public BasicSoftwareVersion getSoftwareVersion() {
return softwareVersion;
}

@Override
public Bytes encodeSystemTransaction(@NonNull StateSignatureTransaction transaction) {
mustafauzunn marked this conversation as resolved.
Show resolved Hide resolved
return StateSignatureTransaction.PROTOBUF.toBytes(transaction);

Check warning on line 215 in platform-sdk/platform-apps/tests/MigrationTestingTool/src/main/java/com/swirlds/demo/migration/MigrationTestingToolMain.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/MigrationTestingTool/src/main/java/com/swirlds/demo/migration/MigrationTestingToolMain.java#L215

Added line #L215 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.swirlds.demo.migration;

import static com.swirlds.demo.migration.MigrationTestingToolMain.PREVIOUS_SOFTWARE_VERSION;
import static com.swirlds.demo.migration.TransactionUtils.isSystemTransaction;
import static com.swirlds.logging.legacy.LogMarker.STARTUP;
import static com.swirlds.platform.test.fixtures.state.FakeStateLifecycles.FAKE_MERKLE_STATE_LIFECYCLES;

Expand Down Expand Up @@ -47,6 +48,7 @@
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.address.AddressBook;
import com.swirlds.platform.system.events.ConsensusEvent;
import com.swirlds.platform.system.events.Event;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.virtualmap.VirtualMap;
import com.swirlds.virtualmap.datasource.VirtualDataSourceBuilder;
Expand Down Expand Up @@ -278,6 +280,18 @@ public void init(
}
}

@Override
public void preHandle(
@NonNull Event event,
@NonNull Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
mustafauzunn marked this conversation as resolved.
Show resolved Hide resolved
event.forEachTransaction(transaction -> {
if (!transaction.isSystem() && isSystemTransaction(transaction.getApplicationTransaction())) {
mustafauzunn marked this conversation as resolved.
Show resolved Hide resolved
stateSignatureTransaction.accept(
new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), transaction));
}
});
}

/**
* {@inheritDoc}
*/
Expand All @@ -295,6 +309,12 @@ public void handleConsensusRound(
if (trans.isSystem()) {
continue;
}
if (isSystemTransaction(trans.getApplicationTransaction())) {
stateSignatureTransaction.accept(
new ScopedSystemTransaction(event.getCreatorId(), event.getSoftwareVersion(), event));
continue;
}

final MigrationTestingToolTransaction mTrans =
TransactionUtils.parseTransaction(trans.getApplicationTransaction());
mTrans.applyTo(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,4 +38,16 @@ public class TransactionUtils {
throw new UncheckedIOException("Could not parse transaction kind:%s".formatted(bytes.toHex()), e);
}
}

public static boolean isSystemTransaction(@NonNull final Bytes bytes) {
final SerializableDataInputStream in = new SerializableDataInputStream(bytes.toInputStream());

try {
mustafauzunn marked this conversation as resolved.
Show resolved Hide resolved
MigrationTestingToolTransaction transaction =
in.readSerializable(false, MigrationTestingToolTransaction::new);
return false;
} catch (final IOException e) {
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* 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.
*/

package com.swirlds.demo.migration;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.hedera.hapi.node.base.Timestamp;
import com.hedera.hapi.platform.event.EventCore;
import com.hedera.hapi.platform.event.EventTransaction;
import com.hedera.hapi.platform.event.EventTransaction.TransactionOneOfType;
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.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.event.PlatformEvent;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.state.StateLifecycles;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.events.ConsensusEvent;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.platform.system.transaction.Transaction;
import com.swirlds.platform.system.transaction.TransactionWrapper;
import java.security.SignatureException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class MigrationTestingToolStateTest {
mustafauzunn marked this conversation as resolved.
Show resolved Hide resolved
private static MigrationTestingToolState state;
mustafauzunn marked this conversation as resolved.
Show resolved Hide resolved
private Random random;
private PlatformStateModifier platformStateModifier;
private Round round;
private ConsensusEvent event;
private List<ScopedSystemTransaction<StateSignatureTransaction>> consumedTransactions;
private Consumer<ScopedSystemTransaction<StateSignatureTransaction>> consumer;
private Transaction transaction;
private StateSignatureTransaction stateSignatureTransaction;

@BeforeEach
void setUp() {
state = new MigrationTestingToolState(mock(StateLifecycles.class), mock(Function.class));
random = new Random();
round = mock(Round.class);
event = mock(ConsensusEvent.class);

consumedTransactions = new ArrayList<>();
consumer = systemTransaction -> consumedTransactions.add(systemTransaction);
transaction = 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();
}

@Test
void handleConsensusRoundWithApplicationTransaction() throws SignatureException {
givenRoundAndEvent();
final TransactionGenerator generator = new TransactionGenerator(5);
final var bytes = Bytes.wrap(generator.generateTransaction());
final var tr = TransactionUtils.parseTransaction(bytes);
when(transaction.getApplicationTransaction()).thenReturn(bytes);

try (MockedStatic<TransactionUtils> utilities =
Mockito.mockStatic(TransactionUtils.class, Mockito.CALLS_REAL_METHODS)) {
MigrationTestingToolTransaction migrationTestingToolTransaction = Mockito.spy(tr);
utilities.when(() -> TransactionUtils.parseTransaction(any())).thenReturn(migrationTestingToolTransaction);
Mockito.doNothing().when(migrationTestingToolTransaction).applyTo(state);
state.handleConsensusRound(round, platformStateModifier, consumer);
}

assertThat(consumedTransactions).isEmpty();
}

@Test
void handleConsensusRoundWithSystemTransaction() {
givenRoundAndEvent();
final var stateSignatureTransactionBytes =
StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction);
when(transaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes);

state.handleConsensusRound(round, platformStateModifier, consumer);

assertThat(consumedTransactions).hasSize(1);
}

@Test
void handleConsensusRoundWithMultipleSystemTransactions() {
final var secondConsensusTransaction = mock(TransactionWrapper.class);
final var thirdConsensusTransaction = mock(TransactionWrapper.class);
when(round.iterator()).thenReturn(Collections.singletonList(event).iterator());
when(event.getConsensusTimestamp()).thenReturn(Instant.now());
when(event.consensusTransactionIterator())
.thenReturn(List.of(
(ConsensusTransaction) transaction,
secondConsensusTransaction,
thirdConsensusTransaction)
.iterator());

final var stateSignatureTransactionBytes =
StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction);
when(transaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes);
when(secondConsensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes);
when(thirdConsensusTransaction.getApplicationTransaction()).thenReturn(stateSignatureTransactionBytes);

state.handleConsensusRound(round, platformStateModifier, consumer);

assertThat(consumedTransactions).hasSize(3);
}

@Test
void handleConsensusRoundWithDeprecatedSystemTransaction() {
givenRoundAndEvent();
when(transaction.getApplicationTransaction()).thenReturn(Bytes.EMPTY);
when(transaction.isSystem()).thenReturn(true);

state.handleConsensusRound(round, platformStateModifier, consumer);

assertThat(consumedTransactions).isEmpty();
}

@Test
void preHandleEventWithMultipleSystemTransactions() {
final var gossipEvent = mock(GossipEvent.class);
final var eventCore = mock(EventCore.class);
when(gossipEvent.eventCore()).thenReturn(eventCore);
when(eventCore.timeCreated()).thenReturn(Timestamp.DEFAULT);
final var eventTransaction = mock(EventTransaction.class);
final var secondEventTransaction = mock(EventTransaction.class);
final var thirdEventTransaction = mock(EventTransaction.class);

final var stateSignatureTransactionBytes =
StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction);
final var transactionProto = com.hedera.hapi.node.base.Transaction.newBuilder()
.bodyBytes(stateSignatureTransactionBytes)
.build();
final var transactionBytes = com.hedera.hapi.node.base.Transaction.PROTOBUF.toBytes(transactionProto);

final var systemTransactionWithType =
new OneOf<>(TransactionOneOfType.APPLICATION_TRANSACTION, transactionBytes);

when(eventTransaction.transaction()).thenReturn(systemTransactionWithType);
when(secondEventTransaction.transaction()).thenReturn(systemTransactionWithType);
when(thirdEventTransaction.transaction()).thenReturn(systemTransactionWithType);
when(gossipEvent.eventTransaction())
.thenReturn(List.of(eventTransaction, secondEventTransaction, thirdEventTransaction));
event = new PlatformEvent(gossipEvent);

state.preHandle(event, consumer);

assertThat(consumedTransactions).hasSize(3);
}

@Test
void preHandleEventWithSystemTransaction() {
final var gossipEvent = mock(GossipEvent.class);
final var eventCore = mock(EventCore.class);
when(eventCore.timeCreated()).thenReturn(Timestamp.DEFAULT);
final var eventTransaction = mock(EventTransaction.class);
when(gossipEvent.eventCore()).thenReturn(eventCore);
when(gossipEvent.eventTransaction()).thenReturn(List.of(eventTransaction));

final var stateSignatureTransactionBytes =
StateSignatureTransaction.PROTOBUF.toBytes(stateSignatureTransaction);
final var transactionProto = com.hedera.hapi.node.base.Transaction.newBuilder()
.bodyBytes(stateSignatureTransactionBytes)
.build();
final var transactionBytes = com.hedera.hapi.node.base.Transaction.PROTOBUF.toBytes(transactionProto);
final var systemTransactionWithType =
new OneOf<>(TransactionOneOfType.APPLICATION_TRANSACTION, transactionBytes);
when(eventTransaction.transaction()).thenReturn(systemTransactionWithType);
event = new PlatformEvent(gossipEvent);

state.preHandle(event, consumer);

assertThat(consumedTransactions).hasSize(1);
}

@Test
void preHandleEventWithDeprecatedSystemTransaction() {
event = mock(PlatformEvent.class);

when(round.iterator()).thenReturn(Collections.singletonList(event).iterator());
when(transaction.isSystem()).thenReturn(true);

state.preHandle(event, consumer);

assertThat(consumedTransactions).isEmpty();
}

private void givenRoundAndEvent() {
when(round.iterator()).thenReturn(Collections.singletonList(event).iterator());
when(event.getConsensusTimestamp()).thenReturn(Instant.now());
when(event.consensusTransactionIterator())
.thenReturn(Collections.singletonList((ConsensusTransaction) transaction)
.iterator());
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,6 +16,8 @@

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.PlatformMerkleStateRoot;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -91,4 +93,14 @@
*/
@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;

Check warning on line 104 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/SwirldMain.java#L104

Added line #L104 was not covered by tests
}
}