Skip to content

Commit

Permalink
Issue 5719 - remove Pretty JSON and make it user configurable (#5766)
Browse files Browse the repository at this point in the history
* add --pretty-json-enabled

* update changelog

Signed-off-by: George Tebrean <[email protected]>

* Update CLI option name in CHANGELOG.md

Signed-off-by: Sally MacFarlane <[email protected]>

---------

Signed-off-by: George Tebrean <[email protected]>
Signed-off-by: Sally MacFarlane <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
  • Loading branch information
gtebrean and macfarla authored Aug 21, 2023
1 parent 3b04f5c commit 92a3c5b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Additions and Improvements
- Added `benchmark` subcommand to `evmtool` [#5754](https://github.com/hyperledger/besu/issues/5754)
- JSON output is now compact by default. This can be overridden by the new `--json-pretty-print-enabled` CLI option. [#5766](https://github.com/hyperledger/besu/pull/5766)

### Bug Fixes
- Make smart contract permissioning features work with london fork [#5727](https://github.com/hyperledger/besu/pull/5727)
Expand Down
7 changes: 7 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.hyperledger.besu.ethereum.api.graphql.GraphQLConfiguration.DEFAULT_GRAPHQL_HTTP_PORT;
import static org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration.DEFAULT_ENGINE_JSON_RPC_PORT;
import static org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration.DEFAULT_JSON_RPC_PORT;
import static org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration.DEFAULT_PRETTY_JSON_ENABLED;
import static org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis.DEFAULT_RPC_APIS;
import static org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis.VALID_APIS;
import static org.hyperledger.besu.ethereum.api.jsonrpc.authentication.EngineAuthService.EPHEMERAL_JWT_FILE;
Expand Down Expand Up @@ -779,6 +780,11 @@ static class JsonRPCHttpOptionGroup {
paramLabel = MANDATORY_LONG_FORMAT_HELP,
description = "Specifies the maximum request content length. (default: ${DEFAULT-VALUE})")
private final Long rpcHttpMaxRequestContentLength = DEFAULT_MAX_REQUEST_CONTENT_LENGTH;

@Option(
names = {"--json-pretty-print-enabled"},
description = "Enable JSON pretty print format (default: ${DEFAULT-VALUE})")
private final Boolean prettyJsonEnabled = DEFAULT_PRETTY_JSON_ENABLED;
}

// JSON-RPC Websocket Options
Expand Down Expand Up @@ -2443,6 +2449,7 @@ && rpcHttpAuthenticationCredentialsFile() == null
jsonRpcConfiguration.setMaxBatchSize(jsonRPCHttpOptionGroup.rpcHttpMaxBatchSize);
jsonRpcConfiguration.setMaxRequestContentLength(
jsonRPCHttpOptionGroup.rpcHttpMaxRequestContentLength);
jsonRpcConfiguration.setPrettyJsonEnabled(jsonRPCHttpOptionGroup.prettyJsonEnabled);
return jsonRpcConfiguration;
}

Expand Down
1 change: 1 addition & 0 deletions besu/src/test/resources/everything_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ rpc-http-tls-cipher-suites=["TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_R
rpc-http-max-batch-size=1
rpc-http-max-request-content-length = 5242880
rpc-max-logs-range=100
json-pretty-print-enabled=false

# PRIVACY TLS
privacy-tls-enabled=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import io.vertx.ext.web.RoutingContext;

public class JsonRpcObjectExecutor extends AbstractJsonRpcExecutor {
private static final ObjectWriter jsonObjectWriter = createObjectWriter();
private final ObjectWriter jsonObjectWriter = createObjectWriter();

public JsonRpcObjectExecutor(
final JsonRpcExecutor jsonRpcExecutor,
Expand Down Expand Up @@ -64,7 +64,7 @@ String getRpcMethodName(final RoutingContext ctx) {
return jsonObject.getString("method");
}

private static void handleJsonObjectResponse(
private void handleJsonObjectResponse(
final HttpServerResponse response,
final JsonRpcResponse jsonRpcResponse,
final RoutingContext ctx)
Expand All @@ -90,9 +90,12 @@ private static HttpResponseStatus status(final JsonRpcResponse response) {
};
}

private static ObjectWriter createObjectWriter() {
return getJsonObjectMapper()
.writerWithDefaultPrettyPrinter()
private ObjectWriter createObjectWriter() {
ObjectWriter writer =
jsonRpcConfiguration.isPrettyJsonEnabled()
? getJsonObjectMapper().writerWithDefaultPrettyPrinter()
: getJsonObjectMapper().writer();
return writer
.without(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)
.with(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class JsonRpcConfiguration {
public static final int DEFAULT_MAX_ACTIVE_CONNECTIONS = 80;
public static final int DEFAULT_MAX_BATCH_SIZE = 1024;
public static final long DEFAULT_MAX_REQUEST_CONTENT_LENGTH = 5 * 1024 * 1024; // 5MB
public static final boolean DEFAULT_PRETTY_JSON_ENABLED = false;

private boolean enabled;
private int port;
Expand All @@ -55,6 +56,7 @@ public class JsonRpcConfiguration {
private int maxActiveConnections;
private int maxBatchSize;
private long maxRequestContentLength;
private boolean prettyJsonEnabled;

public static JsonRpcConfiguration createDefault() {
final JsonRpcConfiguration config = new JsonRpcConfiguration();
Expand All @@ -66,6 +68,7 @@ public static JsonRpcConfiguration createDefault() {
config.setMaxActiveConnections(DEFAULT_MAX_ACTIVE_CONNECTIONS);
config.setMaxBatchSize(DEFAULT_MAX_BATCH_SIZE);
config.setMaxRequestContentLength(DEFAULT_MAX_REQUEST_CONTENT_LENGTH);
config.setPrettyJsonEnabled(DEFAULT_PRETTY_JSON_ENABLED);
return config;
}

Expand Down Expand Up @@ -196,6 +199,14 @@ public void setHttpTimeoutSec(final long httpTimeoutSec) {
this.httpTimeoutSec = httpTimeoutSec;
}

public boolean isPrettyJsonEnabled() {
return prettyJsonEnabled;
}

public void setPrettyJsonEnabled(final boolean prettyJsonEnabled) {
this.prettyJsonEnabled = prettyJsonEnabled;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ private Hash recordPendingTransaction(final int blockNumber, final int transacti
@Test
public void getFilterChanges_noBlocks() throws Exception {
startService();
final String expectedRespBody =
String.format("{%n \"jsonrpc\" : \"2.0\",%n \"id\" : 2,%n \"result\" : [ ]%n}");
final String expectedRespBody = String.format("{\"jsonrpc\":\"2.0\",\"id\":2,\"result\":[]}");
final ResponseBody body = ethNewBlockFilter(1).body();
final String result = getResult(body);
body.close();
Expand All @@ -60,7 +59,7 @@ public void getFilterChanges_oneBlock() throws Exception {
BlockchainSetupUtil blockchainSetupUtil = startServiceWithEmptyChain(DataStorageFormat.FOREST);
final String expectedRespBody =
String.format(
"{%n \"jsonrpc\" : \"2.0\",%n \"id\" : 2,%n \"result\" : [ \"0x10aaf14a53caf27552325374429d3558398a36d3682ede6603c2c6511896e9f9\" ]%n}");
"{\"jsonrpc\":\"2.0\",\"id\":2,\"result\":[\"0x10aaf14a53caf27552325374429d3558398a36d3682ede6603c2c6511896e9f9\"]}");
final ResponseBody body = ethNewBlockFilter(1).body();
final String result = getResult(body);
body.close();
Expand All @@ -75,8 +74,7 @@ public void getFilterChanges_oneBlock() throws Exception {
@Test
public void getFilterChanges_noTransactions() throws Exception {
startService();
final String expectedRespBody =
String.format("{%n \"jsonrpc\" : \"2.0\",%n \"id\" : 2,%n \"result\" : [ ]%n}");
final String expectedRespBody = String.format("{\"jsonrpc\":\"2.0\",\"id\":2,\"result\":[]}");
final ResponseBody body = ethNewPendingTransactionFilter(1).body();
final String result = getResult(body);
body.close();
Expand All @@ -96,18 +94,14 @@ public void getFilterChanges_oneTransaction() throws Exception {
final Response resp = ethGetFilterChanges(2, result);
assertThat(resp.code()).isEqualTo(200);
final String expectedRespBody =
String.format(
"{%n \"jsonrpc\" : \"2.0\",%n \"id\" : 2,%n \"result\" : [ \""
+ transactionHash
+ "\" ]%n}");
String.format("{\"jsonrpc\":\"2.0\",\"id\":2,\"result\":[\"" + transactionHash + "\"]}");
assertThat(resp.body().string()).isEqualTo(expectedRespBody);
}

@Test
public void uninstallFilter() throws Exception {
startService();
final String expectedRespBody =
String.format("{%n \"jsonrpc\" : \"2.0\",%n \"id\" : 2,%n \"result\" : true%n}");
final String expectedRespBody = String.format("{\"jsonrpc\":\"2.0\",\"id\":2,\"result\":true}");
final ResponseBody body = ethNewBlockFilter(1).body();
final String result = getResult(body);
body.close();
Expand Down

0 comments on commit 92a3c5b

Please sign in to comment.