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