Search in sources :

Example 11 with BaseTransactionMeta

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

the class CryptoOpsUsageTest method estimatesUpdateWithAutoAssociationsAsExpected.

@Test
void estimatesUpdateWithAutoAssociationsAsExpected() {
    givenUpdateOpWithMaxAutoAssociations();
    var expected = new UsageAccumulator();
    var baseMeta = new BaseTransactionMeta(memo.length(), 0);
    var opMeta = new CryptoUpdateMeta(txn.getCryptoUpdateAccount(), txn.getTransactionID().getTransactionValidStart().getSeconds());
    expected.resetForTransaction(baseMeta, sigUsage);
    Key oldKey = FileOpsUsage.asKey(KeyUtils.A_KEY_LIST.getKeyList());
    long oldExpiry = expiry - 1_234L;
    String oldMemo = "Lettuce";
    int oldMaxAutoAssociations = maxAutoAssociations - 5;
    var ctx = ExtantCryptoContext.newBuilder().setCurrentExpiry(oldExpiry).setCurrentMemo(oldMemo).setCurrentKey(oldKey).setCurrentlyHasProxy(false).setCurrentNumTokenRels(numTokenRels).setCurrentMaxAutomaticAssociations(oldMaxAutoAssociations).setCurrentCryptoAllowances(Collections.emptyList()).setCurrentNftAllowances(Collections.emptyList()).setCurrentTokenAllowances(Collections.emptyList()).build();
    long keyBytesUsed = getAccountKeyStorageSize(key);
    long msgBytesUsed = BASIC_ENTITY_ID_SIZE + memo.getBytes().length + keyBytesUsed + LONG_SIZE + BASIC_ENTITY_ID_SIZE + INT_SIZE;
    expected.addBpt(msgBytesUsed);
    long newVariableBytes = memo.getBytes().length + keyBytesUsed + BASIC_ENTITY_ID_SIZE;
    long tokenRelBytes = numTokenRels * CRYPTO_ENTITY_SIZES.bytesInTokenAssocRepr();
    long sharedFixedBytes = CRYPTO_ENTITY_SIZES.fixedBytesInAccountRepr() + tokenRelBytes;
    long newLifetime = ESTIMATOR_UTILS.relativeLifetime(txn, expiry);
    long oldLifetime = ESTIMATOR_UTILS.relativeLifetime(txn, oldExpiry);
    long rbsDelta = ESTIMATOR_UTILS.changeInBsUsage(CRYPTO_ENTITY_SIZES.fixedBytesInAccountRepr() + ctx.currentNonBaseRb() + ctx.currentNumTokenRels() * CRYPTO_ENTITY_SIZES.bytesInTokenAssocRepr(), oldLifetime, sharedFixedBytes + newVariableBytes, newLifetime);
    if (rbsDelta > 0) {
        expected.addRbs(rbsDelta);
    }
    final var slotDelta = ESTIMATOR_UTILS.changeInBsUsage(oldMaxAutoAssociations * CryptoOpsUsage.UPDATE_SLOT_MULTIPLIER, oldLifetime, maxAutoAssociations * CryptoOpsUsage.UPDATE_SLOT_MULTIPLIER, newLifetime);
    expected.addRbs(slotDelta);
    var actual = new UsageAccumulator();
    subject.cryptoUpdateUsage(sigUsage, baseMeta, opMeta, ctx, actual);
    assertEquals(expected, actual);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) 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 BaseTransactionMeta

use of com.hedera.services.usage.BaseTransactionMeta 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 13 with BaseTransactionMeta

use of com.hedera.services.usage.BaseTransactionMeta 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 14 with BaseTransactionMeta

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

the class UsageAccumulatorTest method understandsNodePartitioning.

@Test
void understandsNodePartitioning() {
    // when:
    subject.resetForTransaction(new BaseTransactionMeta(memoBytes, numTransfers), sigUsage);
    subject.addBpt(1);
    subject.addBpr(2);
    subject.addSbpr(3);
    // then:
    assertEquals(baseBpt + 1, subject.getUniversalBpt());
    assertEquals(baseBpt + 1, subject.get(NODE, BPT));
    assertEquals(baseBpr + 2, subject.getNodeBpr());
    assertEquals(baseBpr + 2, subject.get(NODE, BPR));
    assertEquals(3, subject.getNodeSbpr());
    assertEquals(3, subject.get(NODE, SBPR));
    assertEquals(sigUsage.numPayerKeys(), subject.getNodeVpt());
    assertEquals(sigUsage.numPayerKeys(), subject.get(NODE, VPT));
    assertEquals(1, subject.get(NODE, CONSTANT));
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) Test(org.junit.jupiter.api.Test)

Example 15 with BaseTransactionMeta

use of com.hedera.services.usage.BaseTransactionMeta 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)

Aggregations

BaseTransactionMeta (com.hedera.services.usage.BaseTransactionMeta)19 UsageAccumulator (com.hedera.services.usage.state.UsageAccumulator)17 Test (org.junit.jupiter.api.Test)10 TokenOpsUsage (com.hedera.services.usage.token.TokenOpsUsage)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