Skip to content

Commit

Permalink
Error prone final - infrastructure module (Consensys#8225)
Browse files Browse the repository at this point in the history
* add missing final

Signed-off-by: Gabriel Fukushima <[email protected]>

* move back to record

Signed-off-by: Gabriel Fukushima <[email protected]>

* remove toString leftover

Signed-off-by: Gabriel Fukushima <[email protected]>

---------

Signed-off-by: Gabriel Fukushima <[email protected]>
  • Loading branch information
gfukushima authored Apr 21, 2024
1 parent cfc8412 commit 147b431
Show file tree
Hide file tree
Showing 184 changed files with 915 additions and 767 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public interface AsyncRunnerFactory {
int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY;
Pattern ASYNC_RUNNER_NAME_PATTERN = Pattern.compile("[a-zA-Z][a-zA-Z0-9_]*");

default AsyncRunner create(String name, int maxThreads) {
default AsyncRunner create(final String name, final int maxThreads) {
validateAsyncRunnerName(name);
return create(name, maxThreads, DEFAULT_MAX_QUEUE_SIZE);
}

default void validateAsyncRunnerName(String asyncRunnerName) {
default void validateAsyncRunnerName(final String asyncRunnerName) {
if (!ASYNC_RUNNER_NAME_PATTERN.matcher(asyncRunnerName).matches()) {
throw new IllegalArgumentException(
String.format(
Expand All @@ -35,7 +35,7 @@ default void validateAsyncRunnerName(String asyncRunnerName) {
}
}

default AsyncRunner create(String name, int maxThreads, int maxQueueSize) {
default AsyncRunner create(final String name, final int maxThreads, final int maxQueueSize) {
return create(name, maxThreads, maxQueueSize, DEFAULT_THREAD_PRIORITY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class FutureUtil {
public static <T> void ignoreFuture(final Future<T> future) {}

static void runWithFixedDelay(
AsyncRunner runner,
ExceptionThrowingRunnable runnable,
Cancellable task,
final AsyncRunner runner,
final ExceptionThrowingRunnable runnable,
final Cancellable task,
final Duration initialDelay,
final Duration duration,
Consumer<Throwable> exceptionHandler) {
final Consumer<Throwable> exceptionHandler) {

runner
.runAfterDelay(
Expand Down Expand Up @@ -85,11 +85,11 @@ static void runWithFixedDelay(
}

static void runAfterDelay(
AsyncRunner runner,
ExceptionThrowingRunnable runnable,
Cancellable task,
final AsyncRunner runner,
final ExceptionThrowingRunnable runnable,
final Cancellable task,
final Duration delay,
Consumer<Throwable> exceptionHandler) {
final Consumer<Throwable> exceptionHandler) {
runner
.runAfterDelay(
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public static <T, X extends CompletionStage<?>> Consumer<T> ifExceptionGetsHereR
return value -> ifExceptionGetsHereRaiseABug(action.apply(value));
}

public static <U> SafeFuture<U> completedFuture(U value) {
public static <U> SafeFuture<U> completedFuture(final U value) {
SafeFuture<U> future = new SafeFuture<>();
future.complete(value);
return future;
}

public static <U> SafeFuture<U> failedFuture(Throwable ex) {
public static <U> SafeFuture<U> failedFuture(final Throwable ex) {
SafeFuture<U> future = new SafeFuture<>();
future.completeExceptionally(ex);
return future;
Expand Down Expand Up @@ -89,7 +89,7 @@ public static <U> SafeFuture<U> of(final ExceptionThrowingSupplier<U> supplier)
*
* @see #orInterrupt(Interruptor...)
*/
public static SafeFuture<Void> notInterrupted(Interruptor... interruptors) {
public static SafeFuture<Void> notInterrupted(final Interruptor... interruptors) {
SafeFuture<Void> delayedFuture = new SafeFuture<>();
SafeFuture<Void> ret = delayedFuture.orInterrupt(interruptors);
delayedFuture.complete(null);
Expand All @@ -112,7 +112,7 @@ public static SafeFuture<Void> notInterrupted(Interruptor... interruptors) {
* @see #orInterrupt(Interruptor...)
*/
public static Interruptor createInterruptor(
CompletableFuture<?> interruptFuture, Supplier<Exception> exceptionSupplier) {
final CompletableFuture<?> interruptFuture, final Supplier<Exception> exceptionSupplier) {
return new Interruptor(interruptFuture, exceptionSupplier);
}

Expand All @@ -122,7 +122,8 @@ public static Interruptor createInterruptor(
* @param loopBody A supplier for generating futures to be run in succession
* @return A future that will complete when looping terminates
*/
public static SafeFuture<Void> asyncDoWhile(ExceptionThrowingFutureSupplier<Boolean> loopBody) {
public static SafeFuture<Void> asyncDoWhile(
final ExceptionThrowingFutureSupplier<Boolean> loopBody) {
// Loop while futures complete immediately in order to avoid stack overflow due to recursion
SafeFuture<Boolean> loopFuture = SafeFuture.of(loopBody);
while (loopFuture.isCompletedNormally()) {
Expand Down Expand Up @@ -480,7 +481,7 @@ public <U> SafeFuture<U> thenApplyChecked(final ExceptionThrowingFunction<T, U>
}

/** Shortcut to process the value when complete and return the same future */
public SafeFuture<T> thenPeek(Consumer<T> fn) {
public SafeFuture<T> thenPeek(final Consumer<T> fn) {
return thenApply(
v -> {
fn.accept(v);
Expand Down Expand Up @@ -681,13 +682,13 @@ public SafeFuture<Void> handleException(final Consumer<Throwable> action) {
* future becomes complete when `waitForStage` completes. If the `waitForStage` completes
* exceptionally the resulting future also completes exceptionally with the same exception
*/
public SafeFuture<T> thenWaitFor(Function<T, CompletionStage<?>> waitForStage) {
public SafeFuture<T> thenWaitFor(final Function<T, CompletionStage<?>> waitForStage) {
return thenCompose(t -> waitForStage.apply(t).thenApply(__ -> t));
}

@SafeVarargs
@SuppressWarnings("unchecked")
public final SafeFuture<T> or(SafeFuture<T>... others) {
public final SafeFuture<T> or(final SafeFuture<T>... others) {
SafeFuture<T>[] futures = Arrays.copyOf(others, others.length + 1);
futures[others.length] = this;
return anyOf(futures).thenApply(o -> (T) o);
Expand All @@ -712,7 +713,7 @@ public final SafeFuture<T> or(SafeFuture<T>... others) {
// The result of anyOf() future is ignored since it is used just to handle completion
// of any future. All possible outcomes are propagated to the returned future instance
@SuppressWarnings("FutureReturnValueIgnored")
public SafeFuture<T> orInterrupt(Interruptor... interruptors) {
public SafeFuture<T> orInterrupt(final Interruptor... interruptors) {
CompletableFuture<?>[] allFuts = new CompletableFuture<?>[interruptors.length + 1];
allFuts[0] = this;
for (int i = 0; i < interruptors.length; i++) {
Expand Down Expand Up @@ -766,7 +767,7 @@ public static class Interruptor {
private final Supplier<Exception> exceptionSupplier;

private Interruptor(
CompletableFuture<?> interruptFuture, Supplier<Exception> exceptionSupplier) {
final CompletableFuture<?> interruptFuture, final Supplier<Exception> exceptionSupplier) {
this.interruptFuture = interruptFuture;
this.exceptionSupplier = exceptionSupplier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public <T> SafeFuture<T> execute(final ExceptionThrowingSupplier<T> callable) {

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public <T> SafeFuture<T> executeFuture(Supplier<SafeFuture<T>> task) {
public <T> SafeFuture<T> executeFuture(final Supplier<SafeFuture<T>> task) {
// Note: started is only set to true after thread has been initialized so if it is true, thread
// must be initialized.
if (!started.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private static Bytes calculateSHA256Checksum(
private static Bytes applyCipherFunction(
final Bytes decryptionKey,
final Cipher cipher,
boolean encryptMode,
final boolean encryptMode,
final byte[] inputMessage) {
// aes-128-ctr needs first 16 bytes for its key. The 2nd 16 bytes are used to create checksum
final SecretKeySpec secretKey =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public KeyStoreBytesModule() {

private static class BytesSerializer extends JsonSerializer<Bytes> {
@Override
public void serialize(Bytes bytes, JsonGenerator jGen, SerializerProvider serializerProvider)
public void serialize(
final Bytes bytes, final JsonGenerator jGen, final SerializerProvider serializerProvider)
throws IOException {
// write bytes in hex without 0x
jGen.writeString(bytes.appendHexTo(new StringBuilder()).toString());
Expand All @@ -54,14 +55,16 @@ public void serialize(Bytes bytes, JsonGenerator jGen, SerializerProvider serial

private static class BytesDeserializer extends JsonDeserializer<Bytes> {
@Override
public Bytes deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
public Bytes deserialize(final JsonParser p, final DeserializationContext ctxt)
throws IOException {
return Bytes.fromHexString(p.getValueAsString());
}
}

private static class Bytes32Serializer extends JsonSerializer<Bytes32> {
@Override
public void serialize(Bytes32 bytes, JsonGenerator jGen, SerializerProvider serializerProvider)
public void serialize(
final Bytes32 bytes, final JsonGenerator jGen, final SerializerProvider serializerProvider)
throws IOException {
// write bytes in hex without 0x
jGen.writeString(bytes.appendHexTo(new StringBuilder()).toString());
Expand All @@ -70,7 +73,8 @@ public void serialize(Bytes32 bytes, JsonGenerator jGen, SerializerProvider seri

private static class Bytes32Deserializer extends JsonDeserializer<Bytes32> {
@Override
public Bytes32 deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
public Bytes32 deserialize(final JsonParser p, final DeserializationContext ctxt)
throws IOException {
return Bytes32.fromHexString(p.getValueAsString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public String toString() {
.toString();
}

private static boolean isPowerOf2(int x) {
private static boolean isPowerOf2(final int x) {
return ((x & (x - 1)) == 0);
}
}
58 changes: 35 additions & 23 deletions infrastructure/bls/src/main/java/tech/pegasys/teku/bls/BLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class BLS {
resetBlsImplementation();
}

public static void setBlsImplementation(BLS12381 blsImpl) {
public static void setBlsImplementation(final BLS12381 blsImpl) {
BLS.blsImpl = blsImpl;
}

Expand All @@ -73,7 +73,7 @@ public static void resetBlsImplementation() {
* @param message The message to sign, not null
* @return The Signature, not null
*/
public static BLSSignature sign(BLSSecretKey secretKey, Bytes message) {
public static BLSSignature sign(final BLSSecretKey secretKey, final Bytes message) {
return new BLSSignature(secretKey.getSecretKey().sign(message));
}

Expand All @@ -87,7 +87,8 @@ public static BLSSignature sign(BLSSecretKey secretKey, Bytes message) {
* @param signature The signature, not null
* @return True if the verification is successful, false otherwise.
*/
public static boolean verify(BLSPublicKey publicKey, Bytes message, BLSSignature signature) {
public static boolean verify(
final BLSPublicKey publicKey, final Bytes message, final BLSSignature signature) {
if (BLSConstants.verificationDisabled) {
LOG.warn("Skipping bls verification.");
return true;
Expand All @@ -112,7 +113,7 @@ public static boolean verify(BLSPublicKey publicKey, Bytes message, BLSSignature
* @return the aggregated signature
* @throws BlsException if any of supplied signatures is invalid
*/
public static BLSSignature aggregate(List<BLSSignature> signatures) throws BlsException {
public static BLSSignature aggregate(final List<BLSSignature> signatures) throws BlsException {
try {
checkArgument(signatures.size() > 0, "Aggregating zero signatures is invalid.");
return new BLSSignature(
Expand Down Expand Up @@ -140,7 +141,9 @@ public static BLSSignature aggregate(List<BLSSignature> signatures) throws BlsEx
* @return True if the verification is successful, false otherwise
*/
public static boolean aggregateVerify(
List<BLSPublicKey> publicKeys, List<Bytes> messages, BLSSignature signature) {
final List<BLSPublicKey> publicKeys,
final List<Bytes> messages,
final BLSSignature signature) {
try {
checkArgument(
publicKeys.size() == messages.size(),
Expand Down Expand Up @@ -179,7 +182,7 @@ public static boolean aggregateVerify(
* @return True if the verification is successful, false otherwise
*/
public static boolean fastAggregateVerify(
List<BLSPublicKey> publicKeys, Bytes message, BLSSignature signature) {
final List<BLSPublicKey> publicKeys, final Bytes message, final BLSSignature signature) {
if (BLSConstants.verificationDisabled) {
LOG.warn("Skipping bls verification.");
return true;
Expand Down Expand Up @@ -224,7 +227,9 @@ public static boolean fastAggregateVerify(
* @return True if the verification is successful, false otherwise
*/
public static boolean batchVerify(
List<List<BLSPublicKey>> publicKeys, List<Bytes> messages, List<BLSSignature> signatures) {
final List<List<BLSPublicKey>> publicKeys,
final List<Bytes> messages,
final List<BLSSignature> signatures) {
try {
checkArgument(
publicKeys.size() == messages.size() && publicKeys.size() == signatures.size(),
Expand Down Expand Up @@ -267,11 +272,11 @@ public static boolean batchVerify(
* @return True if the verification is successful, false otherwise
*/
public static boolean batchVerify(
List<List<BLSPublicKey>> publicKeys,
List<Bytes> messages,
List<BLSSignature> signatures,
boolean doublePairing,
boolean parallel) {
final List<List<BLSPublicKey>> publicKeys,
final List<Bytes> messages,
final List<BLSSignature> signatures,
final boolean doublePairing,
final boolean parallel) {
if (BLSConstants.verificationDisabled) {
LOG.warn("Skipping bls verification.");
return true;
Expand Down Expand Up @@ -345,7 +350,10 @@ public static boolean batchVerify(
* #completeBatchVerify(List)}
*/
public static BatchSemiAggregate prepareBatchVerify(
int index, List<BLSPublicKey> publicKeys, Bytes message, BLSSignature signature) {
final int index,
final List<BLSPublicKey> publicKeys,
final Bytes message,
final BLSSignature signature) {
try {
return getBlsImpl()
.prepareBatchVerify(
Expand All @@ -366,13 +374,13 @@ public static BatchSemiAggregate prepareBatchVerify(
* #prepareBatchVerify(int, List, Bytes, BLSSignature)}
*/
private static BatchSemiAggregate prepareBatchVerify2(
int index,
List<BLSPublicKey> publicKeys1,
Bytes message1,
BLSSignature signature1,
List<BLSPublicKey> publicKeys2,
Bytes message2,
BLSSignature signature2) {
final int index,
final List<BLSPublicKey> publicKeys1,
final Bytes message1,
final BLSSignature signature1,
final List<BLSPublicKey> publicKeys2,
final Bytes message2,
final BLSSignature signature2) {
try {
return getBlsImpl()
.prepareBatchVerify2(
Expand All @@ -395,7 +403,7 @@ private static BatchSemiAggregate prepareBatchVerify2(
*
* @return True if the verification is successful, false otherwise
*/
public static boolean completeBatchVerify(List<BatchSemiAggregate> preparedSignatures) {
public static boolean completeBatchVerify(final List<BatchSemiAggregate> preparedSignatures) {
if (BLSConstants.verificationDisabled) {
LOG.warn("Skipping bls verification.");
return true;
Expand All @@ -415,7 +423,8 @@ public static boolean completeBatchVerify(List<BatchSemiAggregate> preparedSigna
* @param dst domain separation tag (DST), not null
* @return The Signature, not null
*/
public static BLSSignature sign(BLSSecretKey secretKey, Bytes message, String dst) {
public static BLSSignature sign(
final BLSSecretKey secretKey, final Bytes message, final String dst) {
return new BLSSignature(secretKey.getSecretKey().sign(message, dst));
}

Expand All @@ -429,7 +438,10 @@ public static BLSSignature sign(BLSSecretKey secretKey, Bytes message, String ds
* @return True if the verification is successful, false otherwise.
*/
public static boolean verify(
BLSPublicKey publicKey, Bytes message, BLSSignature signature, String dst) {
final BLSPublicKey publicKey,
final Bytes message,
final BLSSignature signature,
final String dst) {
if (BLSConstants.verificationDisabled) {
LOG.warn("Skipping bls verification.");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static BLSKeyPair random(final SecureRandom srng) {
* @param publicKey a BLS public key
* @param secretKey a BLS secret key
*/
public BLSKeyPair(BLSPublicKey publicKey, BLSSecretKey secretKey) {
public BLSKeyPair(final BLSPublicKey publicKey, final BLSSecretKey secretKey) {
this.publicKey = publicKey;
this.secretKey = secretKey;
}
Expand All @@ -50,7 +50,7 @@ public BLSKeyPair(BLSPublicKey publicKey, BLSSecretKey secretKey) {
*
* @param secretKey a BLS secret key
*/
public BLSKeyPair(BLSSecretKey secretKey) {
public BLSKeyPair(final BLSSecretKey secretKey) {
this(new BLSPublicKey(secretKey), secretKey);
}

Expand All @@ -59,7 +59,7 @@ public BLSKeyPair(BLSSecretKey secretKey) {
*
* @param keyPair an implementation-specific key pair
*/
private BLSKeyPair(KeyPair keyPair) {
private BLSKeyPair(final KeyPair keyPair) {
this(new BLSPublicKey(keyPair.getPublicKey()), new BLSSecretKey(keyPair.getSecretKey()));
}

Expand Down
Loading

0 comments on commit 147b431

Please sign in to comment.