Search in sources :

Example 16 with EntityNum

use of com.hedera.services.utils.EntityNum in project hedera-services by hashgraph.

the class MerkleTokenTest method v0120DeserializeWorks.

@Test
void v0120DeserializeWorks() throws IOException {
    final var fin = mock(SerializableDataInputStream.class);
    subject.setFeeScheduleFrom(Collections.emptyList());
    subject.setFeeScheduleKey(MerkleToken.UNUSED_KEY);
    given(serdes.readNullableSerializable(any())).willReturn(autoRenewAccount);
    given(serdes.deserializeKey(fin)).willReturn(adminKey);
    given(serdes.readNullable(argThat(fin::equals), any(IoReadingFunction.class))).willReturn(adminKey).willReturn(freezeKey).willReturn(kycKey).willReturn(supplyKey).willReturn(wipeKey);
    given(fin.readNormalisedString(anyInt())).willReturn(symbol).willReturn(name).willReturn(memo);
    given(fin.readLong()).willReturn(subject.expiry()).willReturn(subject.autoRenewPeriod()).willReturn(subject.totalSupply()).willReturn(subject.getLastUsedSerialNumber());
    given(fin.readInt()).willReturn(subject.decimals()).willReturn(TokenType.FUNGIBLE_COMMON.ordinal()).willReturn(TokenSupplyType.INFINITE.ordinal());
    given(fin.readBoolean()).willReturn(isDeleted).willReturn(subject.accountsAreFrozenByDefault());
    given(fin.readSerializable()).willReturn(subject.treasury());
    final var read = new MerkleToken();
    read.deserialize(fin, MerkleToken.RELEASE_0120_VERSION);
    // then:
    assertNotEquals(subject, read);
    // and when:
    read.setKey(new EntityNum(number));
    // expect:
    assertEquals(subject, read);
}
Also used : IoReadingFunction(com.hedera.services.state.serdes.IoReadingFunction) EntityNum(com.hedera.services.utils.EntityNum) Test(org.junit.jupiter.api.Test)

Example 17 with EntityNum

use of com.hedera.services.utils.EntityNum in project hedera-services by hashgraph.

the class MerkleAccountStateTest method addingAndRemovingToMapsWork.

@Test
void addingAndRemovingToMapsWork() {
    final EntityNum spenderNum = EntityNum.fromLong(2000L);
    final EntityNum tokenForAllowance = EntityNum.fromLong(4000L);
    final Long cryptoAllowance = 100L;
    final boolean approvedForAll = false;
    final Long tokenAllowanceVal = 10L;
    final List<Long> serialNumbers = List.of(3L, 4L);
    final FcTokenAllowanceId tokenAllowanceKey = FcTokenAllowanceId.from(tokenForAllowance, spenderNum);
    subject.addCryptoAllowance(spenderNum, cryptoAllowance);
    subject.addFungibleTokenAllowance(tokenForAllowance, spenderNum, tokenAllowanceVal);
    subject.addNftAllowance(tokenForAllowance, spenderNum, approvedForAll, serialNumbers);
    assertTrue(subject.getCryptoAllowances().containsKey(spenderNum));
    assertTrue(subject.getNftAllowances().containsKey(tokenAllowanceKey));
    assertEquals(2, subject.getCryptoAllowances().size());
    assertEquals(2, subject.getNftAllowances().size());
    subject.removeCryptoAllowance(spenderNum);
    subject.removeFungibleTokenAllowance(tokenForAllowance, spenderNum);
    subject.removeNftAllowance(tokenForAllowance, spenderNum);
    assertFalse(subject.getCryptoAllowances().containsKey(spenderNum));
    assertFalse(subject.getNftAllowances().containsKey(tokenAllowanceKey));
    assertEquals(1, subject.getCryptoAllowances().size());
    assertEquals(1, subject.getNftAllowances().size());
}
Also used : FcTokenAllowanceId(com.hedera.services.state.submerkle.FcTokenAllowanceId) EntityNum(com.hedera.services.utils.EntityNum) Test(org.junit.jupiter.api.Test)

Example 18 with EntityNum

use of com.hedera.services.utils.EntityNum 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 19 with EntityNum

use of com.hedera.services.utils.EntityNum 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 20 with EntityNum

use of com.hedera.services.utils.EntityNum 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

EntityNum (com.hedera.services.utils.EntityNum)23 Test (org.junit.jupiter.api.Test)15 MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)9 MerkleMap (com.swirlds.merkle.map.MerkleMap)5 FcTokenAllowanceId (com.hedera.services.state.submerkle.FcTokenAllowanceId)4 MerkleToken (com.hedera.services.state.merkle.MerkleToken)3 EntityId (com.hedera.services.state.submerkle.EntityId)3 FcTokenAllowance (com.hedera.services.state.submerkle.FcTokenAllowance)3 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 ByteString (com.google.protobuf.ByteString)2 MutableStateChildren (com.hedera.services.context.MutableStateChildren)2 MerkleAccountTokens (com.hedera.services.state.merkle.MerkleAccountTokens)2 MerkleTokenRelStatus (com.hedera.services.state.merkle.MerkleTokenRelStatus)2 MerkleTopic (com.hedera.services.state.merkle.MerkleTopic)2 ExpirableTxnRecord (com.hedera.services.state.submerkle.ExpirableTxnRecord)2 AccountID (com.hederahashgraph.api.proto.java.AccountID)2 CryptoGetAccountBalanceQuery (com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery)2 MerkleDataInputStream (com.swirlds.common.merkle.io.MerkleDataInputStream)2 NetworkInfo (com.hedera.services.config.NetworkInfo)1