Search in sources :

Example 11 with RskJsonRpcRequest

use of co.rsk.rpc.modules.RskJsonRpcRequest in project rskj by rsksmart.

the class RskWebSocketJsonRpcHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
    ByteBuf content = msg.copy().content();
    try (ByteBufInputStream source = new ByteBufInputStream(content)) {
        final JsonNode jsonNodeRequest = mapper.readTree(source);
        RskWebSocketJsonParameterValidator.Result validationResult = parameterValidator.validate(jsonNodeRequest);
        RskJsonRpcRequest request = mapper.treeToValue(jsonNodeRequest, RskJsonRpcRequest.class);
        JsonRpcResultOrError resultOrError = null;
        if (validationResult.isValid()) {
            // TODO(mc) we should support the ModuleDescription method filters
            resultOrError = request.accept(this, ctx);
        } else {
            resultOrError = new JsonRpcError(JsonRpcError.INVALID_PARAMS, validationResult.getMessage());
        }
        JsonRpcIdentifiableMessage response = resultOrError.responseFor(request.getId());
        ctx.writeAndFlush(new TextWebSocketFrame(getJsonWithTypedId(jsonNodeRequest, response)));
        return;
    } catch (IOException e) {
        LOGGER.trace("Not a known or valid JsonRpcRequest", e);
        // We need to release this resource, netty only takes care about 'ByteBufHolder msg'
        content.release(content.refCnt());
    }
    // delegate to the next handler if the message can't be matched to a known JSON-RPC request
    ctx.fireChannelRead(msg);
}
Also used : JsonRpcIdentifiableMessage(co.rsk.jsonrpc.JsonRpcIdentifiableMessage) RskJsonRpcRequest(co.rsk.rpc.modules.RskJsonRpcRequest) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonRpcError(co.rsk.jsonrpc.JsonRpcError) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) JsonRpcResultOrError(co.rsk.jsonrpc.JsonRpcResultOrError)

Example 12 with RskJsonRpcRequest

use of co.rsk.rpc.modules.RskJsonRpcRequest in project rskj by rsksmart.

the class JacksonBasedRpcSerializerTest method testIntegerId_then_convertSuccessfully.

@Test
public void testIntegerId_then_convertSuccessfully() throws IOException {
    String messageInt = "{\"jsonrpc\": \"2.0\",\"method\": \"eth_subscribe\",\"params\": [\"newHeads\"],\"id\": 64}";
    RskJsonRpcRequest requestFromIntId = convertJson(messageInt);
    Assert.assertEquals(new Integer(64), requestFromIntId.getId());
}
Also used : RskJsonRpcRequest(co.rsk.rpc.modules.RskJsonRpcRequest) Test(org.junit.Test)

Aggregations

RskJsonRpcRequest (co.rsk.rpc.modules.RskJsonRpcRequest)12 Test (org.junit.Test)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 RskAddress (co.rsk.core.RskAddress)3 Topic (org.ethereum.rpc.Topic)3 JsonRpcError (co.rsk.jsonrpc.JsonRpcError)1 JsonRpcIdentifiableMessage (co.rsk.jsonrpc.JsonRpcIdentifiableMessage)1 JsonRpcResultOrError (co.rsk.jsonrpc.JsonRpcResultOrError)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 IOException (java.io.IOException)1