|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package org.hiero.otter.fixtures.turtle.app; |
| 3 | + |
| 4 | +import com.hedera.hapi.node.base.Timestamp; |
| 5 | +import com.hedera.hapi.platform.event.StateSignatureTransaction; |
| 6 | +import com.hedera.node.app.hapi.utils.CommonPbjConverters; |
| 7 | +import com.hedera.pbj.runtime.io.buffer.Bytes; |
| 8 | +import com.swirlds.platform.state.service.WritablePlatformStateStore; |
| 9 | +import com.swirlds.platform.test.fixtures.turtle.runner.TurtleTestingToolState; |
| 10 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 11 | +import java.util.function.Consumer; |
| 12 | +import org.hiero.base.utility.CommonUtils; |
| 13 | +import org.hiero.consensus.model.event.Event; |
| 14 | +import org.hiero.consensus.model.transaction.ScopedSystemTransaction; |
| 15 | + |
| 16 | +/** |
| 17 | + * Utility class to handle transactions in the Turtle testing tool. |
| 18 | + */ |
| 19 | +public class TransactionHandlers { |
| 20 | + |
| 21 | + private TransactionHandlers() {} |
| 22 | + |
| 23 | + /** |
| 24 | + * Handles the transaction based on its type. |
| 25 | + * |
| 26 | + * @param state the current state of the Turtle testing tool |
| 27 | + * @param event the event associated with the transaction |
| 28 | + * @param transaction the transaction to handle |
| 29 | + * @param callback the callback to invoke with the new ScopedSystemTransaction |
| 30 | + */ |
| 31 | + public static void handleTransaction( |
| 32 | + @NonNull final TurtleTestingToolState state, |
| 33 | + @NonNull final Event event, |
| 34 | + @NonNull final TurtleTransaction transaction, |
| 35 | + @NonNull Consumer<ScopedSystemTransaction<StateSignatureTransaction>> callback) { |
| 36 | + switch (transaction.getDataCase()) { |
| 37 | + case FREEZETRANSACTION -> handleFreeze(state, transaction.getFreezeTransaction()); |
| 38 | + case STATESIGNATURETRANSACTION -> |
| 39 | + handleStateSignature(event, transaction.getStateSignatureTransaction(), callback); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Handles the freeze transaction by updating the freeze time in the platform state. |
| 45 | + * |
| 46 | + * @param state the current state of the Turtle testing tool |
| 47 | + * @param freezeTransaction the freeze transaction to handle |
| 48 | + */ |
| 49 | + public static void handleFreeze( |
| 50 | + @NonNull final TurtleTestingToolState state, @NonNull final TurtleFreezeTransaction freezeTransaction) { |
| 51 | + final Timestamp freezeTime = CommonPbjConverters.toPbj(freezeTransaction.getFreezeTime()); |
| 52 | + WritablePlatformStateStore store = |
| 53 | + new WritablePlatformStateStore(state.getWritableStates("PlatformStateService")); |
| 54 | + store.setFreezeTime(CommonUtils.fromPbjTimestamp(freezeTime)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Handles the state signature transaction by creating a new ScopedSystemTransaction and passing it to the callback. |
| 59 | + * |
| 60 | + * @param event the event associated with the transaction |
| 61 | + * @param transaction the state signature transaction to handle |
| 62 | + * @param callback the callback to invoke with the new ScopedSystemTransaction |
| 63 | + */ |
| 64 | + public static void handleStateSignature( |
| 65 | + @NonNull final Event event, |
| 66 | + @NonNull final com.hedera.hapi.platform.event.legacy.StateSignatureTransaction transaction, |
| 67 | + @NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> callback) { |
| 68 | + final StateSignatureTransaction newTransaction = new StateSignatureTransaction( |
| 69 | + transaction.getRound(), |
| 70 | + Bytes.wrap(transaction.getSignature().toByteArray()), |
| 71 | + Bytes.wrap(transaction.getHash().toByteArray())); |
| 72 | + callback.accept( |
| 73 | + new ScopedSystemTransaction<>(event.getCreatorId(), event.getSoftwareVersion(), newTransaction)); |
| 74 | + } |
| 75 | +} |
0 commit comments