use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.
the class MerkleSpecialFilesTest method hashSummarizesAsExpected.
@Test
void hashSummarizesAsExpected() throws IOException {
subject.append(fid, Arrays.copyOfRange(stuff, 0, stuff.length / 2));
subject.append(secondFid, Arrays.copyOfRange(stuff, stuff.length / 2, stuff.length));
final var fcq = new FCQueue<FilePart>();
fcq.add(new FilePart(subject.get(fid)));
final var secondFcq = new FCQueue<FilePart>();
secondFcq.add(new FilePart(subject.get(secondFid)));
final var baos = new ByteArrayOutputStream();
baos.write(Longs.toByteArray(fid.getFileNum()));
baos.write(fcq.getHash().getValue());
baos.write(Longs.toByteArray(secondFid.getFileNum()));
baos.write(secondFcq.getHash().getValue());
final var expected = CommonUtils.noThrowSha384HashOf(baos.toByteArray());
assertArrayEquals(expected, subject.getHash().getValue());
}
use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.
the class MerkleSpecialFilesTest method liveFireSerdeWorksWithNonEmpty.
@Test
void liveFireSerdeWorksWithNonEmpty() throws IOException, ConstructableRegistryException {
final var baos = new ByteArrayOutputStream();
final var dos = new SerializableDataOutputStream(baos);
ConstructableRegistry.registerConstructable(new ClassConstructorPair(MerkleSpecialFiles.class, MerkleSpecialFiles::new));
ConstructableRegistry.registerConstructable(new ClassConstructorPair(FCQueue.class, FCQueue::new));
ConstructableRegistry.registerConstructable(new ClassConstructorPair(FilePart.class, FilePart::new));
subject.update(fid, Arrays.copyOfRange(stuff, 0, stuff.length / 2));
subject.update(secondFid, Arrays.copyOfRange(stuff, stuff.length / 2, stuff.length));
subject.serialize(dos);
dos.flush();
final var bytes = baos.toByteArray();
final var bais = new ByteArrayInputStream(bytes);
final var din = new SerializableDataInputStream(bais);
final var newSubject = new MerkleSpecialFiles();
newSubject.deserialize(din, MerkleSpecialFiles.CURRENT_VERSION);
assertArrayEquals(subject.get(fid), newSubject.get(fid), "Deserialized contents should match for first file");
assertArrayEquals(subject.get(secondFid), newSubject.get(secondFid), "Deserialized contents should match for second file");
}
use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.
the class MerkleSpecialFiles method newFcqWith.
private FCQueue<FilePart> newFcqWith(byte[] initialContents) {
final var fileByParts = new FCQueue<FilePart>();
fileByParts.add(new FilePart(initialContents));
return fileByParts;
}
use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.
the class MerkleSpecialFiles method append.
/**
* Appends the given bytes to the contents of the requested file. (Or, if the file
* does not yet exist, creates it with the given contents.)
*
* @param fid
* the id of the file to append to
* @param extraContents
* the contents to append
*/
public synchronized void append(FileID fid, byte[] extraContents) {
throwIfImmutable();
final var fileByParts = fileContents.get(fid);
if (fileByParts == null) {
update(fid, extraContents);
return;
}
fileByParts.add(new FilePart(extraContents));
hashCache.remove(fid);
}
use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.
the class MerkleSpecialFiles method get.
/**
* Gets the contents of the given file.
*
* @param fid
* the id of the file to get
* @return the file's contents
*/
public synchronized byte[] get(FileID fid) {
final var fileByParts = fileContents.get(fid);
if (fileByParts == null) {
return NO_CONTENTS;
}
final var baos = baosSupplier.get();
for (final FilePart part : fileByParts) {
try {
baos.write(part.getData());
} catch (IOException e) {
log.error("Special file concatenation failed for {}", readableId(fid), e);
throw new UncheckedIOException(e);
}
}
return baos.toByteArray();
}
Aggregations