Skip to content

Commit

Permalink
sanding
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Aug 1, 2024
1 parent 2529488 commit 141921d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ObjectEvent<T> {
T val;

public void set(T val) {
public void set(final T val) {
this.val = val;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void publishEvent(final BlockItem blockItem) throws IOException {
LOGGER.log(System.Logger.Level.INFO, "Send a response to end the stream");

// Publish the block for all subscribers to receive
final var endStreamResponse = buildEndStreamResponse();
final SubscribeStreamResponse endStreamResponse = buildEndStreamResponse();
ringBuffer.publishEvent((event, sequence) -> event.set(endStreamResponse));

// Unsubscribe all downstream consumers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private Optional<BlockItem> readBlockItem(final String blockItemPath) throws IOE
return Optional.empty();
}

// FileNotFound is thrown also when a file cannot be read.
// FileNotFound is also thrown when a file cannot be read.
// So re-throw here to make a different decision upstream.
throw io;
}
Expand Down Expand Up @@ -171,7 +171,8 @@ private boolean isVerified(final Path path) {
return true;
}

protected void setPerm(Path path, Set<PosixFilePermission> perms) throws IOException {
protected void setPerm(final Path path, final Set<PosixFilePermission> perms)
throws IOException {
Files.setPosixFilePermissions(path, perms);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public BlockAsDirRemover(
}

@Override
public void remove(long id) throws IOException {
public void remove(final long id) throws IOException {

// Calculate the block path and proactively set the permissions
// for removal
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(id));
if (!Files.exists(blockPath)) {
if (Files.notExists(blockPath)) {
LOGGER.log(System.Logger.Level.ERROR, "Block does not exist: " + id);
return;
}
Expand All @@ -56,14 +56,14 @@ public void remove(long id) throws IOException {
}
}

private static boolean delete(File file) {
private static boolean delete(final File file) {

// Recursively delete the contents
// of the directory
if (file.isDirectory()) {
File[] files = file.listFiles();
final File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
for (final File f : files) {
delete(f);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public void write(final BlockItem blockItem) throws IOException {
System.Logger.Level.ERROR,
"Error writing the BlockItem protobuf to a file",
e);

// Remove the block if repairing the permissions fails
if (retries > 0) {
// Attempt to remove the block
blockRemover.remove(Long.parseLong(currentBlockDir.toString()));
Expand All @@ -111,7 +113,7 @@ public void write(final BlockItem blockItem) throws IOException {

protected void write(final Path blockItemFilePath, final BlockItem blockItem)
throws IOException {
try (FileOutputStream fos = new FileOutputStream(blockItemFilePath.toString())) {
try (final FileOutputStream fos = new FileOutputStream(blockItemFilePath.toString())) {
blockItem.writeTo(fos);
LOGGER.log(
System.Logger.Level.INFO,
Expand Down Expand Up @@ -172,7 +174,8 @@ private Path calculateBlockPath() {
return blockNodeRootPath.resolve(currentBlockDir);
}

private void createPath(Path blockNodePath, System.Logger.Level logLevel) throws IOException {
private void createPath(final Path blockNodePath, final System.Logger.Level logLevel)
throws IOException {
// Initialize the Block directory if it does not exist
if (Files.notExists(blockNodePath)) {
Files.createDirectory(blockNodePath, filePerms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
import java.io.IOException;

public interface BlockRemover {
void remove(long id) throws IOException;
void remove(final long id) throws IOException;
}
19 changes: 10 additions & 9 deletions server/src/main/java/com/hedera/block/server/producer/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ private Util() {}

public static byte[] getFakeHash(BlockItem blockItem)
throws IOException, NoSuchAlgorithmException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (ObjectOutputStream objectOutputStream =
new ObjectOutputStream(byteArrayOutputStream)) {

try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final ObjectOutputStream objectOutputStream =
new ObjectOutputStream(byteArrayOutputStream)) {
objectOutputStream.writeObject(blockItem);
}

// Get the serialized bytes
byte[] serializedObject = byteArrayOutputStream.toByteArray();
// Get the serialized bytes
byte[] serializedObject = byteArrayOutputStream.toByteArray();

// Calculate the SHA-256 hash
MessageDigest digest = MessageDigest.getInstance("SHA-384");
return digest.digest(serializedObject);
// Calculate the SHA-256 hash
MessageDigest digest = MessageDigest.getInstance("SHA-384");
return digest.digest(serializedObject);
}
}
}
2 changes: 1 addition & 1 deletion server/src/test/resources/producer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ trap cleanup SIGINT
echo "{\"block_item\": {\"start_event\": {\"creator_id\": $i},\"value\": \"Payload[...]\"}}"
fi

sleep 0.5
sleep 0.01
done

if [ $iter -eq $2 ]; then
Expand Down

0 comments on commit 141921d

Please sign in to comment.