Search in sources :

Example 1 with PeerNotConnected

use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected in project besu by hyperledger.

the class EthProtocolManager method processMessage.

@Override
public void processMessage(final Capability cap, final Message message) {
    checkArgument(getSupportedCapabilities().contains(cap), "Unsupported capability passed to processMessage(): " + cap);
    final MessageData messageData = message.getData();
    final int code = messageData.getCode();
    LOG.trace("Process 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;
    }
    if (messageData.getSize() > 10 * 1_000_000) /*10MB*/
    {
        LOG.debug("Received message over 10MB. Disconnecting from {}", ethPeer);
        ethPeer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
        return;
    }
    // Handle STATUS processing
    if (code == EthPV62.STATUS) {
        handleStatusMessage(ethPeer, messageData);
        return;
    } else if (!ethPeer.statusHasBeenReceived()) {
        // Peers are required to send status messages before any other message type
        LOG.debug("{} requires a Status ({}) message to be sent first.  Instead, received message {}.  Disconnecting from {}.", this.getClass().getSimpleName(), EthPV62.STATUS, code, ethPeer);
        ethPeer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
        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;
    }
    if (isFinalized() && (code == EthPV62.NEW_BLOCK || code == EthPV62.NEW_BLOCK_HASHES)) {
        LOG.debug("disconnecting peer for sending new blocks after transition to PoS");
        ethPeer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED);
    }
    // This will handle responses
    ethPeers.dispatchMessage(ethPeer, ethMessage, getSupportedProtocol());
    // This will handle requests
    Optional<MessageData> maybeResponseData = Optional.empty();
    try {
        if (EthProtocol.isEth66Compatible(cap) && EthProtocol.requestIdCompatible(code)) {
            final Map.Entry<BigInteger, MessageData> requestIdAndEthMessage = ethMessage.getData().unwrapMessageData();
            maybeResponseData = ethMessages.dispatch(new EthMessage(ethPeer, requestIdAndEthMessage.getValue())).map(responseData -> responseData.wrapMessageData(requestIdAndEthMessage.getKey()));
        } else {
            maybeResponseData = ethMessages.dispatch(ethMessage);
        }
    } catch (final RLPException e) {
        LOG.debug("Received malformed message {} , disconnecting: {}", messageData.getData(), ethPeer, e);
        ethPeer.disconnect(DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL);
    }
    maybeResponseData.ifPresent(responseData -> {
        try {
            ethPeer.send(responseData, getSupportedProtocol());
        } catch (final PeerNotConnected missingPeerException) {
        // Peer disconnected before we could respond - nothing to do
        }
    });
}
Also used : MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) StatusMessage(org.hyperledger.besu.ethereum.eth.messages.StatusMessage) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) NewMergeStateCallback(org.hyperledger.besu.consensus.merge.NewMergeStateCallback) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Bytes(org.apache.tuweni.bytes.Bytes) EthProtocol(org.hyperledger.besu.ethereum.eth.EthProtocol) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) Map(java.util.Map) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) BigInteger(java.math.BigInteger) Block(org.hyperledger.besu.ethereum.core.Block) NewForkchoiceMessageListener(org.hyperledger.besu.consensus.merge.NewForkchoiceMessageListener) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) ProtocolManager(org.hyperledger.besu.ethereum.p2p.network.ProtocolManager) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) Logger(org.slf4j.Logger) PeerValidatorRunner(org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidatorRunner) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) DisconnectMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage) PeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Message(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Message) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) PeerNotConnected(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected) Optional(java.util.Optional) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BlockBroadcaster(org.hyperledger.besu.ethereum.eth.sync.BlockBroadcaster) Collections(java.util.Collections) StampedLock(java.util.concurrent.locks.StampedLock) Hash(org.hyperledger.besu.datatypes.Hash) PeerNotConnected(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) BigInteger(java.math.BigInteger) Map(java.util.Map)

Example 2 with PeerNotConnected

use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected in project besu by hyperledger.

the class PendingPeerRequest method sendRequest.

private synchronized void sendRequest(final EthPeer peer) {
    // Recheck if we should send the request now we're inside the synchronized block
    if (!result.isDone()) {
        try {
            final ResponseStream responseStream = request.sendRequest(peer);
            result.complete(responseStream);
        } catch (final PeerNotConnected e) {
            result.completeExceptionally(new PeerDisconnectedException(peer));
        }
    }
}
Also used : PeerNotConnected(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected) PeerDisconnectedException(org.hyperledger.besu.ethereum.eth.manager.exceptions.PeerDisconnectedException) ResponseStream(org.hyperledger.besu.ethereum.eth.manager.RequestManager.ResponseStream)

Example 3 with PeerNotConnected

use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected in project besu by hyperledger.

the class TransactionsMessageSender method sendTransactionsToPeer.

void sendTransactionsToPeer(final EthPeer peer) {
    final Set<Transaction> allTxToSend = transactionTracker.claimTransactionsToSendToPeer(peer);
    while (!allTxToSend.isEmpty()) {
        final LimitedTransactionsMessages limitedTransactionsMessages = LimitedTransactionsMessages.createLimited(allTxToSend);
        final Set<Transaction> includedTransactions = limitedTransactionsMessages.getIncludedTransactions();
        traceLambda(LOG, "Sending transactions to peer {} all transactions count {}, " + "single message transactions {}, single message list {}", peer::toString, allTxToSend::size, includedTransactions::size, () -> toHashList(includedTransactions));
        allTxToSend.removeAll(limitedTransactionsMessages.getIncludedTransactions());
        try {
            peer.send(limitedTransactionsMessages.getTransactionsMessage());
        } catch (final PeerNotConnected e) {
            return;
        }
    }
}
Also used : Transaction(org.hyperledger.besu.ethereum.core.Transaction) PeerNotConnected(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected) LimitedTransactionsMessages(org.hyperledger.besu.ethereum.eth.messages.LimitedTransactionsMessages)

Example 4 with PeerNotConnected

use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected in project besu by hyperledger.

the class EthPeerTest method getBodiesStream.

@Test
public void getBodiesStream() throws PeerNotConnected {
    final ResponseStreamSupplier getStream = (peer) -> peer.getBodies(asList(gen.hash(), gen.hash()));
    final MessageData targetMessage = BlockBodiesMessage.create(asList(gen.body(), gen.body()));
    final MessageData otherMessage = BlockHeadersMessage.create(asList(gen.header(), gen.header()));
    messageStream(getStream, targetMessage, otherMessage);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) NodeMessagePermissioningProvider(org.hyperledger.besu.plugin.services.permissioning.NodeMessagePermissioningProvider) Bytes(org.apache.tuweni.bytes.Bytes) Collections.singletonList(java.util.Collections.singletonList) NodeDataMessage(org.hyperledger.besu.ethereum.eth.messages.NodeDataMessage) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) ReceiptsMessage(org.hyperledger.besu.ethereum.eth.messages.ReceiptsMessage) PeerInfo(org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Collections.emptyList(java.util.Collections.emptyList) PingMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.PingMessage) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) BlockBodiesMessage(org.hyperledger.besu.ethereum.eth.messages.BlockBodiesMessage) Mockito.verify(org.mockito.Mockito.verify) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) Consumer(java.util.function.Consumer) PeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator) List(java.util.List) Stream(java.util.stream.Stream) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) PeerNotConnected(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) Mockito.mock(org.mockito.Mockito.mock) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) Test(org.junit.Test)

Example 5 with PeerNotConnected

use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected in project besu by hyperledger.

the class EthPeerTest method getReceiptsStream.

@Test
public void getReceiptsStream() throws PeerNotConnected {
    final ResponseStreamSupplier getStream = (peer) -> peer.getReceipts(asList(gen.hash(), gen.hash()));
    final MessageData targetMessage = ReceiptsMessage.create(singletonList(gen.receipts(gen.block())));
    final MessageData otherMessage = BlockHeadersMessage.create(asList(gen.header(), gen.header()));
    messageStream(getStream, targetMessage, otherMessage);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) NodeMessagePermissioningProvider(org.hyperledger.besu.plugin.services.permissioning.NodeMessagePermissioningProvider) Bytes(org.apache.tuweni.bytes.Bytes) Collections.singletonList(java.util.Collections.singletonList) NodeDataMessage(org.hyperledger.besu.ethereum.eth.messages.NodeDataMessage) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) ReceiptsMessage(org.hyperledger.besu.ethereum.eth.messages.ReceiptsMessage) PeerInfo(org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Collections.emptyList(java.util.Collections.emptyList) PingMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.PingMessage) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) BlockBodiesMessage(org.hyperledger.besu.ethereum.eth.messages.BlockBodiesMessage) Mockito.verify(org.mockito.Mockito.verify) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) Consumer(java.util.function.Consumer) PeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator) List(java.util.List) Stream(java.util.stream.Stream) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) PeerNotConnected(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) Mockito.mock(org.mockito.Mockito.mock) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) Test(org.junit.Test)

Aggregations

PeerNotConnected (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected)10 Collections (java.util.Collections)6 List (java.util.List)6 Bytes (org.apache.tuweni.bytes.Bytes)6 PeerValidator (org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator)6 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)6 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)6 Test (org.junit.Test)6 Arrays (java.util.Arrays)5 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.singletonList (java.util.Collections.singletonList)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Consumer (java.util.function.Consumer)5 Collectors (java.util.stream.Collectors)5 Stream (java.util.stream.Stream)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)5 BlockBodiesMessage (org.hyperledger.besu.ethereum.eth.messages.BlockBodiesMessage)5 BlockHeadersMessage (org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage)5