use of com.hedera.services.usage.crypto.ExtantCryptoContext in project hedera-services by hashgraph.
the class MacroFeesChargedSanityCheckSuite method renewalCappedByAffordablePeriod.
private HapiApiSpec renewalCappedByAffordablePeriod() {
final long briefAutoRenew = 10L;
final long normalAutoRenew = THREE_MONTHS_IN_SECONDS;
final long threeHoursInSeconds = 3 * 3600L;
final AtomicLong initialExpiry = new AtomicLong();
final AtomicLong balanceForThreeHourRenew = new AtomicLong();
final var target = "czar";
final String crazyMemo = "Calmer than you are!";
final ExtantCryptoContext cryptoCtx = ExtantCryptoContext.newBuilder().setCurrentExpiry(0L).setCurrentKey(Key.newBuilder().setEd25519(copyFromUtf8(randomUppercase(32))).build()).setCurrentlyHasProxy(true).setCurrentMemo(crazyMemo).setCurrentNumTokenRels(0).build();
return defaultHapiSpec("RenewalCappedByAffordablePeriod").given(withOpContext((spec, opLog) -> {
final var tbFee = autoRenewFeesFor(spec, cryptoCtx);
balanceForThreeHourRenew.set(tbFee.totalTb(3));
opLog.info("Balance {} will pay for three-hour renewal", balanceForThreeHourRenew.get());
}), fileUpdate(APP_PROPERTIES).payingWith(GENESIS).overridingProps(enablingAutoRenewWith(1, 1234L)), sourcing(() -> cryptoCreate(target).entityMemo(crazyMemo).balance(balanceForThreeHourRenew.get()).autoRenewSecs(briefAutoRenew)), /* Despite asking for a three month autorenew here, the account will only
be able to afford a three hour extension. */
cryptoUpdate(target).autoRenewPeriod(normalAutoRenew), getAccountInfo(target).exposingExpiry(initialExpiry::set)).when(sleepFor(briefAutoRenew * 1_000L + 500L), cryptoTransfer(tinyBarsFromTo(DEFAULT_PAYER, FUNDING, 1L))).then(/* The account in question will have expired (and been auto-renewed);
should only have received a three-hour renewal, and its entire balance
been used. */
getAccountBalance(target).hasTinyBars(0L), sourcing(() -> getAccountInfo(target).has(accountWith().expiry(initialExpiry.get() + threeHoursInSeconds, 0L))), cryptoDelete(target));
}
use of com.hedera.services.usage.crypto.ExtantCryptoContext in project hedera-services by hashgraph.
the class OpUsageCtxHelper method ctxForCryptoUpdate.
public ExtantCryptoContext ctxForCryptoUpdate(TransactionBody txn) {
final var op = txn.getCryptoUpdateAccount();
ExtantCryptoContext cryptoContext;
var info = workingView.infoForAccount(op.getAccountIDToUpdate(), aliasManager);
if (info.isPresent()) {
var details = info.get();
cryptoContext = ExtantCryptoContext.newBuilder().setCurrentKey(details.getKey()).setCurrentMemo(details.getMemo()).setCurrentExpiry(details.getExpirationTime().getSeconds()).setCurrentlyHasProxy(details.hasProxyAccountID()).setCurrentNumTokenRels(details.getTokenRelationshipsCount()).setCurrentMaxAutomaticAssociations(details.getMaxAutomaticTokenAssociations()).setCurrentCryptoAllowances(details.getGrantedCryptoAllowancesList()).setCurrentTokenAllowances(details.getGrantedTokenAllowancesList()).setCurrentNftAllowances(details.getGrantedNftAllowancesList()).build();
} else {
cryptoContext = ExtantCryptoContext.newBuilder().setCurrentExpiry(txn.getTransactionID().getTransactionValidStart().getSeconds()).setCurrentMemo(DEFAULT_MEMO).setCurrentKey(Key.getDefaultInstance()).setCurrentlyHasProxy(false).setCurrentNumTokenRels(0).setCurrentMaxAutomaticAssociations(0).setCurrentCryptoAllowances(Collections.emptyList()).setCurrentTokenAllowances(Collections.emptyList()).setCurrentNftAllowances(Collections.emptyList()).build();
}
return cryptoContext;
}
use of com.hedera.services.usage.crypto.ExtantCryptoContext in project hedera-services by hashgraph.
the class OpUsageCtxHelper method ctxForCryptoAllowance.
public ExtantCryptoContext ctxForCryptoAllowance(TransactionBody txn) {
ExtantCryptoContext cryptoContext;
var info = workingView.infoForAccount(txn.getTransactionID().getAccountID(), aliasManager);
if (info.isPresent()) {
var details = info.get();
cryptoContext = ExtantCryptoContext.newBuilder().setCurrentKey(details.getKey()).setCurrentMemo(details.getMemo()).setCurrentExpiry(details.getExpirationTime().getSeconds()).setCurrentlyHasProxy(details.hasProxyAccountID()).setCurrentNumTokenRels(details.getTokenRelationshipsCount()).setCurrentMaxAutomaticAssociations(details.getMaxAutomaticTokenAssociations()).setCurrentCryptoAllowances(details.getGrantedCryptoAllowancesList()).setCurrentTokenAllowances(details.getGrantedTokenAllowancesList()).setCurrentNftAllowances(details.getGrantedNftAllowancesList()).build();
} else {
cryptoContext = ExtantCryptoContext.newBuilder().setCurrentExpiry(txn.getTransactionID().getTransactionValidStart().getSeconds()).setCurrentMemo(DEFAULT_MEMO).setCurrentKey(Key.getDefaultInstance()).setCurrentlyHasProxy(false).setCurrentNumTokenRels(0).setCurrentMaxAutomaticAssociations(0).setCurrentCryptoAllowances(Collections.emptyList()).setCurrentTokenAllowances(Collections.emptyList()).setCurrentNftAllowances(Collections.emptyList()).build();
}
return cryptoContext;
}
use of com.hedera.services.usage.crypto.ExtantCryptoContext in project hedera-services by hashgraph.
the class MacroFeesChargedSanityCheckSuite method feesChargedMatchNumberOfRenewals.
private HapiApiSpec feesChargedMatchNumberOfRenewals() {
final long reqAutoRenew = 2L;
final long startBalance = ONE_HUNDRED_HBARS;
final var target = "czar";
final String crazyMemo = "Calmer than you are!";
final AtomicLong initialExpiry = new AtomicLong();
final AtomicLong finalExpiry = new AtomicLong();
final AtomicLong finalBalance = new AtomicLong();
final AtomicLong duration = new AtomicLong(30);
final AtomicReference<TimeUnit> unit = new AtomicReference<>(SECONDS);
final AtomicInteger maxOpsPerSec = new AtomicInteger(100);
final ExtantCryptoContext cryptoCtx = ExtantCryptoContext.newBuilder().setCurrentExpiry(0L).setCurrentKey(Key.newBuilder().setEd25519(copyFromUtf8(randomUppercase(32))).build()).setCurrentlyHasProxy(true).setCurrentMemo(crazyMemo).setCurrentNumTokenRels(0).build();
return defaultHapiSpec("FeesChargedMatchNumberOfRenewals").given(fileUpdate(APP_PROPERTIES).payingWith(GENESIS).overridingProps(enablingAutoRenewWith(1, 1234L)), cryptoCreate(target).entityMemo(crazyMemo).balance(startBalance).autoRenewSecs(reqAutoRenew), getAccountInfo(target).exposingExpiry(initialExpiry::set)).when(sleepFor(reqAutoRenew * 1_000L + 500L), runWithProvider(getAnyOldXfers()).lasting(duration::get, unit::get).maxOpsPerSec(maxOpsPerSec::get)).then(/* The account with the crazy short auto-renew will have expired (and
been auto-renewed) multiple times during the 30-second burst of
CryptoTransfers. We want to confirm its balance changed as expected
with the number of renewals. */
assertionsHold((spec, opLog) -> {
final var tbFee = autoRenewFeesFor(spec, cryptoCtx).totalTb(1);
opLog.info("Expected fee in tinybars: {}", tbFee);
var infoOp = getAccountInfo(target).exposingBalance(finalBalance::set).exposingExpiry(finalExpiry::set);
allRunFor(spec, infoOp);
final long balanceChange = startBalance - finalBalance.get();
final long expiryChange = finalExpiry.get() - initialExpiry.get();
final int numRenewals = (int) (expiryChange / reqAutoRenew);
opLog.info("{} renewals happened, extending expiry by {} and reducing balance by {}", numRenewals, expiryChange, balanceChange);
Assertions.assertEquals(balanceChange, numRenewals * tbFee);
}), cryptoDelete(target));
}
Aggregations