Search in sources :

Example 1 with FcTokenAllowance

use of com.hedera.services.state.submerkle.FcTokenAllowance in project hedera-services by hashgraph.

the class GetAccountInfoAnswerTest method setup.

@BeforeEach
private void setup() throws Throwable {
    tokenRels = new MerkleMap<>();
    tokenRels.put(fromAccountTokenRel(payerId, firstToken), new MerkleTokenRelStatus(firstBalance, true, true, true));
    tokenRels.put(fromAccountTokenRel(payerId, secondToken), new MerkleTokenRelStatus(secondBalance, false, false, true));
    tokenRels.put(fromAccountTokenRel(payerId, thirdToken), new MerkleTokenRelStatus(thirdBalance, true, true, false));
    tokenRels.put(fromAccountTokenRel(payerId, fourthToken), new MerkleTokenRelStatus(fourthBalance, false, false, true));
    tokenRels.put(fromAccountTokenRel(payerId, missingToken), new MerkleTokenRelStatus(missingBalance, false, false, false));
    var tokens = new MerkleAccountTokens();
    tokens.associateAll(Set.of(firstToken, secondToken, thirdToken, fourthToken, missingToken));
    var tokenAllowanceKey = FcTokenAllowanceId.from(EntityNum.fromLong(1000L), EntityNum.fromLong(2000L));
    var tokenAllowanceValue = FcTokenAllowance.from(false, List.of(1L, 2L));
    TreeMap<EntityNum, Long> cryptoAllowances = new TreeMap();
    TreeMap<FcTokenAllowanceId, Long> fungibleTokenAllowances = new TreeMap();
    TreeMap<FcTokenAllowanceId, FcTokenAllowance> nftAllowances = new TreeMap();
    cryptoAllowances.put(EntityNum.fromLong(1L), 10L);
    fungibleTokenAllowances.put(tokenAllowanceKey, 20L);
    nftAllowances.put(tokenAllowanceKey, tokenAllowanceValue);
    payerAccount = MerkleAccountFactory.newAccount().accountKeys(COMPLEX_KEY_ACCOUNT_KT).memo(memo).proxy(asAccount("1.2.3")).senderThreshold(1_234L).receiverThreshold(4_321L).receiverSigRequired(true).balance(555L).autoRenewPeriod(1_000_000L).expirationTime(9_999_999L).cryptoAllowances(cryptoAllowances).fungibleTokenAllowances(fungibleTokenAllowances).nftAllowances(nftAllowances).get();
    payerAccount.setTokens(tokens);
    final MutableStateChildren children = new MutableStateChildren();
    children.setAccounts(accounts);
    children.setTokenAssociations(tokenRels);
    view = new StateView(tokenStore, scheduleStore, children, networkInfo);
    subject = new GetAccountInfoAnswer(optionValidator, aliasManager);
}
Also used : MerkleAccountTokens(com.hedera.services.state.merkle.MerkleAccountTokens) StateView(com.hedera.services.context.primitives.StateView) TreeMap(java.util.TreeMap) EntityNum(com.hedera.services.utils.EntityNum) MerkleTokenRelStatus(com.hedera.services.state.merkle.MerkleTokenRelStatus) FcTokenAllowanceId(com.hedera.services.state.submerkle.FcTokenAllowanceId) MutableStateChildren(com.hedera.services.context.MutableStateChildren) FcTokenAllowance(com.hedera.services.state.submerkle.FcTokenAllowance) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with FcTokenAllowance

use of com.hedera.services.state.submerkle.FcTokenAllowance in project hedera-services by hashgraph.

the class CryptoApproveAllowanceTransitionLogic method applyNftAllowances.

/**
 * Applies all changes needed for NFT allowances from the transaction
 *
 * @param nftAllowances
 * @param payerAccount
 */
private void applyNftAllowances(final List<NftAllowance> nftAllowances, final Account payerAccount) {
    if (nftAllowances.isEmpty()) {
        return;
    }
    for (var allowance : nftAllowances) {
        final var owner = allowance.getOwner();
        final var accountToApprove = fetchOwnerAccount(owner, payerAccount, accountStore, entitiesChanged);
        final var nftMap = accountToApprove.getMutableNftAllowances();
        final var spender = Id.fromGrpcAccount(allowance.getSpender());
        accountStore.loadAccountOrFailWith(spender, INVALID_ALLOWANCE_SPENDER_ID);
        final var approvedForAll = allowance.getApprovedForAll();
        final var serialNums = allowance.getSerialNumbersList();
        final var tokenId = allowance.getTokenId();
        final var key = FcTokenAllowanceId.from(EntityNum.fromTokenId(tokenId), spender.asEntityNum());
        if (nftMap.containsKey(key)) {
            // No-Op need to submit adjustAllowance to adjust any allowances
            continue;
        }
        final FcTokenAllowance value = approvedForAll.getValue() ? FcTokenAllowance.from(true) : FcTokenAllowance.from(serialNums);
        nftMap.put(key, value);
        validateFalse(exceedsAccountLimit(accountToApprove), MAX_ALLOWANCES_EXCEEDED);
        entitiesChanged.put(accountToApprove.getId().num(), accountToApprove);
    }
}
Also used : FcTokenAllowance(com.hedera.services.state.submerkle.FcTokenAllowance)

Example 3 with FcTokenAllowance

use of com.hedera.services.state.submerkle.FcTokenAllowance in project hedera-services by hashgraph.

the class MerkleAccountPropertyTest method gettersAndSettersWork.

@Test
void gettersAndSettersWork() throws Exception {
    final boolean origIsDeleted = false;
    final boolean origIsReceiverSigReq = false;
    final boolean origIsContract = false;
    final long origBalance = 1L;
    final long origAutoRenew = 1L;
    final long origNumNfts = 123L;
    final long origExpiry = 1L;
    final int origMaxAutoAssociations = 10;
    final int origAlreadyUsedAutoAssociations = 7;
    final var origKey = SignedTxnFactory.DEFAULT_PAYER_KT.asKey();
    final String origMemo = "a";
    final var origProxy = AccountID.getDefaultInstance();
    final List<ExpirableTxnRecord> origRecords = new ArrayList<>();
    origRecords.add(expirableRecord(ResponseCodeEnum.MODIFYING_IMMUTABLE_CONTRACT));
    origRecords.add(expirableRecord(ResponseCodeEnum.INVALID_PAYER_SIGNATURE));
    final List<ExpirableTxnRecord> origPayerRecords = new ArrayList<>();
    origPayerRecords.add(expirableRecord(ResponseCodeEnum.INVALID_CHUNK_NUMBER));
    origPayerRecords.add(expirableRecord(ResponseCodeEnum.INSUFFICIENT_TX_FEE));
    final boolean newIsDeleted = true;
    final boolean newIsReceiverSigReq = true;
    final boolean newIsContract = true;
    final long newBalance = 2L;
    final long newAutoRenew = 2L;
    final long newExpiry = 2L;
    final long newNumNfts = 321L;
    final int newMaxAutoAssociations = 15;
    final int newAlreadyUsedAutoAssociations = 11;
    final JKey newKey = new JKeyList();
    final String newMemo = "b";
    final EntityId newProxy = new EntityId(0, 0, 2);
    final var oldAlias = ByteString.copyFromUtf8("then");
    final var newAlias = ByteString.copyFromUtf8("now");
    final int oldNumKvPairs = 123;
    final int newNumKvPairs = 123;
    final long initialAllowance = 100L;
    final AccountID payer = AccountID.newBuilder().setAccountNum(12345L).build();
    final AccountID owner = AccountID.newBuilder().setAccountNum(12347L).build();
    final EntityNum payerNum = EntityNum.fromAccountId(payer);
    final TokenID fungibleTokenID = TokenID.newBuilder().setTokenNum(1234L).build();
    final TokenID nonFungibleTokenID = TokenID.newBuilder().setTokenNum(1235L).build();
    final FcTokenAllowanceId fungibleAllowanceId = FcTokenAllowanceId.from(EntityNum.fromTokenId(fungibleTokenID), payerNum);
    final FcTokenAllowanceId nftAllowanceId = FcTokenAllowanceId.from(EntityNum.fromTokenId(nonFungibleTokenID), payerNum);
    final TreeMap<EntityNum, Long> cryptoAllowances = new TreeMap<>() {

        {
            put(payerNum, initialAllowance);
        }
    };
    final TreeMap<FcTokenAllowanceId, Long> fungibleAllowances = new TreeMap<>() {

        {
            put(fungibleAllowanceId, initialAllowance);
        }
    };
    final TreeMap<FcTokenAllowanceId, FcTokenAllowance> nftAllowances = new TreeMap<>() {

        {
            put(fungibleAllowanceId, FcTokenAllowance.from(true));
            put(nftAllowanceId, FcTokenAllowance.from(List.of(1L, 2L)));
        }
    };
    final var account = new HederaAccountCustomizer().key(JKey.mapKey(origKey)).expiry(origExpiry).proxy(EntityId.fromGrpcAccountId(origProxy)).autoRenewPeriod(origAutoRenew).isDeleted(origIsDeleted).alias(oldAlias).memo(origMemo).isSmartContract(origIsContract).isReceiverSigRequired(origIsReceiverSigReq).customizing(new MerkleAccount());
    account.setNumContractKvPairs(oldNumKvPairs);
    account.setNftsOwned(origNumNfts);
    account.setBalance(origBalance);
    account.records().offer(origPayerRecords.get(0));
    account.records().offer(origPayerRecords.get(1));
    account.setMaxAutomaticAssociations(origMaxAutoAssociations);
    account.setAlreadyUsedAutomaticAssociations(origAlreadyUsedAutoAssociations);
    final var adminKey = TOKEN_ADMIN_KT.asJKeyUnchecked();
    final var unfrozenToken = new MerkleToken(Long.MAX_VALUE, 100, 1, "UnfrozenToken", "UnfrozenTokenName", false, true, new EntityId(1, 2, 3));
    unfrozenToken.setFreezeKey(adminKey);
    unfrozenToken.setKycKey(adminKey);
    final var frozenToken = new MerkleToken(Long.MAX_VALUE, 100, 1, "FrozenToken", "FrozenTokenName", true, false, new EntityId(1, 2, 3));
    frozenToken.setFreezeKey(adminKey);
    frozenToken.setKycKey(adminKey);
    ALIAS.setter().accept(account, newAlias);
    IS_DELETED.setter().accept(account, newIsDeleted);
    IS_RECEIVER_SIG_REQUIRED.setter().accept(account, newIsReceiverSigReq);
    IS_SMART_CONTRACT.setter().accept(account, newIsContract);
    BALANCE.setter().accept(account, newBalance);
    AUTO_RENEW_PERIOD.setter().accept(account, newAutoRenew);
    EXPIRY.setter().accept(account, newExpiry);
    KEY.setter().accept(account, newKey);
    MEMO.setter().accept(account, newMemo);
    PROXY.setter().accept(account, newProxy);
    NUM_NFTS_OWNED.setter().accept(account, newNumNfts);
    MAX_AUTOMATIC_ASSOCIATIONS.setter().accept(account, newMaxAutoAssociations);
    ALREADY_USED_AUTOMATIC_ASSOCIATIONS.setter().accept(account, newAlreadyUsedAutoAssociations);
    NUM_CONTRACT_KV_PAIRS.setter().accept(account, newNumKvPairs);
    CRYPTO_ALLOWANCES.setter().accept(account, cryptoAllowances);
    FUNGIBLE_TOKEN_ALLOWANCES.setter().accept(account, fungibleAllowances);
    NFT_ALLOWANCES.setter().accept(account, nftAllowances);
    assertEquals(newIsDeleted, IS_DELETED.getter().apply(account));
    assertEquals(newIsReceiverSigReq, IS_RECEIVER_SIG_REQUIRED.getter().apply(account));
    assertEquals(newIsContract, IS_SMART_CONTRACT.getter().apply(account));
    assertEquals(newBalance, BALANCE.getter().apply(account));
    assertEquals(newAutoRenew, AUTO_RENEW_PERIOD.getter().apply(account));
    assertEquals(newExpiry, EXPIRY.getter().apply(account));
    assertEquals(newKey, KEY.getter().apply(account));
    assertEquals(newMemo, MEMO.getter().apply(account));
    assertEquals(newProxy, PROXY.getter().apply(account));
    assertEquals(newNumNfts, NUM_NFTS_OWNED.getter().apply(account));
    assertEquals(newAlreadyUsedAutoAssociations, ALREADY_USED_AUTOMATIC_ASSOCIATIONS.getter().apply(account));
    assertEquals(newMaxAutoAssociations, MAX_AUTOMATIC_ASSOCIATIONS.getter().apply(account));
    assertEquals(newAlias, ALIAS.getter().apply(account));
    assertEquals(newNumKvPairs, NUM_CONTRACT_KV_PAIRS.getter().apply(account));
    assertEquals(cryptoAllowances, CRYPTO_ALLOWANCES.getter().apply(account));
    assertEquals(fungibleAllowances, FUNGIBLE_TOKEN_ALLOWANCES.getter().apply(account));
    assertEquals(nftAllowances, NFT_ALLOWANCES.getter().apply(account));
}
Also used : ExpirableTxnRecord(com.hedera.services.state.submerkle.ExpirableTxnRecord) JKeyList(com.hedera.services.legacy.core.jproto.JKeyList) AccountID(com.hederahashgraph.api.proto.java.AccountID) MerkleToken(com.hedera.services.state.merkle.MerkleToken) ArrayList(java.util.ArrayList) MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) ByteString(com.google.protobuf.ByteString) TreeMap(java.util.TreeMap) EntityNum(com.hedera.services.utils.EntityNum) EntityId(com.hedera.services.state.submerkle.EntityId) FcTokenAllowanceId(com.hedera.services.state.submerkle.FcTokenAllowanceId) JKey(com.hedera.services.legacy.core.jproto.JKey) TokenID(com.hederahashgraph.api.proto.java.TokenID) FcTokenAllowance(com.hedera.services.state.submerkle.FcTokenAllowance) HederaAccountCustomizer(com.hedera.services.ledger.accounts.HederaAccountCustomizer) Test(org.junit.jupiter.api.Test)

Example 4 with FcTokenAllowance

use of com.hedera.services.state.submerkle.FcTokenAllowance in project hedera-services by hashgraph.

the class MerkleAccountStateTest method equalsWorksForAllowances.

@Test
void equalsWorksForAllowances() {
    final EntityNum spenderNum = EntityNum.fromLong(100L);
    final EntityNum tokenForAllowance = EntityNum.fromLong(200L);
    final Long cryptoAllowance = 100L;
    final boolean approvedForAll = true;
    final Long tokenAllowanceVal = 1L;
    final List<Long> serialNumbers = new ArrayList<>();
    final FcTokenAllowanceId tokenAllowanceKey = FcTokenAllowanceId.from(tokenForAllowance, spenderNum);
    final FcTokenAllowance tokenAllowanceValue = FcTokenAllowance.from(approvedForAll, serialNumbers);
    otherCryptoAllowances.put(spenderNum, cryptoAllowance);
    otherNftAllowances.put(tokenAllowanceKey, tokenAllowanceValue);
    otherFungibleTokenAllowances.put(tokenAllowanceKey, tokenAllowanceVal);
    final var otherSubject = new MerkleAccountState(key, expiry, balance, autoRenewSecs, memo, deleted, smartContract, receiverSigRequired, proxy, number, autoAssociationMetadata, alias, otherKvPairs, otherCryptoAllowances, otherFungibleTokenAllowances, otherNftAllowances);
    assertNotEquals(subject, otherSubject);
}
Also used : FcTokenAllowanceId(com.hedera.services.state.submerkle.FcTokenAllowanceId) ArrayList(java.util.ArrayList) FcTokenAllowance(com.hedera.services.state.submerkle.FcTokenAllowance) EntityNum(com.hedera.services.utils.EntityNum) Test(org.junit.jupiter.api.Test)

Example 5 with FcTokenAllowance

use of com.hedera.services.state.submerkle.FcTokenAllowance in project hedera-services by hashgraph.

the class SerializationUtils method deserializeNftAllowances.

public static Map<FcTokenAllowanceId, FcTokenAllowance> deserializeNftAllowances(final SerializableDataInputStream in) throws IOException {
    var numNftAllowances = in.readInt();
    if (numNftAllowances == 0) {
        return Collections.emptyMap();
    }
    final Map<FcTokenAllowanceId, FcTokenAllowance> nftAllowances = new TreeMap<>();
    while (numNftAllowances-- > 0) {
        final FcTokenAllowanceId nftAllowanceId = in.readSerializable();
        final FcTokenAllowance value = in.readSerializable();
        nftAllowances.put(nftAllowanceId, value);
    }
    return nftAllowances;
}
Also used : FcTokenAllowanceId(com.hedera.services.state.submerkle.FcTokenAllowanceId) FcTokenAllowance(com.hedera.services.state.submerkle.FcTokenAllowance) TreeMap(java.util.TreeMap)

Aggregations

FcTokenAllowance (com.hedera.services.state.submerkle.FcTokenAllowance)5 FcTokenAllowanceId (com.hedera.services.state.submerkle.FcTokenAllowanceId)4 EntityNum (com.hedera.services.utils.EntityNum)3 TreeMap (java.util.TreeMap)3 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 ByteString (com.google.protobuf.ByteString)1 MutableStateChildren (com.hedera.services.context.MutableStateChildren)1 StateView (com.hedera.services.context.primitives.StateView)1 HederaAccountCustomizer (com.hedera.services.ledger.accounts.HederaAccountCustomizer)1 JKey (com.hedera.services.legacy.core.jproto.JKey)1 JKeyList (com.hedera.services.legacy.core.jproto.JKeyList)1 MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)1 MerkleAccountTokens (com.hedera.services.state.merkle.MerkleAccountTokens)1 MerkleToken (com.hedera.services.state.merkle.MerkleToken)1 MerkleTokenRelStatus (com.hedera.services.state.merkle.MerkleTokenRelStatus)1 EntityId (com.hedera.services.state.submerkle.EntityId)1 ExpirableTxnRecord (com.hedera.services.state.submerkle.ExpirableTxnRecord)1 AccountID (com.hederahashgraph.api.proto.java.AccountID)1 TokenID (com.hederahashgraph.api.proto.java.TokenID)1