Search in sources :

Example 1 with AccountBalance

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

the class AccountBalanceLineParserV1Test method parse.

@DisplayName("Parse account balance line")
@ParameterizedTest(name = "from \"{0}\"")
@CsvSource(value = { "'0,0,123,700';false;0;123;700", "' 0,0,123,700';false;0;123;700", "'0, 0,123,700';false;0;123;700", "'0,0, 123,700';false;0;123;700", "'0,0,123, 700';false;0;123;700", "'0,0,123,700 ';false;0;123;700", "'1,0,123,700';true;;;", "'x,0,123,700';true;;;", "'0,x,123,700';true;;;", "'0,0,x,700';true;;;", "'0,0,123,a00';true;;;", "'1000000000000000000000000000,0,123,700';true;;;", "'0,1000000000000000000000000000,123,700';true;;;", "'0,0,1000000000000000000000000000,700';true;;;", "'0,0,123,1000000000000000000000000000';true;;;", "'-1,0,123,700';true;;;", "'0,-1,123,700';true;;;", "'0,0,-1,700';true;;;", "'0,0,123,-1';true;;;", "'foobar';true;;;", "'';true;;;", ";true;;;" }, delimiter = ';')
void parse(String line, boolean expectThrow, Long expectedRealm, Long expectedAccount, Long expectedBalance) {
    if (!expectThrow) {
        AccountBalance accountBalance = parser.parse(line, timestamp);
        var id = accountBalance.getId();
        assertThat(accountBalance.getBalance()).isEqualTo(expectedBalance);
        assertThat(id).isNotNull();
        assertThat(id.getAccountId().getShardNum()).isEqualTo(mirrorProperties.getShard());
        assertThat(id.getAccountId().getRealmNum()).isEqualTo(expectedRealm);
        assertThat(id.getAccountId().getEntityNum()).isEqualTo(expectedAccount);
        assertThat(id.getConsensusTimestamp()).isEqualTo(timestamp);
    } else {
        assertThrows(InvalidDatasetException.class, () -> {
            parser.parse(line, timestamp);
        });
    }
}
Also used : AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) CsvSource(org.junit.jupiter.params.provider.CsvSource) DisplayName(org.junit.jupiter.api.DisplayName) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with AccountBalance

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

the class AccountBalanceRepositoryTest method create.

private AccountBalance create(long consensusTimestamp, int accountNum, long balance, int numberOfTokenBalances) {
    AccountBalance.Id id = new AccountBalance.Id();
    id.setConsensusTimestamp(consensusTimestamp);
    id.setAccountId(EntityId.of(0, 0, accountNum, EntityType.ACCOUNT));
    AccountBalance accountBalance = new AccountBalance();
    accountBalance.setBalance(balance);
    accountBalance.setId(id);
    accountBalance.setTokenBalances(createTokenBalances(consensusTimestamp, accountNum, balance, numberOfTokenBalances));
    return accountBalanceRepository.save(accountBalance);
}
Also used : AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) EntityId(com.hedera.mirror.common.domain.entity.EntityId)

Example 3 with AccountBalance

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

the class AccountBalanceRepositoryTest method findByConsensusTimestamp.

@Test
void findByConsensusTimestamp() {
    AccountBalance accountBalance1 = create(1L, 1, 100, 0);
    AccountBalance accountBalance2 = create(1L, 2, 200, 3);
    create(2L, 1, 50, 1);
    List<AccountBalance> result = accountBalanceRepository.findByIdConsensusTimestamp(1);
    result.forEach(ab -> ab.getTokenBalances().sort(Comparator.comparing(o -> o.getId().getTokenId())));
    assertThat(result).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(accountBalance1, accountBalance2);
}
Also used : AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) Test(org.junit.jupiter.api.Test)

Example 4 with AccountBalance

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

the class ProtoBalanceFileReader method toFlux.

private Flux<AccountBalance> toFlux(StreamFileData streamFileData) {
    return Flux.defer(() -> {
        InputStream inputStream = streamFileData.getInputStream();
        ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.getEmptyRegistry();
        CodedInputStream input = CodedInputStream.newInstance(inputStream);
        AtomicLong consensusTimestamp = new AtomicLong(0L);
        UnknownFieldSet.Builder unknownFieldSet = UnknownFieldSet.newBuilder();
        return Flux.<AccountBalance>generate(sink -> {
            try {
                boolean done = false;
                while (!done) {
                    int tag = input.readTag();
                    switch(tag) {
                        case TAG_EOF:
                            done = true;
                            break;
                        case TAG_TIMESTAMP:
                            Timestamp timestamp = input.readMessage(Timestamp.parser(), extensionRegistry);
                            consensusTimestamp.set(DomainUtils.timestampInNanosMax(timestamp));
                            break;
                        case TAG_BALANCE:
                            Assert.state(consensusTimestamp.get() > 0, "Missing consensus timestamp)");
                            var ab = input.readMessage(SingleAccountBalances.parser(), extensionRegistry);
                            sink.next(toAccountBalance(consensusTimestamp.get(), ab));
                            return;
                        default:
                            log.warn("Unsupported tag: {}", tag);
                            done = !unknownFieldSet.mergeFieldFrom(tag, input);
                    }
                }
                Assert.state(consensusTimestamp.get() > 0, "Missing consensus timestamp)");
                sink.complete();
            } catch (IOException e) {
                sink.error(new StreamFileReaderException(e));
            } catch (IllegalStateException e) {
                sink.error(new InvalidStreamFileException(e));
            }
        }).doFinally(s -> IOUtils.closeQuietly(inputStream));
    });
}
Also used : 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) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) StreamFileReaderException(com.hedera.mirror.importer.exception.StreamFileReaderException) IOException(java.io.IOException) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) AtomicLong(java.util.concurrent.atomic.AtomicLong) UnknownFieldSet(com.google.protobuf.UnknownFieldSet)

Example 5 with AccountBalance

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

the class ProtoBalanceFileReader method read.

@Override
public AccountBalanceFile read(StreamFileData streamFileData) {
    Instant loadStart = Instant.now();
    Flux<AccountBalance> items = toFlux(streamFileData);
    long consensusTimestamp = items.map(AccountBalance::getId).map(AccountBalance.Id::getConsensusTimestamp).blockFirst();
    try (InputStream inputStream = streamFileData.getInputStream()) {
        AccountBalanceFile accountBalanceFile = new AccountBalanceFile();
        accountBalanceFile.setBytes(streamFileData.getBytes());
        accountBalanceFile.setConsensusTimestamp(consensusTimestamp);
        accountBalanceFile.setFileHash(DigestUtils.sha384Hex(inputStream));
        accountBalanceFile.setItems(items);
        accountBalanceFile.setLoadStart(loadStart.getEpochSecond());
        accountBalanceFile.setName(streamFileData.getFilename());
        return accountBalanceFile;
    } catch (IOException e) {
        throw new StreamFileReaderException(e);
    }
}
Also used : AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) Instant(java.time.Instant) StreamFileReaderException(com.hedera.mirror.importer.exception.StreamFileReaderException) IOException(java.io.IOException)

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