Make logic in PersistBlockTask more explicit to fix a LGTM warning (#92)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/98/head
Adrian Sutton 5 years ago committed by GitHub
parent 13ae4317b7
commit 9147391634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 63
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/tasks/PersistBlockTask.java

@ -27,6 +27,7 @@ import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.MetricsSystem;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -67,22 +68,20 @@ public class PersistBlockTask<C> extends AbstractEthTask<Block> {
final List<Block> blocks, final List<Block> blocks,
final HeaderValidationMode headerValidationMode, final HeaderValidationMode headerValidationMode,
final MetricsSystem metricsSystem) { final MetricsSystem metricsSystem) {
checkArgument(blocks.size() > 0); checkArgument(!blocks.isEmpty(), "No blocks to import provided");
return () -> { return () -> {
final List<Block> successfulImports = new ArrayList<>(); final List<Block> successfulImports = new ArrayList<>();
CompletableFuture<Block> future = null; final Iterator<Block> blockIterator = blocks.iterator();
for (final Block block : blocks) { CompletableFuture<Block> future =
if (future == null) { importBlockAndAddToList(
future = protocolSchedule,
importBlockAndAddToList( protocolContext,
protocolSchedule, blockIterator.next(),
protocolContext, successfulImports,
block, headerValidationMode,
successfulImports, metricsSystem);
headerValidationMode, while (blockIterator.hasNext()) {
metricsSystem); final Block block = blockIterator.next();
continue;
}
future = future =
future.thenCompose( future.thenCompose(
b -> b ->
@ -122,34 +121,34 @@ public class PersistBlockTask<C> extends AbstractEthTask<Block> {
final List<Block> blocks, final List<Block> blocks,
final HeaderValidationMode headerValidationMode, final HeaderValidationMode headerValidationMode,
final MetricsSystem metricsSystem) { final MetricsSystem metricsSystem) {
checkArgument(blocks.size() > 0); checkArgument(!blocks.isEmpty(), "No blocks to import provided");
return () -> { return () -> {
final CompletableFuture<List<Block>> finalResult = new CompletableFuture<>(); final CompletableFuture<List<Block>> finalResult = new CompletableFuture<>();
final List<Block> successfulImports = new ArrayList<>(); final List<Block> successfulImports = new ArrayList<>();
CompletableFuture<Block> future = null; final Iterator<PersistBlockTask<C>> tasks =
for (final Block block : blocks) { blocks.stream()
if (future == null) { .map(
future = block ->
PersistBlockTask.create( PersistBlockTask.create(
protocolSchedule, protocolContext, block, headerValidationMode, metricsSystem) protocolSchedule,
.run(); protocolContext,
continue; block,
} headerValidationMode,
metricsSystem))
.iterator();
CompletableFuture<Block> future = tasks.next().run();
while (tasks.hasNext()) {
final PersistBlockTask<C> task = tasks.next();
future = future =
future future
.handle((r, t) -> r) .handle((r, t) -> r)
.thenCompose( .thenCompose(
(r) -> { r -> {
if (r != null) { if (r != null) {
successfulImports.add(r); successfulImports.add(r);
} }
return PersistBlockTask.create( return task.run();
protocolSchedule,
protocolContext,
block,
headerValidationMode,
metricsSystem)
.run();
}); });
} }
future.whenComplete( future.whenComplete(

Loading…
Cancel
Save