diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/validation/TruePredicate.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/validation/TruePredicate.java new file mode 100644 index 000000000000..d4c9be3c30b6 --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/validation/TruePredicate.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2023 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.hedera.node.app.spi.validation; + +import java.util.function.Predicate; + +public class TruePredicate implements Predicate { + public static final Predicate INSTANCE = new TruePredicate(); + + @Override + public boolean test(Object ignored) { + return true; + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java index e20acc4e6a98..e876ca9a5b39 100644 --- a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java @@ -34,6 +34,7 @@ import com.hedera.node.app.spi.state.ReadableStates; import com.hedera.node.app.spi.state.WritableQueueState; import com.hedera.node.app.spi.state.WritableStates; +import com.hedera.node.app.spi.validation.TruePredicate; import com.hedera.node.app.state.DeduplicationCache; import com.hedera.node.app.state.HederaRecordCache; import com.hedera.node.app.state.SingleTransactionRecord; @@ -266,32 +267,33 @@ private void removeExpiredTransactions( // Compute the earliest valid start timestamp that is still within the max transaction duration window. final var config = configProvider.getConfiguration().getConfigData(HederaConfig.class); final var earliestValidState = minus(consensusTimestamp, config.transactionMaxValidDuration()); - // Loop in order and expunge every entry where the timestamp is before the current time. Also remove from the // in memory data structures. - final var itr = queue.iterator(); - while (itr.hasNext()) { - final var entry = itr.next(); - final var rec = entry.transactionRecordOrThrow(); - final var txId = rec.transactionIDOrThrow(); - // If the timestamp is before the current time, then it has expired - if (isBefore(txId.transactionValidStartOrThrow(), earliestValidState)) { - // Remove from the histories - itr.remove(); - // Remove from the payer to transaction index - final var payerAccountId = txId.accountIDOrThrow(); // NOTE: Not accurate if the payer was the node - final var transactionIDs = - payerToTransactionIndex.computeIfAbsent(payerAccountId, ignored -> new HashSet<>()); - transactionIDs.remove(txId); - if (transactionIDs.isEmpty()) { - payerToTransactionIndex.remove(payerAccountId); + do { + final var entry = queue.peek(); + if (entry != null) { + final var rec = entry.transactionRecordOrThrow(); + final var txId = rec.transactionIDOrThrow(); + // If the timestamp is before the current time, then it has expired + if (isBefore(txId.transactionValidStartOrThrow(), earliestValidState)) { + // Remove from the histories + queue.removeIf(TruePredicate.INSTANCE); + // Remove from the payer to transaction index + final var payerAccountId = txId.accountIDOrThrow(); // NOTE: Not accurate if the payer was the node + final var transactionIDs = + payerToTransactionIndex.computeIfAbsent(payerAccountId, ignored -> new HashSet<>()); + transactionIDs.remove(txId); + if (transactionIDs.isEmpty()) { + payerToTransactionIndex.remove(payerAccountId); + } + } else { + break; } } else { - return; + break; } - } + } while (true); } - // --------------------------------------------------------------------------------------------------------------- // Implementation methods of RecordCache // ---------------------------------------------------------------------------------------------------------------