Skip to content

Commit

Permalink
Upgrade jackson and jsonrpc libs
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoreliovlabs committed Sep 22, 2022
1 parent 0679f3e commit 8d95b96
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 69 deletions.
83 changes: 83 additions & 0 deletions gradle/verification-metadata.xml
Expand Up @@ -100,11 +100,39 @@
<sha256 value="af650fa4dd400bc5769320bcef08ed93813f349cabe8213d469acafbbd945d8a" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml" name="oss-parent" version="43">
<artifact name="oss-parent-43.pom">
<sha256 value="e5585cc1c37079b2e3817a8997945736f158831844d59d0e4d3a45b27611f926" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson" name="jackson-base" version="2.13.3">
<artifact name="jackson-base-2.13.3.pom">
<sha256 value="72d67291876c63e1896bc7d8ff799033d3a4b9029020110488af9c97315ed939" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson" name="jackson-bom" version="2.13.3">
<artifact name="jackson-bom-2.13.3.pom">
<sha256 value="df675b83b6caba7602fb47d75d4bb513c2af4c0a6155d7e397c0ec0f34112c75" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson" name="jackson-parent" version="2.13">
<artifact name="jackson-parent-2.13.pom">
<sha256 value="2bba89978172af1effcb4d143d09921a3f3082ca4dcf122b1ded98bf55b2ad57" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson" name="jackson-parent" version="2.8">
<artifact name="jackson-parent-2.8.pom">
<sha256 value="3b51994c1a3a29c2c89728226c0be14b69888a1bb0ef311f8d65904cdfbd9358" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson.core" name="jackson-annotations" version="2.13.3">
<artifact name="jackson-annotations-2.13.3.jar">
<sha256 value="5326a6fbcde7cf8817f36c254101cd45f6acea4258518cd3c80ee5b89f4e4b9b" origin="Generated by Gradle"/>
</artifact>
<artifact name="jackson-annotations-2.13.3.module">
<sha256 value="9cff35722597acaae21f8fe630317aced55855f92d3ba21bac4b3f90892ea00c" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson.core" name="jackson-annotations" version="2.8.0">
<artifact name="jackson-annotations-2.8.0.jar">
<sha256 value="e61b7343aceeb6ecda291d4ef133cd3e765f178c631c357ffd081abab7f15db8" origin="Generated by Gradle"/>
Expand All @@ -121,6 +149,14 @@
<sha256 value="648bdea00ab0ce84c45a035b4f4db20345beaa558510a0c233a8874939fcf95d" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson.core" name="jackson-core" version="2.13.3">
<artifact name="jackson-core-2.13.3.jar">
<sha256 value="ab119a8ea3cc69472ebc0e870b849bfbbe536ad57d613dc38453ccd592ca6a3d" origin="Generated by Gradle"/>
</artifact>
<artifact name="jackson-core-2.13.3.module">
<sha256 value="eca2ac4d1d67ae89f43e6390fcbcbf1633fd8c3a1a4968d93214ebe5825f61e3" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson.core" name="jackson-core" version="2.8.10">
<artifact name="jackson-core-2.8.10.jar">
<sha256 value="39a74610521d7fb9eb3f437bb8739bbf47f6435be12d17bf954c731a0c6352bb" origin="Generated by Gradle"/>
Expand All @@ -137,6 +173,14 @@
<sha256 value="0d8f415d56320fbe3883ab7adee73bbb2c9a69c2a49af3608fd6b1efa15d8b5a" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson.core" name="jackson-databind" version="2.13.3">
<artifact name="jackson-databind-2.13.3.jar">
<sha256 value="6444bf08d8cd4629740afc3db1276938f494728deb663ce585c4e91f6b45eb84" origin="Generated by Gradle"/>
</artifact>
<artifact name="jackson-databind-2.13.3.module">
<sha256 value="422d6822037d1a58b918dcebbb475082f07ca7cca7e4b582873c64c6cde4b887" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.fasterxml.jackson.core" name="jackson-databind" version="2.8.11">
<artifact name="jackson-databind-2.8.11.jar">
<sha256 value="071e114e3187bcde9d029df03ae8cf467d2d48383624572060501b8adc7e1431" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -185,6 +229,14 @@
<sha256 value="4f9f5d5f66efe942e7c5b36192fbf61eed9f16546af44ca5a0e6500b39fbafa4" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.github.briandilley.jsonrpc4j" name="jsonrpc4j" version="1.6">
<artifact name="jsonrpc4j-1.6.jar">
<sha256 value="700620939640e1ba55a39713656a1c7df554e7a4e679c6b349c37734ebd04182" origin="Generated by Gradle"/>
</artifact>
<artifact name="jsonrpc4j-1.6.module">
<sha256 value="8a113641486aca6686d2718281811f088b07cbb6d608e2b4e45b891d8ea6b115" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google" name="google" version="1">
<artifact name="google-1.pom">
<sha256 value="cd6db17a11a31ede794ccbd1df0e4d9750f640234731f21cff885a9997277e81" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -844,6 +896,11 @@
<sha256 value="5c19e6848cc550a95664fb082304bc5f9fcf7b672faf03af1635f0e93c268177" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache" name="apache" version="13">
<artifact name="apache-13.pom">
<sha256 value="ff513db0361fd41237bef4784968bc15aae478d4ec0a9496f811072ccaf3841d" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache" name="apache" version="15">
<artifact name="apache-15.pom">
<sha256 value="36c2f2f979ac67b450c0cb480e4e9baf6b40f3a681f22ba9692287d1139ad494" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -900,6 +957,32 @@
<sha256 value="75dbe8f34e98e4c3ff42daae4a2f9eb4cbcd3b5f1047d54460ace906dbb4502e" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.httpcomponents" name="httpcomponents-core" version="4.4.5">
<artifact name="httpcomponents-core-4.4.5.pom">
<sha256 value="18d9243541c80848d5378bad89fccc5e1bf5c0465b2481d948b0c06851091cdc" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.httpcomponents" name="httpcore" version="4.4.5">
<artifact name="httpcore-4.4.5.jar">
<sha256 value="64d5453874cab7e40a7065cb01a9a9ca1053845a9786b478878b679e0580cec3" origin="Generated by Gradle"/>
</artifact>
<artifact name="httpcore-4.4.5.pom">
<sha256 value="147fa18df753534e50054d018a360f789902af1e5c24322a3f0b200d0684fed2" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.httpcomponents" name="httpcore-nio" version="4.4.5">
<artifact name="httpcore-nio-4.4.5.jar">
<sha256 value="9da82cfb9f50318333d3892e00904f3b74af0825f0f6de32eea7090a2565d0d1" origin="Generated by Gradle"/>
</artifact>
<artifact name="httpcore-nio-4.4.5.pom">
<sha256 value="74971c6d1f4f09f5ac4cd25427da10b1e6a3024fb29913593908461134b06951" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.httpcomponents" name="project" version="7">
<artifact name="project-7.pom">
<sha256 value="3d6eba428555a558de046b5d76eacc1f5a54b4f5f20b84d636ed7aff18aa48c3" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.awaitility" name="awaitility" version="3.0.0">
<artifact name="awaitility-3.0.0.jar">
<sha256 value="a02982e89585a52c1c84296a895bfeb86ea250cca1a53bcfc8a14092fffa87c4" origin="Generated by Gradle"/>
Expand Down
4 changes: 2 additions & 2 deletions rskj-core/build.gradle
Expand Up @@ -65,11 +65,11 @@ ext {
rocksdbJniVer : '6.27.3',
slf4jVer : '1.7.36',
logbackVer : '1.2.10',
jacksonVer : '2.8.11',
jacksonVer : '2.13.3',
commonsLang3Ver : '3.12.0',
typesafeConfigVer : '1.4.2',
mapdbVer : '2.0-beta13',
jsonrpc4jVer : '1.5.3',
jsonrpc4jVer : '1.6',
jaxwsRtVer : '2.3.5',

bitcoinjThinVer: '0.14.4-rsk-12',
Expand Down
14 changes: 12 additions & 2 deletions rskj-core/src/main/java/co/rsk/config/RemascConfigFactory.java
Expand Up @@ -19,6 +19,7 @@
package co.rsk.config;

import co.rsk.remasc.RemascException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
Expand All @@ -43,12 +44,21 @@ public RemascConfigFactory(String remascConfigFile) {
public RemascConfig createRemascConfig(String config) {
RemascConfig remascConfig;

try (InputStream is = RemascConfigFactory.class.getClassLoader().getResourceAsStream(this.configPath)){
try (InputStream is = RemascConfigFactory.class.getClassLoader().getResourceAsStream(this.configPath)) {
JsonNode node = mapper.readTree(is);

if (node.isEmpty()) {
throw JsonMappingException.from(node.traverse(), "Json node is empty");
}

remascConfig = mapper.treeToValue(node.get(config), RemascConfig.class);

if (remascConfig == null) {
throw new NullPointerException();
}
} catch (Exception ex) {
logger.error("Error reading REMASC configuration[{}]: {}", config, ex);
throw new RemascException("Error reading REMASC configuration[" + config +"]: ", ex);
throw new RemascException("Error reading REMASC configuration[" + config + "]: ", ex);
}

return remascConfig;
Expand Down
Expand Up @@ -86,6 +86,12 @@ private ByteBuf buildErrorContent(int errorCode, String errorMessage) throws Jso
errorProperties.put("code", jsonNodeFactory.numberNode(errorCode));
errorProperties.put("message", jsonNodeFactory.textNode(errorMessage));
JsonNode error = jsonNodeFactory.objectNode().set("error", jsonNodeFactory.objectNode().setAll(errorProperties));
return Unpooled.wrappedBuffer(mapper.writeValueAsBytes(mapper.treeToValue(error, Object.class)));
Object object = mapper.treeToValue(error, Object.class);

if (object == null) {
throw new NullPointerException();
}

return Unpooled.wrappedBuffer(mapper.writeValueAsBytes(object));
}
}
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonMappingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -51,92 +52,100 @@
* Eventually, we might want to implement all methods in this style and remove jsonrpc4j.
*
* We make this object Sharable so it can be instanced once in the netty pipeline
* and since all objects used by this object are thread safe,
* and since all objects used by this object are thread safe,
*/
@Sharable
public class RskWebSocketJsonRpcHandler extends SimpleChannelInboundHandler<ByteBufHolder> implements RskJsonRpcRequestVisitor {

private static final Logger LOGGER = LoggerFactory.getLogger(RskWebSocketJsonRpcHandler.class);
private static final Logger LOGGER = LoggerFactory.getLogger(RskWebSocketJsonRpcHandler.class);

private static final String ID = "id";
private static final String ID = "id";

private final EthSubscriptionNotificationEmitter emitter;
private final EthSubscriptionNotificationEmitter emitter;

private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = new ObjectMapper();

private final RskWebSocketJsonParameterValidator parameterValidator = new RskWebSocketJsonParameterValidator();
private final RskWebSocketJsonParameterValidator parameterValidator = new RskWebSocketJsonParameterValidator();

public RskWebSocketJsonRpcHandler(EthSubscriptionNotificationEmitter emitter) {
this.emitter = emitter;
}
public RskWebSocketJsonRpcHandler(EthSubscriptionNotificationEmitter emitter) {
this.emitter = emitter;
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
ByteBuf content = msg.copy().content();
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
ByteBuf content = msg.copy().content();

try (ByteBufInputStream source = new ByteBufInputStream(content)) {
try (ByteBufInputStream source = new ByteBufInputStream(content)) {

final JsonNode jsonNodeRequest = mapper.readTree(source);
final JsonNode jsonNodeRequest = mapper.readTree(source);

RskWebSocketJsonParameterValidator.Result validationResult = parameterValidator.validate(jsonNodeRequest);
if (jsonNodeRequest.isEmpty()) {
throw JsonMappingException.from(jsonNodeRequest.traverse(), "Request is empty");
}

RskJsonRpcRequest request = mapper.treeToValue(jsonNodeRequest, RskJsonRpcRequest.class);
RskWebSocketJsonParameterValidator.Result validationResult = parameterValidator.validate(jsonNodeRequest);

JsonRpcResultOrError resultOrError = null;
RskJsonRpcRequest request = mapper.treeToValue(jsonNodeRequest, RskJsonRpcRequest.class);

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());
}
if (request == null) {
throw new NullPointerException();
}

JsonRpcIdentifiableMessage response = resultOrError.responseFor(request.getId());
JsonRpcResultOrError resultOrError = null;

ctx.writeAndFlush(new TextWebSocketFrame(getJsonWithTypedId(jsonNodeRequest, response)));
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());
}

return;
JsonRpcIdentifiableMessage response = resultOrError.responseFor(request.getId());

} catch (IOException e) {
LOGGER.trace("Not a known or valid JsonRpcRequest", e);
ctx.writeAndFlush(new TextWebSocketFrame(getJsonWithTypedId(jsonNodeRequest, response)));

// We need to release this resource, netty only takes care about 'ByteBufHolder msg'
content.release(content.refCnt());
}
return;

// delegate to the next handler if the message can't be matched to a known JSON-RPC request
ctx.fireChannelRead(msg);
}
} catch (IOException e) {
LOGGER.trace("Not a known or valid JsonRpcRequest", e);

/**
* Uses the ID of the request to set the response so it can have the same type in json payload
*/
private String getJsonWithTypedId(JsonNode jsonNodeRequest, JsonRpcIdentifiableMessage response) throws JsonProcessingException {

// get the json representation of the response object
JsonNode jsonNodeResponse = mapper.valueToTree(response);

// set its ID with the the one that was provided in the request
((ObjectNode) jsonNodeResponse).set(ID, jsonNodeRequest.get(ID));

// creates the string json payload
return mapper.writeValueAsString(jsonNodeResponse);
}
// We need to release this resource, netty only takes care about 'ByteBufHolder msg'
content.release(content.refCnt());
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
emitter.unsubscribe(ctx.channel());
super.channelInactive(ctx);
}
// delegate to the next handler if the message can't be matched to a known JSON-RPC request
ctx.fireChannelRead(msg);
}

@Override
public JsonRpcResultOrError visit(EthUnsubscribeRequest request, ChannelHandlerContext ctx) {
boolean unsubscribed = emitter.unsubscribe(request.getParams().getSubscriptionId());
return new JsonRpcBooleanResult(unsubscribed);
}
/**
* Uses the ID of the request to set the response so it can have the same type in json payload
*/
private String getJsonWithTypedId(JsonNode jsonNodeRequest, JsonRpcIdentifiableMessage response) throws JsonProcessingException {

@Override
public JsonRpcResultOrError visit(EthSubscribeRequest request, ChannelHandlerContext ctx) {
return request.getParams().accept(emitter, ctx.channel());
}
// get the json representation of the response object
JsonNode jsonNodeResponse = mapper.valueToTree(response);

// set its ID with the the one that was provided in the request
((ObjectNode) jsonNodeResponse).set(ID, jsonNodeRequest.get(ID));

// creates the string json payload
return mapper.writeValueAsString(jsonNodeResponse);
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
emitter.unsubscribe(ctx.channel());
super.channelInactive(ctx);
}

@Override
public JsonRpcResultOrError visit(EthUnsubscribeRequest request, ChannelHandlerContext ctx) {
boolean unsubscribed = emitter.unsubscribe(request.getParams().getSubscriptionId());
return new JsonRpcBooleanResult(unsubscribed);
}

@Override
public JsonRpcResultOrError visit(EthSubscribeRequest request, ChannelHandlerContext ctx) {
return request.getParams().accept(emitter, ctx.channel());
}
}
Expand Up @@ -2,6 +2,7 @@

import co.rsk.rpc.CorsConfiguration;
import co.rsk.rpc.ModuleDescription;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
Expand Down Expand Up @@ -118,6 +119,10 @@ private void smokeTest(String contentType, String host, InetAddress rpcAddress,
String responseBody = response.body().string();
JsonNode jsonRpcResponse = OBJECT_MAPPER.readTree(responseBody);

if (jsonRpcResponse.isEmpty()) {
throw JsonMappingException.from(jsonRpcResponse.traverse(), "Empty response");
}

assertThat(response.code(), is(HttpResponseStatus.OK.code()));
assertThat(response.header("Content-Length"), is(notNullValue()));
assertThat(Integer.parseInt(response.header("Content-Length")), is(responseBody.getBytes().length));
Expand Down
Expand Up @@ -203,8 +203,13 @@ private byte[] getJsonRpcDummyMessage(String value) {

byte[] request = new byte[0];
try {
request = OBJECT_MAPPER.writeValueAsBytes(OBJECT_MAPPER.treeToValue(
JSON_NODE_FACTORY.objectNode().setAll(jsonRpcRequestProperties), Object.class));
Object object = OBJECT_MAPPER.treeToValue(JSON_NODE_FACTORY.objectNode().setAll(jsonRpcRequestProperties), Object.class);

if (object == null) {
throw new NullPointerException();
}

request = OBJECT_MAPPER.writeValueAsBytes(object);
} catch (JsonProcessingException e) {
fail(e.getMessage());
}
Expand Down

0 comments on commit 8d95b96

Please sign in to comment.