Skip to content

Commit

Permalink
code smells
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Li <[email protected]>
  • Loading branch information
xin-hedera committed Jan 9, 2025
1 parent 8d29a13 commit b47c4f0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

import com.hedera.hapi.block.stream.protoc.BlockItem;
import com.hedera.mirror.common.util.DomainUtils;
import com.hedera.mirror.importer.exception.StreamFileReaderException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.Value;
import lombok.experimental.NonFinal;

Expand All @@ -38,6 +40,8 @@
class BlockRootHashDigest {

private static final byte[] EMPTY_HASH = createMessageDigest().digest(new byte[0]);
private static final String PREVIOUSHASH = "previousHash";
private static final String STARTOFBLOCKSTATEHASH = "startOfBlockStateHash";

@NonFinal
private boolean finalized;
Expand Down Expand Up @@ -65,8 +69,8 @@ public String digest() {
throw new IllegalStateException("Block root hash is already calculated");
}

Objects.requireNonNull(previousHash, "Null previousHash");
Objects.requireNonNull(startOfBlockStateHash, "Null startOfBlockStateHash");
validateHash(previousHash, PREVIOUSHASH);
validateHash(startOfBlockStateHash, STARTOFBLOCKSTATEHASH);

List<byte[]> leaves = new ArrayList<>();
leaves.add(previousHash);
Expand All @@ -81,27 +85,21 @@ public String digest() {
}

public void setPreviousHash(byte[] previousHash) {
if (Objects.requireNonNull(previousHash, "Null previousHash").length != SHA_384.getSize()) {
throw new IllegalArgumentException(String.format("previousHash is not %d bytes", SHA_384.getSize()));
}

validateHash(previousHash, PREVIOUSHASH);
this.previousHash = previousHash;
}

public void setStartOfBlockStateHash(byte[] startOfBlockStateHash) {
if (Objects.requireNonNull(startOfBlockStateHash, "Null previousHash").length != SHA_384.getSize()) {
throw new IllegalArgumentException(
String.format("startOfBlockStateHash is not %d bytes", SHA_384.getSize()));
}

validateHash(startOfBlockStateHash, STARTOFBLOCKSTATEHASH);
this.startOfBlockStateHash = startOfBlockStateHash;
}

@SneakyThrows
private static MessageDigest createMessageDigest() {
try {
return MessageDigest.getInstance(SHA_384.getName());
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
throw new StreamFileReaderException(ex);
}
}

Expand Down Expand Up @@ -135,4 +133,10 @@ private static byte[] getRootHash(List<byte[]> leaves) {

return leaves.getFirst();
}

private static void validateHash(byte[] hash, String name) {
if (Objects.requireNonNull(hash, "Null " + name).length != SHA_384.getSize()) {
throw new IllegalArgumentException(String.format("%s is not %d bytes", name, SHA_384.getSize()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public BlockItem readBlockItemFor(ItemCase itemCase) {
case EVENT_HEADER, EVENT_TRANSACTION -> blockRootHashDigest.addInputBlockItem(blockItem);
case STATE_CHANGES, TRANSACTION_OUTPUT, TRANSACTION_RESULT -> blockRootHashDigest.addOutputBlockItem(
blockItem);
default -> {}
}

return blockItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
import com.hedera.hapi.block.stream.output.protoc.BlockHeader;
import com.hedera.hapi.block.stream.output.protoc.StateChanges;
import com.hedera.hapi.block.stream.protoc.BlockItem;
import lombok.CustomLog;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;

@CustomLog
class BlockRootHashDigestTest {

private static final byte[] EMPTY_HASH = Hex.decode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import lombok.CustomLog;
import lombok.SneakyThrows;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.junit.jupiter.api.Test;
Expand All @@ -51,7 +50,6 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.core.io.ClassPathResource;

@CustomLog
class ProtoBlockFileReaderTest {

private final ProtoBlockFileReader reader = new ProtoBlockFileReader();
Expand All @@ -69,7 +67,6 @@ void read(String filename, StreamFileData streamFileData, BlockFile expected, lo
.returns(expectedIndex, BlockFile::getIndex)
.satisfies(a -> assertThat(a.getBlockHeader()).isNotNull())
.satisfies(a -> assertThat(a.getBlockProof()).isNotNull());
;
}

@Test
Expand Down

0 comments on commit b47c4f0

Please sign in to comment.