Skip to content

Commit

Permalink
Make logic in PersistBlockTask more explicit to fix a LGTM warning (h…
Browse files Browse the repository at this point in the history
…yperledger#92)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
  • Loading branch information
ajsutton authored Oct 9, 2019
1 parent 13ae431 commit 9147391
Showing 1 changed file with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
Expand Down Expand Up @@ -67,22 +68,20 @@ public static <C> Supplier<CompletableFuture<List<Block>>> forSequentialBlocks(
final List<Block> blocks,
final HeaderValidationMode headerValidationMode,
final MetricsSystem metricsSystem) {
checkArgument(blocks.size() > 0);
checkArgument(!blocks.isEmpty(), "No blocks to import provided");
return () -> {
final List<Block> successfulImports = new ArrayList<>();
CompletableFuture<Block> future = null;
for (final Block block : blocks) {
if (future == null) {
future =
importBlockAndAddToList(
protocolSchedule,
protocolContext,
block,
successfulImports,
headerValidationMode,
metricsSystem);
continue;
}
final Iterator<Block> blockIterator = blocks.iterator();
CompletableFuture<Block> future =
importBlockAndAddToList(
protocolSchedule,
protocolContext,
blockIterator.next(),
successfulImports,
headerValidationMode,
metricsSystem);
while (blockIterator.hasNext()) {
final Block block = blockIterator.next();
future =
future.thenCompose(
b ->
Expand Down Expand Up @@ -122,34 +121,34 @@ public static <C> Supplier<CompletableFuture<List<Block>>> forUnorderedBlocks(
final List<Block> blocks,
final HeaderValidationMode headerValidationMode,
final MetricsSystem metricsSystem) {
checkArgument(blocks.size() > 0);
checkArgument(!blocks.isEmpty(), "No blocks to import provided");
return () -> {
final CompletableFuture<List<Block>> finalResult = new CompletableFuture<>();
final List<Block> successfulImports = new ArrayList<>();
CompletableFuture<Block> future = null;
for (final Block block : blocks) {
if (future == null) {
future =
PersistBlockTask.create(
protocolSchedule, protocolContext, block, headerValidationMode, metricsSystem)
.run();
continue;
}
final Iterator<PersistBlockTask<C>> tasks =
blocks.stream()
.map(
block ->
PersistBlockTask.create(
protocolSchedule,
protocolContext,
block,
headerValidationMode,
metricsSystem))
.iterator();

CompletableFuture<Block> future = tasks.next().run();
while (tasks.hasNext()) {
final PersistBlockTask<C> task = tasks.next();
future =
future
.handle((r, t) -> r)
.thenCompose(
(r) -> {
r -> {
if (r != null) {
successfulImports.add(r);
}
return PersistBlockTask.create(
protocolSchedule,
protocolContext,
block,
headerValidationMode,
metricsSystem)
.run();
return task.run();
});
}
future.whenComplete(
Expand Down

0 comments on commit 9147391

Please sign in to comment.