Search in sources :

Example 6 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class TokenBalanceRepositoryTest method save.

@Test
void save() {
    TokenBalance tokenBalance1 = create(1L, 1, 1, 100);
    TokenBalance tokenBalance2 = create(2L, 2, 2, 200);
    TokenBalance tokenBalance3 = create(3L, 3, 2, 300);
    assertThat(tokenBalanceRepository.findById(tokenBalance1.getId())).get().isEqualTo(tokenBalance1);
    assertThat(tokenBalanceRepository.findAll()).containsExactlyInAnyOrder(tokenBalance1, tokenBalance2, tokenBalance3);
}
Also used : TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) Test(org.junit.jupiter.api.Test)

Example 7 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance 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 8 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class AccountBalanceLineParserV2 method parseTokenBalanceList.

private List<TokenBalance> parseTokenBalanceList(String tokenBalancesProtoString, long consensusTimestamp, EntityId accountId) throws InvalidProtocolBufferException {
    List<com.hederahashgraph.api.proto.java.TokenBalance> tokenBalanceProtoList = TokenBalances.parseFrom(Base64.decodeBase64(tokenBalancesProtoString)).getTokenBalancesList();
    List<TokenBalance> tokenBalances = new ArrayList<>();
    for (com.hederahashgraph.api.proto.java.TokenBalance tokenBalanceProto : tokenBalanceProtoList) {
        TokenID tokenId = tokenBalanceProto.getTokenId();
        TokenBalance tokenBalance = new TokenBalance(tokenBalanceProto.getBalance(), new TokenBalance.Id(consensusTimestamp, accountId, EntityId.of(tokenId)));
        tokenBalances.add(tokenBalance);
    }
    return tokenBalances;
}
Also used : ArrayList(java.util.ArrayList) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) TokenID(com.hederahashgraph.api.proto.java.TokenID)

Example 9 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance 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 10 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class AccountBalanceFileParser method doParse.

@Override
protected void doParse(AccountBalanceFile accountBalanceFile) {
    log.info("Starting processing account balances file {}", accountBalanceFile.getName());
    DateRangeFilter filter = mirrorDateRangePropertiesProcessor.getDateRangeFilter(StreamType.BALANCE);
    int batchSize = ((BalanceParserProperties) parserProperties).getBatchSize();
    long count = 0L;
    if (filter.filter(accountBalanceFile.getConsensusTimestamp())) {
        List<AccountBalance> accountBalances = new ArrayList<>(batchSize);
        Map<TokenBalance.Id, TokenBalance> tokenBalances = new HashMap<>(batchSize);
        count = accountBalanceFile.getItems().doOnNext(accountBalance -> {
            accountBalances.add(accountBalance);
            for (var tokenBalance : accountBalance.getTokenBalances()) {
                if (tokenBalances.putIfAbsent(tokenBalance.getId(), tokenBalance) != null) {
                    log.warn("Skipping duplicate token balance: {}", tokenBalance);
                }
            }
            if (accountBalances.size() >= batchSize) {
                batchPersister.persist(accountBalances);
                accountBalances.clear();
            }
            if (tokenBalances.size() >= batchSize) {
                batchPersister.persist(tokenBalances.values());
                tokenBalances.clear();
            }
        }).count().block();
        batchPersister.persist(accountBalances);
        batchPersister.persist(tokenBalances.values());
    }
    Instant loadEnd = Instant.now();
    accountBalanceFile.setCount(count);
    accountBalanceFile.setLoadEnd(loadEnd.getEpochSecond());
    streamFileListener.onEnd(accountBalanceFile);
    streamFileRepository.save(accountBalanceFile);
}
Also used : MirrorDateRangePropertiesProcessor(com.hedera.mirror.importer.config.MirrorDateRangePropertiesProcessor) Leader(com.hedera.mirror.importer.leader.Leader) StreamType(com.hedera.mirror.common.domain.StreamType) AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) HashMap(java.util.HashMap) Instant(java.time.Instant) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) Backoff(org.springframework.retry.annotation.Backoff) ArrayList(java.util.ArrayList) DateRangeFilter(com.hedera.mirror.importer.config.MirrorDateRangePropertiesProcessor.DateRangeFilter) List(java.util.List) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Map(java.util.Map) Named(javax.inject.Named) AbstractStreamFileParser(com.hedera.mirror.importer.parser.AbstractStreamFileParser) Retryable(org.springframework.retry.annotation.Retryable) BatchPersister(com.hedera.mirror.importer.parser.batch.BatchPersister) StreamFileRepository(com.hedera.mirror.importer.repository.StreamFileRepository) Transactional(org.springframework.transaction.annotation.Transactional) HashMap(java.util.HashMap) Instant(java.time.Instant) ArrayList(java.util.ArrayList) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) DateRangeFilter(com.hedera.mirror.importer.config.MirrorDateRangePropertiesProcessor.DateRangeFilter)

Aggregations

TokenBalance (com.hedera.mirror.common.domain.balance.TokenBalance)10 AccountBalance (com.hedera.mirror.common.domain.balance.AccountBalance)6 EntityId (com.hedera.mirror.common.domain.entity.EntityId)6 List (java.util.List)4 AccountBalanceFile (com.hedera.mirror.common.domain.balance.AccountBalanceFile)3 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 Test (org.junit.jupiter.api.Test)3 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)2 EntityType (com.hedera.mirror.common.domain.entity.EntityType)2 DomainUtils (com.hedera.mirror.common.util.DomainUtils)2 StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)2 InvalidDatasetException (com.hedera.mirror.importer.exception.InvalidDatasetException)2 InvalidStreamFileException (com.hedera.mirror.importer.exception.InvalidStreamFileException)2 SingleAccountBalances (com.hedera.services.stream.proto.SingleAccountBalances)2 Timestamp (com.hederahashgraph.api.proto.java.Timestamp)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Named (javax.inject.Named)2