Skip to content

Commit

Permalink
implemented correctly the remove elemnts from the queue
Browse files Browse the repository at this point in the history
Signed-off-by: Lev Povolotsky <lev@swirldslabs.com>
  • Loading branch information
povolev15 committed Dec 15, 2023
1 parent ccf4a06 commit 9a5700b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
@@ -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();

Check warning on line 22 in hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/validation/TruePredicate.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/validation/TruePredicate.java#L21-L22

Added lines #L21 - L22 were not covered by tests

@Override
public boolean test(Object ignored) {
return true;

Check warning on line 26 in hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/validation/TruePredicate.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/validation/TruePredicate.java#L26

Added line #L26 was not covered by tests
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Check warning on line 280 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java#L280

Added line #L280 was not covered by tests
// 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);

Check warning on line 285 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java#L282-L285

Added lines #L282 - L285 were not covered by tests
if (transactionIDs.isEmpty()) {
payerToTransactionIndex.remove(payerAccountId);

Check warning on line 287 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java#L287

Added line #L287 was not covered by tests
}
} else {
break;
}
} else {
return;
break;
}
}
} while (true);

Check warning on line 295 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/recordcache/RecordCacheImpl.java#L295

Added line #L295 was not covered by tests
}

// ---------------------------------------------------------------------------------------------------------------
// Implementation methods of RecordCache
// ---------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 9a5700b

Please sign in to comment.