Skip to content

Commit ee93a91

Browse files
authored
rpcdaemon: rename json_rpc::JsonRpcValidator (#2114)
1 parent 92b865f commit ee93a91

File tree

9 files changed

+104
-104
lines changed

9 files changed

+104
-104
lines changed

docs/fuzzer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ To help with analysing a single request you can run the diagnostic tool:
3636
```
3737

3838
## Trophies
39-
1. Various validation errors which led to the introduction of `JsonRpcValidator`
39+
1. Various validation errors which led to the introduction of `rpc::json_rpc::Validator`
4040
2. BlockNum accepting ill-formatted numbers, e.g. `5x5`
4141
3. `{"jsonrpc":"2.0","id":1,"method":"eth_feeHistory","params":["0x1A","0x2",[95,99]]}` - triggers ASAN error
4242

silkworm/rpc/daemon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Daemon::Daemon(DaemonSettings settings, std::optional<mdbx::env> chaindata_env)
247247
compatibility::set_erigon_json_api_compatibility_required(settings_.erigon_json_rpc_compatibility);
248248

249249
// Load JSON RPC specification for Ethereum API
250-
rpc::json_rpc::JsonRpcValidator::load_specification();
250+
rpc::json_rpc::Validator::load_specification();
251251
}
252252

253253
void Daemon::add_private_services() {

silkworm/rpc/json_rpc/request_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ nlohmann::json RequestHandler::prevalidate_and_parse(const std::string& request)
119119
return nlohmann::json::parse(request);
120120
}
121121

122-
JsonRpcValidationResult RequestHandler::is_valid_jsonrpc(const nlohmann::json& request_json) {
122+
ValidationResult RequestHandler::is_valid_jsonrpc(const nlohmann::json& request_json) {
123123
return json_rpc_validator_.validate(request_json);
124124
}
125125

silkworm/rpc/json_rpc/request_handler.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class RequestHandler : public rpc::RequestHandler {
5555

5656
private:
5757
nlohmann::json prevalidate_and_parse(const std::string& request);
58-
JsonRpcValidationResult is_valid_jsonrpc(const nlohmann::json& request_json);
58+
ValidationResult is_valid_jsonrpc(const nlohmann::json& request_json);
5959

6060
Task<void> handle_request(
6161
commands::RpcApiTable::HandleMethod handler,
@@ -73,7 +73,7 @@ class RequestHandler : public rpc::RequestHandler {
7373

7474
const commands::RpcApiTable& rpc_api_table_;
7575

76-
JsonRpcValidator json_rpc_validator_;
76+
Validator json_rpc_validator_;
7777

7878
std::shared_ptr<InterfaceLog> ifc_log_;
7979
};

silkworm/rpc/json_rpc/validator.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static const std::string kRequestFieldParameters{"params"};
3131
static const std::string kRequestRequiredFields{
3232
kRequestFieldJsonRpc + "," + kRequestFieldId + "," + kRequestFieldMethod + "," + kRequestFieldParameters};
3333

34-
void JsonRpcValidator::load_specification() {
34+
void Validator::load_specification() {
3535
const auto spec = nlohmann::json::parse(specification_json, nullptr, /*allow_exceptions=*/false);
3636
if (spec.contains("methods")) {
3737
for (const auto& method : spec["methods"]) {
@@ -43,15 +43,15 @@ void JsonRpcValidator::load_specification() {
4343
}
4444
}
4545

46-
JsonRpcValidationResult JsonRpcValidator::validate(const nlohmann::json& request) {
46+
ValidationResult Validator::validate(const nlohmann::json& request) {
4747
if (auto valid_result{check_request_fields(request)}; !valid_result) {
4848
return valid_result;
4949
}
5050

5151
return validate_params(request);
5252
}
5353

54-
JsonRpcValidationResult JsonRpcValidator::check_request_fields(const nlohmann::json& request) {
54+
ValidationResult Validator::check_request_fields(const nlohmann::json& request) {
5555
// Expected fields: jsonrpc, id, method, params (optional)
5656
auto required_fields = 0b111;
5757

@@ -87,7 +87,7 @@ JsonRpcValidationResult JsonRpcValidator::check_request_fields(const nlohmann::j
8787
return {};
8888
}
8989

90-
JsonRpcValidationResult JsonRpcValidator::validate_params(const nlohmann::json& request) {
90+
ValidationResult Validator::validate_params(const nlohmann::json& request) {
9191
const auto& method = request.find(kRequestFieldMethod).value().get<std::string>();
9292
const auto& params_field = request.find(kRequestFieldParameters);
9393
const auto& params = params_field != request.end() ? params_field.value() : nlohmann::json::array();
@@ -128,7 +128,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_params(const nlohmann::json&
128128
return {};
129129
}
130130

131-
JsonRpcValidationResult JsonRpcValidator::validate_schema(const nlohmann::json& value, const nlohmann::json& schema) {
131+
ValidationResult Validator::validate_schema(const nlohmann::json& value, const nlohmann::json& schema) {
132132
if (schema.contains("type")) {
133133
if (auto result{validate_type(value, schema)}; !result) {
134134
return result;
@@ -140,7 +140,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_schema(const nlohmann::json&
140140
schema_of_collection = schema.find("oneOf");
141141
}
142142

143-
JsonRpcValidationResult result;
143+
ValidationResult result;
144144
if (schema_of_collection != schema.end()) {
145145
for (const auto& schema_of : schema_of_collection.value()) {
146146
result = validate_type(value, schema_of);
@@ -152,7 +152,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_schema(const nlohmann::json&
152152
return result;
153153
}
154154

155-
JsonRpcValidationResult JsonRpcValidator::validate_type(const nlohmann::json& value, const nlohmann::json& schema) {
155+
ValidationResult Validator::validate_type(const nlohmann::json& value, const nlohmann::json& schema) {
156156
const auto& schema_type = schema["type"].get<std::string>();
157157

158158
if (schema_type == "string") {
@@ -172,7 +172,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_type(const nlohmann::json& va
172172
}
173173
}
174174

175-
JsonRpcValidationResult JsonRpcValidator::validate_string(const nlohmann::json& string, const nlohmann::json& schema) {
175+
ValidationResult Validator::validate_string(const nlohmann::json& string, const nlohmann::json& schema) {
176176
if (!string.is_string()) {
177177
return tl::make_unexpected("Invalid string: " + string.dump());
178178
}
@@ -213,12 +213,12 @@ JsonRpcValidationResult JsonRpcValidator::validate_string(const nlohmann::json&
213213
return {};
214214
}
215215

216-
JsonRpcValidationResult JsonRpcValidator::validate_array(const nlohmann::json& array, const nlohmann::json& schema) {
216+
ValidationResult Validator::validate_array(const nlohmann::json& array, const nlohmann::json& schema) {
217217
if (!array.is_array() && !array.is_null() && array.empty()) {
218218
return tl::make_unexpected("Invalid array: " + array.dump());
219219
}
220220

221-
JsonRpcValidationResult result;
221+
ValidationResult result;
222222
const auto& schema_items = schema["items"];
223223
for (const auto& item : array) {
224224
result = validate_schema(item, schema_items);
@@ -230,7 +230,7 @@ JsonRpcValidationResult JsonRpcValidator::validate_array(const nlohmann::json& a
230230
return result;
231231
}
232232

233-
JsonRpcValidationResult JsonRpcValidator::validate_object(const nlohmann::json& object, const nlohmann::json& schema) {
233+
ValidationResult Validator::validate_object(const nlohmann::json& object, const nlohmann::json& schema) {
234234
if (!object.is_object()) {
235235
return tl::make_unexpected("Invalid object: " + object.dump());
236236
}
@@ -262,21 +262,21 @@ JsonRpcValidationResult JsonRpcValidator::validate_object(const nlohmann::json&
262262
return {};
263263
}
264264

265-
JsonRpcValidationResult JsonRpcValidator::validate_boolean(const nlohmann::json& boolean) {
265+
ValidationResult Validator::validate_boolean(const nlohmann::json& boolean) {
266266
if (!boolean.is_boolean()) {
267267
return tl::make_unexpected("Invalid boolean: " + boolean.dump());
268268
}
269269
return {};
270270
}
271271

272-
JsonRpcValidationResult JsonRpcValidator::validate_number(const nlohmann::json& number) {
272+
ValidationResult Validator::validate_number(const nlohmann::json& number) {
273273
if (!number.is_number()) {
274274
return tl::make_unexpected("Invalid number: " + number.dump());
275275
}
276276
return {};
277277
}
278278

279-
JsonRpcValidationResult JsonRpcValidator::validate_null(const nlohmann::json& value) {
279+
ValidationResult Validator::validate_null(const nlohmann::json& value) {
280280
if (value.is_null()) {
281281
return {};
282282
}

silkworm/rpc/json_rpc/validator.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@
2525

2626
namespace silkworm::rpc::json_rpc {
2727

28-
using JsonRpcValidationResult = tl::expected<void, std::string>;
28+
using ValidationResult = tl::expected<void, std::string>;
2929

30-
class JsonRpcValidator {
30+
class Validator {
3131
public:
3232
static void load_specification();
3333
static const std::string& openrpc_version() { return openrpc_version_; }
3434

35-
JsonRpcValidationResult validate(const nlohmann::json& request);
35+
ValidationResult validate(const nlohmann::json& request);
3636

3737
private:
38-
JsonRpcValidationResult check_request_fields(const nlohmann::json& request);
39-
JsonRpcValidationResult validate_params(const nlohmann::json& request);
40-
JsonRpcValidationResult validate_schema(const nlohmann::json& value, const nlohmann::json& schema);
41-
JsonRpcValidationResult validate_type(const nlohmann::json& value, const nlohmann::json& schema);
42-
JsonRpcValidationResult validate_string(const nlohmann::json& string, const nlohmann::json& schema);
43-
JsonRpcValidationResult validate_array(const nlohmann::json& array, const nlohmann::json& schema);
44-
JsonRpcValidationResult validate_object(const nlohmann::json& object, const nlohmann::json& schema);
45-
JsonRpcValidationResult validate_boolean(const nlohmann::json& boolean);
46-
JsonRpcValidationResult validate_number(const nlohmann::json& number);
47-
JsonRpcValidationResult validate_null(const nlohmann::json& value);
38+
ValidationResult check_request_fields(const nlohmann::json& request);
39+
ValidationResult validate_params(const nlohmann::json& request);
40+
ValidationResult validate_schema(const nlohmann::json& value, const nlohmann::json& schema);
41+
ValidationResult validate_type(const nlohmann::json& value, const nlohmann::json& schema);
42+
ValidationResult validate_string(const nlohmann::json& string, const nlohmann::json& schema);
43+
ValidationResult validate_array(const nlohmann::json& array, const nlohmann::json& schema);
44+
ValidationResult validate_object(const nlohmann::json& object, const nlohmann::json& schema);
45+
ValidationResult validate_boolean(const nlohmann::json& boolean);
46+
ValidationResult validate_number(const nlohmann::json& number);
47+
ValidationResult validate_null(const nlohmann::json& value);
4848

4949
inline static std::string openrpc_version_;
5050
inline static std::map<std::string, nlohmann::json> method_specs_;

silkworm/rpc/json_rpc/validator_benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "validator.hpp"
2323

24-
static silkworm::rpc::json_rpc::JsonRpcValidator validator{};
24+
static silkworm::rpc::json_rpc::Validator validator{};
2525

2626
const nlohmann::json requests[2] = {
2727
{

0 commit comments

Comments
 (0)