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

fix: check throttle usage on committing changes #11064

Merged
merged 62 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
6b25463
fix: reclaim throttle usage for failed auto-creations
petreze Jan 12, 2024
8770cd4
Merge branch 'develop' into 10216-throttle-usage-auto-creation
petreze Jan 12, 2024
ff569ff
chore: remove comment
petreze Jan 12, 2024
2eaac48
fix: move throttle reclaiming in inner class when AutoAccountCreator …
petreze Jan 12, 2024
83b64de
chore: apply spotless
petreze Jan 12, 2024
eb803d7
fix: unit test in HandleContextImplTest
petreze Jan 12, 2024
b3289a0
Merge branch 'develop' into 10216-throttle-usage-auto-creation
petreze Jan 13, 2024
50a5619
Merge branch '10216-throttle-usage-auto-creation' into 10215-throttle…
petreze Jan 15, 2024
37bed6f
Merge branch 'develop' into 10216-throttle-usage-auto-creation
petreze Jan 16, 2024
1a1b451
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 16, 2024
312f1b7
fix: use frontend instead of backend throttle and add checks to handl…
petreze Jan 17, 2024
77cde88
Merge branch 'develop' into 10216-throttle-usage-auto-creation
petreze Jan 17, 2024
3cc2d18
fix: simplify conditions on auto-account creation throttle reclaim
petreze Jan 18, 2024
0eedfdd
Merge branch 'develop' into 10216-throttle-usage-auto-creation
petreze Jan 18, 2024
f35d75c
Merge branch '10216-throttle-usage-auto-creation' into 10215-throttle…
petreze Jan 18, 2024
44574b1
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 18, 2024
1a2fe2e
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 18, 2024
733e565
fix: implement capacity check if should throttle auto-creations
petreze Jan 19, 2024
0378cad
fix: implement capacity check if should throttle child records
petreze Jan 19, 2024
208570b
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 19, 2024
26c2c2d
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 19, 2024
41a0c8f
chore: rename to OperationContext and clarify description
petreze Jan 19, 2024
c903b94
chore: add interface description to OperationContext
petreze Jan 19, 2024
262e388
fix: address PR comments with several conceptual changes
petreze Jan 22, 2024
b07a527
chore: apply spotless
petreze Jan 22, 2024
73bb8a3
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 23, 2024
d11de6c
fix: potential fix with reverse condition
petreze Jan 23, 2024
841a22b
fix: NullPointerException for some TXs yielding FAIL_INVALID
petreze Jan 23, 2024
92518ee
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 23, 2024
8725674
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 23, 2024
0172eca
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 24, 2024
8830e25
fix: use context functionality instead of the one from child records
petreze Jan 24, 2024
5d1c362
fix: try adding both functionalities
petreze Jan 24, 2024
c881553
fix: return to child functionalities
petreze Jan 24, 2024
854410c
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 25, 2024
bef03da
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 26, 2024
90ecac3
fix: mimic mono behaviour and return false if null is encountered
petreze Jan 29, 2024
f1b6d2c
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 29, 2024
e772299
fix: apply spotless
petreze Jan 29, 2024
8df8991
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 29, 2024
c3eadf8
fix: refactor after changing the approach
petreze Jan 30, 2024
49ec85f
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 30, 2024
221dd14
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Jan 31, 2024
81b104e
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Feb 1, 2024
1da1cd7
fix: update decision time before using it in frontend throttle
petreze Feb 1, 2024
9298418
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Feb 1, 2024
a8219b1
fix: fix constructor issue
petreze Feb 1, 2024
560998f
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Feb 2, 2024
ad4cebf
fix: update decision time after checks
petreze Feb 5, 2024
afad468
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Feb 5, 2024
b8818f7
fix: try with userTransactionConsensusTime
petreze Feb 5, 2024
0d18a80
fix: test cli workflow
petreze Feb 5, 2024
80ce685
fix: test cli workflow
petreze Feb 6, 2024
77f8470
fix: use the backend throttle instead
petreze Feb 6, 2024
65e0882
fix: remove unnecessary method
petreze Feb 6, 2024
5ac2bc4
fix: add check if property enforceCreationThrottle is true
petreze Feb 6, 2024
0044e33
fix: refactor to simplify implementation and add method comments
petreze Feb 7, 2024
0cc3983
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Feb 7, 2024
4aff875
fix: inject HandleContext into the constructor
petreze Feb 8, 2024
2835c3c
fix: unit test in RootProxyWorldUpdaterTest
petreze Feb 9, 2024
3743eac
fix: throw more specific ResourceExhaustedEx
petreze Feb 12, 2024
1956173
Merge branch 'develop' into 10215-throttle-on-committing-changes
petreze Feb 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,34 @@
import java.util.List;

public interface HandleThrottleParser {
/*
* Rebuilds the throttle requirements based on the given throttle definitions.
*
* @param defs the throttle definitions to rebuild the throttle requirements based on
*/
void rebuildFor(@NonNull final ThrottleDefinitions defs);

/*
* Rebuilds the gas throttle based on the current configuration.
*/
void applyGasConfig();

/*
* Gets the current list of active throttles.
*
* @return the current list of active throttles
*/
@NonNull
List<DeterministicThrottle> allActiveThrottles();

/*
* Resets the usage for all snapshots.
*/
void resetUsageThrottlesTo(List<DeterministicThrottle.UsageSnapshot> snapshots);

/*
* Gets the gas throttle.
*/
@Nullable
public GasLimitDeterministicThrottle gasLimitThrottle();
GasLimitDeterministicThrottle gasLimitThrottle();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.hedera.hapi.node.base.Key;
import com.hedera.hapi.node.base.SubType;
import com.hedera.hapi.node.transaction.TransactionBody;
import com.hedera.node.app.hapi.utils.throttles.DeterministicThrottle;
import com.hedera.node.app.spi.authorization.SystemPrivilege;
import com.hedera.node.app.spi.fees.ExchangeRateInfo;
import com.hedera.node.app.spi.fees.FeeAccumulator;
Expand All @@ -43,6 +44,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.time.Instant;
import java.util.List;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -668,6 +670,26 @@ default <T> T dispatchRemovableChildTransaction(
*/
void reclaimPreviouslyReservedThrottle(int n, HederaFunctionality function);

/**
* Verifies if the throttle in this operation context has enough capacity to handle the given number of the
* given function at the given time. (The time matters because we want to consider how much
* will have leaked between now and that time.)
*
* @param n the number of the given function
* @param function the function
* @return true if the system should throttle the given number of the given function
* at the instant for which throttling should be calculated
*/
boolean shouldThrottleNOfUnscaled(int n, HederaFunctionality function);

/**
* For each following child transaction consumes the capacity
* required for that child transaction in the consensus throttle buckets.
*
* @return true if all the child transactions were allowed through the throttle consideration, false otherwise.
*/
boolean hasThrottleCapacityForChildTransactions();

/**
* Create a checkpoint for the current childRecords.
*
Expand All @@ -677,6 +699,19 @@ default <T> T dispatchRemovableChildTransaction(
@NonNull
RecordListCheckPoint createRecordListCheckPoint();

/**
* Returns a list of snapshots of the current usage of all active throttles.
* @return the active snapshots
*/
List<DeterministicThrottle.UsageSnapshot> getUsageSnapshots();

/**
* Resets the current usage of all active throttles to the given snapshots.
*
* @param snapshots the snapshots to reset to
*/
void resetUsageThrottlesTo(List<DeterministicThrottle.UsageSnapshot> snapshots);

/**
* Returns whether the current transaction being processed was submitted by this node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public List<DeterministicThrottle> allActiveThrottles() {
return Collections.emptyList();
}

@Override
public void resetUsageThrottlesTo(List<DeterministicThrottle.UsageSnapshot> snapshots) {}

@Nullable
@Override
public GasLimitDeterministicThrottle gasLimitThrottle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package com.hedera.node.app.throttle;

import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.HederaFunctionality;
import com.hedera.node.app.hapi.utils.throttles.DeterministicThrottle;
import com.hedera.node.app.state.HederaState;
import com.hedera.node.app.workflows.TransactionInfo;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.time.Instant;
import java.util.List;

/**
* Interface which purpose is to do the work of tracking network utilization (and its impact on
Expand Down Expand Up @@ -78,4 +81,43 @@ void trackFeePayments(
* @param state the state of the node
*/
void saveTo(@NonNull final HederaState state);

/*
* Updates the throttle requirements for the given transaction and returns whether the transaction
* should be throttled for the current time(Instant.now).
*
* @param txnInfo the transaction to update the throttle requirements for
* @param state the current state of the node
* @param consensusTime the consensus time
* @return whether the transaction should be throttled
*/
boolean shouldThrottle(
@NonNull final TransactionInfo txnInfo,
@NonNull final HederaState state,
@NonNull final Instant consensusTime);

/**
* Verifies if the throttle in this operation context has enough capacity to handle the given number of the
* given function at the given time. (The time matters because we want to consider how much
* will have leaked between now and that time.)
*
* @param n the number of the given function
* @param function the function
* @return true if the system should throttle the given number of the given function
* at the instant for which throttling should be calculated
*/
boolean shouldThrottleNOfUnscaled(int n, @NonNull HederaFunctionality function, @NonNull Instant consensusTime);

/**
* Returns a list of snapshots of the current usage of all active throttles.
* @return the active snapshots
*/
List<DeterministicThrottle.UsageSnapshot> getUsageSnapshots();

/**
* Resets the current usage of all active throttles to the given snapshots.
*
* @param snapshots the snapshots to reset to
*/
void resetUsageThrottlesTo(List<DeterministicThrottle.UsageSnapshot> snapshots);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public SynchronizedThrottleAccumulator(ThrottleAccumulator frontendThrottle) {
* @return whether the transaction should be throttled
*/
public synchronized boolean shouldThrottle(@NonNull TransactionInfo txnInfo, HederaState state) {
setDecisionTime();
setDecisionTime(Instant.now());
return frontendThrottle.shouldThrottle(txnInfo, lastDecisionTime, state);
}

Expand All @@ -67,16 +67,15 @@ public synchronized boolean shouldThrottle(@NonNull TransactionInfo txnInfo, Hed
* @return whether the query should be throttled
*/
public synchronized boolean shouldThrottle(HederaFunctionality queryFunction, Query query, AccountID queryPayerId) {
setDecisionTime();
setDecisionTime(Instant.now());
return frontendThrottle.shouldThrottle(queryFunction, lastDecisionTime, query, queryPayerId);
}

private void setDecisionTime() {
final var now = Instant.now();
lastDecisionTime = now.isBefore(lastDecisionTime) ? lastDecisionTime : now;
}

public void leakUnusedThrottlePreviouslyReserved(int n, HederaFunctionality function) {
frontendThrottle.leakCapacityForNOfUnscaled(n, function);
}

private void setDecisionTime(@NonNull final Instant time) {
lastDecisionTime = time.isBefore(lastDecisionTime) ? lastDecisionTime : time;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ public void resetUsage() {
}
}

/*
* Resets the usage for all snapshots.
*/
@Override
public void resetUsageThrottlesTo(final List<DeterministicThrottle.UsageSnapshot> snapshots) {
for (int i = 0, n = activeThrottles.size(); i < n; i++) {
activeThrottles.get(i).resetUsageTo(snapshots.get(i));
}
}

private boolean shouldThrottleTxn(
final boolean isScheduled,
@NonNull final TransactionInfo txnInfo,
Expand Down Expand Up @@ -487,11 +497,14 @@ private boolean shouldThrottleScheduleSign(
}

public static boolean throttleExempt(
@NonNull final AccountID accountID, @NonNull final Configuration configuration) {
@Nullable final AccountID accountID, @NonNull final Configuration configuration) {
final long maxThrottleExemptNum =
configuration.getConfigData(AccountsConfig.class).lastThrottleExempt();
final long accountNum = accountID.accountNum().longValue();
return 1L <= accountNum && accountNum <= maxThrottleExemptNum;
if (accountID != null) {
final long accountNum = accountID.accountNum().longValue();
return 1L <= accountNum && accountNum <= maxThrottleExemptNum;
}
return false;
}

private void reclaimLastAllowedUse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.HederaFunctionality;
import com.hedera.hapi.node.base.SignatureMap;
import com.hedera.hapi.node.base.Timestamp;
import com.hedera.hapi.node.base.Transaction;
Expand Down Expand Up @@ -201,4 +202,30 @@ public boolean wasLastTxnGasThrottled() {
public void leakUnusedGasPreviouslyReserved(@NonNull final TransactionInfo txnInfo, long value) {
backendThrottle.leakUnusedGasPreviouslyReserved(txnInfo, value);
}

@Override
public boolean shouldThrottle(
@NonNull final TransactionInfo txnInfo,
@NonNull final HederaState state,
@NonNull final Instant consensusTime) {
return backendThrottle.shouldThrottle(txnInfo, consensusTime, state);
}

@Override
public boolean shouldThrottleNOfUnscaled(
final int n, @NonNull final HederaFunctionality function, @NonNull final Instant consensusTime) {
return backendThrottle.shouldThrottleNOfUnscaled(n, function, consensusTime);
}

@Override
public List<DeterministicThrottle.UsageSnapshot> getUsageSnapshots() {
return backendThrottle.allActiveThrottles().stream()
.map(DeterministicThrottle::usageSnapshot)
.toList();
}

@Override
public void resetUsageThrottlesTo(List<DeterministicThrottle.UsageSnapshot> snapshots) {
backendThrottle.resetUsageThrottlesTo(snapshots);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.hedera.hapi.node.transaction.TransactionBody;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

/**
* Contains information related to a parsed transaction.
Expand All @@ -49,8 +50,8 @@
public record TransactionInfo(
@NonNull Transaction transaction,
@NonNull TransactionBody txBody,
@NonNull TransactionID transactionID,
@NonNull AccountID payerID,
@Nullable TransactionID transactionID,
@Nullable AccountID payerID,
@NonNull SignatureMap signatureMap,
@NonNull Bytes signedBytes,
@NonNull HederaFunctionality functionality) {
Expand All @@ -70,4 +71,22 @@ public TransactionInfo(
signedBytes,
functionality);
}

public static TransactionInfo from(
@NonNull Transaction transaction,
@NonNull TransactionBody txBody,
@NonNull SignatureMap signatureMap,
@NonNull Bytes signedBytes,
@NonNull HederaFunctionality functionality) {
TransactionID transactionId = null;
AccountID payerId = null;
if (txBody.transactionID() != null) {
transactionId = txBody.transactionID();
if (transactionId.accountID() != null) {
payerId = txBody.transactionID().accountID();
}
}
return new TransactionInfo(
transaction, txBody, transactionId, payerId, signatureMap, signedBytes, functionality);
}
}