|
|
@ -23,7 +23,6 @@ import org.hyperledger.besu.ethereum.ProtocolContext; |
|
|
|
import org.hyperledger.besu.ethereum.chain.BadBlockManager; |
|
|
|
import org.hyperledger.besu.ethereum.chain.BadBlockManager; |
|
|
|
import org.hyperledger.besu.ethereum.chain.MutableBlockchain; |
|
|
|
import org.hyperledger.besu.ethereum.chain.MutableBlockchain; |
|
|
|
import org.hyperledger.besu.ethereum.core.Block; |
|
|
|
import org.hyperledger.besu.ethereum.core.Block; |
|
|
|
import org.hyperledger.besu.ethereum.core.BlockHeader; |
|
|
|
|
|
|
|
import org.hyperledger.besu.ethereum.eth.manager.EthContext; |
|
|
|
import org.hyperledger.besu.ethereum.eth.manager.EthContext; |
|
|
|
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState; |
|
|
|
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState; |
|
|
|
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; |
|
|
|
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; |
|
|
@ -253,7 +252,6 @@ public class BackwardSyncContext { |
|
|
|
|
|
|
|
|
|
|
|
protected Void saveBlock(final Block block) { |
|
|
|
protected Void saveBlock(final Block block) { |
|
|
|
traceLambda(LOG, "Going to validate block {}", block::toLogString); |
|
|
|
traceLambda(LOG, "Going to validate block {}", block::toLogString); |
|
|
|
checkFinalizedSuccessionRuleBeforeSave(block); |
|
|
|
|
|
|
|
var optResult = |
|
|
|
var optResult = |
|
|
|
this.getBlockValidatorForBlock(block) |
|
|
|
this.getBlockValidatorForBlock(block) |
|
|
|
.validateAndProcessBlock( |
|
|
|
.validateAndProcessBlock( |
|
|
@ -285,63 +283,6 @@ public class BackwardSyncContext { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting |
|
|
|
|
|
|
|
protected synchronized void checkFinalizedSuccessionRuleBeforeSave(final Block block) { |
|
|
|
|
|
|
|
final Optional<Hash> finalized = findMaybeFinalized(); |
|
|
|
|
|
|
|
if (finalized.isPresent()) { |
|
|
|
|
|
|
|
final Optional<BlockHeader> maybeFinalizedHeader = |
|
|
|
|
|
|
|
protocolContext |
|
|
|
|
|
|
|
.getBlockchain() |
|
|
|
|
|
|
|
.getBlockByHash(finalized.get()) |
|
|
|
|
|
|
|
.map(Block::getHeader) |
|
|
|
|
|
|
|
.or(() -> backwardChain.getHeader(finalized.get())); |
|
|
|
|
|
|
|
if (maybeFinalizedHeader.isEmpty()) { |
|
|
|
|
|
|
|
throw new BackwardSyncException( |
|
|
|
|
|
|
|
"We know a block " |
|
|
|
|
|
|
|
+ finalized.get().toHexString() |
|
|
|
|
|
|
|
+ " was finalized, but we don't have it downloaded yet, cannot save new block", |
|
|
|
|
|
|
|
true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
final BlockHeader finalizedHeader = maybeFinalizedHeader.get(); |
|
|
|
|
|
|
|
if (finalizedHeader.getHash().equals(block.getHash())) { |
|
|
|
|
|
|
|
debugLambda(LOG, "Saving new finalized block {}", block::toLogString); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (finalizedHeader.getNumber() == block.getHeader().getNumber()) { |
|
|
|
|
|
|
|
throw new BackwardSyncException( |
|
|
|
|
|
|
|
"This block is not the target finalized block. Is " |
|
|
|
|
|
|
|
+ block.toLogString() |
|
|
|
|
|
|
|
+ " but was expecting " |
|
|
|
|
|
|
|
+ finalizedHeader.toLogString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!getProtocolContext().getBlockchain().contains(finalizedHeader.getHash())) { |
|
|
|
|
|
|
|
debugLambda( |
|
|
|
|
|
|
|
LOG, |
|
|
|
|
|
|
|
"Saving block {} before finalized {} reached", |
|
|
|
|
|
|
|
block::toLogString, |
|
|
|
|
|
|
|
finalizedHeader::toLogString); // todo: some check here??
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
final Hash canonicalHash = |
|
|
|
|
|
|
|
getProtocolContext() |
|
|
|
|
|
|
|
.getBlockchain() |
|
|
|
|
|
|
|
.getBlockByNumber(finalizedHeader.getNumber()) |
|
|
|
|
|
|
|
.orElseThrow() |
|
|
|
|
|
|
|
.getHash(); |
|
|
|
|
|
|
|
if (finalizedHeader.getNumber() < block.getHeader().getNumber() |
|
|
|
|
|
|
|
&& !canonicalHash.equals(finalizedHeader.getHash())) { |
|
|
|
|
|
|
|
throw new BackwardSyncException( |
|
|
|
|
|
|
|
"Finalized block " |
|
|
|
|
|
|
|
+ finalizedHeader.toLogString() |
|
|
|
|
|
|
|
+ " is not on canonical chain. Canonical is" |
|
|
|
|
|
|
|
+ canonicalHash.toHexString() |
|
|
|
|
|
|
|
+ ". We need to reorg before saving this block."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LOG.debug("Finalized block not known yet..."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting |
|
|
|
@VisibleForTesting |
|
|
|
protected void possiblyMoveHead(final Block lastSavedBlock) { |
|
|
|
protected void possiblyMoveHead(final Block lastSavedBlock) { |
|
|
|
final MutableBlockchain blockchain = getProtocolContext().getBlockchain(); |
|
|
|
final MutableBlockchain blockchain = getProtocolContext().getBlockchain(); |
|
|
|