use of com.hederahashgraph.api.proto.java.Key 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);
}
use of com.hederahashgraph.api.proto.java.Key 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);
}
use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.
the class RequestBuilderTest method assertGetFileCreateBuilder.
@Test
void assertGetFileCreateBuilder() throws InvalidProtocolBufferException {
Timestamp fileExpiration = Timestamp.newBuilder().setSeconds(123_456L).build();
List<Key> keyList = List.of(validED25519Key);
var transaction = RequestBuilder.getFileCreateBuilder(accountId.getAccountNum(), accountId.getRealmNum(), accountId.getShardNum(), nodeId.getAccountNum(), nodeId.getRealmNum(), nodeId.getShardNum(), transactionFee, startTime, transactionDuration, generateRecord, memo, hash, fileExpiration, keyList);
var tb = TransactionBody.parseFrom(transaction.getBodyBytes());
assertEquals(memo, tb.getMemo());
assertEquals(generateRecord, tb.getGenerateRecord());
assertEquals(transactionFee, tb.getTransactionFee());
assertEquals(startTime, tb.getTransactionID().getTransactionValidStart());
assertEquals(transactionDuration, tb.getTransactionValidDuration());
assertEquals(transactionFee, tb.getTransactionFee());
assertEquals(hash, tb.getFileCreate().getContents());
assertEquals(fileExpiration, tb.getFileCreate().getExpirationTime());
assertEquals(nodeId.getAccountNum(), tb.getNodeAccountID().getAccountNum());
assertEquals(nodeId.getRealmNum(), tb.getNodeAccountID().getRealmNum());
assertEquals(nodeId.getShardNum(), tb.getNodeAccountID().getShardNum());
assertEquals(accountId.getAccountNum(), tb.getTransactionID().getAccountID().getAccountNum());
assertEquals(accountId.getRealmNum(), tb.getTransactionID().getAccountID().getRealmNum());
assertEquals(accountId.getShardNum(), tb.getTransactionID().getAccountID().getShardNum());
}
use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.
the class SpecKey method registerPemWith.
private void registerPemWith(HapiApiSpec spec, RegistryForms forms) {
var qPemLoc = qualifiedKeyLoc(pemLoc, spec);
var aes256EncryptedPkcs8Pem = new File(qPemLoc);
if (!aes256EncryptedPkcs8Pem.exists()) {
if (!generateIfMissing) {
throw new IllegalStateException(String.format("File missing at PEM loc '%s'!", qPemLoc));
}
Key simpleKey = spec.keys().generate(spec, KeyFactory.KeyType.SIMPLE);
forms.completeIntake(spec.registry(), simpleKey);
try {
spec.keys().exportSimpleKey(qPemLoc, forms.name(), passphrase);
log.info("Created new simple key at PEM loc '{}'.", qPemLoc);
} catch (KeyStoreException e) {
throw new IllegalStateException(String.format("Cannot generate key to PEM loc '%s'!", qPemLoc), e);
}
return;
}
var keyPair = readFirstKpFromPem(aes256EncryptedPkcs8Pem, passphrase);
var publicKey = (EdDSAPublicKey) keyPair.getPublic();
var hederaKey = asSimpleHederaKey(publicKey.getAbyte());
forms.completeIntake(spec.registry(), hederaKey);
/* When we incorporate the key into the spec's key factory, it will:
(1) Update the mapping from hexed public keys to PrivateKeys; and,
(2) Set the given SigControl as default for signing requests with the Key. */
spec.keys().incorporate(forms.name(), CommonUtils.hex(publicKey.getAbyte()), keyPair.getPrivate(), SigControl.ON);
}
use of com.hederahashgraph.api.proto.java.Key in project hedera-services by hashgraph.
the class LedgerBalanceChangesTest method happyPathTransfersWithAutoCreation.
@Test
void happyPathTransfersWithAutoCreation() {
final Key aliasA = KeyFactory.getDefaultInstance().newEd25519();
final AccountID a = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(10L).build();
final AccountID validAliasAccountWithAlias = AccountID.newBuilder().setAlias(aliasA.toByteString()).build();
final AccountID validAliasAccountWithId = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(11L).build();
final AccountID funding = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(98L).build();
final EntityNum validAliasEntityNum = new EntityNum(11);
List<BalanceChange> changes = new ArrayList<>();
changes.add(hbarChange(a, -100));
changes.add(hbarChange(validAliasAccountWithAlias, 0));
final var validAliasAccount = MerkleAccountFactory.newAccount().get();
final var fundingAccount = MerkleAccountFactory.newAccount().get();
final var aAccount = MerkleAccountFactory.newAccount().balance(aStartBalance).get();
backingAccounts.put(a, aAccount);
backingAccounts.put(validAliasAccountWithId, validAliasAccount);
backingAccounts.put(funding, fundingAccount);
given(autoCreationLogic.create(any(), eq(accountsLedger))).willAnswer(invocationOnMock -> {
final var change = (BalanceChange) invocationOnMock.getArgument(0);
change.replaceAliasWith(validAliasEntityNum.toGrpcAccountId());
return Pair.of(OK, 100L);
});
given(dynamicProperties.fundingAccount()).willReturn(funding);
subject.begin();
assertDoesNotThrow(() -> subject.doZeroSum(changes));
subject.commit();
assertEquals(aStartBalance - 100, backingAccounts.getImmutableRef(a).getBalance());
assertEquals(0, backingAccounts.getImmutableRef(validAliasEntityNum.toGrpcAccountId()).getBalance());
}
Aggregations