Search in sources :

Example 1 with EthMessage

use of org.hyperledger.besu.ethereum.eth.manager.EthMessage in project besu by hyperledger.

the class SnapProtocolManager method processMessage.

/**
 * This function is called by the P2P framework when an "SNAP message has been received.
 *
 * @param cap The capability under which the message was transmitted.
 * @param message The message to be decoded.
 */
@Override
public void processMessage(final Capability cap, final Message message) {
    final MessageData messageData = AbstractSnapMessageData.create(message);
    final int code = messageData.getCode();
    LOG.trace("Process snap message {}, {}", cap, code);
    final EthPeer ethPeer = ethPeers.peer(message.getConnection());
    if (ethPeer == null) {
        LOG.debug("Ignoring message received from unknown peer connection: " + message.getConnection());
        return;
    }
    final EthMessage ethMessage = new EthMessage(ethPeer, messageData);
    if (!ethPeer.validateReceivedMessage(ethMessage, getSupportedProtocol())) {
        LOG.debug("Unsolicited message received from, disconnecting: {}", ethPeer);
        ethPeer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
        return;
    }
    // This will handle responses
    ethPeers.dispatchMessage(ethPeer, ethMessage, getSupportedProtocol());
    // This will handle requests
    Optional<MessageData> maybeResponseData = Optional.empty();
    try {
        final Map.Entry<BigInteger, MessageData> requestIdAndEthMessage = ethMessage.getData().unwrapMessageData();
        maybeResponseData = snapMessages.dispatch(new EthMessage(ethPeer, requestIdAndEthMessage.getValue())).map(responseData -> responseData.wrapMessageData(requestIdAndEthMessage.getKey()));
    } catch (final RLPException e) {
        LOG.debug("Received malformed message {} , disconnecting: {}", messageData.getData(), ethPeer, e);
        ethPeer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
    }
    maybeResponseData.ifPresent(responseData -> {
        try {
            ethPeer.send(responseData, getSupportedProtocol());
        } catch (final PeerConnection.PeerNotConnected error) {
            // Peer disconnected before we could respond - nothing to do
            LOG.trace("Peer disconnected before we could respond - nothing to do " + error.getMessage());
        }
    });
}
Also used : SnapProtocol(org.hyperledger.besu.ethereum.eth.SnapProtocol) ProtocolManager(org.hyperledger.besu.ethereum.p2p.network.ProtocolManager) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) Logger(org.slf4j.Logger) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) LoggerFactory(org.slf4j.LoggerFactory) PeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator) List(java.util.List) Message(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Message) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) ImmutableList(com.google.common.collect.ImmutableList) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) Map(java.util.Map) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) AbstractSnapMessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractSnapMessageData) Optional(java.util.Optional) BigInteger(java.math.BigInteger) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) EthMessage(org.hyperledger.besu.ethereum.eth.manager.EthMessage) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) EthMessage(org.hyperledger.besu.ethereum.eth.manager.EthMessage) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) AbstractSnapMessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractSnapMessageData) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) BigInteger(java.math.BigInteger) Map(java.util.Map)

Example 2 with EthMessage

use of org.hyperledger.besu.ethereum.eth.manager.EthMessage 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

List (java.util.List)2 Optional (java.util.Optional)2 EthMessage (org.hyperledger.besu.ethereum.eth.manager.EthMessage)2 EthPeer (org.hyperledger.besu.ethereum.eth.manager.EthPeer)2 DisconnectReason (org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason)2 RLPException (org.hyperledger.besu.ethereum.rlp.RLPException)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Range (com.google.common.collect.Range)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1