Search in sources :

Example 6 with InvalidDatasetException

use of com.hedera.mirror.importer.exception.InvalidDatasetException 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 7 with InvalidDatasetException

use of com.hedera.mirror.importer.exception.InvalidDatasetException in project hedera-mirror-node by hashgraph.

the class AccountBalanceLineParserV1 method parse.

/**
 * Parses an account balance line to extract shard, realm, account, and balance. 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);
        if (parts.size() != 4) {
            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));
        }
        return new AccountBalance(balance, Collections.emptyList(), new AccountBalance.Id(consensusTimestamp, EntityId.of(shardNum, realmNum, accountNum, EntityType.ACCOUNT)));
    } catch (NumberFormatException ex) {
        throw new InvalidDatasetException("Invalid account balance line: " + line, ex);
    }
}
Also used : InvalidDatasetException(com.hedera.mirror.importer.exception.InvalidDatasetException) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance)

Example 8 with InvalidDatasetException

use of com.hedera.mirror.importer.exception.InvalidDatasetException 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)

Aggregations

InvalidDatasetException (com.hedera.mirror.importer.exception.InvalidDatasetException)8 AccountBalance (com.hedera.mirror.common.domain.balance.AccountBalance)4 IOException (java.io.IOException)3 EntityId (com.hedera.mirror.common.domain.entity.EntityId)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 HashSet (java.util.HashSet)2 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)1 AccountBalanceFile (com.hedera.mirror.common.domain.balance.AccountBalanceFile)1 TokenBalance (com.hedera.mirror.common.domain.balance.TokenBalance)1 FileData (com.hedera.mirror.common.domain.file.FileData)1 NftTransferId (com.hedera.mirror.common.domain.token.NftTransferId)1 TokenId (com.hedera.mirror.common.domain.token.TokenId)1 AssessedCustomFee (com.hedera.mirror.common.domain.transaction.AssessedCustomFee)1 CustomFee (com.hedera.mirror.common.domain.transaction.CustomFee)1 TransactionSignature (com.hedera.mirror.common.domain.transaction.TransactionSignature)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1