use of org.ethereum.net.message.Message in project rskj by rsksmart.
the class MessageCodec method decode.
@Override
protected void decode(ChannelHandlerContext ctx, Frame frame, List<Object> out) throws Exception {
if (frame.isChunked()) {
if (!supportChunkedFrames && frame.totalFrameSize > 0) {
throw new RuntimeException("Faming is not supported in this configuration.");
}
Pair<? extends List<Frame>, AtomicInteger> frameParts = incompleteFrames.get(frame.contextId);
if (frameParts == null) {
if (frame.totalFrameSize < 0) {
// TODO: refactor this logic (Cpp sends non-chunked frames with context-id)
Message message = decodeMessage(Collections.singletonList(frame));
out.add(message);
return;
} else {
frameParts = Pair.of(new ArrayList<Frame>(), new AtomicInteger(0));
incompleteFrames.put(frame.contextId, frameParts);
}
} else {
if (frame.totalFrameSize >= 0) {
loggerNet.warn("Non-initial chunked frame shouldn't contain totalFrameSize field (context-id: " + frame.contextId + ", totalFrameSize: " + frame.totalFrameSize + "). Discarding this frame and all previous.");
incompleteFrames.remove(frame.contextId);
return;
}
}
frameParts.getLeft().add(frame);
int curSize = frameParts.getRight().addAndGet(frame.size);
if (loggerWire.isDebugEnabled()) {
loggerWire.debug("Recv: Chunked (" + curSize + " of " + frameParts.getLeft().get(0).totalFrameSize + ") [size: " + frame.getSize() + "]");
}
if (curSize > frameParts.getLeft().get(0).totalFrameSize) {
loggerNet.warn("The total frame chunks size (" + curSize + ") is greater than expected (" + frameParts.getLeft().get(0).totalFrameSize + "). Discarding the frame.");
incompleteFrames.remove(frame.contextId);
return;
}
if (curSize == frameParts.getLeft().get(0).totalFrameSize) {
Message message = decodeMessage(frameParts.getLeft());
incompleteFrames.remove(frame.contextId);
out.add(message);
}
} else {
Message message = decodeMessage(Collections.singletonList(frame));
out.add(message);
}
}
use of org.ethereum.net.message.Message in project rskj by rsksmart.
the class MessageCodec method decodeMessage.
private Message decodeMessage(List<Frame> frames) throws IOException {
long frameType = frames.get(0).getType();
byte[] payload = new byte[frames.size() == 1 ? frames.get(0).getSize() : frames.get(0).totalFrameSize];
int pos = 0;
for (Frame frame : frames) {
pos += ByteStreams.read(frame.getStream(), payload, pos, frame.getSize());
}
if (loggerWire.isDebugEnabled()) {
loggerWire.debug("Recv: Encoded: {} [{}]", frameType, Hex.toHexString(payload));
}
Message msg = createMessage((byte) frameType, payload);
loggerNet.trace("From: \t{} \tRecv: \t{}", channel, msg);
ethereumListener.onRecvMessage(channel, msg);
channel.getNodeStatistics().rlpxInMessages.add();
return msg;
}
use of org.ethereum.net.message.Message in project rskj by rsksmart.
the class MessageFilterTest method filterMessagesWhenCommandsAreSpecified.
@Test
public void filterMessagesWhenCommandsAreSpecified() {
List<String> commands = new ArrayList<>();
commands.add("RSK_MESSAGE");
commands.add("TRANSACTIONS");
commands.add("BLOCKS");
MessageFilter filter = new MessageFilter(commands);
Message rskMessage = WriterMessageRecorderTest.createRskMessage();
Message ethMessage = WriterMessageRecorderTest.createEthMessage();
Assert.assertTrue(filter.acceptMessage(rskMessage));
Assert.assertTrue(filter.acceptMessage(ethMessage));
}
use of org.ethereum.net.message.Message in project rskj by rsksmart.
the class MessageFilterTest method filterWithoutFilteredCommandsAcceptsAnyMessage.
@Test
public void filterWithoutFilteredCommandsAcceptsAnyMessage() {
MessageFilter filter = new MessageFilter(null);
Message rskMessage = WriterMessageRecorderTest.createRskMessage();
Message ethMessage = WriterMessageRecorderTest.createEthMessage();
Assert.assertTrue(filter.acceptMessage(rskMessage));
Assert.assertTrue(filter.acceptMessage(ethMessage));
}
use of org.ethereum.net.message.Message in project rskj by rsksmart.
the class WriterMessageRecorderTest method recordRskMessage.
@Test
public void recordRskMessage() throws IOException {
Message message = createRskMessage();
StringWriter writer = new StringWriter();
BufferedWriter bwriter = new BufferedWriter(writer);
WriterMessageRecorder recorder = new WriterMessageRecorder(bwriter, null);
recorder.recordMessage(null, message);
bwriter.close();
String result = writer.toString();
String encoded = Hex.toHexString(message.getEncoded());
Assert.assertTrue(result.contains("," + encoded + ","));
Assert.assertTrue(result.contains(",0,RSK_MESSAGE,GET_BLOCK_MESSAGE,"));
}
Aggregations