Search in sources :

Example 1 with NewBlockHash

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);
    }
}
Also used : NewBlockHash(org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage.NewBlockHash) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) SyncState(org.hyperledger.besu.ethereum.eth.sync.state.SyncState) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RetryingGetBlockFromPeersTask(org.hyperledger.besu.ethereum.eth.manager.task.RetryingGetBlockFromPeersTask) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) PendingBlocksManager(org.hyperledger.besu.ethereum.eth.sync.state.PendingBlocksManager) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) Lists(com.google.common.collect.Lists) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) BlockAddedEvent(org.hyperledger.besu.ethereum.chain.BlockAddedEvent) Block(org.hyperledger.besu.ethereum.core.Block) EventType(org.hyperledger.besu.ethereum.chain.BlockAddedEvent.EventType) EthMessage(org.hyperledger.besu.ethereum.eth.manager.EthMessage) Slf4jLambdaHelper.traceLambda(org.hyperledger.besu.util.Slf4jLambdaHelper.traceLambda) ProcessableBlockHeader(org.hyperledger.besu.ethereum.core.ProcessableBlockHeader) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) PersistBlockTask(org.hyperledger.besu.ethereum.eth.sync.tasks.PersistBlockTask) NewBlockHash(org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage.NewBlockHash) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Logger(org.slf4j.Logger) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) NewBlockHashesMessage(org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Collection(java.util.Collection) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) NewBlockMessage(org.hyperledger.besu.ethereum.eth.messages.NewBlockMessage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Range(com.google.common.collect.Range) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) ArrayList(java.util.ArrayList) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) NewBlockHashesMessage(org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Lists (com.google.common.collect.Lists)1 Range (com.google.common.collect.Range)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Hash (org.hyperledger.besu.datatypes.Hash)1 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)1 BadBlockManager (org.hyperledger.besu.ethereum.chain.BadBlockManager)1 BlockAddedEvent (org.hyperledger.besu.ethereum.chain.BlockAddedEvent)1 EventType (org.hyperledger.besu.ethereum.chain.BlockAddedEvent.EventType)1