use of com.hedera.services.state.merkle.MerkleUniqueToken in project hedera-services by hashgraph.
the class LedgerBalanceChangesTest method givenInitialBalancesAndOwnership.
private void givenInitialBalancesAndOwnership() {
final var aAccount = MerkleAccountFactory.newAccount().balance(aStartBalance).get();
backingAccounts.put(aModel, aAccount);
final var bAccount = MerkleAccountFactory.newAccount().balance(bStartBalance).get();
backingAccounts.put(bModel, bAccount);
final var cAccount = MerkleAccountFactory.newAccount().balance(cStartBalance).get();
backingAccounts.put(cModel, cAccount);
Pair<AccountID, TokenID> bTokenKey = rel(bModel, token);
final var bTokenRel = new MerkleTokenRelStatus(bTokenStartBalance, false, true, false);
backingRels.put(bTokenKey, bTokenRel);
Pair<AccountID, TokenID> cTokenKey = rel(cModel, token);
final var cTokenRel = new MerkleTokenRelStatus(cTokenStartBalance, false, true, false);
backingRels.put(cTokenKey, cTokenRel);
Pair<AccountID, TokenID> aAnotherTokenKey = rel(aModel, anotherToken);
final var aAnotherTokenRel = new MerkleTokenRelStatus(aAnotherTokenStartBalance, false, true, true);
backingRels.put(aAnotherTokenKey, aAnotherTokenRel);
Pair<AccountID, TokenID> bAnotherTokenKey = rel(bModel, anotherToken);
final var bAnotherTokenRel = new MerkleTokenRelStatus(bAnotherTokenStartBalance, false, true, false);
backingRels.put(bAnotherTokenKey, bAnotherTokenRel);
Pair<AccountID, TokenID> cAnotherTokenKey = rel(cModel, anotherToken);
final var cAnotherTokenRel = new MerkleTokenRelStatus(cAnotherTokenStartBalance, false, true, true);
backingRels.put(cAnotherTokenKey, cAnotherTokenRel);
Pair<AccountID, TokenID> aYaTokenKey = rel(aModel, yetAnotherToken);
final var aYaTokenRel = new MerkleTokenRelStatus(aYetAnotherTokenBalance, false, true, false);
backingRels.put(aYaTokenKey, aYaTokenRel);
Pair<AccountID, TokenID> bYaTokenKey = rel(bModel, yetAnotherToken);
final var bYaTokenRel = new MerkleTokenRelStatus(bYetAnotherTokenBalance, false, true, false);
backingRels.put(bYaTokenKey, bYaTokenRel);
Pair<AccountID, TokenID> aaNftTokenKey = rel(aModel, aNft);
final var aaNftTokenRel = new MerkleTokenRelStatus(2, false, true, false);
backingRels.put(aaNftTokenKey, aaNftTokenRel);
Pair<AccountID, TokenID> abNftTokenKey = rel(aModel, bNft);
final var abNftTokenRel = new MerkleTokenRelStatus(2, false, true, true);
backingRels.put(abNftTokenKey, abNftTokenRel);
Pair<AccountID, TokenID> baNftTokenKey = rel(bModel, aNft);
final var baNftTokenRel = new MerkleTokenRelStatus(2, false, true, false);
backingRels.put(baNftTokenKey, baNftTokenRel);
Pair<AccountID, TokenID> bbNftTokenKey = rel(bModel, bNft);
final var bbNftTokenRel = new MerkleTokenRelStatus(2, false, true, true);
backingRels.put(bbNftTokenKey, bbNftTokenRel);
Pair<AccountID, TokenID> caNftTokenKey = rel(cModel, aNft);
final var caNftTokenRel = new MerkleTokenRelStatus(2, false, true, true);
backingRels.put(caNftTokenKey, caNftTokenRel);
Pair<AccountID, TokenID> cbNftTokenKey = rel(cModel, bNft);
final var cbNftTokenRel = new MerkleTokenRelStatus(2, false, true, false);
backingRels.put(cbNftTokenKey, cbNftTokenRel);
backingNfts.put(aaNft, new MerkleUniqueToken(EntityId.fromGrpcAccountId(aModel), "aa".getBytes(), MISSING_INSTANT));
backingNfts.put(baNft, new MerkleUniqueToken(EntityId.fromGrpcAccountId(bModel), "ba".getBytes(), MISSING_INSTANT));
backingNfts.put(bbNft, new MerkleUniqueToken(EntityId.fromGrpcAccountId(cModel), "bb".getBytes(), MISSING_INSTANT));
backingRels.rebuildFromSources();
}
use of com.hedera.services.state.merkle.MerkleUniqueToken in project hedera-services by hashgraph.
the class TypedTokenStoreTest method savesTokenAsExpected.
/* --- Token saving --- */
@Test
void savesTokenAsExpected() {
// setup:
final var mintedSerialNo = 33L;
final var wipedSerialNo = 33L;
final var mintedSerialNo2 = 44;
final var burnedSerialNo = 44;
final var nftMeta = "abcdefgh".getBytes();
final var treasuryId = new EntityId(0, 0, treasuryAccountNum);
final var tokenEntityId = new EntityId(0, 0, tokenNum);
final var creationTime = new RichInstant(1_234_567L, 8);
final var modelTreasuryId = new Id(0, 0, treasuryAccountNum);
final var mintedToken = new UniqueToken(tokenId, mintedSerialNo, creationTime, Id.DEFAULT, nftMeta);
final var wipedToken = new UniqueToken(tokenId, wipedSerialNo, creationTime, modelTreasuryId, nftMeta);
final var mintedToken2 = new UniqueToken(tokenId, mintedSerialNo2, creationTime, Id.DEFAULT, nftMeta);
final var burnedToken = new UniqueToken(tokenId, burnedSerialNo, creationTime, modelTreasuryId, nftMeta);
// and:
final var expectedReplacementToken = new MerkleToken(expiry, tokenSupply * 2, 0, symbol, name, freezeDefault, true, new EntityId(0, 0, autoRenewAccountNum));
expectedReplacementToken.setAutoRenewAccount(treasuryId);
expectedReplacementToken.setSupplyKey(supplyKey);
expectedReplacementToken.setFreezeKey(freezeKey);
expectedReplacementToken.setKycKey(kycKey);
expectedReplacementToken.setPauseKey(pauseKey);
expectedReplacementToken.setAccountsFrozenByDefault(!freezeDefault);
expectedReplacementToken.setMemo(memo);
expectedReplacementToken.setAutoRenewPeriod(autoRenewPeriod);
// and:
final var expectedReplacementToken2 = new MerkleToken(expiry, tokenSupply * 4, 0, symbol, name, freezeDefault, true, new EntityId(0, 0, treasuryAccountNum));
expectedReplacementToken2.setAutoRenewAccount(treasuryId);
expectedReplacementToken2.setSupplyKey(supplyKey);
expectedReplacementToken2.setFreezeKey(freezeKey);
expectedReplacementToken2.setKycKey(kycKey);
expectedReplacementToken2.setPauseKey(pauseKey);
expectedReplacementToken2.setAccountsFrozenByDefault(!freezeDefault);
expectedReplacementToken2.setMemo(memo);
expectedReplacementToken2.setAutoRenewPeriod(autoRenewPeriod);
// and:
final var expectedNewUniqTokenId = NftId.withDefaultShardRealm(tokenEntityId.num(), mintedSerialNo);
final var expectedNewUniqTokenId2 = NftId.withDefaultShardRealm(tokenEntityId.num(), mintedSerialNo2);
final var expectedNewUniqToken = new MerkleUniqueToken(MISSING_ENTITY_ID, nftMeta, creationTime);
final var expectedPastUniqTokenId = NftId.withDefaultShardRealm(tokenEntityId.num(), wipedSerialNo);
final var expectedPastUniqTokenId2 = NftId.withDefaultShardRealm(tokenEntityId.num(), burnedSerialNo);
givenModifiableToken(merkleTokenId, merkleToken);
givenToken(merkleTokenId, merkleToken);
// when:
var modelToken = subject.loadToken(tokenId);
// and:
modelToken.setTotalSupply(tokenSupply * 2);
modelToken.setAutoRenewAccount(treasuryAccount);
modelToken.setTreasury(autoRenewAccount);
modelToken.setFrozenByDefault(!freezeDefault);
modelToken.mintedUniqueTokens().add(mintedToken);
modelToken.setIsDeleted(false);
modelToken.setExpiry(expiry);
modelToken.setAutoRenewPeriod(autoRenewPeriod);
modelToken.removedUniqueTokens().add(wipedToken);
modelToken.setAutoRenewPeriod(autoRenewPeriod);
modelToken.setCustomFees(List.of());
modelToken.setMemo(memo);
// and:
subject.commitToken(modelToken);
// then:
assertEquals(expectedReplacementToken, merkleToken);
// and:
verify(sideEffectsTracker).trackTokenChanges(modelToken);
verify(uniqueTokens).put(expectedNewUniqTokenId, expectedNewUniqToken);
verify(uniqueTokens).put(NftId.withDefaultShardRealm(tokenEntityId.num(), mintedSerialNo), expectedNewUniqToken);
verify(uniqueTokens).remove(expectedPastUniqTokenId);
// when:
modelToken = subject.loadToken(tokenId);
// and:
modelToken.setTotalSupply(tokenSupply * 4);
modelToken.setAutoRenewAccount(treasuryAccount);
modelToken.setTreasury(treasuryAccount);
modelToken.setFrozenByDefault(!freezeDefault);
modelToken.mintedUniqueTokens().add(mintedToken2);
modelToken.setIsDeleted(false);
modelToken.setExpiry(expiry);
modelToken.removedUniqueTokens().add(burnedToken);
modelToken.setCustomFees(List.of());
// and:
subject.commitToken(modelToken);
// then:
assertEquals(expectedReplacementToken2, merkleToken);
// and:
verify(sideEffectsTracker).trackTokenChanges(modelToken);
verify(uniqueTokens).put(expectedNewUniqTokenId2, expectedNewUniqToken);
verify(uniqueTokens).remove(expectedPastUniqTokenId2);
}
use of com.hedera.services.state.merkle.MerkleUniqueToken in project hedera-services by hashgraph.
the class TypedTokenStoreTest method loadsUniqueTokens.
@Test
void loadsUniqueTokens() {
final var aToken = new Token(miscId);
final var merkleUniqueToken = mock(MerkleUniqueToken.class);
final var serialNumbers = List.of(1L, 2L);
given(merkleUniqueToken.getOwner()).willReturn(new EntityId(Id.DEFAULT));
given(uniqueTokens.getImmutableRef(any())).willReturn(merkleUniqueToken);
subject.loadUniqueTokens(aToken, serialNumbers);
assertEquals(2, aToken.getLoadedUniqueTokens().size());
given(uniqueTokens.getImmutableRef(any())).willReturn(null);
assertThrows(InvalidTransactionException.class, () -> subject.loadUniqueTokens(aToken, serialNumbers));
}
use of com.hedera.services.state.merkle.MerkleUniqueToken in project hedera-services by hashgraph.
the class HederaTokenStoreTest method setup.
@BeforeEach
void setup() {
token = mock(MerkleToken.class);
given(token.expiry()).willReturn(expiry);
given(token.symbol()).willReturn(symbol);
given(token.hasAutoRenewAccount()).willReturn(true);
given(token.adminKey()).willReturn(Optional.of(TOKEN_ADMIN_KT.asJKeyUnchecked()));
given(token.name()).willReturn(name);
given(token.hasAdminKey()).willReturn(true);
given(token.hasFeeScheduleKey()).willReturn(true);
given(token.treasury()).willReturn(EntityId.fromGrpcAccountId(treasury));
given(token.tokenType()).willReturn(TokenType.FUNGIBLE_COMMON);
given(token.decimals()).willReturn(2);
nonfungibleToken = mock(MerkleToken.class);
given(nonfungibleToken.hasAdminKey()).willReturn(true);
given(nonfungibleToken.tokenType()).willReturn(TokenType.NON_FUNGIBLE_UNIQUE);
ids = mock(EntityIdSource.class);
given(ids.newTokenId(sponsor)).willReturn(created);
hederaLedger = mock(HederaLedger.class);
nftsLedger = (TransactionalLedger<NftId, NftProperty, MerkleUniqueToken>) mock(TransactionalLedger.class);
given(nftsLedger.get(aNft, NftProperty.OWNER)).willReturn(EntityId.fromGrpcAccountId(sponsor));
given(nftsLedger.get(tNft, NftProperty.OWNER)).willReturn(EntityId.fromGrpcAccountId(primaryTreasury));
given(nftsLedger.exists(aNft)).willReturn(true);
given(nftsLedger.exists(tNft)).willReturn(true);
accountsLedger = (TransactionalLedger<AccountID, AccountProperty, MerkleAccount>) mock(TransactionalLedger.class);
given(accountsLedger.exists(treasury)).willReturn(true);
given(accountsLedger.exists(anotherFeeCollector)).willReturn(true);
given(accountsLedger.exists(autoRenewAccount)).willReturn(true);
given(accountsLedger.exists(newAutoRenewAccount)).willReturn(true);
given(accountsLedger.exists(primaryTreasury)).willReturn(true);
given(accountsLedger.exists(sponsor)).willReturn(true);
given(accountsLedger.exists(counterparty)).willReturn(true);
given(accountsLedger.get(treasury, IS_DELETED)).willReturn(false);
given(accountsLedger.get(autoRenewAccount, IS_DELETED)).willReturn(false);
given(accountsLedger.get(newAutoRenewAccount, IS_DELETED)).willReturn(false);
given(accountsLedger.get(sponsor, IS_DELETED)).willReturn(false);
given(accountsLedger.get(counterparty, IS_DELETED)).willReturn(false);
given(accountsLedger.get(primaryTreasury, IS_DELETED)).willReturn(false);
backingTokens = mock(BackingTokens.class);
given(backingTokens.contains(misc)).willReturn(true);
given(backingTokens.contains(nonfungible)).willReturn(true);
given(backingTokens.getRef(created)).willReturn(token);
given(backingTokens.getImmutableRef(created)).willReturn(token);
given(backingTokens.getRef(misc)).willReturn(token);
given(backingTokens.getImmutableRef(misc)).willReturn(token);
given(backingTokens.getRef(nonfungible)).willReturn(nonfungibleToken);
given(backingTokens.getImmutableRef(tNft.tokenId())).willReturn(nonfungibleToken);
given(backingTokens.getImmutableRef(tNft.tokenId()).treasury()).willReturn(EntityId.fromGrpcAccountId(primaryTreasury));
given(backingTokens.idSet()).willReturn(Set.of(created));
tokenRelsLedger = mock(TransactionalLedger.class);
given(tokenRelsLedger.exists(sponsorMisc)).willReturn(true);
given(tokenRelsLedger.exists(treasuryNft)).willReturn(true);
given(tokenRelsLedger.exists(sponsorNft)).willReturn(true);
given(tokenRelsLedger.exists(counterpartyNft)).willReturn(true);
given(tokenRelsLedger.get(sponsorMisc, TOKEN_BALANCE)).willReturn(sponsorBalance);
given(tokenRelsLedger.get(sponsorMisc, IS_FROZEN)).willReturn(false);
given(tokenRelsLedger.get(sponsorMisc, IS_KYC_GRANTED)).willReturn(true);
given(tokenRelsLedger.exists(treasuryMisc)).willReturn(true);
given(tokenRelsLedger.exists(anotherFeeCollectorMisc)).willReturn(true);
given(tokenRelsLedger.get(treasuryMisc, TOKEN_BALANCE)).willReturn(treasuryBalance);
given(tokenRelsLedger.get(treasuryMisc, IS_FROZEN)).willReturn(false);
given(tokenRelsLedger.get(treasuryMisc, IS_KYC_GRANTED)).willReturn(true);
given(tokenRelsLedger.get(treasuryNft, TOKEN_BALANCE)).willReturn(123L);
given(tokenRelsLedger.get(treasuryNft, IS_FROZEN)).willReturn(false);
given(tokenRelsLedger.get(treasuryNft, IS_KYC_GRANTED)).willReturn(true);
given(tokenRelsLedger.get(sponsorNft, TOKEN_BALANCE)).willReturn(123L);
given(tokenRelsLedger.get(sponsorNft, IS_FROZEN)).willReturn(false);
given(tokenRelsLedger.get(sponsorNft, IS_KYC_GRANTED)).willReturn(true);
given(tokenRelsLedger.get(counterpartyNft, TOKEN_BALANCE)).willReturn(123L);
given(tokenRelsLedger.get(counterpartyNft, IS_FROZEN)).willReturn(false);
given(tokenRelsLedger.get(counterpartyNft, IS_KYC_GRANTED)).willReturn(true);
given(tokenRelsLedger.get(newTreasuryNft, TOKEN_BALANCE)).willReturn(1L);
properties = mock(GlobalDynamicProperties.class);
given(properties.maxTokensPerAccount()).willReturn(MAX_TOKENS_PER_ACCOUNT);
given(properties.maxTokenSymbolUtf8Bytes()).willReturn(MAX_TOKEN_SYMBOL_UTF8_BYTES);
given(properties.maxTokenNameUtf8Bytes()).willReturn(MAX_TOKEN_NAME_UTF8_BYTES);
given(properties.maxCustomFeesAllowed()).willReturn(maxCustomFees);
sideEffectsTracker = new SideEffectsTracker();
subject = new HederaTokenStore(ids, TEST_VALIDATOR, sideEffectsTracker, properties, tokenRelsLedger, nftsLedger, backingTokens);
subject.setAccountsLedger(accountsLedger);
subject.setHederaLedger(hederaLedger);
subject.knownTreasuries.put(treasury, new HashSet<>() {
{
add(misc);
}
});
}
use of com.hedera.services.state.merkle.MerkleUniqueToken in project hedera-services by hashgraph.
the class WorldLedgersTest method wrapsAsExpectedWithoutCommitInterceptors.
@Test
void wrapsAsExpectedWithoutCommitInterceptors() {
final var liveTokenRels = new TransactionalLedger<>(TokenRelProperty.class, MerkleTokenRelStatus::new, new HashMapBackingTokenRels(), new ChangeSummaryManager<>());
final var liveAccounts = new TransactionalLedger<>(AccountProperty.class, MerkleAccount::new, new HashMapBackingAccounts(), new ChangeSummaryManager<>());
final var liveNfts = new TransactionalLedger<>(NftProperty.class, MerkleUniqueToken::new, new HashMapBackingNfts(), new ChangeSummaryManager<>());
final var liveTokens = new TransactionalLedger<>(TokenProperty.class, MerkleToken::new, new HashMapBackingTokens(), new ChangeSummaryManager<>());
final var liveAliases = new AliasManager();
final var source = new WorldLedgers(liveAliases, liveTokenRels, liveAccounts, liveNfts, liveTokens);
assertTrue(source.areMutable());
final var nullTokenRels = new WorldLedgers(liveAliases, null, liveAccounts, liveNfts, liveTokens);
final var nullAccounts = new WorldLedgers(liveAliases, liveTokenRels, null, liveNfts, liveTokens);
final var nullNfts = new WorldLedgers(liveAliases, liveTokenRels, liveAccounts, null, liveTokens);
final var nullTokens = new WorldLedgers(liveAliases, liveTokenRels, liveAccounts, liveNfts, null);
assertFalse(nullTokenRels.areMutable());
assertFalse(nullAccounts.areMutable());
assertFalse(nullNfts.areMutable());
assertFalse(nullTokens.areMutable());
final var wrappedUnusable = nullAccounts.wrapped();
assertSame(((StackedContractAliases) wrappedUnusable.aliases()).wrappedAliases(), nullAccounts.aliases());
assertFalse(wrappedUnusable.areMutable());
final var wrappedSource = source.wrapped();
assertSame(liveTokenRels, wrappedSource.tokenRels().getEntitiesLedger());
assertSame(liveAccounts, wrappedSource.accounts().getEntitiesLedger());
assertSame(liveNfts, wrappedSource.nfts().getEntitiesLedger());
assertSame(liveTokens, wrappedSource.tokens().getEntitiesLedger());
final var stackedAliases = (StackedContractAliases) wrappedSource.aliases();
assertSame(liveAliases, stackedAliases.wrappedAliases());
}
Aggregations