Search in sources :

Example 11 with Message

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);
    }
}
Also used : Frame(org.ethereum.net.rlpx.FrameCodec.Frame) Message(org.ethereum.net.message.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 12 with 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;
}
Also used : Frame(org.ethereum.net.rlpx.FrameCodec.Frame) Message(org.ethereum.net.message.Message)

Example 13 with Message

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));
}
Also used : Message(org.ethereum.net.message.Message) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with Message

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));
}
Also used : Message(org.ethereum.net.message.Message) Test(org.junit.Test)

Example 15 with Message

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,"));
}
Also used : Message(org.ethereum.net.message.Message) GetBlockMessage(co.rsk.net.messages.GetBlockMessage) TransactionsMessage(org.ethereum.net.eth.message.TransactionsMessage) StringWriter(java.io.StringWriter) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Aggregations

Message (org.ethereum.net.message.Message)16 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)7 GetBlockMessage (co.rsk.net.messages.GetBlockMessage)4 BufferedWriter (java.io.BufferedWriter)4 StringWriter (java.io.StringWriter)4 TransactionsMessage (org.ethereum.net.eth.message.TransactionsMessage)4 DisconnectMessage (org.ethereum.net.p2p.DisconnectMessage)3 Frame (org.ethereum.net.rlpx.FrameCodec.Frame)3 NodeID (co.rsk.net.NodeID)2 Random (java.util.Random)2 EthMessage (org.ethereum.net.eth.message.EthMessage)2 PingMessage (org.ethereum.net.p2p.PingMessage)2 ByteBuf (io.netty.buffer.ByteBuf)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HelloMessage (org.ethereum.net.p2p.HelloMessage)1 P2pMessageFactory (org.ethereum.net.p2p.P2pMessageFactory)1 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)1 ECPoint (org.spongycastle.math.ec.ECPoint)1