use of com.hedera.mirror.importer.reader.HashObject in project hedera-mirror-node by hashgraph.
the class RecordFileReaderImplV5 method readBody.
private void readBody(ValidatedDataInputStream vdis, DigestInputStream metadataDigestInputStream, RecordFile recordFile) throws IOException {
String filename = recordFile.getName();
// object stream version
vdis.readInt();
// start object running hash
HashObject startHashObject = new HashObject(vdis, DIGEST_ALGORITHM);
// metadata hash is not calculated on record stream objects
metadataDigestInputStream.on(false);
long hashObjectClassId = startHashObject.getClassId();
int count = 0;
long consensusStart = 0;
List<RecordItem> items = new ArrayList<>();
RecordItem lastRecordItem = null;
// read record stream objects
while (!isHashObject(vdis, hashObjectClassId)) {
RecordStreamObject recordStreamObject = new RecordStreamObject(vdis, recordFile.getHapiVersion(), count);
var recordItem = RecordItem.builder().hapiVersion(recordFile.getHapiVersion()).previous(lastRecordItem).recordBytes(recordStreamObject.recordBytes).transactionIndex(count).transactionBytes(recordStreamObject.transactionBytes).build();
items.add(recordItem);
if (count == 0) {
consensusStart = recordItem.getConsensusTimestamp();
}
lastRecordItem = recordItem;
count++;
}
if (count == 0) {
throw new InvalidStreamFileException("No record stream objects in record file " + filename);
}
long consensusEnd = lastRecordItem.getConsensusTimestamp();
// end object running hash, metadata hash is calculated on it
metadataDigestInputStream.on(true);
HashObject endHashObject = new HashObject(vdis, DIGEST_ALGORITHM);
if (vdis.available() != 0) {
throw new InvalidStreamFileException("Extra data discovered in record file " + filename);
}
recordFile.setCount((long) count);
recordFile.setConsensusEnd(consensusEnd);
recordFile.setConsensusStart(consensusStart);
recordFile.setHash(Hex.encodeHexString(endHashObject.getHash()));
recordFile.setItems(Flux.fromIterable(items));
recordFile.setPreviousHash(Hex.encodeHexString(startHashObject.getHash()));
}
use of com.hedera.mirror.importer.reader.HashObject in project hedera-mirror-node by hashgraph.
the class SignatureFileReaderV5 method read.
@Override
public FileStreamSignature read(StreamFileData signatureFileData) {
String filename = signatureFileData.getFilename();
try (ValidatedDataInputStream vdis = new ValidatedDataInputStream(signatureFileData.getInputStream(), filename)) {
vdis.readByte(SIGNATURE_FILE_FORMAT_VERSION, "fileVersion");
// Read the objectStreamSignatureVersion, which is not used
vdis.readInt();
HashObject fileHashObject = new HashObject(vdis, "entireFile", SHA384);
SignatureObject fileHashSignatureObject = new SignatureObject(vdis, "entireFile");
HashObject metadataHashObject = new HashObject(vdis, "metadata", SHA384);
SignatureObject metadataHashSignatureObject = new SignatureObject(vdis, "metadata");
if (vdis.available() != 0) {
throw new SignatureFileParsingException("Extra data discovered in signature file " + filename);
}
FileStreamSignature fileStreamSignature = new FileStreamSignature();
fileStreamSignature.setBytes(signatureFileData.getBytes());
fileStreamSignature.setFileHash(fileHashObject.getHash());
fileStreamSignature.setFileHashSignature(fileHashSignatureObject.getSignature());
fileStreamSignature.setFilename(filename);
fileStreamSignature.setMetadataHash(metadataHashObject.getHash());
fileStreamSignature.setMetadataHashSignature(metadataHashSignatureObject.getSignature());
fileStreamSignature.setSignatureType(fileHashSignatureObject.getSignatureType());
return fileStreamSignature;
} catch (InvalidStreamFileException | IOException e) {
throw new SignatureFileParsingException(e);
}
}
Aggregations