use of com.hedera.services.utils.EntityNum in project hedera-services by hashgraph.
the class LedgerBalanceChangesTest method happyPathTransfersWithAutoCreation.
@Test
void happyPathTransfersWithAutoCreation() {
final Key aliasA = KeyFactory.getDefaultInstance().newEd25519();
final AccountID a = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(10L).build();
final AccountID validAliasAccountWithAlias = AccountID.newBuilder().setAlias(aliasA.toByteString()).build();
final AccountID validAliasAccountWithId = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(11L).build();
final AccountID funding = AccountID.newBuilder().setShardNum(0).setRealmNum(0).setAccountNum(98L).build();
final EntityNum validAliasEntityNum = new EntityNum(11);
List<BalanceChange> changes = new ArrayList<>();
changes.add(hbarChange(a, -100));
changes.add(hbarChange(validAliasAccountWithAlias, 0));
final var validAliasAccount = MerkleAccountFactory.newAccount().get();
final var fundingAccount = MerkleAccountFactory.newAccount().get();
final var aAccount = MerkleAccountFactory.newAccount().balance(aStartBalance).get();
backingAccounts.put(a, aAccount);
backingAccounts.put(validAliasAccountWithId, validAliasAccount);
backingAccounts.put(funding, fundingAccount);
given(autoCreationLogic.create(any(), eq(accountsLedger))).willAnswer(invocationOnMock -> {
final var change = (BalanceChange) invocationOnMock.getArgument(0);
change.replaceAliasWith(validAliasEntityNum.toGrpcAccountId());
return Pair.of(OK, 100L);
});
given(dynamicProperties.fundingAccount()).willReturn(funding);
subject.begin();
assertDoesNotThrow(() -> subject.doZeroSum(changes));
subject.commit();
assertEquals(aStartBalance - 100, backingAccounts.getImmutableRef(a).getBalance());
assertEquals(0, backingAccounts.getImmutableRef(validAliasEntityNum.toGrpcAccountId()).getBalance());
}
use of com.hedera.services.utils.EntityNum 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));
}
use of com.hedera.services.utils.EntityNum in project hedera-services by hashgraph.
the class AliasManagerTest method settersAndGettersWork.
@Test
void settersAndGettersWork() {
final var a = new EntityNum(1);
final var b = new EntityNum(2);
ByteString aliasA = ByteString.copyFromUtf8("aaaa");
ByteString aliasB = ByteString.copyFromUtf8("bbbb");
Map<ByteString, EntityNum> expectedMap = new HashMap<>() {
{
put(aliasA, a);
put(aliasB, b);
}
};
assertTrue(subject.getAliases().isEmpty());
subject.setAliases(expectedMap);
assertEquals(expectedMap, subject.getAliases());
assertEquals(b, subject.lookupIdBy(ByteString.copyFromUtf8("bbbb")));
assertTrue(subject.contains(aliasA));
}
use of com.hedera.services.utils.EntityNum in project hedera-services by hashgraph.
the class MerkleAccountTest method gettersDelegate.
@Test
void gettersDelegate() {
// expect:
assertEquals(new EntityNum(number), subject.getKey());
assertEquals(state.expiry(), subject.getExpiry());
assertEquals(state.balance(), subject.getBalance());
assertEquals(state.autoRenewSecs(), subject.getAutoRenewSecs());
assertEquals(state.isReleased(), subject.isReleased());
assertEquals(state.isSmartContract(), subject.isSmartContract());
assertEquals(state.isReceiverSigRequired(), subject.isReceiverSigRequired());
assertEquals(state.memo(), subject.getMemo());
assertEquals(state.proxy(), subject.getProxy());
assertTrue(equalUpToDecodability(state.key(), subject.getAccountKey()));
assertSame(tokens, subject.tokens());
assertEquals(2L, subject.getNftsOwned());
assertEquals(state.getMaxAutomaticAssociations(), subject.getMaxAutomaticAssociations());
assertEquals(state.getAlreadyUsedAutomaticAssociations(), subject.getAlreadyUsedAutoAssociations());
assertEquals(state.getAlias(), subject.getAlias());
assertEquals(state.getNumContractKvPairs(), subject.getNumContractKvPairs());
assertEquals(state.getCryptoAllowances().entrySet(), subject.getCryptoAllowances().entrySet());
assertEquals(state.getFungibleTokenAllowances().entrySet(), subject.getFungibleTokenAllowances().entrySet());
assertEquals(state.getNftAllowances().entrySet(), subject.getNftAllowances().entrySet());
}
use of com.hedera.services.utils.EntityNum in project hedera-services by hashgraph.
the class MerkleTokenTest method v0160DeserializeWorks.
@Test
void v0160DeserializeWorks() throws IOException {
final var fin = mock(SerializableDataInputStream.class);
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).willReturn(feeScheduleKey);
given(fin.readNormalisedString(anyInt())).willReturn(symbol).willReturn(name).willReturn(memo);
given(fin.readLong()).willReturn(subject.expiry()).willReturn(subject.autoRenewPeriod()).willReturn(subject.totalSupply()).willReturn(subject.maxSupply()).willReturn(subject.getLastUsedSerialNumber());
given(fin.readInt()).willReturn(subject.decimals()).willReturn(subject.tokenType().ordinal()).willReturn(subject.supplyType().ordinal());
given(fin.readBoolean()).willReturn(isDeleted).willReturn(subject.accountsAreFrozenByDefault());
given(fin.readSerializable()).willReturn(subject.treasury());
given(fin.<FcCustomFee>readSerializableList(eq(Integer.MAX_VALUE), eq(true), any())).willReturn(feeSchedule);
final var read = new MerkleToken();
read.deserialize(fin, MerkleToken.RELEASE_0160_VERSION);
// and when:
read.setKey(new EntityNum(number));
// expect:
assertEquals(subject, read);
}
Aggregations