Skip to content

Commit

Permalink
refactor: consumer for system transactions (#17140)
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Uzun <[email protected]>
  • Loading branch information
mustafauzunn authored Dec 20, 2024
1 parent ca2e9a8 commit 89e2f35
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ public synchronized CryptocurrencyDemoState copy() {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
throwIfImmutable();
round.forEachEventTransaction((event, transaction) -> handleTransaction(event.getCreatorId(), transaction));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ private HelloSwirldDemoState(final HelloSwirldDemoState sourceState) {
public synchronized void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
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 @@ -83,9 +82,7 @@ private StatsDemoState(final StatsDemoState sourceState) {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {}
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {}

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
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;
Expand Down Expand Up @@ -222,9 +221,7 @@ public void init(
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
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 @@ -44,7 +44,6 @@
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;
Expand Down Expand Up @@ -246,9 +245,7 @@ private void applyTransactionToState(final @NonNull ConsensusTransaction transac
@Override
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
event.forEachTransaction(transaction -> {
if (transaction.isSystem()) {
return;
Expand All @@ -272,9 +269,7 @@ public void preHandle(
public void handleConsensusRound(
final @NonNull Round round,
final @NonNull PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
Objects.requireNonNull(round);
Objects.requireNonNull(platformState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ <T extends SelfSerializable> void writeObjectByChildIndex(int index, List<T> lis
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
throwIfImmutable();
final Iterator<ConsensusEvent> eventIterator = round.iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,7 @@ public void init(
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
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 @@ -1056,9 +1056,7 @@ protected void preHandleTransaction(final Transaction transaction) {
public synchronized void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
throwIfImmutable();
if (!initialized.get()) {
throw new IllegalStateException("handleConsensusRound() called before init()");
Expand Down Expand Up @@ -1662,9 +1660,7 @@ private static class ChildIndices {
@Override
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
event.forEachTransaction(this::preHandleTransaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ public synchronized StatsSigningTestingToolState copy() {
@Override
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
final SttTransactionPool sttTransactionPool = transactionPoolSupplier.get();
if (sttTransactionPool != null) {
event.forEachTransaction(transaction -> {
Expand All @@ -141,9 +139,7 @@ public void preHandle(
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.time.Duration;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -103,9 +102,7 @@ public void init(
@Override
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
busyWait(config.preHandleTime());
}

Expand All @@ -116,9 +113,7 @@ public void preHandle(
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public class DefaultTransactionPrehandler implements TransactionPrehandler {
private static final Logger logger = LogManager.getLogger(DefaultTransactionPrehandler.class);

public static final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>> NO_OP_CONSUMER =
public static final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> NO_OP_CONSUMER =
systemTransactions -> {};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.swirlds.state.spi.WritableStates;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -153,9 +152,7 @@ protected PlatformMerkleStateRoot copyingConstructor() {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
throwIfImmutable();
lifecycles.onHandleConsensusRound(round, this);
}
Expand All @@ -173,9 +170,7 @@ public void sealConsensusRound(@NonNull final Round round) {
@Override
public void preHandle(
@NonNull final Event event,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
lifecycles.onPreHandle(event, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void setInitialState(@NonNull final MerkleRoot state) {

/**
* Handles the events in a consensus round. Implementations are responsible for invoking
* {@link SwirldState#handleConsensusRound(Round, PlatformStateModifier, Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>)}.
* {@link SwirldState#handleConsensusRound(Round, PlatformStateModifier, Consumer<ScopedSystemTransaction<StateSignatureTransaction>>)}.
*
* @param round the round to handle
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.swirlds.platform.system.transaction.Transaction;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

Expand Down Expand Up @@ -72,25 +71,25 @@ default void init(
* <strong>This method is always invoked on an immutable state.</strong>
*
* @param event the event to perform pre-handling on
* @param stateSignatureTransactions a consumer that accepts a list of {@link ScopedSystemTransaction}s that
* @param stateSignatureTransaction a consumer that accepts a list of {@link ScopedSystemTransaction}s that
* will be used for callbacks
*/
default void preHandle(
final Event event,
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>> stateSignatureTransactions) {}
final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {}

/**
* This method should apply the transactions in the provided round to the state. Only called on mutable states.
*
* @param round the round to apply
* @param platformState the platform state
* @param stateSignatureTransactions a consumer that accepts a list of {@link ScopedSystemTransaction}s that
* @param stateSignatureTransaction a consumer that accepts a list of {@link ScopedSystemTransaction}s that
* will be used for callbacks
*/
void handleConsensusRound(
final Round round,
final PlatformStateModifier platformState,
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>> stateSignatureTransactions);
final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction);

/**
* Called by the platform after it has made all its changes to this state for the given round.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.swirlds.platform.system.BasicSoftwareVersion;
import com.swirlds.platform.system.Round;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.List;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -84,9 +83,7 @@ public int getVersion() {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
state = NonCryptographicHashing.hash64(
state,
round.getRoundNum(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.swirlds.state.merkle.singleton.StringLeaf;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
Expand Down Expand Up @@ -86,9 +85,7 @@ private BlockingSwirldState(final BlockingSwirldState that) {
public void handleConsensusRound(
@NonNull final Round round,
@NonNull final PlatformStateModifier platformState,
@NonNull
final Consumer<List<ScopedSystemTransaction<StateSignatureTransaction>>>
stateSignatureTransactions) {
@NonNull final Consumer<ScopedSystemTransaction<StateSignatureTransaction>> stateSignatureTransaction) {
// intentionally does nothing
}

Expand Down

0 comments on commit 89e2f35

Please sign in to comment.