Skip to content

Commit

Permalink
Out of bound exception
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Peinlich <[email protected]>
  • Loading branch information
gezero committed Mar 16, 2022
1 parent be6d86b commit 3034f51
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected CompletableFuture<Void> possiblyMoreBackwardSteps(final BlockHeader bl
context.getProtocolContext().getBlockchain().getChainHead().getHeight(),
context.getProtocolContext().getBlockchain().getChainHead().getHash().toHexString());
}
LOG.info("Backward sync did not reach a know block, need to go deeper");
LOG.debug("Backward sync did not reach a know block, need to go deeper");
completableFuture.complete(null);
return completableFuture.thenCompose(this::executeAsync);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected BlockHeader processKnownAncestors(final Void unused) {
() -> header.getHash().toHexString());
saveBlock(backwardChain.getTrustedBlock(header.getHash()));
} else {
debugLambda(LOG, "First unprocessed header is {}", header::getNumber);
return header;
}
}
Expand Down Expand Up @@ -114,7 +115,7 @@ public CompletableFuture<Void> possibleRequestBlock(final BlockHeader blockHeade
if (blockHeader == null) {
return CompletableFuture.completedFuture(null);
} else {
debugLambda(
infoLambda(
LOG,
"We don't have body of block {}, going to request it",
() -> blockHeader.getHash().toHexString());
Expand All @@ -127,7 +128,7 @@ public CompletableFuture<Void> possibleRequestBodies(final List<BlockHeader> blo
if (blockHeaders.isEmpty()) {
return CompletableFuture.completedFuture(null);
} else {
debugLambda(
infoLambda(
LOG,
"We don't have body of {} blocks {}->{} ({}), going to request it",
blockHeaders::size,
Expand Down Expand Up @@ -226,7 +227,7 @@ protected CompletableFuture<Void> possiblyMoreForwardSteps(final BlockHeader fir
CompletableFuture<Void> completableFuture = CompletableFuture.completedFuture(null);
if (firstNotSynced == null) {
final List<Block> successors = backwardChain.getSuccessors();
LOG.info("Importing {} blocks provided by consensus layer...", successors.size());
LOG.info("Importing {} block(s) provided by consensus layer...", successors.size());
successors.forEach(
block -> {
if (!context.getProtocolContext().getBlockchain().contains(block.getHash())) {
Expand All @@ -238,10 +239,10 @@ protected CompletableFuture<Void> possiblyMoreForwardSteps(final BlockHeader fir
return CompletableFuture.completedFuture(null);
}
if (context.getProtocolContext().getBlockchain().contains(firstNotSynced.getParentHash())) {
debugLambda(
infoLambda(
LOG,
"Block {}({}) is not yet imported, we need to run another step of ForwardSync",
() -> firstNotSynced.getNumber(),
firstNotSynced::getNumber,
() -> firstNotSynced.getHash().toHexString());
return completableFuture.thenCompose(this::executeAsync);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Optional<BlockHeader> getFirstAncestorHeader() {
@Override
public List<BlockHeader> getFirstNAncestorHeaders(final int size) {
List<Hash> resultList = new ArrayList<>(size);
for (int i = size; i > 0; --i) {
for (int i = Math.min(size, ancestors.size()); i > 0; --i) {
resultList.add(ancestors.get(ancestors.size() - i));
}
return resultList.stream()
Expand Down Expand Up @@ -247,12 +247,16 @@ public Optional<BlockHeader> getHeaderOnHeight(final long height) {
final long firstAncestor = headers.get(ancestors.get(0)).orElseThrow().getNumber();
if (firstAncestor >= height) {
if (firstAncestor - height < ancestors.size()) {
LOG.info(
"First: {} Height: {}, result: {}",
firstAncestor,
height,
headers.get(ancestors.get((int) (firstAncestor - height))).orElseThrow().getNumber());
return headers.get(ancestors.get((int) (firstAncestor - height)));
final Optional<BlockHeader> blockHeader =
headers.get(ancestors.get((int) (firstAncestor - height)));
blockHeader.ifPresent(
blockHeader1 ->
LOG.info(
"First: {} Height: {}, result: {}",
firstAncestor,
height,
blockHeader.orElseThrow().getNumber()));
return blockHeader;
} else {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -184,7 +185,7 @@ public void shouldMergeWhenPossible() {

final InMemoryBackwardChain historicalChain =
createBackwardChain(REMOTE_HEIGHT - 10, REMOTE_HEIGHT - 4);
when(context.findCorrectChainFromPivot(REMOTE_HEIGHT - 4)).thenReturn(historicalChain);
when(context.findCorrectChainFromPivot(REMOTE_HEIGHT - 4)).thenReturn(Optional.of(historicalChain));

assertThat(backwardChain.getFirstAncestorHeader().orElseThrow())
.isEqualTo(getBlockByNumber(REMOTE_HEIGHT - 3).getHeader());
Expand Down

0 comments on commit 3034f51

Please sign in to comment.