Search in sources :

Example 16 with AccountBalanceFile

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

the class ProtoBalanceFileReaderTest method getExpectedAccountBalanceFile.

private AccountBalanceFile getExpectedAccountBalanceFile(StreamFileData streamFileData) {
    Instant instant = Instant.parse(TIMESTAMP.replace("_", ":"));
    long consensusTimestamp = DomainUtils.convertToNanosMax(instant);
    long accountNum = 2000;
    long hbarBalance = 3000;
    long tokenNum = 5000;
    long tokenBalance = 6000;
    List<AccountBalance> accountBalances = IntStream.range(0, 10).mapToObj(i -> {
        EntityId accountId = EntityId.of(0, 0, accountNum + i, EntityType.ACCOUNT);
        List<TokenBalance> tokenBalances = IntStream.range(0, 5).mapToObj(j -> {
            EntityId tokenId = EntityId.of(0, 0, tokenNum + i * 5 + j, EntityType.TOKEN);
            return new TokenBalance(tokenBalance + i * 5 + j, new TokenBalance.Id(consensusTimestamp, accountId, tokenId));
        }).collect(Collectors.toList());
        return new AccountBalance(hbarBalance + i, tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
    }).collect(Collectors.toList());
    return AccountBalanceFile.builder().bytes(streamFileData.getBytes()).consensusTimestamp(consensusTimestamp).fileHash("67c2fd054621366dd5a37b6ee36a51bc590361379d539fdac2265af08cb8097729218c7d9ff1f1e354c85b820c5b8cf8").items(Flux.fromIterable(accountBalances)).name(streamFileData.getFilename()).build();
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) EntityId(com.hedera.mirror.common.domain.entity.EntityId) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) EntityType(com.hedera.mirror.common.domain.entity.EntityType) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) TestUtils(com.hedera.mirror.importer.TestUtils) SingleAccountBalances(com.hedera.services.stream.proto.SingleAccountBalances) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) ValueSource(org.junit.jupiter.params.provider.ValueSource) DomainUtils(com.hedera.mirror.common.util.DomainUtils) Instant(java.time.Instant) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) Collectors(java.util.stream.Collectors) AllAccountBalances(com.hedera.services.stream.proto.AllAccountBalances) File(java.io.File) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) EntityId(com.hedera.mirror.common.domain.entity.EntityId) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) Instant(java.time.Instant) List(java.util.List) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) EntityId(com.hedera.mirror.common.domain.entity.EntityId)

Example 17 with AccountBalanceFile

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

the class ProtoBalanceFileReaderTest method unknownFields.

@Test
void unknownFields() {
    UnknownFieldSet.Field field = UnknownFieldSet.Field.newBuilder().addFixed32(11).build();
    AllAccountBalances allAccountBalances = AllAccountBalances.newBuilder().setConsensusTimestamp(Timestamp.newBuilder().setSeconds(1L).build()).mergeUnknownFields(UnknownFieldSet.newBuilder().addField(23, field).build()).addAllAccounts(SingleAccountBalances.newBuilder().build()).build();
    byte[] bytes = allAccountBalances.toByteArray();
    StreamFileData streamFileData = StreamFileData.from(TIMESTAMP + "_Balances.pb", bytes);
    AccountBalanceFile accountBalanceFile = protoBalanceFileReader.read(streamFileData);
    assertThat(accountBalanceFile).isNotNull();
    assertThat(accountBalanceFile.getItems().count().block()).isEqualTo(1L);
}
Also used : AllAccountBalances(com.hedera.services.stream.proto.AllAccountBalances) AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with AccountBalanceFile

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

the class ProtoBalanceFileReaderTest method readGzippedProtoBalanceFile.

@Test
void readGzippedProtoBalanceFile() {
    AccountBalanceFile actual = protoBalanceFileReader.read(streamFileData);
    assertThat(actual).usingRecursiveComparison().ignoringFields("loadStart", "nodeAccountId", "items").isEqualTo(expected);
    assertThat(expected.getItems().collectList().block()).isEqualTo(actual.getItems().collectList().block());
    assertThat(actual.getLoadStart()).isNotNull().isPositive();
}
Also used : AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with AccountBalanceFile

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

the class CsvBalanceFileReader method read.

@Override
public AccountBalanceFile read(StreamFileData streamFileData) {
    MessageDigest messageDigest = DigestUtils.getSha384Digest();
    int bufferSize = balanceParserProperties.getFileBufferSize();
    try (InputStream inputStream = new DigestInputStream(streamFileData.getInputStream(), messageDigest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, CHARSET), bufferSize)) {
        long consensusTimestamp = parseConsensusTimestamp(reader);
        AtomicLong count = new AtomicLong(0L);
        List<AccountBalance> items = new ArrayList<>();
        AccountBalanceFile accountBalanceFile = new AccountBalanceFile();
        accountBalanceFile.setBytes(streamFileData.getBytes());
        accountBalanceFile.setConsensusTimestamp(consensusTimestamp);
        accountBalanceFile.setLoadStart(Instant.now().getEpochSecond());
        accountBalanceFile.setName(streamFileData.getFilename());
        reader.lines().map(line -> {
            try {
                AccountBalance accountBalance = parser.parse(line, consensusTimestamp);
                count.incrementAndGet();
                return accountBalance;
            } catch (InvalidDatasetException ex) {
                log.error(ex);
                return null;
            }
        }).filter(Objects::nonNull).forEachOrdered(items::add);
        accountBalanceFile.setCount(count.get());
        accountBalanceFile.setFileHash(DomainUtils.bytesToHex(messageDigest.digest()));
        accountBalanceFile.setItems(Flux.fromIterable(items));
        return accountBalanceFile;
    } catch (IOException ex) {
        throw new InvalidDatasetException("Error reading account balance file", ex);
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) InputStreamReader(java.io.InputStreamReader) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) InvalidDatasetException(com.hedera.mirror.importer.exception.InvalidDatasetException) BufferedReader(java.io.BufferedReader) MessageDigest(java.security.MessageDigest)

Example 20 with AccountBalanceFile

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

AccountBalanceFile (com.hedera.mirror.common.domain.balance.AccountBalanceFile)23 Test (org.junit.jupiter.api.Test)18 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)9 AccountBalance (com.hedera.mirror.common.domain.balance.AccountBalance)8 StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)7 Instant (java.time.Instant)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)2 TokenBalance (com.hedera.mirror.common.domain.balance.TokenBalance)2 EntityId (com.hedera.mirror.common.domain.entity.EntityId)2 AllAccountBalances (com.hedera.services.stream.proto.AllAccountBalances)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Stopwatch (com.google.common.base.Stopwatch)1 CodedInputStream (com.google.protobuf.CodedInputStream)1 StreamType (com.hedera.mirror.common.domain.StreamType)1 EntityType (com.hedera.mirror.common.domain.entity.EntityType)1 DomainUtils (com.hedera.mirror.common.util.DomainUtils)1