use of org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage.NewBlockHash in project besu by hyperledger.
the class BlockPropagationManager method handleNewBlockHashesFromNetwork.
private void handleNewBlockHashesFromNetwork(final EthMessage message) {
final Blockchain blockchain = protocolContext.getBlockchain();
final NewBlockHashesMessage newBlockHashesMessage = NewBlockHashesMessage.readFrom(message.getData());
try {
// Register announced blocks
final List<NewBlockHash> announcedBlocks = Lists.newArrayList(newBlockHashesMessage.getNewHashes());
traceLambda(LOG, "New block hashes from network {} from peer {}. Current status {}", () -> toLogString(announcedBlocks), message::getPeer, () -> this);
for (final NewBlockHash announcedBlock : announcedBlocks) {
message.getPeer().registerKnownBlock(announcedBlock.hash());
message.getPeer().registerHeight(announcedBlock.hash(), announcedBlock.number());
}
// Filter announced blocks for blocks we care to import
final long localChainHeight = protocolContext.getBlockchain().getChainHeadBlockNumber();
final long bestChainHeight = syncState.bestChainHeight(localChainHeight);
final List<NewBlockHash> relevantAnnouncements = announcedBlocks.stream().filter(a -> shouldImportBlockAtHeight(a.number(), localChainHeight, bestChainHeight)).collect(Collectors.toList());
// Filter for blocks we don't yet know about
final List<NewBlockHash> newBlocks = new ArrayList<>();
for (final NewBlockHash announcedBlock : relevantAnnouncements) {
if (pendingBlocksManager.contains(announcedBlock.hash())) {
LOG.trace("New block hash from network {} is already pending", announcedBlock);
continue;
}
if (importingBlocks.contains(announcedBlock.hash())) {
LOG.trace("New block hash from network {} is already importing", announcedBlock);
continue;
}
if (blockchain.contains(announcedBlock.hash())) {
LOG.trace("New block hash from network {} was already imported", announcedBlock);
continue;
}
if (requestedBlocks.add(announcedBlock.hash())) {
newBlocks.add(announcedBlock);
} else {
LOG.trace("New block hash from network {} was already requested", announcedBlock);
}
}
// Process known blocks we care about
for (final NewBlockHash newBlock : newBlocks) {
processAnnouncedBlock(message.getPeer(), newBlock);
}
} catch (final RLPException e) {
LOG.debug("Malformed NEW_BLOCK_HASHES message received from peer, disconnecting: {}", message.getPeer(), e);
message.getPeer().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
}
}
Aggregations