Skip to content

Commit

Permalink
calculate expiration on usage snapshot save
Browse files Browse the repository at this point in the history
Signed-off-by: Zhivko Kelchev <zhivko.kelchev@limechain.tech>
  • Loading branch information
JivkoKelchev committed Nov 21, 2024
1 parent e09d474 commit 01d1edc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import static com.hedera.hapi.util.HapiUtils.functionOf;
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.schedule.impl.handlers.HandlerUtility.calculateExpiration;
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.isEntityNumAlias;
import static com.hedera.node.app.service.token.AliasUtils.isOfEvmAddressSize;
import static com.hedera.node.app.service.token.AliasUtils.isSerializedProtoKey;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.BACKEND_THROTTLE;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.FRONTEND_THROTTLE;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.SCHEDULING_THROTTLE;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -464,17 +464,19 @@ private boolean shouldThrottleScheduleCreate(
}
return !manager.allReqsMetAt(now);
} else {
if (throttleType == BACKEND_THROTTLE) {
if (!manager.allReqsMetAt(now)) {
return true;
}
}

if (throttleType == SCHEDULING_THROTTLE) {
if (throttleType != SCHEDULING_THROTTLE) {
return !manager.allReqsMetAt(now);
} else {
var effectivePayer = scheduleCreate.hasPayerAccountID()
? scheduleCreate.payerAccountID()
: txnBody.transactionID().accountID();

final var expirationSecond = calculateExpiration(
scheduleCreate.expirationTime(),
now,
configuration.getConfigData(SchedulingConfig.class).maxExpirationFutureSeconds(),
true);

final var innerTxnInfo = new TransactionInfo(
Transaction.DEFAULT,
innerTxn,
Expand All @@ -484,14 +486,8 @@ private boolean shouldThrottleScheduleCreate(
Bytes.EMPTY,
scheduledFunction,
null);
return shouldThrottleTxn(
true,
innerTxnInfo,
Instant.ofEpochSecond(scheduleCreate.expirationTime().seconds()),
state);
return shouldThrottleTxn(true, innerTxnInfo, Instant.ofEpochSecond(expirationSecond), state);
}

return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static com.hedera.hapi.node.base.HederaFunctionality.TRANSACTION_GET_RECEIPT;
import static com.hedera.node.app.service.schedule.impl.schemas.V0490ScheduleSchema.SCHEDULES_BY_ID_KEY;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.FRONTEND_THROTTLE;
import static com.hedera.node.app.throttle.ThrottleAccumulator.ThrottleType.SCHEDULING_THROTTLE;
import static com.hedera.pbj.runtime.ProtoTestTools.getThreadLocalDataBuffer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -1257,7 +1258,8 @@ void alwaysRejectsIfNoThrottle(ThrottleAccumulator.ThrottleType throttleType) {
}

@ParameterizedTest
@EnumSource
@CsvSource({"FRONTEND_THROTTLE", "BACKEND_THROTTLE"})
// schedule throttle cant leak unused gas in the future
void verifyLeakUnusedGas(ThrottleAccumulator.ThrottleType throttleType) throws IOException, ParseException {
subject = new ThrottleAccumulator(() -> CAPACITY_SPLIT, configProvider, throttleType, throttleMetrics);
given(configProvider.getConfiguration()).willReturn(configuration);
Expand Down Expand Up @@ -1863,15 +1865,7 @@ void usesCryptoCreateThrottleForCryptoTransferWithAutoAssociationsInScheduleCrea

// then
assertFalse(ans);
if (longTermEnabled && throttleType == FRONTEND_THROTTLE) {
// with long term enabled, we count the schedule create in addition to the auto
// associations, which
// is how it should have been to start with
assertEquals(11 * BucketThrottle.capacityUnitsPerTxn(), aNow.used());
} else {
assertEquals(BucketThrottle.capacityUnitsPerTxn(), aNow.used());
}

assertEquals(BucketThrottle.capacityUnitsPerTxn(), aNow.used());
assertEquals(0, subject.activeThrottlesFor(CRYPTO_TRANSFER).get(0).used());
}

Expand Down Expand Up @@ -1951,7 +1945,7 @@ void usesScheduleCreateThrottleForAliasedCryptoTransferWithNoAutoCreation(
void reclaimsAllUsagesOnThrottledCheckAndEnforceThrottleTxn() throws IOException, ParseException {
// given
subject = new ThrottleAccumulator(
() -> CAPACITY_SPLIT, configProvider, FRONTEND_THROTTLE, throttleMetrics, gasThrottle);
() -> CAPACITY_SPLIT, configProvider, SCHEDULING_THROTTLE, throttleMetrics, gasThrottle);

given(configProvider.getConfiguration()).willReturn(configuration);
given(configuration.getConfigData(AccountsConfig.class)).willReturn(accountsConfig);
Expand All @@ -1960,6 +1954,7 @@ void reclaimsAllUsagesOnThrottledCheckAndEnforceThrottleTxn() throws IOException
given(contractsConfig.throttleThrottleByGas()).willReturn(false);
given(configuration.getConfigData(SchedulingConfig.class)).willReturn(schedulingConfig);
given(schedulingConfig.longTermEnabled()).willReturn(true);
given(schedulingConfig.maxExpirationFutureSeconds()).willReturn(30L);

final var scheduledSubmit = SchedulableTransactionBody.newBuilder()
.consensusSubmitMessage(ConsensusSubmitMessageTransactionBody.DEFAULT)
Expand All @@ -1976,19 +1971,16 @@ void reclaimsAllUsagesOnThrottledCheckAndEnforceThrottleTxn() throws IOException

assertFalse(firstAns);
assertTrue(subsequentAns);
assertEquals(
4999250000000L,
subject.activeThrottlesFor(SCHEDULE_CREATE).get(0).used());

// SCHEDULING_THROTTLE is responsible only for the scheduled transaction, and not the schedule create!
assertEquals(
4999999250000L,
5000000000000L,
subject.activeThrottlesFor(CONSENSUS_SUBMIT_MESSAGE).get(0).used());

// when
subject.resetUsage();

// then
assertEquals(0L, subject.activeThrottlesFor(SCHEDULE_CREATE).get(0).used());
assertEquals(
0L, subject.activeThrottlesFor(CONSENSUS_SUBMIT_MESSAGE).get(0).used());
}
Expand Down

0 comments on commit 01d1edc

Please sign in to comment.