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

feat: schedule create throttling #9994

Merged
merged 16 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
2 changes: 2 additions & 0 deletions hedera-node/hedera-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mainModuleInfo {
}

testModuleInfo {
requires("com.fasterxml.jackson.databind")
requires("com.hedera.node.app")
requires("com.hedera.node.app.spi.test.fixtures")
requires("com.hedera.node.config.test.fixtures")
Expand All @@ -41,6 +42,7 @@ testModuleInfo {
requires("org.hamcrest")
requires("org.junit.jupiter.api")
requires("org.junit.jupiter.params")
requires("org.junitpioneer")
jsync-swirlds marked this conversation as resolved.
Show resolved Hide resolved
requires("org.mockito")
requires("org.mockito.junit.jupiter")
requires("uk.org.webcompere.systemstubs.core")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@
import static com.hedera.hapi.node.base.HederaFunctionality.CONTRACT_CALL_LOCAL;
import static com.hedera.hapi.node.base.HederaFunctionality.CONTRACT_CREATE;
import static com.hedera.hapi.node.base.HederaFunctionality.CRYPTO_CREATE;
import static com.hedera.hapi.node.base.HederaFunctionality.CRYPTO_TRANSFER;
import static com.hedera.hapi.node.base.HederaFunctionality.ETHEREUM_TRANSACTION;
import static com.hedera.node.app.hapi.utils.ethereum.EthTxData.populateEthTxData;
import static com.hedera.node.app.hapi.utils.sysfiles.domain.throttling.ScaleFactor.ONE_TO_ONE;
import static com.hedera.node.app.service.evm.accounts.HederaEvmContractAliases.isMirror;
import static com.hedera.node.app.service.mono.utils.EntityIdUtils.isOfEvmAddressSize;
import static com.hedera.node.app.service.schedule.impl.handlers.HandlerUtility.childAsOrdinary;
import static com.hedera.node.app.service.token.AliasUtils.isAlias;
import static com.hedera.node.app.service.token.AliasUtils.isSerializedProtoKey;
import static com.hedera.node.app.spi.HapiUtils.functionOf;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.FRONTEND_THROTTLE;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.AccountAmount;
import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.HederaFunctionality;
import com.hedera.hapi.node.base.NftTransfer;
import com.hedera.hapi.node.base.SignatureMap;
import com.hedera.hapi.node.base.Transaction;
import com.hedera.hapi.node.base.TransactionID;
import com.hedera.hapi.node.state.schedule.Schedule;
import com.hedera.hapi.node.token.CryptoTransferTransactionBody;
import com.hedera.hapi.node.token.TokenMintTransactionBody;
import com.hedera.hapi.node.transaction.Query;
Expand All @@ -45,7 +53,9 @@
import com.hedera.node.app.hapi.utils.throttles.GasLimitDeterministicThrottle;
import com.hedera.node.app.service.mono.throttling.ThrottleReqsManager;
import com.hedera.node.app.service.token.ReadableAccountStore;
import com.hedera.node.app.spi.UnknownHederaFunctionality;
import com.hedera.node.app.spi.throttle.HandleThrottleParser;
import com.hedera.node.app.spi.workflows.HandleException;
import com.hedera.node.app.state.HederaState;
import com.hedera.node.app.workflows.TransactionInfo;
import com.hedera.node.app.workflows.dispatcher.ReadableStoreFactory;
Expand All @@ -54,6 +64,7 @@
import com.hedera.node.config.data.AutoCreationConfig;
import com.hedera.node.config.data.ContractsConfig;
import com.hedera.node.config.data.LazyCreationConfig;
import com.hedera.node.config.data.SchedulingConfig;
import com.hedera.node.config.data.TokensConfig;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.config.api.Configuration;
Expand Down Expand Up @@ -117,7 +128,7 @@ public boolean shouldThrottle(
@NonNull final HederaState state) {
resetLastAllowedUse();
lastTxnWasGasThrottled = false;
if (shouldThrottleTxn(txnInfo, consensusTime, state)) {
if (shouldThrottleTxn(false, txnInfo, consensusTime, state)) {
reclaimLastAllowedUse();
return true;
}
Expand Down Expand Up @@ -230,8 +241,22 @@ public static boolean isGasThrottled(@NonNull final HederaFunctionality function
return GAS_THROTTLED_FUNCTIONS.contains(function);
}

/*
* Resets the usage for all underlying throttles.
*/
public void resetUsage() {
lastTxnWasGasThrottled = false;
activeThrottles.forEach(DeterministicThrottle::resetUsage);
if (gasThrottle != null) {
gasThrottle.resetUsage();
}
}

private boolean shouldThrottleTxn(
@NonNull final TransactionInfo txnInfo, @NonNull final Instant now, @NonNull final HederaState state) {
boolean isChild,
MiroslavGatsanoga marked this conversation as resolved.
Show resolved Hide resolved
@NonNull final TransactionInfo txnInfo,
@NonNull final Instant now,
@NonNull final HederaState state) {
final var function = txnInfo.functionality();
final var configuration = configProvider.getConfiguration();

Expand Down Expand Up @@ -259,6 +284,13 @@ private boolean shouldThrottleTxn(
}

return switch (function) {
case SCHEDULE_CREATE -> {
if (isChild) {
MiroslavGatsanoga marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalStateException("ScheduleCreate cannot be a child!");
}

yield shouldThrottleScheduleCreate(manager, txnInfo, now, state);
}
case TOKEN_MINT -> shouldThrottleMint(manager, txnInfo.txBody().tokenMint(), now, configuration);
case CRYPTO_TRANSFER -> {
final var accountStore = new ReadableStoreFactory(state).getStore(ReadableAccountStore.class);
Expand All @@ -274,6 +306,77 @@ yield shouldThrottleEthTxn(
};
}

private boolean shouldThrottleScheduleCreate(
final ThrottleReqsManager manager,
final TransactionInfo txnInfo,
final Instant now,
final HederaState state) {
final var txnBody = txnInfo.txBody();
final var scheduleCreate = txnBody.scheduleCreate();
final var scheduled = scheduleCreate.scheduledTransactionBody();
final var schedule = Schedule.newBuilder()
.originalCreateTransaction(txnBody)
.payerAccountId(txnInfo.payerID())
.scheduledTransaction(scheduled)
.build();

TransactionBody innerTxn;
HederaFunctionality scheduledFunction;
try {
innerTxn = childAsOrdinary(schedule);
scheduledFunction = functionOf(innerTxn);
} catch (HandleException | UnknownHederaFunctionality ex) {
log.debug("ScheduleCreate was associated with an invalid txn.", ex);
return true;
}

// maintain legacy behaviour
final var configuration = configProvider.getConfiguration();
if (!configuration.getConfigData(SchedulingConfig.class).longTermEnabled()) {
final var isAutoCreationEnabled =
configuration.getConfigData(AutoCreationConfig.class).enabled();
final var isLazyCreationEnabled =
MiroslavGatsanoga marked this conversation as resolved.
Show resolved Hide resolved
configuration.getConfigData(LazyCreationConfig.class).enabled();
if ((isAutoCreationEnabled || isLazyCreationEnabled) && scheduledFunction == CRYPTO_TRANSFER) {
MiroslavGatsanoga marked this conversation as resolved.
Show resolved Hide resolved
final var transfer = scheduled.cryptoTransfer();
if (usesAliases(transfer)) {
final var accountStore = new ReadableStoreFactory(state).getStore(ReadableAccountStore.class);
final var transferTxnBody = TransactionBody.newBuilder()
.cryptoTransfer(transfer)
.build();
final var implicitCreationsCount = getImplicitCreationsCount(transferTxnBody, accountStore);
MiroslavGatsanoga marked this conversation as resolved.
Show resolved Hide resolved
if (implicitCreationsCount > 0) {
return shouldThrottleImplicitCreations(implicitCreationsCount, now);
}
}
}
return !manager.allReqsMetAt(now);
}

if (!manager.allReqsMetAt(now)) {
return true;
}

if ((!scheduleCreate.waitForExpiry()) && (throttleType == FRONTEND_THROTTLE)) {
jsync-swirlds marked this conversation as resolved.
Show resolved Hide resolved
var effectivePayer = scheduleCreate.hasPayerAccountID()
? scheduleCreate.payerAccountID()
: txnBody.transactionID().accountID();

final var innerTxnInfo = new TransactionInfo(
Transaction.DEFAULT,
innerTxn,
TransactionID.DEFAULT,
effectivePayer,
SignatureMap.DEFAULT,
Bytes.EMPTY,
scheduledFunction);

return shouldThrottleTxn(true, innerTxnInfo, now, state);
}

return false;
}

public static boolean throttleExempt(
@NonNull final AccountID accountID, @NonNull final Configuration configuration) {
final var maxThrottleExemptNum =
Expand Down Expand Up @@ -437,6 +540,29 @@ private int tokenAdjustsImplicitCreationsCount(
return implicitCreationsCount;
}

private boolean usesAliases(final CryptoTransferTransactionBody transferBody) {
for (var adjust : transferBody.transfers().accountAmounts()) {
if (isAlias(adjust.accountID())) {
return true;
}
}

for (var tokenAdjusts : transferBody.tokenTransfers()) {
for (var ownershipChange : tokenAdjusts.nftTransfers()) {
if (isAlias(ownershipChange.senderAccountID()) || isAlias(ownershipChange.receiverAccountID())) {
return true;
}
}
for (var tokenAdjust : tokenAdjusts.transfers()) {
if (isAlias(tokenAdjust.accountID())) {
return true;
}
}
}

return false;
}

private boolean isKnownAlias(@NonNull final AccountID idOrAlias, @NonNull final ReadableAccountStore accountStore) {
if (isAlias(idOrAlias)) {
final var alias = idOrAlias.alias();
Expand Down
Loading
Loading