Search in sources :

Example 16 with MerkleAccount

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

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

the class BackingAccountsTest method auxiliarySetIsRebuiltFromScratch.

@Test
void auxiliarySetIsRebuiltFromScratch() throws ConstructableRegistryException {
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(MerkleAccount.class, MerkleAccount::new));
    final var idSet = subject.getExistingAccounts();
    subject.rebuildFromSources();
    assertTrue(idSet.contains(aKey.toGrpcAccountId()));
    assertTrue(idSet.contains(bKey.toGrpcAccountId()));
    delegate.remove(aKey);
    subject.rebuildFromSources();
    assertFalse(idSet.contains(aKey.toGrpcAccountId()));
    assertTrue(idSet.contains(bKey.toGrpcAccountId()));
}
Also used : MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Example 18 with MerkleAccount

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

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

the class BackedSystemAccountsCreatorTest method withExpectedBalance.

private MerkleAccount withExpectedBalance(long balance) throws NegativeAccountBalanceException {
    MerkleAccount hAccount = new HederaAccountCustomizer().isReceiverSigRequired(false).proxy(EntityId.MISSING_ENTITY_ID).isDeleted(false).expiry(expiry).memo("").isSmartContract(false).key(genesisKey).autoRenewPeriod(expiry).customizing(new MerkleAccount());
    hAccount.setBalance(balance);
    return hAccount;
}
Also used : MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) HederaAccountCustomizer(com.hedera.services.ledger.accounts.HederaAccountCustomizer)

Example 20 with MerkleAccount

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

the class RenewalHelperTest method removesAutoAccountEntityWhenExpired.

@Test
void removesAutoAccountEntityWhenExpired() {
    MerkleMap<EntityNum, MerkleAccount> accountsMap = new MerkleMap<>();
    accountsMap.put(EntityNum.fromLong(nonExpiredAccountNum), nonExpiredAccount);
    accountsMap.put(EntityNum.fromLong(brokeExpiredAccountNum), expiredAccountZeroBalance);
    AliasManager liveAliasManager = new AliasManager();
    linkWellKnownEntities(liveAliasManager);
    final var backingAccounts = new BackingAccounts(() -> accountsMap);
    backingAccounts.put(IdUtils.asAccount("0.0." + nonExpiredAccountNum), nonExpiredAccount);
    backingAccounts.put(IdUtils.asAccount("0.0." + brokeExpiredAccountNum), expiredAccountZeroBalance);
    subject = new RenewalHelper(tokenStore, sigImpactHistorian, dynamicProps, () -> tokens, () -> accountsMap, () -> tokenRels, backingAccounts, liveAliasManager);
    final var expiredKey = EntityNum.fromLong(brokeExpiredAccountNum);
    givenTokenPresent(deletedTokenId, deletedToken);
    givenTokenPresent(survivedTokenId, longLivedToken);
    givenRelPresent(expiredKey, deletedTokenId, Long.MAX_VALUE);
    givenRelPresent(expiredKey, survivedTokenId, tokenBalance);
    givenRelPresent(expiredKey, EntityNum.fromTokenId(missingTokenGrpcId), 0);
    givenModifiableRelPresent(EntityNum.fromAccountId(treasuryGrpcId), survivedTokenId, 0L);
    assertTrue(liveAliasManager.contains(expiredAccountZeroBalance.getAlias()));
    assertTrue(backingAccounts.contains(AccountID.newBuilder().setAccountNum(brokeExpiredAccountNum).build()));
    subject.classify(brokeExpiredAccountNum, now);
    subject.removeLastClassifiedAccount();
    assertFalse(backingAccounts.contains(AccountID.newBuilder().setAccountNum(brokeExpiredAccountNum).build()));
}
Also used : AliasManager(com.hedera.services.ledger.accounts.AliasManager) BackingAccounts(com.hedera.services.ledger.backing.BackingAccounts) MerkleMap(com.swirlds.merkle.map.MerkleMap) MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) EntityNum(com.hedera.services.utils.EntityNum) Test(org.junit.jupiter.api.Test)

Aggregations

MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)22 Test (org.junit.jupiter.api.Test)10 EntityNum (com.hedera.services.utils.EntityNum)9 MerkleToken (com.hedera.services.state.merkle.MerkleToken)8 MerkleTokenRelStatus (com.hedera.services.state.merkle.MerkleTokenRelStatus)6 TransactionalLedger (com.hedera.services.ledger.TransactionalLedger)5 MerkleUniqueToken (com.hedera.services.state.merkle.MerkleUniqueToken)5 HederaAccountCustomizer (com.hedera.services.ledger.accounts.HederaAccountCustomizer)4 HashMapBackingAccounts (com.hedera.services.ledger.backing.HashMapBackingAccounts)4 HashMapBackingNfts (com.hedera.services.ledger.backing.HashMapBackingNfts)4 HashMapBackingTokenRels (com.hedera.services.ledger.backing.HashMapBackingTokenRels)4 HashMapBackingTokens (com.hedera.services.ledger.backing.HashMapBackingTokens)4 EntityId (com.hedera.services.state.submerkle.EntityId)4 AliasManager (com.hedera.services.ledger.accounts.AliasManager)3 ExpirableTxnRecord (com.hedera.services.state.submerkle.ExpirableTxnRecord)3 ClassConstructorPair (com.swirlds.common.constructable.ClassConstructorPair)3 MerkleMap (com.swirlds.merkle.map.MerkleMap)3 StackedContractAliases (com.hedera.services.ledger.accounts.StackedContractAliases)2 TxnReceipt (com.hedera.services.legacy.core.jproto.TxnReceipt)2 MerkleAccountState (com.hedera.services.state.merkle.MerkleAccountState)2