Search in sources :

Example 11 with UsageAccumulator

use of com.hedera.services.usage.state.UsageAccumulator in project hedera-services by hashgraph.

the class CryptoOpsUsageTest method estimatesApprovalAsExpected.

@Test
void estimatesApprovalAsExpected() {
    givenApprovalOp();
    var expected = new UsageAccumulator();
    var baseMeta = new BaseTransactionMeta(0, 0);
    var opMeta = new CryptoApproveAllowanceMeta(txn.getCryptoApproveAllowance(), txn.getTransactionID().getTransactionValidStart().getSeconds());
    SigUsage sigUsage = new SigUsage(1, sigSize, 1);
    expected.resetForTransaction(baseMeta, sigUsage);
    Key oldKey = FileOpsUsage.asKey(KeyUtils.A_KEY_LIST.getKeyList());
    long oldExpiry = expiry - 1_234L;
    String oldMemo = "Lettuce";
    var ctx = ExtantCryptoContext.newBuilder().setCurrentExpiry(oldExpiry).setCurrentMemo(oldMemo).setCurrentKey(oldKey).setCurrentlyHasProxy(false).setCurrentNumTokenRels(numTokenRels).setCurrentMaxAutomaticAssociations(maxAutoAssociations).setCurrentCryptoAllowances(Collections.emptyList()).setCurrentNftAllowances(Collections.emptyList()).setCurrentTokenAllowances(Collections.emptyList()).build();
    long msgBytesUsed = (approveOp.getCryptoAllowancesCount() * CRYPTO_ALLOWANCE_SIZE) + (approveOp.getTokenAllowancesCount() * TOKEN_ALLOWANCE_SIZE) + (approveOp.getNftAllowancesCount() * NFT_ALLOWANCE_SIZE) + countSerials(approveOp.getNftAllowancesList()) * LONG_SIZE;
    expected.addBpt(msgBytesUsed);
    long lifetime = ESTIMATOR_UTILS.relativeLifetime(txn, oldExpiry);
    expected.addRbs(msgBytesUsed * lifetime);
    var actual = new UsageAccumulator();
    subject.cryptoApproveAllowanceUsage(sigUsage, baseMeta, opMeta, ctx, actual);
    assertEquals(expected, actual);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) SigUsage(com.hedera.services.usage.SigUsage) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) ByteString(com.google.protobuf.ByteString) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 12 with UsageAccumulator

use of com.hedera.services.usage.state.UsageAccumulator in project hedera-services by hashgraph.

the class OpsTransferUsageTest method matchesWithLegacyEstimate.

@Test
void matchesWithLegacyEstimate() {
    givenOp();
    // and given legacy estimate:
    final var expected = FeeData.newBuilder().setNetworkdata(FeeComponents.newBuilder().setConstant(1).setBpt(18047).setVpt(3).setRbh(1)).setNodedata(FeeComponents.newBuilder().setConstant(1).setBpt(18047).setVpt(1).setBpr(4)).setServicedata(FeeComponents.newBuilder().setConstant(1).setRbh(904)).build();
    // when:
    final var accum = new UsageAccumulator();
    subject.cryptoTransferUsage(sigUsage, new CryptoTransferMeta(tokenMultiplier, 3, 7, 0), new BaseTransactionMeta(memo.getBytes().length, 3), accum);
    // then:
    assertEquals(expected, feeDataFrom(accum));
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) Test(org.junit.jupiter.api.Test)

Example 13 with UsageAccumulator

use of com.hedera.services.usage.state.UsageAccumulator in project hedera-services by hashgraph.

the class HapiCryptoUpdate method feeFor.

@Override
protected long feeFor(HapiApiSpec spec, Transaction txn, int numPayerKeys) throws Throwable {
    try {
        final CryptoGetInfoResponse.AccountInfo info = lookupInfo(spec);
        FeeCalculator.ActivityMetrics metricsCalc = (_txn, svo) -> {
            var ctx = ExtantCryptoContext.newBuilder().setCurrentNumTokenRels(info.getTokenRelationshipsCount()).setCurrentExpiry(info.getExpirationTime().getSeconds()).setCurrentMemo(info.getMemo()).setCurrentKey(info.getKey()).setCurrentlyHasProxy(info.hasProxyAccountID()).setCurrentMaxAutomaticAssociations(info.getMaxAutomaticTokenAssociations()).build();
            var baseMeta = new BaseTransactionMeta(_txn.getMemoBytes().size(), 0);
            var opMeta = new CryptoUpdateMeta(_txn.getCryptoUpdateAccount(), _txn.getTransactionID().getTransactionValidStart().getSeconds());
            var accumulator = new UsageAccumulator();
            cryptoOpsUsage.cryptoUpdateUsage(suFrom(svo), baseMeta, opMeta, ctx, accumulator);
            return AdapterUtils.feeDataFrom(accumulator);
        };
        return spec.fees().forActivityBasedOp(HederaFunctionality.CryptoUpdate, metricsCalc, txn, numPayerKeys);
    } catch (Throwable ignore) {
        return HapiApiSuite.ONE_HBAR;
    }
}
Also used : CryptoGetInfoResponse(com.hederahashgraph.api.proto.java.CryptoGetInfoResponse) Transaction(com.hederahashgraph.api.proto.java.Transaction) CryptoGetInfoResponse(com.hederahashgraph.api.proto.java.CryptoGetInfoResponse) BoolValue(com.google.protobuf.BoolValue) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) UInt64Value(com.google.protobuf.UInt64Value) StringValue(com.google.protobuf.StringValue) Duration(com.hederahashgraph.api.proto.java.Duration) Function(java.util.function.Function) TxnUtils.defaultUpdateSigners(com.hedera.services.bdd.spec.transactions.TxnUtils.defaultUpdateSigners) OptionalLong(java.util.OptionalLong) PropertySource.asAccountString(com.hedera.services.bdd.spec.PropertySource.asAccountString) BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) QueryVerbs.getAccountInfo(com.hedera.services.bdd.spec.queries.QueryVerbs.getAccountInfo) TxnUtils(com.hedera.services.bdd.spec.transactions.TxnUtils) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) TransactionResponse(com.hederahashgraph.api.proto.java.TransactionResponse) CryptoUpdateMeta(com.hedera.services.usage.crypto.CryptoUpdateMeta) ReferenceType(com.hedera.services.bdd.spec.queries.crypto.ReferenceType) AccountID(com.hederahashgraph.api.proto.java.AccountID) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) CryptoUpdateTransactionBody(com.hederahashgraph.api.proto.java.CryptoUpdateTransactionBody) Int32Value(com.google.protobuf.Int32Value) MoreObjects(com.google.common.base.MoreObjects) HapiPropertySource(com.hedera.services.bdd.spec.HapiPropertySource) ExtantCryptoContext(com.hedera.services.usage.crypto.ExtantCryptoContext) SUCCESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS) HapiGetAccountInfo(com.hedera.services.bdd.spec.queries.crypto.HapiGetAccountInfo) Consumer(java.util.function.Consumer) Key(com.hederahashgraph.api.proto.java.Key) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TxnUtils.asIdForKeyLookUp(com.hedera.services.bdd.spec.transactions.TxnUtils.asIdForKeyLookUp) AdapterUtils(com.hedera.services.bdd.spec.fees.AdapterUtils) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) TxnUtils.suFrom(com.hedera.services.bdd.spec.transactions.TxnUtils.suFrom) FeeCalculator(com.hedera.services.bdd.spec.fees.FeeCalculator) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) FeeCalculator(com.hedera.services.bdd.spec.fees.FeeCalculator) CryptoUpdateMeta(com.hedera.services.usage.crypto.CryptoUpdateMeta) BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator)

Example 14 with UsageAccumulator

use of com.hedera.services.usage.state.UsageAccumulator in project hedera-services by hashgraph.

the class HapiTokenMint method usageEstimate.

private FeeData usageEstimate(TransactionBody txn, SigValueObj svo) throws Throwable {
    UsageAccumulator accumulator = new UsageAccumulator();
    long lifetime = 0L;
    if (subType == SubType.TOKEN_NON_FUNGIBLE_UNIQUE) {
        lifetime = info.getExpiry().getSeconds() - txn.getTransactionID().getTransactionValidStart().getSeconds();
    }
    final var tokenBurnMeta = TOKEN_OPS_USAGE_UTILS.tokenMintUsageFrom(txn, subType, lifetime);
    final var baseTransactionMeta = new BaseTransactionMeta(txn.getMemoBytes().size(), 0);
    TokenOpsUsage tokenOpsUsage = new TokenOpsUsage();
    tokenOpsUsage.tokenMintUsage(suFrom(svo), baseTransactionMeta, tokenBurnMeta, accumulator);
    return AdapterUtils.feeDataFrom(accumulator);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) TokenOpsUsage(com.hedera.services.usage.token.TokenOpsUsage)

Example 15 with UsageAccumulator

use of com.hedera.services.usage.state.UsageAccumulator in project hedera-services by hashgraph.

the class HapiTokenPause method usageEstimate.

private FeeData usageEstimate(TransactionBody txn, SigValueObj svo) {
    UsageAccumulator accumulator = new UsageAccumulator();
    final var tokenPauseMeta = TOKEN_OPS_USAGE_UTILS.tokenPauseUsageFrom();
    final var baseTransactionMeta = new BaseTransactionMeta(txn.getMemoBytes().size(), 0);
    TokenOpsUsage tokenOpsUsage = new TokenOpsUsage();
    tokenOpsUsage.tokenPauseUsage(suFrom(svo), baseTransactionMeta, tokenPauseMeta, accumulator);
    return AdapterUtils.feeDataFrom(accumulator);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) TokenOpsUsage(com.hedera.services.usage.token.TokenOpsUsage)

Aggregations

BaseTransactionMeta (com.hedera.services.usage.BaseTransactionMeta)17 UsageAccumulator (com.hedera.services.usage.state.UsageAccumulator)17 TokenOpsUsage (com.hedera.services.usage.token.TokenOpsUsage)8 Test (org.junit.jupiter.api.Test)8 ByteString (com.google.protobuf.ByteString)6 Key (com.hederahashgraph.api.proto.java.Key)5 SigUsage (com.hedera.services.usage.SigUsage)4 SignatureMap (com.hederahashgraph.api.proto.java.SignatureMap)2 MoreObjects (com.google.common.base.MoreObjects)1 BoolValue (com.google.protobuf.BoolValue)1 Int32Value (com.google.protobuf.Int32Value)1 StringValue (com.google.protobuf.StringValue)1 UInt64Value (com.google.protobuf.UInt64Value)1 HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)1 HapiPropertySource (com.hedera.services.bdd.spec.HapiPropertySource)1 PropertySource.asAccountString (com.hedera.services.bdd.spec.PropertySource.asAccountString)1 AdapterUtils (com.hedera.services.bdd.spec.fees.AdapterUtils)1 FeeCalculator (com.hedera.services.bdd.spec.fees.FeeCalculator)1 QueryVerbs.getAccountInfo (com.hedera.services.bdd.spec.queries.QueryVerbs.getAccountInfo)1 HapiGetAccountInfo (com.hedera.services.bdd.spec.queries.crypto.HapiGetAccountInfo)1