Search in sources :

Example 1 with InvalidEventFileException

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

the class EventFileReaderV3 method read.

@Override
public EventFile read(StreamFileData streamFileData) {
    String filename = streamFileData.getFilename();
    int fileVersion = 0;
    Stopwatch stopwatch = Stopwatch.createStarted();
    boolean success = false;
    long consensusStart = DomainUtils.convertToNanosMax(streamFileData.getInstant());
    EventFile eventFile = new EventFile();
    eventFile.setBytes(streamFileData.getBytes());
    eventFile.setConsensusEnd(consensusStart);
    eventFile.setConsensusStart(consensusStart);
    eventFile.setCount(0L);
    eventFile.setDigestAlgorithm(DIGEST_ALGORITHM);
    eventFile.setLoadStart(Instant.now().getEpochSecond());
    eventFile.setName(filename);
    try (DataInputStream dis = new DataInputStream(streamFileData.getInputStream())) {
        // MessageDigest for getting the file Hash
        // suppose file[i] = p[i] || h[i] || c[i];
        // p[i] denotes the bytes before previousFileHash;
        // h[i] denotes the hash of file i - 1, i.e., previousFileHash;
        // c[i] denotes the bytes after previousFileHash;
        // '||' means concatenation
        // for Version2, h[i + 1] = hash(p[i] || h[i] || c[i]);
        // for Version3, h[i + 1] = hash(p[i] || h[i] || hash(c[i]))
        MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM.getName());
        fileVersion = dis.readInt();
        md.update(Ints.toByteArray(fileVersion));
        if (fileVersion < EVENT_STREAM_FILE_VERSION_2 || fileVersion > EVENT_STREAM_FILE_VERSION_3) {
            throw new InvalidEventFileException("Invalid event stream file version " + fileVersion);
        }
        byte typePrevHash = dis.readByte();
        md.update(typePrevHash);
        if (typePrevHash != EVENT_TYPE_PREV_HASH) {
            throw new InvalidEventFileException("Expect EVENT_TYPE_PREV_HASH marker, got " + typePrevHash);
        }
        byte[] prevFileHash = new byte[EVENT_PREV_HASH_LENGTH];
        dis.readFully(prevFileHash);
        md.update(prevFileHash);
        byte[] remaining = dis.readAllBytes();
        if (remaining.length != 0) {
            if (fileVersion == EVENT_STREAM_FILE_VERSION_2) {
                md.update(remaining);
            } else {
                MessageDigest mdForEventData = MessageDigest.getInstance(DIGEST_ALGORITHM.getName());
                md.update(mdForEventData.digest(remaining));
            }
        }
        String fileHash = Hex.encodeHexString(md.digest());
        eventFile.setFileHash(fileHash);
        eventFile.setHash(fileHash);
        eventFile.setPreviousHash(Hex.encodeHexString(prevFileHash));
        eventFile.setVersion(fileVersion);
        success = true;
        return eventFile;
    } catch (InvalidEventFileException e) {
        throw e;
    } catch (Exception e) {
        throw new InvalidEventFileException("Error reading bad event file " + filename, e);
    } finally {
        log.info("Read v{} event file {} {}successfully in {}", fileVersion, filename, success ? "" : "un", stopwatch);
    }
}
Also used : Stopwatch(com.google.common.base.Stopwatch) InvalidEventFileException(com.hedera.mirror.importer.exception.InvalidEventFileException) DataInputStream(java.io.DataInputStream) MessageDigest(java.security.MessageDigest) EventFile(com.hedera.mirror.common.domain.event.EventFile) InvalidEventFileException(com.hedera.mirror.importer.exception.InvalidEventFileException)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 EventFile (com.hedera.mirror.common.domain.event.EventFile)1 InvalidEventFileException (com.hedera.mirror.importer.exception.InvalidEventFileException)1 DataInputStream (java.io.DataInputStream)1 MessageDigest (java.security.MessageDigest)1