Search in sources :

Example 1 with MerkleAccountTokens

use of com.hedera.services.state.merkle.MerkleAccountTokens in project hedera-services by hashgraph.

the class HederaLedger method allTokenBalancesVanish.

public boolean allTokenBalancesVanish(AccountID aId) {
    if (tokenRelsLedger == null) {
        throw new IllegalStateException("Ledger has no manageable token relationships!");
    }
    var tokens = (MerkleAccountTokens) accountsLedger.get(aId, TOKENS);
    for (TokenID tId : tokens.asTokenIds()) {
        if (tokenStore.get(tId).isDeleted()) {
            continue;
        }
        var relationship = asTokenRel(aId, tId);
        var balance = (long) tokenRelsLedger.get(relationship, TOKEN_BALANCE);
        if (balance > 0) {
            return false;
        }
    }
    return true;
}
Also used : MerkleAccountTokens(com.hedera.services.state.merkle.MerkleAccountTokens) TokenID(com.hederahashgraph.api.proto.java.TokenID)

Example 2 with MerkleAccountTokens

use of com.hedera.services.state.merkle.MerkleAccountTokens 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 3 with MerkleAccountTokens

use of com.hedera.services.state.merkle.MerkleAccountTokens in project hedera-services by hashgraph.

the class BaseHederaLedgerTestHelper method addToLedger.

protected void addToLedger(AccountID id, long balance, Map<TokenID, TokenInfo> tokenInfo) {
    when(accountsLedger.get(id, EXPIRY)).thenReturn(1_234_567_890L);
    when(accountsLedger.get(id, PROXY)).thenReturn(new EntityId(0, 0, 1_234L));
    when(accountsLedger.get(id, AUTO_RENEW_PERIOD)).thenReturn(7776000L);
    when(accountsLedger.get(id, BALANCE)).thenReturn(balance);
    when(accountsLedger.get(id, IS_DELETED)).thenReturn(false);
    when(accountsLedger.get(id, IS_RECEIVER_SIG_REQUIRED)).thenReturn(true);
    when(accountsLedger.get(id, IS_SMART_CONTRACT)).thenReturn(false);
    when(accountsLedger.get(id, MAX_AUTOMATIC_ASSOCIATIONS)).thenReturn(8);
    when(accountsLedger.get(id, ALREADY_USED_AUTOMATIC_ASSOCIATIONS)).thenReturn(5);
    when(accountsLedger.exists(id)).thenReturn(true);
    var tokens = new MerkleAccountTokens();
    tokens.associateAll(tokenInfo.keySet());
    when(accountsLedger.get(id, TOKENS)).thenReturn(tokens);
    // and:
    for (TokenID tId : tokenInfo.keySet()) {
        var info = tokenInfo.get(tId);
        var relationship = BackingTokenRels.asTokenRel(id, tId);
        when(tokenRelsLedger.get(relationship, TOKEN_BALANCE)).thenReturn(info.balance);
    }
}
Also used : EntityId(com.hedera.services.state.submerkle.EntityId) MerkleAccountTokens(com.hedera.services.state.merkle.MerkleAccountTokens) TokenID(com.hederahashgraph.api.proto.java.TokenID)

Example 4 with MerkleAccountTokens

use of com.hedera.services.state.merkle.MerkleAccountTokens in project hedera-services by hashgraph.

the class ToStringAccountsExporterTest method producesExpectedText.

@Test
void producesExpectedText() throws Exception {
    // setup:
    TreeMap<EntityNum, Long> cryptoAllowances = new TreeMap();
    cryptoAllowances.put(EntityNum.fromLong(1L), 10L);
    account1.setBalance(1L);
    account1.setTokens(new MerkleAccountTokens(new CopyOnWriteIds(new long[] { 1L, 2L, 3L, 3L, 2L, 1L })));
    account1.setMaxAutomaticAssociations(10);
    account1.setAlreadyUsedAutomaticAssociations(7);
    account1.setCryptoAllowances(cryptoAllowances);
    account2.setBalance(2L);
    account2.setTokens(new MerkleAccountTokens(new CopyOnWriteIds(new long[] { 0L, 0L, 1234L })));
    // and:
    var desired = "0.0.1\n" + "---\n" + "MerkleAccount{state=MerkleAccountState{number=1 <-> 0.0.1, key=ed25519: \"first-fake\"\n" + ", expiry=1234567, balance=1, autoRenewSecs=555555, memo=This ecstasy doth unperplex, deleted=false, " + "smartContract=true, numContractKvPairs=0, receiverSigRequired=true, " + "proxy=EntityId{shard=0, realm=0, num=0}, nftsOwned=0, " + "alreadyUsedAutoAssociations=7, maxAutoAssociations=10, alias=, " + "cryptoAllowances={EntityNum{value=1}=10}, " + "fungibleTokenAllowances={}, nftAllowances={}}, # records=0, tokens=[3.2.1, 1.2.3]}\n\n0.0.2\n---\n" + "MerkleAccount{state=MerkleAccountState{number=2 <-> 0.0.2, key=ed25519: \"second-fake\"\n" + ", expiry=7654321, balance=2, autoRenewSecs=444444, memo=We said, and show us what we love, " + "deleted=true, smartContract=false, numContractKvPairs=0, receiverSigRequired=false, " + "proxy=EntityId{shard=0, realm=0, num=0}, nftsOwned=0, alreadyUsedAutoAssociations=0, " + "maxAutoAssociations=0, alias=, cryptoAllowances={}, fungibleTokenAllowances={}, nftAllowances={}}, #" + " records=0, tokens=[1234.0.0]}\n";
    // given:
    MerkleMap<EntityNum, MerkleAccount> accounts = new MerkleMap<>();
    // and:
    accounts.put(EntityNum.fromInt(2), account2);
    accounts.put(EntityNum.fromInt(1), account1);
    // and:
    given(nodeLocalProperties.exportAccountsOnStartup()).willReturn(true);
    given(nodeLocalProperties.accountsExportPath()).willReturn(testExportLoc);
    // when:
    subject.toFile(accounts);
    // and:
    var result = Files.readString(Paths.get(testExportLoc));
    // then:
    assertEquals(desired, result);
}
Also used : MerkleAccountTokens(com.hedera.services.state.merkle.MerkleAccountTokens) CopyOnWriteIds(com.hedera.services.state.merkle.internals.CopyOnWriteIds) MerkleMap(com.swirlds.merkle.map.MerkleMap) MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) TreeMap(java.util.TreeMap) EntityNum(com.hedera.services.utils.EntityNum) Test(org.junit.jupiter.api.Test)

Example 5 with MerkleAccountTokens

use of com.hedera.services.state.merkle.MerkleAccountTokens in project hedera-services by hashgraph.

the class MerkleAccountFactory method get.

public MerkleAccount get() {
    MerkleAccount value = new MerkleAccount();
    memo.ifPresent(value::setMemo);
    alias.ifPresent(value::setAlias);
    proxy.ifPresent(p -> value.setProxy(EntityId.fromGrpcAccountId(p)));
    balance.ifPresent(b -> {
        try {
            value.setBalance(b);
        } catch (Exception ignore) {
        }
    });
    deleted.ifPresent(value::setDeleted);
    accountKeys.ifPresent(value::setAccountKey);
    expirationTime.ifPresent(value::setExpiry);
    autoRenewPeriod.ifPresent(value::setAutoRenewSecs);
    isSmartContract.ifPresent(value::setSmartContract);
    receiverSigRequired.ifPresent(value::setReceiverSigRequired);
    maxAutoAssociations.ifPresent(value::setMaxAutomaticAssociations);
    alreadyUsedAutoAssociations.ifPresent(value::setAlreadyUsedAutomaticAssociations);
    var tokens = new MerkleAccountTokens();
    if (useNewStyleTokenIds) {
        tokens.associate(assocTokens);
    } else {
        tokens.associateAll(associatedTokens);
    }
    value.setTokens(tokens);
    value.setNumContractKvPairs(numKvPairs);
    value.setCryptoAllowances(cryptoAllowances);
    value.setFungibleTokenAllowances(fungibleTokenAllowances);
    value.setNftAllowances(nftAllowances);
    return value;
}
Also used : MerkleAccountTokens(com.hedera.services.state.merkle.MerkleAccountTokens) MerkleAccount(com.hedera.services.state.merkle.MerkleAccount)

Aggregations

MerkleAccountTokens (com.hedera.services.state.merkle.MerkleAccountTokens)5 MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)2 EntityNum (com.hedera.services.utils.EntityNum)2 TokenID (com.hederahashgraph.api.proto.java.TokenID)2 TreeMap (java.util.TreeMap)2 MutableStateChildren (com.hedera.services.context.MutableStateChildren)1 StateView (com.hedera.services.context.primitives.StateView)1 MerkleTokenRelStatus (com.hedera.services.state.merkle.MerkleTokenRelStatus)1 CopyOnWriteIds (com.hedera.services.state.merkle.internals.CopyOnWriteIds)1 EntityId (com.hedera.services.state.submerkle.EntityId)1 FcTokenAllowance (com.hedera.services.state.submerkle.FcTokenAllowance)1 FcTokenAllowanceId (com.hedera.services.state.submerkle.FcTokenAllowanceId)1 MerkleMap (com.swirlds.merkle.map.MerkleMap)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1