Search in sources :

Example 1 with MessageQueue

use of org.ethereum.net.MessageQueue in project rskj by rsksmart.

the class EthereumChannelInitializer method initChannel.

@Override
public void initChannel(NioSocketChannel ch) {
    try {
        logger.info("Open {} connection, channel: {}", isInbound() ? "inbound" : "outbound", ch);
        if (isInbound()) {
            InetAddress address = ch.remoteAddress().getAddress();
            if (channelManager.isRecentlyDisconnected(address)) {
                // avoid too frequent connection attempts
                logger.info("Drop connection - the same IP was disconnected recently, channel: {}", ch);
                ch.disconnect();
                return;
            } else if (!channelManager.isAddressBlockAvailable(address)) {
                // avoid too many connection from same block address
                logger.info("IP range is full, IP {} is not accepted for new connection", address);
                ch.disconnect();
                return;
            } else if (peerScoringManager.isAddressBanned(address)) {
                // avoid connections to banned addresses
                logger.info("Drop connection - the address is banned, channel: {}", address);
                ch.disconnect();
                return;
            }
        }
        MessageQueue messageQueue = new MessageQueue();
        P2pHandler p2pHandler = new P2pHandler(ethereumListener, messageQueue, config.getPeerP2PPingInterval());
        MessageCodec messageCodec = new MessageCodec(ethereumListener, config);
        HandshakeHandler handshakeHandler = new HandshakeHandler(config, peerScoringManager, p2pHandler, messageCodec, configCapabilities);
        Channel channel = new Channel(messageQueue, messageCodec, nodeManager, rskWireProtocolFactory, eth62MessageFactory, staticMessages, remoteId);
        ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(config.peerChannelReadTimeout(), TimeUnit.SECONDS));
        ch.pipeline().addLast("handshakeHandler", handshakeHandler);
        handshakeHandler.setRemoteId(remoteId, channel);
        messageCodec.setChannel(channel);
        messageQueue.setChannel(channel);
        messageCodec.setP2pMessageFactory(new P2pMessageFactory());
        channelManager.add(channel);
        // limit the size of receiving buffer to 1024
        SocketChannelConfig channelConfig = ch.config();
        channelConfig.setRecvByteBufAllocator(new FixedRecvByteBufAllocator(16_777_216));
        channelConfig.setOption(ChannelOption.SO_RCVBUF, 16_777_216);
        channelConfig.setOption(ChannelOption.SO_BACKLOG, 1024);
        // be aware of channel closing
        ch.closeFuture().addListener(future -> channelManager.notifyDisconnect(channel));
    } catch (Exception e) {
        logger.error("Unexpected error: ", e);
    }
}
Also used : HandshakeHandler(org.ethereum.net.rlpx.HandshakeHandler) MessageQueue(org.ethereum.net.MessageQueue) P2pHandler(org.ethereum.net.p2p.P2pHandler) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) P2pMessageFactory(org.ethereum.net.p2p.P2pMessageFactory) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) MessageCodec(org.ethereum.net.rlpx.MessageCodec) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) InetAddress(java.net.InetAddress)

Example 2 with MessageQueue

use of org.ethereum.net.MessageQueue in project rskj by rsksmart.

the class RskWireProtocolTest method setup.

@Before
public void setup() {
    config = mock(RskSystemProperties.class);
    peerScoringManager = mock(PeerScoringManager.class);
    messageHandler = mock(MessageHandler.class);
    compositeEthereumListener = mock(CompositeEthereumListener.class);
    genesis = mock(Genesis.class);
    messageRecorder = mock(MessageRecorder.class);
    statusResolver = mock(StatusResolver.class);
    messageQueue = mock(MessageQueue.class);
    channel = mock(Channel.class);
    target = new RskWireProtocol(config, peerScoringManager, messageHandler, compositeEthereumListener, genesis, messageRecorder, statusResolver, messageQueue, channel);
    EmbeddedChannel ch = new EmbeddedChannel();
    ch.pipeline().addLast(target);
    ctx = ch.pipeline().firstContext();
}
Also used : StatusResolver(co.rsk.net.StatusResolver) PeerScoringManager(co.rsk.scoring.PeerScoringManager) CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) MessageHandler(co.rsk.net.MessageHandler) MessageQueue(org.ethereum.net.MessageQueue) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(org.ethereum.net.server.Channel) Genesis(org.ethereum.core.Genesis) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) RskSystemProperties(co.rsk.config.RskSystemProperties) Before(org.junit.Before)

Aggregations

MessageQueue (org.ethereum.net.MessageQueue)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 MessageHandler (co.rsk.net.MessageHandler)1 StatusResolver (co.rsk.net.StatusResolver)1 PeerScoringManager (co.rsk.scoring.PeerScoringManager)1 FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 SocketChannelConfig (io.netty.channel.socket.SocketChannelConfig)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)1 InetAddress (java.net.InetAddress)1 Genesis (org.ethereum.core.Genesis)1 CompositeEthereumListener (org.ethereum.listener.CompositeEthereumListener)1 P2pHandler (org.ethereum.net.p2p.P2pHandler)1 P2pMessageFactory (org.ethereum.net.p2p.P2pMessageFactory)1 HandshakeHandler (org.ethereum.net.rlpx.HandshakeHandler)1 MessageCodec (org.ethereum.net.rlpx.MessageCodec)1 Channel (org.ethereum.net.server.Channel)1 Before (org.junit.Before)1