This is an automated email from the ASF dual-hosted git repository. toulmean pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/main by this push: new cc40d2ed Add test and better defaults for the JSON-RPC proxy new b8c91f34 Merge pull request #415 from atoulme/better_jsonrpc_app_defaults cc40d2ed is described below commit cc40d2ed6c16fd334571c997716c02012c79184c Author: Antoine Toulme <anto...@lunar-ocean.com> AuthorDate: Thu Jun 16 20:43:17 2022 -0700 Add test and better defaults for the JSON-RPC proxy --- .../org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt | 12 +-- .../apache/tuweni/jsonrpc/app/JSONRPCConfigTest.kt | 88 ++++++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) diff --git a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt index 1d8fd571..509aab43 100644 --- a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt +++ b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt @@ -26,7 +26,7 @@ import java.util.Collections /** * Configuration of the JSON-RPC server as a TOML-based file. */ -class JSONRPCConfig(val filePath: Path) { +class JSONRPCConfig(val filePath: Path? = null) { companion object { @@ -36,7 +36,7 @@ class JSONRPCConfig(val filePath: Path) { .addString("metricsNetworkInterface", "localhost", "Metric service network interface", null) .addBoolean("metricsGrpcPushEnabled", false, "Enable pushing metrics to gRPC service", null) .addBoolean("metricsPrometheusEnabled", false, "Enable exposing metrics on the Prometheus endpoint", null) - .addInteger("port", 8545, "JSON-RPC server port", PropertyValidator.isValidPort()) + .addInteger("port", 18545, "JSON-RPC server port", PropertyValidator.isValidPort()) .addString("networkInterface", "127.0.0.1", "JSON-RPC server network interface", null) .addString("clientFingerprintsFile", "fingerprints.txt", "File recording client connection fingerprints", null) .addBoolean("ssl", false, "Whether the JSON-RPC server should serve data over SSL", null) @@ -44,15 +44,15 @@ class JSONRPCConfig(val filePath: Path) { .addString("basicAuthUsername", "", "HTTP Basic Auth username", null) .addString("basicAuthPassword", "", "HTTP Basic Auth password", null) .addString("basicAuthRealm", "Apache Tuweni JSON-RPC proxy", "HTTP Basic Auth realm", null) - .addListOfString("allowedMethods", Collections.emptyList(), "Allowed JSON-RPC methods", null) + .addListOfString("allowedMethods", listOf("eth_", "net_version"), "Allowed JSON-RPC methods", null) .addListOfString("allowedRanges", Collections.singletonList("0.0.0.0/0"), "Allowed IP ranges", null) .addListOfString("rejectedRanges", Collections.emptyList(), "Rejected IP ranges", null) .addString("endpointUrl", "http://localhost:8545", "JSON-RPC endpoint", null) .addBoolean("endpointBasicAuthEnabled", false, "Enable basic authentication for the endpoint", null) .addString("endpointBasicAuthUsername", "", "Basic authentication username for the endpoint", null) .addString("endpointBasicAuthPassword", "", "Basic authentication password for the endpoint", null) - .addListOfString("cachedMethods", Collections.emptyList(), "Cached JSON-RPC methods", null) - .addBoolean("cacheEnabled", false, "Enable caching", null) + .addListOfString("cachedMethods", listOf("eth_blockNumber", "eth_getBlockByNumber", "eth_getBlockByHash", "eth_getTransactionReceipt", "eth_getTransactionByHash", "eth_getLogs"), "Cached JSON-RPC methods", null) + .addBoolean("cacheEnabled", true, "Enable caching", null) .addString("cacheStoragePath", "", "Location of cache storage", null) .addInteger("maxConcurrentRequests", 30, "Maximum concurrent requests", null) .addString("metricsGrpcEndpoint", "http://localhost:4317", "Metrics GRPC push endpoint", null) @@ -62,7 +62,7 @@ class JSONRPCConfig(val filePath: Path) { .toSchema() } - val config = Configuration.fromToml(filePath, schema()) + val config = if (filePath != null) Configuration.fromToml(filePath, schema()) else Configuration.empty(schema()) fun numberOfThreads() = config.getInteger("numberOfThreads") fun metricsPort() = config.getInteger("metricsPort") diff --git a/jsonrpc-app/src/test/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfigTest.kt b/jsonrpc-app/src/test/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfigTest.kt new file mode 100644 index 00000000..bbc34e9e --- /dev/null +++ b/jsonrpc-app/src/test/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfigTest.kt @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuweni.jsonrpc.app + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class JSONRPCConfigTest { + + @Test + fun testDefaultConfig() { + val configAsString = JSONRPCConfig().config.toToml() + assertEquals( + """## Allowed JSON-RPC methods +#allowedMethods = ["eth_", "net_version"] +## Allowed IP ranges +#allowedRanges = ["0.0.0.0/0"] +## Whether the JSON-RPC server should authenticate incoming requests with HTTP Basic Authentication +#basicAuth = false +## HTTP Basic Auth password +#basicAuthPassword = "" +## HTTP Basic Auth realm +#basicAuthRealm = "Apache Tuweni JSON-RPC proxy" +## HTTP Basic Auth username +#basicAuthUsername = "" +## Enable caching +#cacheEnabled = true +## Lifespan time for entries on cache in milliseconds +#cacheLifespan = 5000 +## Max idle time for entries on cache in milliseconds +#cacheMaxIdle = 1000 +## Location of cache storage +#cacheStoragePath = "" +## Cached JSON-RPC methods +#cachedMethods = ["eth_blockNumber", "eth_getBlockByNumber", "eth_getBlockByHash", "eth_getTransactionReceipt", "eth_getTransactionByHash", "eth_getLogs"] +## File recording client connection fingerprints +#clientFingerprintsFile = "fingerprints.txt" +## Enable basic authentication for the endpoint +#endpointBasicAuthEnabled = false +## Basic authentication password for the endpoint +#endpointBasicAuthPassword = "" +## Basic authentication username for the endpoint +#endpointBasicAuthUsername = "" +## JSON-RPC endpoint +#endpointUrl = "http://localhost:8545" +## Maximum concurrent requests +#maxConcurrentRequests = 30 +## Metrics GRPC push endpoint +#metricsGrpcEndpoint = "http://localhost:4317" +## Enable pushing metrics to gRPC service +#metricsGrpcPushEnabled = false +## Metrics GRPC push timeout +#metricsGrpcTimeout = 2000 +## Metric service network interface +#metricsNetworkInterface = "localhost" +## Metric service port +#metricsPort = 9090 +## Enable exposing metrics on the Prometheus endpoint +#metricsPrometheusEnabled = false +## JSON-RPC server network interface +#networkInterface = "127.0.0.1" +## Number of threads for each thread pool +#numberOfThreads = 10 +## JSON-RPC server port +#port = 18545 +## Rejected IP ranges +#rejectedRanges = [] +## Whether the JSON-RPC server should serve data over SSL +#ssl = false +""".split("\n").joinToString(System.lineSeparator()), + configAsString + ) + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@tuweni.apache.org For additional commands, e-mail: commits-h...@tuweni.apache.org