Search in sources :

Example 91 with EntityId

use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.

the class ProtoBalanceFileReader method toAccountBalance.

private AccountBalance toAccountBalance(long consensusTimestamp, SingleAccountBalances balances) {
    EntityId accountId = EntityId.of(balances.getAccountID());
    List<TokenBalance> tokenBalances = balances.getTokenUnitBalancesList().stream().map(tokenBalance -> {
        EntityId tokenId = EntityId.of(tokenBalance.getTokenId());
        TokenBalance.Id id = new TokenBalance.Id(consensusTimestamp, accountId, tokenId);
        return new TokenBalance(tokenBalance.getBalance(), id);
    }).collect(Collectors.toList());
    return new AccountBalance(balances.getHbarBalance(), tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) EntityId(com.hedera.mirror.common.domain.entity.EntityId) AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) Named(javax.inject.Named) SingleAccountBalances(com.hedera.services.stream.proto.SingleAccountBalances) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) StreamFileReaderException(com.hedera.mirror.importer.exception.StreamFileReaderException) DomainUtils(com.hedera.mirror.common.util.DomainUtils) IOException(java.io.IOException) Instant(java.time.Instant) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) Collectors(java.util.stream.Collectors) AtomicLong(java.util.concurrent.atomic.AtomicLong) IOUtils(org.apache.commons.io.IOUtils) Flux(reactor.core.publisher.Flux) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) List(java.util.List) CodedInputStream(com.google.protobuf.CodedInputStream) Log4j2(lombok.extern.log4j.Log4j2) DigestUtils(org.apache.commons.codec.digest.DigestUtils) InputStream(java.io.InputStream) Assert(org.springframework.util.Assert) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) EntityId(com.hedera.mirror.common.domain.entity.EntityId)

Example 92 with EntityId

use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.

the class AccountBalanceLineParserV2 method parse.

/**
 * Parses an account balance line to extract shard, realm, account, balance, and token balances. If the shard
 * matches systemShardNum, creates and returns an {@code AccountBalance} entity object. The account balance line
 * should be in the format of "shard,realm,account,balance"
 *
 * @param line               The account balance line
 * @param consensusTimestamp The consensus timestamp of the account balance line
 * @return {@code AccountBalance} entity object
 * @throws InvalidDatasetException if the line is malformed or the shard does not match {@code systemShardNum}
 */
@Override
public AccountBalance parse(String line, long consensusTimestamp) {
    try {
        if (line == null) {
            throw new InvalidDatasetException("Null line cannot be parsed");
        }
        List<String> parts = SPLITTER.splitToList(line);
        boolean hasTokenBalance;
        if (parts.size() == 5) {
            hasTokenBalance = true;
        } else if (parts.size() == 4) {
            hasTokenBalance = false;
        } else {
            throw new InvalidDatasetException("Invalid account balance line: " + line);
        }
        long shardNum = Long.parseLong(parts.get(0));
        int realmNum = Integer.parseInt(parts.get(1));
        int accountNum = Integer.parseInt(parts.get(2));
        long balance = Long.parseLong(parts.get(3));
        if (shardNum < 0 || realmNum < 0 || accountNum < 0 || balance < 0) {
            throw new InvalidDatasetException("Invalid account balance line: " + line);
        }
        if (shardNum != mirrorProperties.getShard()) {
            throw new InvalidDatasetException(String.format("Invalid account balance line: %s. Expect " + "shard (%d), got shard (%d)", line, mirrorProperties.getShard(), shardNum));
        }
        EntityId accountId = EntityId.of(shardNum, realmNum, accountNum, EntityType.ACCOUNT);
        List<TokenBalance> tokenBalances = hasTokenBalance ? parseTokenBalanceList(parts.get(4), consensusTimestamp, accountId) : Collections.emptyList();
        return new AccountBalance(balance, tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
    } catch (NumberFormatException | InvalidProtocolBufferException ex) {
        throw new InvalidDatasetException("Invalid account balance line: " + line, ex);
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) EntityId(com.hedera.mirror.common.domain.entity.EntityId) InvalidDatasetException(com.hedera.mirror.importer.exception.InvalidDatasetException) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance)

Example 93 with EntityId

use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.

the class ContractUpdateTransactionHandler method updateStakingInfo.

private void updateStakingInfo(RecordItem recordItem, Contract contract) {
    var transactionBody = recordItem.getTransactionBody().getContractUpdateInstance();
    if (transactionBody.hasDeclineReward()) {
        contract.setDeclineReward(transactionBody.getDeclineReward().getValue());
    }
    switch(transactionBody.getStakedIdCase()) {
        case STAKEDID_NOT_SET:
            break;
        case STAKED_NODE_ID:
            contract.setStakedNodeId(transactionBody.getStakedNodeId());
            contract.setStakedAccountId(-1L);
            break;
        case STAKED_ACCOUNT_ID:
            EntityId accountId = EntityId.of(transactionBody.getStakedAccountId());
            contract.setStakedAccountId(AccountIdConverter.INSTANCE.convertToDatabaseColumn(accountId));
            contract.setStakedNodeId(-1L);
            break;
    }
    // If the stake node id or the decline reward value has changed, we start a new stake period.
    if (transactionBody.getStakedIdCase() != STAKEDID_NOT_SET || transactionBody.hasDeclineReward()) {
        contract.setStakePeriodStart(Utility.getEpochDay(recordItem.getConsensusTimestamp()));
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId)

Example 94 with EntityId

use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.

the class CryptoApproveAllowanceTransactionHandler method parseCryptoAllowances.

private void parseCryptoAllowances(List<com.hederahashgraph.api.proto.java.CryptoAllowance> cryptoAllowances, RecordItem recordItem) {
    var consensusTimestamp = recordItem.getConsensusTimestamp();
    var cryptoAllowanceState = new HashMap<CryptoAllowance.Id, CryptoAllowance>();
    var payerAccountId = recordItem.getPayerAccountId();
    // iterate the crypto allowance list in reverse order and honor the last allowance for the same owner and spender
    var iterator = cryptoAllowances.listIterator(cryptoAllowances.size());
    while (iterator.hasPrevious()) {
        var cryptoApproval = iterator.previous();
        EntityId ownerAccountId = getOwnerAccountId(cryptoApproval.getOwner(), payerAccountId);
        if (ownerAccountId == EntityId.EMPTY) {
            // and the partialDataAction is SKIP
            continue;
        }
        CryptoAllowance cryptoAllowance = new CryptoAllowance();
        cryptoAllowance.setAmount(cryptoApproval.getAmount());
        cryptoAllowance.setOwner(ownerAccountId.getId());
        cryptoAllowance.setPayerAccountId(payerAccountId);
        cryptoAllowance.setSpender(EntityId.of(cryptoApproval.getSpender()).getId());
        cryptoAllowance.setTimestampLower(consensusTimestamp);
        if (cryptoAllowanceState.putIfAbsent(cryptoAllowance.getId(), cryptoAllowance) == null) {
            entityListener.onCryptoAllowance(cryptoAllowance);
        }
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) HashMap(java.util.HashMap)

Example 95 with EntityId

use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.

the class CryptoApproveAllowanceTransactionHandler method parseTokenAllowances.

private void parseTokenAllowances(List<com.hederahashgraph.api.proto.java.TokenAllowance> tokenAllowances, RecordItem recordItem) {
    var consensusTimestamp = recordItem.getConsensusTimestamp();
    var payerAccountId = recordItem.getPayerAccountId();
    var tokenAllowanceState = new HashMap<TokenAllowance.Id, TokenAllowance>();
    // iterate the token allowance list in reverse order and honor the last allowance for the same owner, spender,
    // and token
    var iterator = tokenAllowances.listIterator(tokenAllowances.size());
    while (iterator.hasPrevious()) {
        var tokenApproval = iterator.previous();
        EntityId ownerAccountId = getOwnerAccountId(tokenApproval.getOwner(), payerAccountId);
        if (ownerAccountId == EntityId.EMPTY) {
            // and the partialDataAction is SKIP
            continue;
        }
        TokenAllowance tokenAllowance = new TokenAllowance();
        tokenAllowance.setAmount(tokenApproval.getAmount());
        tokenAllowance.setOwner(ownerAccountId.getId());
        tokenAllowance.setPayerAccountId(payerAccountId);
        tokenAllowance.setSpender(EntityId.of(tokenApproval.getSpender()).getId());
        tokenAllowance.setTokenId(EntityId.of(tokenApproval.getTokenId()).getId());
        tokenAllowance.setTimestampLower(consensusTimestamp);
        if (tokenAllowanceState.putIfAbsent(tokenAllowance.getId(), tokenAllowance) == null) {
            entityListener.onTokenAllowance(tokenAllowance);
        }
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) HashMap(java.util.HashMap) TokenAllowance(com.hedera.mirror.common.domain.entity.TokenAllowance)

Aggregations

EntityId (com.hedera.mirror.common.domain.entity.EntityId)134 Test (org.junit.jupiter.api.Test)64 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)43 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)33 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)22 EntityType (com.hedera.mirror.common.domain.entity.EntityType)21 TokenAccount (com.hedera.mirror.common.domain.token.TokenAccount)21 Token (com.hedera.mirror.common.domain.token.Token)20 Entity (com.hedera.mirror.common.domain.entity.Entity)17 Transaction (com.hedera.mirror.common.domain.transaction.Transaction)17 TransactionBody (com.hederahashgraph.api.proto.java.TransactionBody)16 Assertions (org.junit.jupiter.api.Assertions)16 Contract (com.hedera.mirror.common.domain.contract.Contract)15 ACCOUNT (com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT)15 DomainUtils (com.hedera.mirror.common.util.DomainUtils)15 ByteString (com.google.protobuf.ByteString)14 RecordItem (com.hedera.mirror.common.domain.transaction.RecordItem)14 AccountID (com.hederahashgraph.api.proto.java.AccountID)14 ContractID (com.hederahashgraph.api.proto.java.ContractID)14 Consumer (java.util.function.Consumer)13