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
}
});
}
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));
}
}
}
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;
}
}
}
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);
}
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);
}
Aggregations