Search in sources :

Example 1 with EventFile

use of com.hedera.mirror.common.domain.event.EventFile in project hedera-mirror-node by hashgraph.

the class EventFileParserTest method getStreamFile.

@Override
protected StreamFile getStreamFile() {
    long id = ++count;
    Instant instant = Instant.ofEpochSecond(0L, id);
    String filename = StreamFilename.getFilename(parserProperties.getStreamType(), DATA, instant);
    EventFile eventFile = new EventFile();
    eventFile.setBytes(new byte[] { 0, 1, 2 });
    eventFile.setConsensusEnd(id);
    eventFile.setConsensusStart(id);
    eventFile.setConsensusEnd(id);
    eventFile.setCount(id);
    eventFile.setDigestAlgorithm(DigestAlgorithm.SHA384);
    eventFile.setFileHash("fileHash" + id);
    eventFile.setHash("hash" + id);
    eventFile.setLoadEnd(id);
    eventFile.setLoadStart(id);
    eventFile.setName(filename);
    eventFile.setNodeAccountId(EntityId.of("0.0.3", EntityType.ACCOUNT));
    eventFile.setPreviousHash("previousHash" + (id - 1));
    eventFile.setVersion(1);
    eventFile.setItems(Flux.just(new EventItem()));
    return eventFile;
}
Also used : EventItem(com.hedera.mirror.common.domain.event.EventItem) Instant(java.time.Instant) EventFile(com.hedera.mirror.common.domain.event.EventFile)

Example 2 with EventFile

use of com.hedera.mirror.common.domain.event.EventFile in project hedera-mirror-node by hashgraph.

the class EventFileReaderV3Test method readValidFileVersion2.

@Test
void readValidFileVersion2() {
    StreamFileData validFile = createEventFile(EventFileReaderV3.EVENT_STREAM_FILE_VERSION_2, EventFileReaderV3.EVENT_TYPE_PREV_HASH, PREVIOUS_HASH, CONTENT);
    EventFile eventFile = eventFileReader.read(validFile);
    verifyForSuccess(eventFile, validFile, EventFileReaderV3.EVENT_STREAM_FILE_VERSION_2, PREVIOUS_HASH);
}
Also used : StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) EventFile(com.hedera.mirror.common.domain.event.EventFile) Test(org.junit.jupiter.api.Test)

Example 3 with EventFile

use of com.hedera.mirror.common.domain.event.EventFile in project hedera-mirror-node by hashgraph.

the class EventFileReaderV3Test method readValidFileVersion3.

@Test
void readValidFileVersion3() {
    StreamFileData validFile = createEventFile(EventFileReaderV3.EVENT_STREAM_FILE_VERSION_3, EventFileReaderV3.EVENT_TYPE_PREV_HASH, PREVIOUS_HASH, CONTENT);
    EventFile eventFile = eventFileReader.read(validFile);
    verifyForSuccess(eventFile, validFile, EventFileReaderV3.EVENT_STREAM_FILE_VERSION_3, PREVIOUS_HASH);
}
Also used : StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) EventFile(com.hedera.mirror.common.domain.event.EventFile) Test(org.junit.jupiter.api.Test)

Example 4 with EventFile

use of com.hedera.mirror.common.domain.event.EventFile in project hedera-mirror-node by hashgraph.

the class EventFileRepositoryTest method findLatest.

@Test
void findLatest() {
    EventFile eventFile1 = eventFile();
    EventFile eventFile2 = eventFile();
    EventFile eventFile3 = eventFile();
    eventFileRepository.saveAll(List.of(eventFile1, eventFile2, eventFile3));
    assertThat(eventFileRepository.findLatest()).get().isEqualTo(eventFile3);
}
Also used : EventFile(com.hedera.mirror.common.domain.event.EventFile) Test(org.junit.jupiter.api.Test)

Example 5 with EventFile

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

EventFile (com.hedera.mirror.common.domain.event.EventFile)6 Test (org.junit.jupiter.api.Test)3 StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)2 Stopwatch (com.google.common.base.Stopwatch)1 EventItem (com.hedera.mirror.common.domain.event.EventItem)1 InvalidEventFileException (com.hedera.mirror.importer.exception.InvalidEventFileException)1 DataInputStream (java.io.DataInputStream)1 MessageDigest (java.security.MessageDigest)1 Instant (java.time.Instant)1