Search in sources :

Example 6 with MerkleMap

use of com.swirlds.merkle.map.MerkleMap in project hedera-services by hashgraph.

the class BackingAccountsTest method twoPutsChangesG4M.

@Test
void twoPutsChangesG4M() throws ConstructableRegistryException {
    // setup:
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(KeyedMerkleLong.class, KeyedMerkleLong::new));
    final var oneGrandKey = new FcLong(1000L);
    final var twoGrandKey = new FcLong(2000L);
    final var evilKey = new FcLong(666L);
    final var nonEvilKey = new FcLong(667L);
    /* Case 1: g4m a leaf; then put ONE new leaf; then change the mutable leaf and re-get to verify new value */
    final MerkleMap<FcLong, KeyedMerkleLong<FcLong>> firstMm = new MerkleMap<>();
    final var oneGrandEntry = new KeyedMerkleLong<>(oneGrandKey, 1000L);
    firstMm.put(oneGrandKey, oneGrandEntry);
    final var mutableOne = firstMm.getForModify(oneGrandKey);
    /* Putting just one new leaf */
    final var evilEntry = new KeyedMerkleLong<>(evilKey, 666L);
    firstMm.put(evilKey, evilEntry);
    /* Then the mutable value is retained */
    assertSame(mutableOne, firstMm.get(oneGrandKey));
    /* Case 2: g4m a leaf; then put TWO new leaves; then change the mutable leaf and re-get to verify new value */
    final var secondFcm = new MerkleMap<FcLong, KeyedMerkleLong<FcLong>>();
    final var twoGrandEntry = new KeyedMerkleLong<>(twoGrandKey, 2000L);
    final var evilEntry2 = new KeyedMerkleLong<>(evilKey, 666L);
    final var nonEvilEntry2 = new KeyedMerkleLong<>(nonEvilKey, 667L);
    secondFcm.put(twoGrandKey, twoGrandEntry);
    final var mutableTwo = secondFcm.getForModify(twoGrandEntry.getKey());
    /* Putting two new leaves now */
    secondFcm.put(evilEntry2.getKey(), evilEntry2);
    secondFcm.put(nonEvilEntry2.getKey(), nonEvilEntry2);
    /* And now changing the once-mutable value throws MutabilityException */
    assertThrows(MutabilityException.class, mutableTwo::increment);
}
Also used : FcLong(com.hedera.services.utils.FcLong) MerkleMap(com.swirlds.merkle.map.MerkleMap) KeyedMerkleLong(com.swirlds.common.merkle.utility.KeyedMerkleLong) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Example 7 with MerkleMap

use of com.swirlds.merkle.map.MerkleMap 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 8 with MerkleMap

use of com.swirlds.merkle.map.MerkleMap 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)

Example 9 with MerkleMap

use of com.swirlds.merkle.map.MerkleMap in project hedera-services by hashgraph.

the class StateViewTest method setup.

@BeforeEach
@SuppressWarnings("unchecked")
private void setup() throws Throwable {
    metadata = new HFileMeta(false, TxnHandlingScenario.MISC_FILE_WACL_KT.asJKey(), expiry, fileMemo);
    immutableMetadata = new HFileMeta(false, StateView.EMPTY_WACL, expiry);
    expectedImmutable = FileGetInfoResponse.FileInfo.newBuilder().setLedgerId(ledgerId).setDeleted(false).setExpirationTime(Timestamp.newBuilder().setSeconds(expiry)).setFileID(target).setSize(data.length).build();
    expected = expectedImmutable.toBuilder().setKeys(TxnHandlingScenario.MISC_FILE_WACL_KT.asKey().getKeyList()).setMemo(fileMemo).build();
    tokenAccount = MerkleAccountFactory.newAccount().isSmartContract(false).tokens(tokenId).get();
    tokenAccount.setNftsOwned(10);
    tokenAccount.setMaxAutomaticAssociations(123);
    tokenAccount.setAlias(TxnHandlingScenario.TOKEN_ADMIN_KT.asKey().getEd25519());
    contract = MerkleAccountFactory.newAccount().alias(create2Address).memo("Stay cold...").numKvPairs(wellKnownNumKvPairs).isSmartContract(true).accountKeys(COMPLEX_KEY_ACCOUNT_KT).proxy(asAccount("0.0.3")).senderThreshold(1_234L).receiverThreshold(4_321L).receiverSigRequired(true).balance(555L).autoRenewPeriod(1_000_000L).deleted(true).expirationTime(9_999_999L).get();
    contracts = (MerkleMap<EntityNum, MerkleAccount>) mock(MerkleMap.class);
    topics = (MerkleMap<EntityNum, MerkleTopic>) mock(MerkleMap.class);
    tokenRels = new MerkleMap<>();
    tokenRels.put(EntityNumPair.fromLongs(tokenAccountId.getAccountNum(), tokenId.getTokenNum()), new MerkleTokenRelStatus(123L, false, true, true));
    tokenStore = mock(TokenStore.class);
    token = new MerkleToken(Long.MAX_VALUE, 100, 1, "UnfrozenToken", "UnfrozenTokenName", true, true, new EntityId(0, 0, 3));
    token.setMemo(tokenMemo);
    token.setAdminKey(TxnHandlingScenario.TOKEN_ADMIN_KT.asJKey());
    token.setFreezeKey(TxnHandlingScenario.TOKEN_FREEZE_KT.asJKey());
    token.setKycKey(TxnHandlingScenario.TOKEN_KYC_KT.asJKey());
    token.setSupplyKey(COMPLEX_KEY_ACCOUNT_KT.asJKey());
    token.setWipeKey(MISC_ACCOUNT_KT.asJKey());
    token.setFeeScheduleKey(MISC_ACCOUNT_KT.asJKey());
    token.setPauseKey(TxnHandlingScenario.TOKEN_PAUSE_KT.asJKey());
    token.setAutoRenewAccount(EntityId.fromGrpcAccountId(autoRenew));
    token.setExpiry(expiry);
    token.setAutoRenewPeriod(autoRenewPeriod);
    token.setDeleted(true);
    token.setPaused(true);
    token.setTokenType(TokenType.FUNGIBLE_COMMON);
    token.setSupplyType(TokenSupplyType.FINITE);
    token.setFeeScheduleFrom(grpcCustomFees);
    scheduleStore = mock(ScheduleStore.class);
    final var scheduleMemo = "For what but eye and ear";
    parentScheduleCreate = scheduleCreateTxnWith(SCHEDULE_ADMIN_KT.asKey(), scheduleMemo, payerAccountId, creatorAccountID, MiscUtils.asTimestamp(now.toJava()));
    schedule = MerkleSchedule.from(parentScheduleCreate.toByteArray(), expiry);
    schedule.witnessValidSignature("01234567890123456789012345678901".getBytes());
    schedule.witnessValidSignature("_123456789_123456789_123456789_1".getBytes());
    schedule.witnessValidSignature("_o23456789_o23456789_o23456789_o".getBytes());
    contents = mock(Map.class);
    attrs = mock(Map.class);
    bytecode = mock(Map.class);
    specialFiles = mock(MerkleSpecialFiles.class);
    mockTokenRelsFn = (BiFunction<StateView, EntityNum, List<TokenRelationship>>) mock(BiFunction.class);
    StateView.tokenRelsFn = mockTokenRelsFn;
    final var uniqueTokens = new MerkleMap<EntityNumPair, MerkleUniqueToken>();
    uniqueTokens.put(targetNftKey, targetNft);
    uniqueTokens.put(treasuryNftKey, treasuryNft);
    storage = (VirtualMap<VirtualBlobKey, VirtualBlobValue>) mock(VirtualMap.class);
    contractStorage = (VirtualMap<ContractKey, ContractValue>) mock(VirtualMap.class);
    children = new MutableStateChildren();
    children.setUniqueTokens(uniqueTokens);
    children.setAccounts(contracts);
    children.setTokenAssociations(tokenRels);
    children.setSpecialFiles(specialFiles);
    networkInfo = mock(NetworkInfo.class);
    subject = new StateView(tokenStore, scheduleStore, children, networkInfo);
    subject.fileAttrs = attrs;
    subject.fileContents = contents;
    subject.contractBytecode = bytecode;
}
Also used : NetworkInfo(com.hedera.services.config.NetworkInfo) MerkleToken(com.hedera.services.state.merkle.MerkleToken) MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) MerkleTopic(com.hedera.services.state.merkle.MerkleTopic) EntityNum(com.hedera.services.utils.EntityNum) MerkleTokenRelStatus(com.hedera.services.state.merkle.MerkleTokenRelStatus) EntityId(com.hedera.services.state.submerkle.EntityId) MerkleSpecialFiles(com.hedera.services.state.merkle.MerkleSpecialFiles) ContractValue(com.hedera.services.state.virtual.ContractValue) MutableStateChildren(com.hedera.services.context.MutableStateChildren) ContractKey(com.hedera.services.state.virtual.ContractKey) HFileMeta(com.hedera.services.files.HFileMeta) MerkleMap(com.swirlds.merkle.map.MerkleMap) VirtualBlobValue(com.hedera.services.state.virtual.VirtualBlobValue) KeyList(com.hederahashgraph.api.proto.java.KeyList) List(java.util.List) TokenStore(com.hedera.services.store.tokens.TokenStore) ScheduleStore(com.hedera.services.store.schedule.ScheduleStore) Map(java.util.Map) VirtualMap(com.swirlds.virtualmap.VirtualMap) MerkleMap(com.swirlds.merkle.map.MerkleMap) VirtualBlobKey(com.hedera.services.state.virtual.VirtualBlobKey) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MerkleMap (com.swirlds.merkle.map.MerkleMap)9 Test (org.junit.jupiter.api.Test)6 EntityNum (com.hedera.services.utils.EntityNum)5 MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)4 MerkleToken (com.hedera.services.state.merkle.MerkleToken)3 MutableStateChildren (com.hedera.services.context.MutableStateChildren)2 AliasManager (com.hedera.services.ledger.accounts.AliasManager)2 MerkleOptionalBlob (com.hedera.services.state.merkle.MerkleOptionalBlob)2 MerkleTokenRelStatus (com.hedera.services.state.merkle.MerkleTokenRelStatus)2 EntityId (com.hedera.services.state.submerkle.EntityId)2 ScheduleStore (com.hedera.services.store.schedule.ScheduleStore)2 TokenStore (com.hedera.services.store.tokens.TokenStore)2 ClassConstructorPair (com.swirlds.common.constructable.ClassConstructorPair)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 NetworkInfo (com.hedera.services.config.NetworkInfo)1 StateView (com.hedera.services.context.primitives.StateView)1 NodeLocalProperties (com.hedera.services.context.properties.NodeLocalProperties)1 HFileMeta (com.hedera.services.files.HFileMeta)1 BackingAccounts (com.hedera.services.ledger.backing.BackingAccounts)1 TxnReceipt (com.hedera.services.legacy.core.jproto.TxnReceipt)1