Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore updating chain head and finalized block during backward sync #4718

Merged
merged 52 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
a74b942
Log UX, retry switching peer and dead code removal
fab-10 Nov 10, 2022
f179550
Merge branch 'main' into sync-log2
fab-10 Nov 11, 2022
b22d1a0
Backward sync log UX improvements
fab-10 Nov 11, 2022
65af5ef
Add CHANGELOG entry
fab-10 Nov 11, 2022
78beca3
Merge branch 'bwsync-log-ux' into sync-log2
fab-10 Nov 11, 2022
af2e8fc
Backward sync, use retry switching peer when fetching data from peers
fab-10 Nov 11, 2022
cbff279
Add CHANGELOG entry
fab-10 Nov 11, 2022
5003ad7
Fix log in case of sync failures
fab-10 Nov 11, 2022
f1826c8
Merge branch 'bwsync-log-ux' into bwsync-retry-switching-peer
fab-10 Nov 11, 2022
287116d
Merge branch 'bwsync-retry-switching-peer' into sync-log2
fab-10 Nov 11, 2022
8568ccd
Delete unneeded file
fab-10 Nov 11, 2022
5ebd45f
Log transient error at debug level
fab-10 Nov 11, 2022
ca71f6e
Backward sync log UX improvements
fab-10 Nov 11, 2022
00a5468
Add CHANGELOG entry
fab-10 Nov 11, 2022
0ad7d31
Fix log in case of sync failures
fab-10 Nov 11, 2022
8f78389
Backward sync log UX improvements
fab-10 Nov 11, 2022
91d5d3a
Add CHANGELOG entry
fab-10 Nov 11, 2022
3c20270
Backward sync, use retry switching peer when fetching data from peers
fab-10 Nov 11, 2022
816e4f2
Add CHANGELOG entry
fab-10 Nov 11, 2022
869f2c6
Fix log in case of sync failures
fab-10 Nov 11, 2022
e8190d1
Log transient error at debug level
fab-10 Nov 11, 2022
d8c4628
Merge branch 'bwsync-log-ux' of github.com:fab-10/besu into bwsync-lo…
fab-10 Nov 14, 2022
6c0c302
Rework log of imported block to support also ProcessKnownAncestorsStep
fab-10 Nov 14, 2022
5c1d801
Merge branch 'bwsync-retry-switching-peer' of github.com:fab-10/besu …
fab-10 Nov 14, 2022
e74a9c3
Merge branch 'bwsync-log-ux' into bwsync-retry-switching-peer
fab-10 Nov 14, 2022
de6a2c8
Merge branch 'bwsync-retry-switching-peer' into sync-log2
fab-10 Nov 14, 2022
7ed79a0
Reset the batch size only if we got a full batch
fab-10 Nov 14, 2022
d98618a
Fix log
fab-10 Nov 14, 2022
8166f8d
Merge branch 'bwsync-retry-switching-peer' into sync-log2
fab-10 Nov 14, 2022
d8d0d67
Reset the batch size only if we got a full batch
fab-10 Nov 14, 2022
0651587
Halve the batch size in case the request failed
fab-10 Nov 14, 2022
9c35587
Merge branch 'bwsync-retry-switching-peer' into sync-log2
fab-10 Nov 14, 2022
5a342d1
Merge branch 'main' into sync-log2
fab-10 Nov 15, 2022
2e6eb01
Merge branch 'main' into sync-log2
fab-10 Nov 15, 2022
149cf66
Merge branch 'main' into sync-log2
fab-10 Nov 16, 2022
4409b62
Update backward sync head during FcU
fab-10 Nov 16, 2022
14bd4c4
Add CHANGELOG entry
fab-10 Nov 16, 2022
5f92032
Merge branch 'main' into sync-log2
fab-10 Nov 17, 2022
f05d1fb
Update the target height whenever a new sync target of head is set
fab-10 Nov 17, 2022
ad2efaf
Merge branch 'main' into sync-log2
fab-10 Nov 17, 2022
a73590b
remove logs that keep objects live until backward sync is done
fab-10 Nov 17, 2022
3f0d3fb
Revert "Update the target height whenever a new sync target of head i…
fab-10 Nov 17, 2022
8ec7a6f
Merge branch 'main' into sync-log2
fab-10 Nov 21, 2022
d8a6a29
Set finalized block in merge context when backward sync is done
fab-10 Nov 22, 2022
6842620
Merge branch 'main' into sync-log2
fab-10 Nov 22, 2022
bf62808
Update CHANGELOG
fab-10 Nov 22, 2022
5d795cf
Merge branch 'main' into bwsync-restore-update-heads
fab-10 Nov 22, 2022
f67c4f1
Fix: move the head to the last imported block
fab-10 Nov 23, 2022
6cf3e4d
Merge branch 'main' into bwsync-restore-update-heads
fab-10 Nov 23, 2022
1763a8a
Optimize edge case when CL sends new payload out of order
fab-10 Nov 24, 2022
cb71c68
Merge branch 'main' into bwsync-restore-update-heads
fab-10 Nov 24, 2022
5185dc0
Fix and more tests
fab-10 Nov 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Print an overview of configuration and system information at startup [#4451](https://github.com/hyperledger/besu/pull/4451)

### Bug Fixes
- Restore updating chain head and finalized block during backward sync [#4718](https://github.com/hyperledger/besu/pull/4718)

### Download Links

Expand Down Expand Up @@ -76,7 +77,6 @@
https://hyperledger.jfrog.io/hyperledger/besu-binaries/besu/22.10.0/besu-22.10.0.tar.gz / sha256: 88fb5df567e4ec3547d7d2970cfef00debbd020c0da66b19166d43779b3b2b85
https://hyperledger.jfrog.io/hyperledger/besu-binaries/besu/22.10.0/besu-22.10.0.zip / sha256: c8e39f7c879409cb9b47f4d3de5e9c521249083830a8c9a45e8a14a319fe195d


## 22.10.0-RC2

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,33 +390,45 @@ private boolean canRetryBlockCreation(final Throwable throwable) {
}

@Override
public Optional<BlockHeader> getOrSyncHeaderByHash(final Hash blockHash) {
public Optional<BlockHeader> getOrSyncHeadByHash(final Hash headHash, final Hash finalizedHash) {
final var chain = protocolContext.getBlockchain();
final var optHeader = chain.getBlockHeader(blockHash);
final var maybeHeadHeader = chain.getBlockHeader(headHash);

if (optHeader.isPresent()) {
debugLambda(LOG, "BlockHeader {} is already present", () -> optHeader.get().toLogString());
if (maybeHeadHeader.isPresent()) {
debugLambda(LOG, "BlockHeader {} is already present", maybeHeadHeader.get()::toLogString);
} else {
debugLambda(LOG, "appending block hash {} to backward sync", blockHash::toHexString);
backwardSyncContext.syncBackwardsUntil(blockHash);
debugLambda(LOG, "Appending new head block hash {} to backward sync", headHash::toHexString);
backwardSyncContext.updateHead(headHash);
backwardSyncContext
.syncBackwardsUntil(headHash)
.thenRun(() -> updateFinalized(finalizedHash));
}
return optHeader;
return maybeHeadHeader;
}

@Override
public Optional<BlockHeader> getOrSyncHeaderByHash(
final Hash blockHash, final Hash finalizedBlockHash) {
final var chain = protocolContext.getBlockchain();
final var optHeader = chain.getBlockHeader(blockHash);

if (optHeader.isPresent()) {
debugLambda(LOG, "BlockHeader {} is already present", () -> optHeader.get().toLogString());
} else {
debugLambda(LOG, "appending block hash {} to backward sync", blockHash::toHexString);
backwardSyncContext.updateHeads(blockHash, finalizedBlockHash);
backwardSyncContext.syncBackwardsUntil(blockHash);
private void updateFinalized(final Hash finalizedHash) {
if (mergeContext
.getFinalized()
.map(BlockHeader::getHash)
.map(finalizedHash::equals)
.orElse(Boolean.FALSE)) {
LOG.debug("Finalized block already set to {}, nothing to do", finalizedHash);
return;
}
return optHeader;

protocolContext
.getBlockchain()
.getBlockHeader(finalizedHash)
.ifPresentOrElse(
finalizedHeader -> {
debugLambda(
LOG, "Setting finalized block header to {}", finalizedHeader::toLogString);
mergeContext.setFinalized(finalizedHeader);
},
() ->
LOG.warn(
"Internal error, backward sync completed but failed to import finalized block {}",
finalizedHash));
}

@Override
Expand Down Expand Up @@ -693,12 +705,11 @@ private Optional<Hash> findValidAncestor(

@Override
public boolean isDescendantOf(final BlockHeader ancestorBlock, final BlockHeader newBlock) {
LOG.debug(
"checking if block {}:{} is ancestor of {}:{}",
ancestorBlock.getNumber(),
ancestorBlock.getBlockHash(),
newBlock.getNumber(),
newBlock.getBlockHash());
debugLambda(
LOG,
"checking if block {} is ancestor of {}",
ancestorBlock::toLogString,
newBlock::toLogString);

// start with self, because descending from yourself is valid
Optional<BlockHeader> parentOf = Optional.of(newBlock);
Expand All @@ -714,10 +725,11 @@ public boolean isDescendantOf(final BlockHeader ancestorBlock, final BlockHeader
&& ancestorBlock.getBlockHash().equals(parentOf.get().getBlockHash())) {
return true;
} else {
LOG.debug(
debugLambda(
LOG,
"looped all the way back, did not find ancestor {} of child {}",
ancestorBlock.getBlockHash(),
newBlock.getBlockHash());
ancestorBlock::toLogString,
newBlock::toLogString);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ ForkchoiceResult updateForkChoice(

CompletableFuture<Void> appendNewPayloadToSync(Block newPayload);

Optional<BlockHeader> getOrSyncHeaderByHash(Hash blockHash);
Optional<BlockHeader> getOrSyncHeadByHash(Hash headHash, Hash finalizedHash);

boolean isMiningBeforeMerge();

Optional<BlockHeader> getOrSyncHeaderByHash(Hash blockHash, Hash finalizedBlockHash);

void addBadBlock(final Block block, Optional<Throwable> maybeCause);

boolean isBadBlock(Hash blockHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,8 @@ public CompletableFuture<Void> appendNewPayloadToSync(final Block newPayload) {
}

@Override
public Optional<BlockHeader> getOrSyncHeaderByHash(final Hash blockHash) {
return mergeCoordinator.getOrSyncHeaderByHash(blockHash);
}

@Override
public Optional<BlockHeader> getOrSyncHeaderByHash(
final Hash blockHash, final Hash finalizedBlockHash) {
return mergeCoordinator.getOrSyncHeaderByHash(blockHash, finalizedBlockHash);
public Optional<BlockHeader> getOrSyncHeadByHash(final Hash headHash, final Hash finalizedHash) {
return mergeCoordinator.getOrSyncHeadByHash(headHash, finalizedHash);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public void assertGetOrSyncForBlockAlreadyPresent() {
BlockHeader mockHeader =
headerGenerator.parentHash(Hash.fromHexStringLenient("0xdead")).buildHeader();
when(blockchain.getBlockHeader(mockHeader.getHash())).thenReturn(Optional.of(mockHeader));
var res = coordinator.getOrSyncHeaderByHash(mockHeader.getHash());
var res = coordinator.getOrSyncHeadByHash(mockHeader.getHash(), Hash.ZERO);

assertThat(res).isPresent();
}
Expand All @@ -710,7 +710,7 @@ public void assertGetOrSyncForBlockNotPresent() {
when(backwardSyncContext.syncBackwardsUntil(mockHeader.getBlockHash()))
.thenReturn(CompletableFuture.completedFuture(null));

var res = coordinator.getOrSyncHeaderByHash(mockHeader.getHash());
var res = coordinator.getOrSyncHeadByHash(mockHeader.getHash(), Hash.ZERO);

assertThat(res).isNotPresent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,40 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
Optional.of(forkChoice.getHeadBlockHash() + " is an invalid block")));
}

Optional<BlockHeader> newHead =
mergeCoordinator.getOrSyncHeaderByHash(forkChoice.getHeadBlockHash());
final Optional<BlockHeader> maybeNewHead =
mergeCoordinator.getOrSyncHeadByHash(
forkChoice.getHeadBlockHash(), forkChoice.getFinalizedBlockHash());

if (newHead.isEmpty()) {
if (maybeNewHead.isEmpty()) {
return syncingResponse(requestId, forkChoice);
}

final BlockHeader newHead = maybeNewHead.get();

maybePayloadAttributes.ifPresentOrElse(
this::logPayload, () -> LOG.debug("Payload attributes are null"));

if (!isValidForkchoiceState(
forkChoice.getSafeBlockHash(), forkChoice.getFinalizedBlockHash(), newHead.get())) {
forkChoice.getSafeBlockHash(), forkChoice.getFinalizedBlockHash(), newHead)) {
logForkchoiceUpdatedCall(INVALID, forkChoice);
return new JsonRpcErrorResponse(requestId, JsonRpcError.INVALID_FORKCHOICE_STATE);
}

// TODO: post-merge cleanup, this should be unnecessary after merge
if (!mergeCoordinator.latestValidAncestorDescendsFromTerminal(newHead.get())) {
if (!mergeCoordinator.latestValidAncestorDescendsFromTerminal(newHead)) {
logForkchoiceUpdatedCall(INVALID, forkChoice);
return new JsonRpcSuccessResponse(
requestId,
new EngineUpdateForkchoiceResult(
INVALID,
Hash.ZERO,
null,
Optional.of(newHead.get() + " did not descend from terminal block")));
Optional.of(newHead + " did not descend from terminal block")));
}

ForkchoiceResult result =
mergeCoordinator.updateForkChoice(
newHead.get(),
newHead,
forkChoice.getFinalizedBlockHash(),
forkChoice.getSafeBlockHash(),
maybePayloadAttributes.map(
Expand All @@ -149,7 +152,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
maybePayloadAttributes.map(
payloadAttributes ->
mergeCoordinator.preparePayload(
newHead.get(),
newHead,
payloadAttributes.getTimestamp(),
payloadAttributes.getPrevRandao(),
payloadAttributes.getSuggestedFeeRecipient()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void shouldReturnSyncingIfMissingNewHead() {
public void shouldReturnInvalidOnBadTerminalBlock() {
BlockHeader mockHeader = new BlockHeaderTestFixture().baseFeePerGas(Wei.ONE).buildHeader();

when(mergeCoordinator.getOrSyncHeaderByHash(mockHeader.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(mockHeader.getHash(), Hash.ZERO))
.thenReturn(Optional.of(mockHeader));
when(mergeCoordinator.latestValidAncestorDescendsFromTerminal(mockHeader)).thenReturn(false);
assertSuccessWithPayloadForForkchoiceResult(
Expand Down Expand Up @@ -148,7 +148,7 @@ public void shouldReturnSyncingOnHeadNotFound() {
@Test
public void shouldReturnValidWithoutFinalizedOrPayload() {
BlockHeader mockHeader = new BlockHeaderTestFixture().baseFeePerGas(Wei.ONE).buildHeader();
when(mergeCoordinator.getOrSyncHeaderByHash(mockHeader.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(mockHeader.getHash(), Hash.ZERO))
.thenReturn(Optional.of(mockHeader));
when(mergeCoordinator.latestValidAncestorDescendsFromTerminal(mockHeader)).thenReturn(true);

Expand All @@ -172,7 +172,7 @@ public void shouldReturnInvalidOnOldTimestamp() {
when(mergeCoordinator.latestValidAncestorDescendsFromTerminal(mockHeader)).thenReturn(true);
when(mergeCoordinator.isDescendantOf(any(), any())).thenReturn(true);
when(mergeContext.isSyncing()).thenReturn(false);
when(mergeCoordinator.getOrSyncHeaderByHash(mockHeader.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(mockHeader.getHash(), parent.getHash()))
.thenReturn(Optional.of(mockHeader));
when(mergeCoordinator.updateForkChoice(
mockHeader, parent.getHash(), parent.getHash(), Optional.empty()))
Expand Down Expand Up @@ -202,7 +202,7 @@ public void shouldReturnValidWithNewHeadAndFinalizedNoPayload() {
BlockHeader mockParent = builder.number(9L).buildHeader();
BlockHeader mockHeader = builder.number(10L).parentHash(mockParent.getHash()).buildHeader();
when(blockchain.getBlockHeader(any())).thenReturn(Optional.of(mockHeader));
when(mergeCoordinator.getOrSyncHeaderByHash(mockHeader.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(mockHeader.getHash(), Hash.ZERO))
.thenReturn(Optional.of(mockHeader));
when(mergeCoordinator.latestValidAncestorDescendsFromTerminal(mockHeader)).thenReturn(true);
when(mergeCoordinator.isDescendantOf(any(), any())).thenReturn(true);
Expand All @@ -217,7 +217,7 @@ public void shouldReturnValidWithNewHeadAndFinalizedNoPayload() {
@Test
public void shouldReturnValidWithoutFinalizedWithPayload() {
BlockHeader mockHeader = new BlockHeaderTestFixture().baseFeePerGas(Wei.ONE).buildHeader();
when(mergeCoordinator.getOrSyncHeaderByHash(mockHeader.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(mockHeader.getHash(), Hash.ZERO))
.thenReturn(Optional.of(mockHeader));
when(mergeCoordinator.latestValidAncestorDescendsFromTerminal(mockHeader)).thenReturn(true);

Expand Down Expand Up @@ -254,7 +254,7 @@ public void shouldReturnInvalidForkchoiceStateIfFinalizedBlockIsUnknown() {

when(blockchain.getBlockHeader(finalizedBlockHash)).thenReturn(Optional.empty());
when(mergeContext.isSyncing()).thenReturn(false);
when(mergeCoordinator.getOrSyncHeaderByHash(newHead.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(newHead.getHash(), finalizedBlockHash))
.thenReturn(Optional.of(newHead));

var resp =
Expand All @@ -275,7 +275,7 @@ public void shouldReturnInvalidForkchoiceStateIfFinalizedBlockIsNotAnAncestorOfN
when(blockchain.getBlockHeader(newHead.getHash())).thenReturn(Optional.of(newHead));
when(blockchain.getBlockHeader(finalized.getHash())).thenReturn(Optional.of(finalized));
when(mergeContext.isSyncing()).thenReturn(false);
when(mergeCoordinator.getOrSyncHeaderByHash(newHead.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(newHead.getHash(), finalized.getBlockHash()))
.thenReturn(Optional.of(newHead));
when(mergeCoordinator.isDescendantOf(finalized, newHead)).thenReturn(false);

Expand All @@ -301,7 +301,7 @@ public void shouldReturnInvalidForkchoiceStateIfSafeHeadZeroWithFinalizedBlock()

when(blockchain.getBlockHeader(parent.getHash())).thenReturn(Optional.of(parent));
when(mergeContext.isSyncing()).thenReturn(false);
when(mergeCoordinator.getOrSyncHeaderByHash(newHead.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(newHead.getHash(), parent.getBlockHash()))
.thenReturn(Optional.of(newHead));

var resp =
Expand All @@ -328,7 +328,7 @@ public void shouldReturnInvalidForkchoiceStateIfSafeBlockIsUnknown() {
when(blockchain.getBlockHeader(finalized.getHash())).thenReturn(Optional.of(finalized));
when(blockchain.getBlockHeader(safeBlockBlockHash)).thenReturn(Optional.empty());
when(mergeContext.isSyncing()).thenReturn(false);
when(mergeCoordinator.getOrSyncHeaderByHash(newHead.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(newHead.getHash(), finalized.getBlockHash()))
.thenReturn(Optional.of(newHead));
when(mergeCoordinator.isDescendantOf(finalized, newHead)).thenReturn(true);

Expand All @@ -352,7 +352,7 @@ public void shouldReturnInvalidForkchoiceStateIfSafeBlockIsNotADescendantOfFinal
when(blockchain.getBlockHeader(finalized.getHash())).thenReturn(Optional.of(finalized));
when(blockchain.getBlockHeader(safeBlock.getHash())).thenReturn(Optional.of(safeBlock));
when(mergeContext.isSyncing()).thenReturn(false);
when(mergeCoordinator.getOrSyncHeaderByHash(newHead.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(newHead.getHash(), finalized.getBlockHash()))
.thenReturn(Optional.of(newHead));
when(mergeCoordinator.isDescendantOf(finalized, newHead)).thenReturn(true);
when(mergeCoordinator.isDescendantOf(finalized, safeBlock)).thenReturn(false);
Expand All @@ -377,7 +377,7 @@ public void shouldReturnInvalidForkchoiceStateIfSafeBlockIsNotAnAncestorOfNewHea
when(blockchain.getBlockHeader(finalized.getHash())).thenReturn(Optional.of(finalized));
when(blockchain.getBlockHeader(safeBlock.getHash())).thenReturn(Optional.of(safeBlock));
when(mergeContext.isSyncing()).thenReturn(false);
when(mergeCoordinator.getOrSyncHeaderByHash(newHead.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(newHead.getHash(), finalized.getBlockHash()))
.thenReturn(Optional.of(newHead));
when(mergeCoordinator.isDescendantOf(finalized, newHead)).thenReturn(true);
when(mergeCoordinator.isDescendantOf(finalized, safeBlock)).thenReturn(true);
Expand All @@ -397,7 +397,7 @@ public void shouldReturnInvalidForkchoiceStateIfSafeBlockIsNotAnAncestorOfNewHea
public void shouldIgnoreUpdateToOldHeadAndNotPreparePayload() {
BlockHeader mockHeader = new BlockHeaderTestFixture().baseFeePerGas(Wei.ONE).buildHeader();

when(mergeCoordinator.getOrSyncHeaderByHash(mockHeader.getHash()))
when(mergeCoordinator.getOrSyncHeadByHash(mockHeader.getHash(), Hash.ZERO))
.thenReturn(Optional.of(mockHeader));
when(mergeCoordinator.latestValidAncestorDescendsFromTerminal(mockHeader)).thenReturn(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,14 @@ public synchronized void prependAncestorsHeader(final BlockHeader blockHeader) {
headers.put(blockHeader.getHash(), blockHeader);
return;
}
BlockHeader firstHeader = firstStoredAncestor.get();
final BlockHeader firstHeader = firstStoredAncestor.get();
headers.put(blockHeader.getHash(), blockHeader);
chainStorage.put(blockHeader.getHash(), firstStoredAncestor.get().getHash());
chainStorage.put(blockHeader.getHash(), firstHeader.getHash());
firstStoredAncestor = Optional.of(blockHeader);
debugLambda(
LOG,
"Added header {} on height {} to backward chain led by pivot {} on height {}",
"Added header {} to backward chain led by pivot {} on height {}",
blockHeader::toLogString,
blockHeader::getNumber,
() -> lastStoredPivot.orElseThrow().toLogString(),
firstHeader::getNumber);
}
Expand All @@ -127,16 +126,23 @@ public synchronized void dropFirstHeader() {
}

public synchronized void appendTrustedBlock(final Block newPivot) {
debugLambda(LOG, "appending trusted block {}", newPivot::toLogString);
debugLambda(LOG, "Appending trusted block {}", newPivot::toLogString);
headers.put(newPivot.getHash(), newPivot.getHeader());
blocks.put(newPivot.getHash(), newPivot);
if (lastStoredPivot.isEmpty()) {
firstStoredAncestor = Optional.of(newPivot.getHeader());
} else {
if (newPivot.getHeader().getParentHash().equals(lastStoredPivot.get().getHash())) {
debugLambda(
LOG,
"Added block {} to backward chain led by pivot {} on height {}",
newPivot::toLogString,
lastStoredPivot.get()::toLogString,
firstStoredAncestor.get()::getNumber);
chainStorage.put(lastStoredPivot.get().getHash(), newPivot.getHash());
} else {
firstStoredAncestor = Optional.of(newPivot.getHeader());
debugLambda(LOG, "Re-pivoting to new target block {}", newPivot::toLogString);
}
}
lastStoredPivot = Optional.of(newPivot.getHeader());
Expand Down
Loading