Search in sources :

Example 1 with HederaFunctionality

use of com.hederahashgraph.api.proto.java.HederaFunctionality in project hedera-services by hashgraph.

the class PlatformTxnAccessorTest method usesExtractorToGetFunctionAsExpected.

@Test
void usesExtractorToGetFunctionAsExpected() {
    // setup:
    var memory = SignedTxnAccessor.functionExtractor;
    Function<TransactionBody, HederaFunctionality> mockFn = (Function<TransactionBody, HederaFunctionality>) mock(Function.class);
    SignedTxnAccessor.functionExtractor = mockFn;
    // and:
    someTxn = someTxn.toBuilder().setConsensusCreateTopic(ConsensusCreateTopicTransactionBody.newBuilder()).build();
    Transaction signedTxn = Transaction.newBuilder().setBodyBytes(someTxn.toByteString()).build();
    given(mockFn.apply(any())).willReturn(ConsensusCreateTopic);
    var subject = SignedTxnAccessor.uncheckedFrom(signedTxn);
    // when:
    var first = subject.getFunction();
    var second = subject.getFunction();
    // then:
    assertEquals(ConsensusCreateTopic, first);
    assertEquals(second, first);
    // and:
    verify(mockFn, times(1)).apply(any());
    // cleanup:
    SignedTxnAccessor.functionExtractor = memory;
}
Also used : HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Function(java.util.function.Function) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) ConsensusCreateTopicTransactionBody(com.hederahashgraph.api.proto.java.ConsensusCreateTopicTransactionBody) Transaction(com.hederahashgraph.api.proto.java.Transaction) SignedTransaction(com.hederahashgraph.api.proto.java.SignedTransaction) SwirldTransaction(com.swirlds.common.SwirldTransaction) Test(org.junit.jupiter.api.Test)

Example 2 with HederaFunctionality

use of com.hederahashgraph.api.proto.java.HederaFunctionality in project hedera-services by hashgraph.

the class ProviderRun method submitOp.

@Override
protected boolean submitOp(HapiApiSpec spec) {
    int MAX_N = Runtime.getRuntime().availableProcessors();
    int MAX_OPS_PER_SEC = maxOpsPerSecSupplier.getAsInt();
    int MAX_PENDING_OPS = maxPendingOpsSupplier.getAsInt();
    int BACKOFF_SLEEP_SECS = backoffSleepSecsSupplier.getAsInt();
    long duration = durationSupplier.getAsLong();
    OpProvider provider = providerFn.apply(spec);
    allRunFor(spec, provider.suggestedInitializers().toArray(new HapiSpecOperation[0]));
    log.info("Finished initialization for provider run...");
    TimeUnit unit = unitSupplier.get();
    Stopwatch stopwatch = Stopwatch.createStarted();
    final var remainingOpsToSubmit = new AtomicInteger(totalOpsToSubmit.getAsInt());
    final boolean fixedOpSubmission = (remainingOpsToSubmit.get() < 0) ? false : true;
    int submittedSoFar = 0;
    long durationMs = unit.toMillis(duration);
    long logIncrementMs = durationMs / 100;
    long nextLogTargetMs = logIncrementMs;
    long lastDeltaLogged = -1;
    final var opsThisSecond = new AtomicInteger(0);
    final var submissionBoundaryMs = new AtomicLong(stopwatch.elapsed(MILLISECONDS) + 1_000);
    while (stopwatch.elapsed(unit) < duration) {
        long elapsedMs = stopwatch.elapsed(MILLISECONDS);
        if (elapsedMs > submissionBoundaryMs.get()) {
            submissionBoundaryMs.getAndAdd(1_000);
            opsThisSecond.set(0);
        }
        int numPending = spec.numPendingOps();
        if (elapsedMs > nextLogTargetMs) {
            nextLogTargetMs += logIncrementMs;
            long delta = duration - stopwatch.elapsed(unit);
            if (delta != lastDeltaLogged) {
                log.info(delta + " " + unit.toString().toLowerCase() + (fixedOpSubmission ? (" or " + remainingOpsToSubmit + " ops ") : "") + " left in test - " + submittedSoFar + " ops submitted so far (" + numPending + " pending).");
                log.info("Precheck txn status counts :: " + spec.precheckStatusCounts());
                log.info("Resolved txn status counts :: " + spec.finalizedStatusCounts());
                log.info("\n------------------------------\n");
                lastDeltaLogged = delta;
            }
        }
        if (fixedOpSubmission && remainingOpsToSubmit.get() <= 0) {
            if (numPending > 0) {
                continue;
            }
            log.info("Finished submission of total {} operations", totalOpsToSubmit.getAsInt());
            break;
        }
        if (numPending < MAX_PENDING_OPS) {
            HapiSpecOperation[] burst = IntStream.range(0, Math.min(MAX_N, fixedOpSubmission ? Math.min(remainingOpsToSubmit.get(), MAX_OPS_PER_SEC - opsThisSecond.get()) : MAX_OPS_PER_SEC - opsThisSecond.get())).mapToObj(ignore -> provider.get()).flatMap(Optional::stream).peek(op -> counts.get(op.type()).getAndIncrement()).toArray(HapiSpecOperation[]::new);
            if (burst.length > 0) {
                allRunFor(spec, inParallel(burst));
                submittedSoFar += burst.length;
                if (fixedOpSubmission) {
                    remainingOpsToSubmit.getAndAdd(-burst.length);
                }
                opsThisSecond.getAndAdd(burst.length);
            }
        } else {
            log.warn("Now " + numPending + " ops pending; backing off for " + BACKOFF_SLEEP_SECS + "s!");
            try {
                Thread.sleep(BACKOFF_SLEEP_SECS * 1_000L);
            } catch (InterruptedException ignore) {
            }
        }
    }
    Map<HederaFunctionality, Integer> finalCounts = counts.entrySet().stream().filter(entry -> entry.getValue().get() > 0).collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get()));
    log.info("Final breakdown of *provided* ops: " + finalCounts);
    log.info("Final breakdown of *resolved* statuses: " + spec.finalizedStatusCounts());
    return false;
}
Also used : IntStream(java.util.stream.IntStream) LongSupplier(java.util.function.LongSupplier) Stopwatch(com.google.common.base.Stopwatch) HashMap(java.util.HashMap) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) CustomSpecAssert.allRunFor(com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) UtilVerbs.inParallel(com.hedera.services.bdd.spec.utilops.UtilVerbs.inParallel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Map(java.util.Map) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) Optional(java.util.Optional) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) IntSupplier(java.util.function.IntSupplier) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) Stopwatch(com.google.common.base.Stopwatch) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) TimeUnit(java.util.concurrent.TimeUnit) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with HederaFunctionality

use of com.hederahashgraph.api.proto.java.HederaFunctionality in project hedera-services by hashgraph.

the class DeterministicThrottling method rebuildFor.

@Override
public void rebuildFor(ThrottleDefinitions defs) {
    List<DeterministicThrottle> newActiveThrottles = new ArrayList<>();
    EnumMap<HederaFunctionality, List<Pair<DeterministicThrottle, Integer>>> reqLists = new EnumMap<>(HederaFunctionality.class);
    int n = capacitySplitSource.getAsInt();
    for (var bucket : defs.getBuckets()) {
        try {
            var mapping = bucket.asThrottleMapping(n);
            var throttle = mapping.getLeft();
            var reqs = mapping.getRight();
            for (var req : reqs) {
                reqLists.computeIfAbsent(req.getLeft(), ignore -> new ArrayList<>()).add(Pair.of(throttle, req.getRight()));
            }
            newActiveThrottles.add(throttle);
        } catch (IllegalStateException badBucket) {
            log.error("When constructing bucket '{}' from state: {}", bucket.getName(), badBucket.getMessage());
        }
    }
    EnumMap<HederaFunctionality, ThrottleReqsManager> newFunctionReqs = new EnumMap<>(HederaFunctionality.class);
    reqLists.forEach((function, reqs) -> newFunctionReqs.put(function, new ThrottleReqsManager(reqs)));
    functionReqs = newFunctionReqs;
    activeThrottles = newActiveThrottles;
    logResolvedDefinitions();
}
Also used : MiscUtils.scheduledFunctionOf(com.hedera.services.utils.MiscUtils.scheduledFunctionOf) DeterministicThrottle(com.hedera.services.throttles.DeterministicThrottle) ScheduleCreate(com.hederahashgraph.api.proto.java.HederaFunctionality.ScheduleCreate) CryptoCreate(com.hederahashgraph.api.proto.java.HederaFunctionality.CryptoCreate) TxnAccessor(com.hedera.services.utils.TxnAccessor) ArrayList(java.util.ArrayList) MiscUtils.isGasThrottled(com.hedera.services.utils.MiscUtils.isGasThrottled) Pair(org.apache.commons.lang3.tuple.Pair) IntSupplier(java.util.function.IntSupplier) CryptoTransfer(com.hederahashgraph.api.proto.java.HederaFunctionality.CryptoTransfer) GasLimitDeterministicThrottle(com.hedera.services.throttles.GasLimitDeterministicThrottle) AliasResolver(com.hedera.services.grpc.marshalling.AliasResolver) Query(com.hederahashgraph.api.proto.java.Query) EnumMap(java.util.EnumMap) Instant(java.time.Instant) SchedulableTransactionBody(com.hederahashgraph.api.proto.java.SchedulableTransactionBody) GlobalDynamicProperties(com.hedera.services.context.properties.GlobalDynamicProperties) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AliasResolver.usesAliases(com.hedera.services.grpc.marshalling.AliasResolver.usesAliases) ThrottleDefinitions(com.hedera.services.sysfiles.domain.throttling.ThrottleDefinitions) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) TokenMintTransactionBody(com.hederahashgraph.api.proto.java.TokenMintTransactionBody) TokenMint(com.hederahashgraph.api.proto.java.HederaFunctionality.TokenMint) ThrottleReqOpsScaleFactor(com.hedera.services.sysfiles.domain.throttling.ThrottleReqOpsScaleFactor) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) AliasManager(com.hedera.services.ledger.accounts.AliasManager) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) ArrayList(java.util.ArrayList) DeterministicThrottle(com.hedera.services.throttles.DeterministicThrottle) GasLimitDeterministicThrottle(com.hedera.services.throttles.GasLimitDeterministicThrottle) TokenMint(com.hederahashgraph.api.proto.java.HederaFunctionality.TokenMint) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap)

Example 4 with HederaFunctionality

use of com.hederahashgraph.api.proto.java.HederaFunctionality in project hedera-services by hashgraph.

the class UsageBasedFeeCalculatorTest method setup.

@BeforeEach
private void setup() throws Throwable {
    view = mock(StateView.class);
    query = mock(Query.class);
    payerKey = complexKey.asJKey();
    exchange = mock(HbarCentExchange.class);
    signedTxn = newSignedCryptoCreate().balance(balance).payerKt(complexKey).txnValidStart(at).get();
    accessor = new SignedTxnAccessor(signedTxn);
    usagePrices = mock(UsagePricesProvider.class);
    given(usagePrices.activePrices(accessor)).willReturn(currentPrices);
    correctOpEstimator = mock(TxnResourceUsageEstimator.class);
    incorrectOpEstimator = mock(TxnResourceUsageEstimator.class);
    correctQueryEstimator = mock(QueryResourceUsageEstimator.class);
    incorrectQueryEstimator = mock(QueryResourceUsageEstimator.class);
    autoRenewCalcs = mock(AutoRenewCalcs.class);
    pricedUsageCalculator = mock(PricedUsageCalculator.class);
    txnUsageEstimators = (Map<HederaFunctionality, List<TxnResourceUsageEstimator>>) mock(Map.class);
    subject = new UsageBasedFeeCalculator(autoRenewCalcs, exchange, mock(AutoCreationLogic.class), usagePrices, new NestedMultiplierSource(), pricedUsageCalculator, Set.of(incorrectQueryEstimator, correctQueryEstimator), txnUsageEstimators);
}
Also used : HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Query(com.hederahashgraph.api.proto.java.Query) StateView(com.hedera.services.context.primitives.StateView) SignedTxnAccessor(com.hedera.services.utils.SignedTxnAccessor) HbarCentExchange(com.hedera.services.fees.HbarCentExchange) List(java.util.List) PricedUsageCalculator(com.hedera.services.fees.calculation.utils.PricedUsageCalculator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with HederaFunctionality

use of com.hederahashgraph.api.proto.java.HederaFunctionality in project hedera-services by hashgraph.

the class GasCalculatorHederaUtil method logOperationGasCost.

@SuppressWarnings("unused")
public static Gas logOperationGasCost(final UsagePricesProvider usagePrices, final HbarCentExchange exchange, final MessageFrame frame, final long storageDuration, final long dataOffset, final long dataLength, final int numTopics) {
    long gasPrice = frame.getGasPrice().toLong();
    long timestamp = frame.getBlockValues().getTimestamp();
    long logStorageTotalSize = GasCalculatorHederaUtil.calculateLogSize(numTopics, dataLength);
    HederaFunctionality functionType = GasCalculatorHederaUtil.getFunctionType(frame);
    long gasCost = GasCalculatorHederaUtil.calculateStorageGasNeeded(logStorageTotalSize, storageDuration, GasCalculatorHederaUtil.ramByteHoursTinyBarsGiven(usagePrices, exchange, timestamp, functionType), gasPrice);
    return Gas.of(gasCost);
}
Also used : HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality)

Aggregations

HederaFunctionality (com.hederahashgraph.api.proto.java.HederaFunctionality)5 Query (com.hederahashgraph.api.proto.java.Query)2 Function (java.util.function.Function)2 IntSupplier (java.util.function.IntSupplier)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Stopwatch (com.google.common.base.Stopwatch)1 HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)1 HapiSpecOperation (com.hedera.services.bdd.spec.HapiSpecOperation)1 OpProvider (com.hedera.services.bdd.spec.infrastructure.OpProvider)1 CustomSpecAssert.allRunFor (com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor)1 UtilVerbs.inParallel (com.hedera.services.bdd.spec.utilops.UtilVerbs.inParallel)1 StateView (com.hedera.services.context.primitives.StateView)1 GlobalDynamicProperties (com.hedera.services.context.properties.GlobalDynamicProperties)1 HbarCentExchange (com.hedera.services.fees.HbarCentExchange)1 PricedUsageCalculator (com.hedera.services.fees.calculation.utils.PricedUsageCalculator)1 AliasResolver (com.hedera.services.grpc.marshalling.AliasResolver)1 AliasResolver.usesAliases (com.hedera.services.grpc.marshalling.AliasResolver.usesAliases)1 AliasManager (com.hedera.services.ledger.accounts.AliasManager)1 ThrottleDefinitions (com.hedera.services.sysfiles.domain.throttling.ThrottleDefinitions)1