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 SwirldState preHandle and handleConsensusRound methods with consumer for system transactions #16897

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -28,10 +28,12 @@

import com.hedera.hapi.node.base.SemanticVersion;
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.ConstructableIgnored;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.SwirldsPlatform;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
Expand All @@ -50,6 +52,7 @@
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -205,7 +208,12 @@ public synchronized CryptocurrencyDemoState copy() {
}

@Override
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
throwIfImmutable();
round.forEachEventTransaction((event, transaction) -> handleTransaction(event.getCreatorId(), transaction));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
*/

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.PlatformStateModifier;
Expand All @@ -38,6 +40,7 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -105,7 +108,12 @@ private HelloSwirldDemoState(final HelloSwirldDemoState sourceState) {
}

@Override
public synchronized void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
public synchronized void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
*/

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SoftwareVersion;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -76,7 +80,12 @@ private StatsDemoState(final StatsDemoState sourceState) {
}

@Override
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {}
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {}

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.common.context.PlatformContext;
import com.swirlds.common.platform.NodeId;
import com.swirlds.common.utility.ByteUtils;
import com.swirlds.common.utility.StackTrace;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.config.AddressBookConfig;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.roster.RosterUtils;
Expand All @@ -68,9 +70,11 @@
import java.text.ParseException;
import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -206,7 +210,12 @@ public void init(
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(@NonNull final Round round, @NonNull final PlatformStateModifier platformState) {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
Objects.requireNonNull(round, "the round cannot be null");
Objects.requireNonNull(platformState, "the platform state cannot be null");
throwIfImmutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import static com.swirlds.platform.test.fixtures.state.FakeMerkleStateLifecycles.FAKE_MERKLE_STATE_LIFECYCLES;

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.swirlds.common.config.StateCommonConfig;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.common.utility.NonCryptographicHashing;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.PlatformStateModifier;
Expand All @@ -42,9 +44,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -239,7 +243,11 @@ private void applyTransactionToState(final @NonNull ConsensusTransaction transac
* Keeps track of which transactions have been prehandled.
*/
@Override
public void preHandle(@NonNull final Event event) {
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
event.forEachTransaction(transaction -> {
if (transaction.isSystem()) {
return;
Expand All @@ -260,7 +268,12 @@ public void preHandle(@NonNull final Event event) {
* Writes the round and its contents to a log on disk
*/
@Override
public void handleConsensusRound(final @NonNull Round round, final @NonNull PlatformStateModifier platformState) {
public void handleConsensusRound(
final @NonNull Round round,
final @NonNull PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
Objects.requireNonNull(round);
Objects.requireNonNull(platformState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.hapi.node.state.roster.RosterEntry;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.common.io.SelfSerializable;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.merkle.utility.SerializableLong;
import com.swirlds.common.platform.NodeId;
import com.swirlds.common.utility.ByteUtils;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.scratchpad.Scratchpad;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
Expand Down Expand Up @@ -69,6 +71,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -236,7 +239,12 @@ <T extends SelfSerializable> void writeObjectByChildIndex(int index, List<T> lis
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
throwIfImmutable();
final Iterator<ConsensusEvent> eventIterator = round.iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.swirlds.logging.legacy.LogMarker.STARTUP;

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.common.crypto.DigestType;
import com.swirlds.common.merkle.MerkleNode;
Expand All @@ -35,6 +36,7 @@
import com.swirlds.merkledb.MerkleDbDataSourceBuilder;
import com.swirlds.merkledb.MerkleDbTableConfig;
import com.swirlds.merkledb.config.MerkleDbConfig;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.PlatformStateModifier;
Expand All @@ -51,6 +53,7 @@
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -271,7 +274,12 @@ public void init(
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
throwIfImmutable();
for (final Iterator<ConsensusEvent> eventIt = round.iterator(); eventIt.hasNext(); ) {
final ConsensusEvent event = eventIt.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.protobuf.InvalidProtocolBufferException;
import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.swirlds.common.constructable.*;
import com.swirlds.common.crypto.CryptographyHolder;
import com.swirlds.common.crypto.SignatureType;
Expand Down Expand Up @@ -84,6 +85,7 @@
import com.swirlds.merkle.test.fixtures.map.pta.MapKey;
import com.swirlds.platform.ParameterProvider;
import com.swirlds.platform.Utilities;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
Expand Down Expand Up @@ -116,6 +118,7 @@
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -1048,7 +1051,12 @@ protected void preHandleTransaction(final Transaction transaction) {
}

@Override
public synchronized void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
public synchronized void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
throwIfImmutable();
if (!initialized.get()) {
throw new IllegalStateException("handleConsensusRound() called before init()");
Expand Down Expand Up @@ -1647,7 +1655,11 @@ private static class ChildIndices {
}

@Override
public void preHandle(final Event event) {
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
event.forEachTransaction(this::preHandleTransaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import static com.swirlds.logging.legacy.LogMarker.TESTING_EXCEPTIONS_ACCEPTABLE_RECONNECT;

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.common.crypto.CryptographyHolder;
import com.swirlds.common.crypto.TransactionSignature;
import com.swirlds.common.crypto.VerificationStatus;
import com.swirlds.platform.components.transaction.system.ScopedSystemTransaction;
import com.swirlds.platform.state.MerkleStateLifecycles;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.PlatformStateModifier;
Expand All @@ -49,6 +51,7 @@
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -110,7 +113,11 @@ public synchronized StatsSigningTestingToolState copy() {
* {@inheritDoc}
*/
@Override
public void preHandle(final Event event) {
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
final SttTransactionPool sttTransactionPool = transactionPoolSupplier.get();
if (sttTransactionPool != null) {
event.forEachTransaction(transaction -> {
Expand All @@ -131,7 +138,12 @@ public void preHandle(final Event event) {
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Loading