Search in sources :

Example 1 with FileOperationException

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

the class ErrataMigration method missingTransactions.

// Adds the transactions and records that are missing due to the insufficient fee funding issue in services.
private void missingTransactions() throws IOException {
    Set<Long> consensusTimestamps = new HashSet<>();
    var resourceResolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resourceResolver.getResources("classpath*:errata/mainnet/missingtransactions/*.bin");
    recordStreamFileListener.onStart();
    var dateRangeFilter = new DateRangeFilter(mirrorProperties.getStartDate(), mirrorProperties.getEndDate());
    for (Resource resource : resources) {
        String name = resource.getFilename();
        log.info("Loading file: {}", name);
        try (var in = new ValidatedDataInputStream(resource.getInputStream(), name)) {
            byte[] recordBytes = in.readLengthAndBytes(1, MAX_TRANSACTION_LENGTH, false, "record");
            byte[] transactionBytes = in.readLengthAndBytes(1, MAX_TRANSACTION_LENGTH, false, "transaction");
            var transactionRecord = TransactionRecord.parseFrom(recordBytes);
            var transaction = Transaction.parseFrom(transactionBytes);
            var recordItem = new RecordItem(transaction, transactionRecord);
            long timestamp = recordItem.getConsensusTimestamp();
            if (transactionRepository.findById(timestamp).isEmpty() && dateRangeFilter.filter(timestamp)) {
                entityRecordItemListener.onItem(recordItem);
                consensusTimestamps.add(timestamp);
            }
        } catch (IOException e) {
            recordStreamFileListener.onError();
            throw new FileOperationException("Error parsing errata file " + name, e);
        }
    }
    if (consensusTimestamps.isEmpty()) {
        log.info("Previously inserted all missing transactions");
        return;
    }
    recordStreamFileListener.onEnd(null);
    var ids = new MapSqlParameterSource("ids", consensusTimestamps);
    jdbcOperations.update("update crypto_transfer set errata = 'INSERT' where consensus_timestamp in (:ids)", ids);
    jdbcOperations.update("update transaction set errata = 'INSERT' where consensus_timestamp in (:ids)", ids);
    Long min = consensusTimestamps.stream().min(Long::compareTo).orElse(null);
    Long max = consensusTimestamps.stream().max(Long::compareTo).orElse(null);
    log.info("Inserted {} missing transactions between {} and {}", consensusTimestamps.size(), min, max);
}
Also used : FileOperationException(com.hedera.mirror.importer.exception.FileOperationException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ValidatedDataInputStream(com.hedera.mirror.importer.reader.ValidatedDataInputStream) DateRangeFilter(com.hedera.mirror.importer.config.MirrorDateRangePropertiesProcessor.DateRangeFilter) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) HashSet(java.util.HashSet)

Aggregations

RecordItem (com.hedera.mirror.common.domain.transaction.RecordItem)1 DateRangeFilter (com.hedera.mirror.importer.config.MirrorDateRangePropertiesProcessor.DateRangeFilter)1 FileOperationException (com.hedera.mirror.importer.exception.FileOperationException)1 ValidatedDataInputStream (com.hedera.mirror.importer.reader.ValidatedDataInputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1