Search in sources :

Example 6 with UsageAccumulator

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

the class CryptoOpsUsageTest method accumulatesBptAndRbhAsExpectedForCryptoCreateWithoutMaxAutoAssociations.

@Test
void accumulatesBptAndRbhAsExpectedForCryptoCreateWithoutMaxAutoAssociations() {
    givenCreationOpWithOutMaxAutoAssociaitons();
    final ByteString canonicalSig = ByteString.copyFromUtf8("0123456789012345678901234567890123456789012345678901234567890123");
    final SignatureMap onePairSigMap = SignatureMap.newBuilder().addSigPair(SignaturePair.newBuilder().setPubKeyPrefix(ByteString.copyFromUtf8("a")).setEd25519(canonicalSig)).build();
    final SigUsage singleSigUsage = new SigUsage(1, onePairSigMap.getSerializedSize(), 1);
    final var opMeta = new CryptoCreateMeta(txn.getCryptoCreateAccount());
    final var baseMeta = new BaseTransactionMeta(memo.length(), 0);
    var actual = new UsageAccumulator();
    var expected = new UsageAccumulator();
    var baseSize = memo.length() + getAccountKeyStorageSize(key) + BASIC_ENTITY_ID_SIZE;
    expected.resetForTransaction(baseMeta, singleSigUsage);
    expected.addBpt(baseSize + 2 * LONG_SIZE + BOOL_SIZE);
    expected.addRbs((CRYPTO_ENTITY_SIZES.fixedBytesInAccountRepr() + baseSize) * secs);
    expected.addNetworkRbs(BASIC_ENTITY_ID_SIZE * USAGE_PROPERTIES.legacyReceiptStorageSecs());
    subject.cryptoCreateUsage(singleSigUsage, baseMeta, opMeta, actual);
    assertEquals(expected, actual);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) ByteString(com.google.protobuf.ByteString) SigUsage(com.hedera.services.usage.SigUsage) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) SignatureMap(com.hederahashgraph.api.proto.java.SignatureMap) Test(org.junit.jupiter.api.Test)

Example 7 with UsageAccumulator

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

the class CryptoOpsUsageTest method accumulatesBptAndRbhAsExpectedForCryptoCreateWithMaxAutoAssociations.

@Test
void accumulatesBptAndRbhAsExpectedForCryptoCreateWithMaxAutoAssociations() {
    givenCreationOpWithMaxAutoAssociaitons();
    final ByteString canonicalSig = ByteString.copyFromUtf8("0123456789012345678901234567890123456789012345678901234567890123");
    final SignatureMap onePairSigMap = SignatureMap.newBuilder().addSigPair(SignaturePair.newBuilder().setPubKeyPrefix(ByteString.copyFromUtf8("a")).setEd25519(canonicalSig)).build();
    final SigUsage singleSigUsage = new SigUsage(1, onePairSigMap.getSerializedSize(), 1);
    final var opMeta = new CryptoCreateMeta(txn.getCryptoCreateAccount());
    final var baseMeta = new BaseTransactionMeta(memo.length(), 0);
    var actual = new UsageAccumulator();
    var expected = new UsageAccumulator();
    var baseSize = memo.length() + getAccountKeyStorageSize(key) + BASIC_ENTITY_ID_SIZE + INT_SIZE;
    expected.resetForTransaction(baseMeta, singleSigUsage);
    expected.addBpt(baseSize + 2 * LONG_SIZE + BOOL_SIZE);
    expected.addRbs((CRYPTO_ENTITY_SIZES.fixedBytesInAccountRepr() + baseSize) * secs);
    expected.addRbs(maxAutoAssociations * secs * CryptoOpsUsage.CREATE_SLOT_MULTIPLIER);
    expected.addNetworkRbs(BASIC_ENTITY_ID_SIZE * USAGE_PROPERTIES.legacyReceiptStorageSecs());
    subject.cryptoCreateUsage(singleSigUsage, baseMeta, opMeta, actual);
    assertEquals(expected, actual);
}
Also used : BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) ByteString(com.google.protobuf.ByteString) SigUsage(com.hedera.services.usage.SigUsage) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) SignatureMap(com.hederahashgraph.api.proto.java.SignatureMap) Test(org.junit.jupiter.api.Test)

Example 8 with UsageAccumulator

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

the class CryptoOpsUsageTest method estimatesUpdateWithOutAutoAssociationsAsExpected.

@Test
void estimatesUpdateWithOutAutoAssociationsAsExpected() {
    givenUpdateOpWithOutMaxAutoAssociations();
    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";
    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 keyBytesUsed = getAccountKeyStorageSize(key);
    long msgBytesUsed = BASIC_ENTITY_ID_SIZE + memo.getBytes().length + keyBytesUsed + LONG_SIZE + BASIC_ENTITY_ID_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(maxAutoAssociations * 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 9 with UsageAccumulator

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

the class CryptoOpsUsageTest method estimatesAdjustAsExpected.

@Test
void estimatesAdjustAsExpected() {
    givenAdjustOp();
    GrantedCryptoAllowance existingCryptoAllowances = GrantedCryptoAllowance.newBuilder().setSpender(proxy).setAmount(100L).build();
    GrantedTokenAllowance existingTokenAllowances = GrantedTokenAllowance.newBuilder().setSpender(proxy).setAmount(100L).setTokenId(IdUtils.asToken("0.0.1000")).build();
    GrantedNftAllowance existingNftAllowances = GrantedNftAllowance.newBuilder().setSpender(proxy).setTokenId(IdUtils.asToken("0.0.1000")).addAllSerialNumbers(List.of()).build();
    var expected = new UsageAccumulator();
    var baseMeta = new BaseTransactionMeta(0, 0);
    var opMeta = new CryptoAdjustAllowanceMeta(txn.getCryptoAdjustAllowance(), 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(List.of(existingCryptoAllowances)).setCurrentNftAllowances(List.of(existingNftAllowances)).setCurrentTokenAllowances(List.of(existingTokenAllowances)).build();
    long msgBytesUsed = (adjustOp.getCryptoAllowancesCount() * CRYPTO_ALLOWANCE_SIZE) + (adjustOp.getTokenAllowancesCount() * TOKEN_ALLOWANCE_SIZE) + (adjustOp.getNftAllowancesCount() * NFT_ALLOWANCE_SIZE) + countSerials(adjustOp.getNftAllowancesList()) * LONG_SIZE;
    expected.addBpt(msgBytesUsed);
    long lifetime = ESTIMATOR_UTILS.relativeLifetime(txn, oldExpiry);
    var expectedAdjustedBytes = LONG_SIZE;
    // 3990128
    expected.addRbs(expectedAdjustedBytes * lifetime);
    var actual = new UsageAccumulator();
    subject.cryptoAdjustAllowanceUsage(sigUsage, baseMeta, opMeta, ctx, actual);
    assertEquals(expected, actual);
}
Also used : GrantedTokenAllowance(com.hederahashgraph.api.proto.java.GrantedTokenAllowance) BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) GrantedCryptoAllowance(com.hederahashgraph.api.proto.java.GrantedCryptoAllowance) SigUsage(com.hedera.services.usage.SigUsage) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) ByteString(com.google.protobuf.ByteString) GrantedNftAllowance(com.hederahashgraph.api.proto.java.GrantedNftAllowance) Key(com.hederahashgraph.api.proto.java.Key) Test(org.junit.jupiter.api.Test)

Example 10 with UsageAccumulator

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

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