Search in sources :

Example 6 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class ChannelManagerImpl method sendMessageTo.

@Override
public boolean sendMessageTo(NodeID nodeID, MessageWithId message) {
    synchronized (activePeers) {
        EthMessage msg = new RskMessage(config, message);
        Channel channel = activePeers.get(nodeID);
        if (channel == null) {
            return false;
        }
        channel.sendMessage(msg);
    }
    return true;
}
Also used : EthMessage(org.ethereum.net.eth.message.EthMessage) RskMessage(co.rsk.net.eth.RskMessage)

Example 7 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class ChannelManagerImpl method broadcastStatus.

@Override
public int broadcastStatus(Status status) {
    final EthMessage message = new RskMessage(config, new StatusMessage(status));
    synchronized (activePeers) {
        if (activePeers.isEmpty()) {
            return 0;
        }
        int numberOfPeersToSendStatusTo = getNumberOfPeersToSendStatusTo(activePeers.size());
        List<Channel> shuffledPeers = new ArrayList<>(activePeers.values());
        Collections.shuffle(shuffledPeers);
        shuffledPeers.stream().limit(numberOfPeersToSendStatusTo).forEach(c -> c.sendMessage(message));
        return numberOfPeersToSendStatusTo;
    }
}
Also used : EthMessage(org.ethereum.net.eth.message.EthMessage) RskMessage(co.rsk.net.eth.RskMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 8 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class RskWireProtocol method channelRead0.

@Override
public void channelRead0(final ChannelHandlerContext ctx, EthMessage msg) throws InterruptedException {
    super.channelRead0(ctx, msg);
    if (this.messageRecorder != null) {
        this.messageRecorder.recordMessage(messageSender.getPeerNodeID(), msg);
    }
    if (!hasGoodReputation(ctx)) {
        ctx.disconnect();
        return;
    }
    Metrics.messageBytes(messageSender.getPeerNodeID(), msg.getEncoded().length);
    switch(msg.getCommand()) {
        case STATUS:
            processStatus((org.ethereum.net.eth.message.StatusMessage) msg, ctx);
            break;
        case RSK_MESSAGE:
            RskMessage rskmessage = (RskMessage) msg;
            Message message = rskmessage.getMessage();
            switch(message.getMessageType()) {
                case BLOCK_MESSAGE:
                    loggerNet.trace("RSK Block Message: Block {} {} from {}", ((BlockMessage) message).getBlock().getNumber(), ((BlockMessage) message).getBlock().getShortHash(), this.messageSender.getPeerNodeID());
                    syncStats.addBlocks(1);
                    break;
                case GET_BLOCK_MESSAGE:
                    loggerNet.trace("RSK Get Block Message: Block {} from {}", Hex.toHexString(((GetBlockMessage) message).getBlockHash()).substring(0, 10), this.messageSender.getPeerNodeID());
                    syncStats.getBlock();
                    break;
                case STATUS_MESSAGE:
                    loggerNet.trace("RSK Status Message: Block {} {} from {}", ((StatusMessage) message).getStatus().getBestBlockNumber(), Hex.toHexString(((StatusMessage) message).getStatus().getBestBlockHash()).substring(0, 10), this.messageSender.getPeerNodeID());
                    syncStats.addStatus();
                    break;
            }
            if (this.messageHandler != null) {
                this.messageHandler.postMessage(this.messageSender, rskmessage.getMessage());
            }
            break;
        default:
            break;
    }
}
Also used : BlockMessage(co.rsk.net.messages.BlockMessage) GetBlockMessage(co.rsk.net.messages.GetBlockMessage) StatusMessage(co.rsk.net.messages.StatusMessage) Message(co.rsk.net.messages.Message) BlockMessage(co.rsk.net.messages.BlockMessage) EthMessage(org.ethereum.net.eth.message.EthMessage) GetBlockMessage(co.rsk.net.messages.GetBlockMessage) TransactionsMessage(org.ethereum.net.eth.message.TransactionsMessage) co.rsk.net(co.rsk.net) StatusMessage(co.rsk.net.messages.StatusMessage)

Example 9 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class RskMessageTest method encodeDecodeStatusMessage.

@Test
public void encodeDecodeStatusMessage() {
    Block block = new BlockGenerator().getBlock(1);
    Status status = new Status(block.getNumber(), block.getHash().getBytes());
    StatusMessage message = new StatusMessage(status);
    RskMessage rskmessage = new RskMessage(config, message);
    byte[] encoded = rskmessage.getEncoded();
    Eth62MessageFactory factory = new Eth62MessageFactory(config);
    EthMessage ethmessage = (EthMessage) factory.create((byte) 0x08, encoded);
    Assert.assertNotNull(ethmessage);
    Assert.assertEquals(EthMessageCodes.RSK_MESSAGE, ethmessage.getCommand());
    RskMessage result = (RskMessage) ethmessage;
    Assert.assertNotNull(result.getMessage());
    Message resultMessage = result.getMessage();
    Assert.assertEquals(MessageType.STATUS_MESSAGE, resultMessage.getMessageType());
    Assert.assertArrayEquals(block.getHash().getBytes(), ((StatusMessage) resultMessage).getStatus().getBestBlockHash());
    Assert.assertEquals(block.getNumber(), ((StatusMessage) resultMessage).getStatus().getBestBlockNumber());
}
Also used : Status(co.rsk.net.Status) EthMessage(org.ethereum.net.eth.message.EthMessage) EthMessage(org.ethereum.net.eth.message.EthMessage) Eth62MessageFactory(org.ethereum.net.eth.message.Eth62MessageFactory) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 10 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class RskMessageTest method encodeDecodeBlockMessage.

@Test
public void encodeDecodeBlockMessage() {
    Block block = new BlockGenerator().getBlock(1);
    BlockMessage message = new BlockMessage(block);
    RskMessage rskmessage = new RskMessage(config, message);
    byte[] encoded = rskmessage.getEncoded();
    Eth62MessageFactory factory = new Eth62MessageFactory(config);
    EthMessage ethmessage = (EthMessage) factory.create((byte) 0x08, encoded);
    Assert.assertNotNull(ethmessage);
    Assert.assertEquals(EthMessageCodes.RSK_MESSAGE, ethmessage.getCommand());
    RskMessage result = (RskMessage) ethmessage;
    Assert.assertNotNull(result.getMessage());
    Message resultMessage = result.getMessage();
    Assert.assertEquals(MessageType.BLOCK_MESSAGE, resultMessage.getMessageType());
    Assert.assertEquals(block.getHash(), ((BlockMessage) resultMessage).getBlock().getHash());
    Assert.assertArrayEquals(block.getEncoded(), ((BlockMessage) resultMessage).getBlock().getEncoded());
    Assert.assertEquals(block.getNumber(), ((BlockMessage) resultMessage).getBlock().getNumber());
}
Also used : EthMessage(org.ethereum.net.eth.message.EthMessage) EthMessage(org.ethereum.net.eth.message.EthMessage) Eth62MessageFactory(org.ethereum.net.eth.message.Eth62MessageFactory) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Aggregations

EthMessage (org.ethereum.net.eth.message.EthMessage)11 RskMessage (co.rsk.net.eth.RskMessage)6 Block (org.ethereum.core.Block)6 Metrics (co.rsk.net.Metrics)4 Status (co.rsk.net.Status)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 RskSystemProperties (co.rsk.config.RskSystemProperties)3 NodeID (co.rsk.net.NodeID)3 co.rsk.net.messages (co.rsk.net.messages)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 InetAddress (java.net.InetAddress)3 java.util (java.util)3 Executors (java.util.concurrent.Executors)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3 CollectionUtils (org.apache.commons.collections4.CollectionUtils)3