Search in sources :

Example 16 with AccountBalance

use of com.hedera.mirror.common.domain.balance.AccountBalance 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)

Example 17 with AccountBalance

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

the class AccountBalanceFileParserTest method duplicateFile.

@Test
void duplicateFile() {
    // given
    AccountBalanceFile accountBalanceFile = accountBalanceFile(1);
    AccountBalanceFile duplicate = accountBalanceFile(1);
    List<AccountBalance> items = accountBalanceFile.getItems().collectList().block();
    // when
    accountBalanceFileParser.parse(accountBalanceFile);
    // Will be ignored
    accountBalanceFileParser.parse(duplicate);
    // then
    assertThat(accountBalanceFileRepository.count()).isEqualTo(1L);
    assertAccountBalanceFile(accountBalanceFile, items);
}
Also used : AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) Test(org.junit.jupiter.api.Test) IntegrationTest(com.hedera.mirror.importer.IntegrationTest)

Example 18 with AccountBalance

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

the class AccountBalanceFileParserTest method errata.

@Test
void errata() {
    // given
    var network = mirrorProperties.getNetwork();
    mirrorProperties.setNetwork(MirrorProperties.HederaNetwork.MAINNET);
    AccountBalanceFile accountBalanceFile = accountBalanceFile(BAD_TIMESTAMP1);
    List<AccountBalance> items = accountBalanceFile.getItems().collectList().block();
    // when
    accountBalanceFileParser.parse(accountBalanceFile);
    // then
    assertAccountBalanceFile(accountBalanceFile, items);
    assertThat(accountBalanceFile.getTimeOffset()).isEqualTo(-1);
    mirrorProperties.setNetwork(network);
}
Also used : AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) Test(org.junit.jupiter.api.Test) IntegrationTest(com.hedera.mirror.importer.IntegrationTest)

Aggregations

AccountBalance (com.hedera.mirror.common.domain.balance.AccountBalance)18 AccountBalanceFile (com.hedera.mirror.common.domain.balance.AccountBalanceFile)10 TokenBalance (com.hedera.mirror.common.domain.balance.TokenBalance)7 Test (org.junit.jupiter.api.Test)7 EntityId (com.hedera.mirror.common.domain.entity.EntityId)6 InvalidDatasetException (com.hedera.mirror.importer.exception.InvalidDatasetException)5 IOException (java.io.IOException)5 Instant (java.time.Instant)5 List (java.util.List)5 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)4 InputStream (java.io.InputStream)4 Collectors (java.util.stream.Collectors)4 CodedInputStream (com.google.protobuf.CodedInputStream)3 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)3 DomainUtils (com.hedera.mirror.common.util.DomainUtils)3 StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)3 InvalidStreamFileException (com.hedera.mirror.importer.exception.InvalidStreamFileException)3 StreamFileReaderException (com.hedera.mirror.importer.exception.StreamFileReaderException)3 SingleAccountBalances (com.hedera.services.stream.proto.SingleAccountBalances)3 Timestamp (com.hederahashgraph.api.proto.java.Timestamp)3