Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 95d3396

Browse files
lupin012canepat
andauthored
net_version and web3_clientVersion implemented via backend (#96)
Co-authored-by: canepat <16927169+canepat@users.noreply.github.com>
1 parent 312954e commit 95d3396

File tree

9 files changed

+80
-20
lines changed

9 files changed

+80
-20
lines changed

docs/API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ The following table shows the current [JSON RPC API](https://eth.wiki/json-rpc/A
44

55
| Command | Availability | Notes |
66
| :-------------------------------------- | :----------: | -----------------------------------------: |
7-
| web3_clientVersion | Yes | hard-coded (needs ethbackend integration) |
7+
| web3_clientVersion | Yes | |
88
| web3_sha3 | Yes | |
99
| | | |
1010
| net_listening | Yes | hard-coded (needs p2pSentry integration) |
1111
| net_peerCount | Yes | hard-coded (needs p2pSentry integration) |
12-
| net_version | Yes | hard-coded (needs ethbackend integration) |
12+
| net_version | Yes | |
1313
| | | |
1414
| eth_blockNumber | Yes | |
1515
| eth_chainId | Yes | |

silkrpc/commands/net_api.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "net_api.hpp"
1818

1919
#include <silkrpc/json/types.hpp>
20+
#include <string>
2021

2122
namespace silkrpc::commands {
2223

@@ -36,9 +37,17 @@ asio::awaitable<void> NetRpcApi::handle_net_peer_count(const nlohmann::json& req
3637

3738
// https://eth.wiki/json-rpc/API#net_version
3839
asio::awaitable<void> NetRpcApi::handle_net_version(const nlohmann::json& request, nlohmann::json& reply) {
39-
reply = make_json_content(request["id"], 5);
40-
// TODO(canepat): use NetVersion RPC by exposed ETHBACKEND gRPC service
41-
co_return;
40+
try {
41+
const auto net_version = co_await backend_->get_net_version();
42+
const auto net_version_str = std::to_string(net_version);
43+
reply = make_json_content(request["id"], net_version_str);
44+
} catch (const std::exception& e) {
45+
SILKRPC_ERROR << "exception: " << e.what() << " processing request: " << request.dump() << "\n";
46+
reply = make_json_error(request["id"], -32000, e.what());
47+
} catch (...) {
48+
SILKRPC_ERROR << "unexpected exception processing request: " << request.dump() << "\n";
49+
reply = make_json_error(request["id"], 100, "unexpected exception");
50+
}
4251
}
4352

4453
} // namespace silkrpc::commands

silkrpc/commands/net_api.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
#include <nlohmann/json.hpp>
2727

2828
#include <silkrpc/json/types.hpp>
29+
#include <silkrpc/context_pool.hpp>
30+
#include <silkrpc/ethbackend/backend.hpp>
2931

3032
namespace silkrpc::http { class RequestHandler; }
3133

3234
namespace silkrpc::commands {
3335

3436
class NetRpcApi {
3537
public:
36-
NetRpcApi() = default;
38+
explicit NetRpcApi(std::unique_ptr<ethbackend::BackEnd>& backend) : backend_(backend) {}
3739
virtual ~NetRpcApi() = default;
3840

3941
NetRpcApi(const NetRpcApi&) = delete;
@@ -46,8 +48,9 @@ class NetRpcApi {
4648

4749
private:
4850
friend class silkrpc::http::RequestHandler;
49-
};
5051

52+
std::unique_ptr<ethbackend::BackEnd>& backend_;
53+
};
5154
} // namespace silkrpc::commands
5255

5356
#endif // SILKRPC_COMMANDS_NET_API_HPP_

silkrpc/commands/rpc_api.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace silkrpc::commands {
3535
class RpcApi : protected EthereumRpcApi, NetRpcApi, Web3RpcApi, DebugRpcApi, ParityRpcApi, TurboGethRpcApi, TraceRpcApi {
3636
public:
3737
explicit RpcApi(Context& context) :
38-
EthereumRpcApi{context}, NetRpcApi{}, Web3RpcApi{context.database}, DebugRpcApi{context.database},
38+
EthereumRpcApi{context}, NetRpcApi{context.backend}, Web3RpcApi{context}, DebugRpcApi{context.database},
3939
ParityRpcApi{context.database}, TurboGethRpcApi{context.database}, TraceRpcApi{context.database} {}
4040
virtual ~RpcApi() {}
4141

silkrpc/commands/web3_api.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ namespace silkrpc::commands {
2929

3030
// https://eth.wiki/json-rpc/API#web3_clientversion
3131
asio::awaitable<void> Web3RpcApi::handle_web3_client_version(const nlohmann::json& request, nlohmann::json& reply) {
32-
reply = make_json_content(request["id"], common::kEthereumNodeName);
33-
// TODO(canepat): use ClientVersion RPC exposed by ETHBACKEND gRPC service
32+
try {
33+
const auto web3_client_version = co_await backend_->get_client_version();
34+
reply = make_json_content(request["id"], web3_client_version);
35+
} catch (const std::exception& e) {
36+
SILKRPC_ERROR << "exception: " << e.what() << " processing request: " << request.dump() << "\n";
37+
reply = make_json_error(request["id"], -32000, e.what());
38+
} catch (...) {
39+
SILKRPC_ERROR << "unexpected exception processing request: " << request.dump() << "\n";
40+
reply = make_json_error(request["id"], 100, "unexpected exception");
41+
}
3442
co_return;
3543
}
3644

silkrpc/commands/web3_api.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@
2626

2727
#include <silkrpc/core/rawdb/accessors.hpp>
2828
#include <silkrpc/json/types.hpp>
29+
#include <silkrpc/context_pool.hpp>
2930
#include <silkrpc/ethdb/database.hpp>
31+
#include <silkrpc/ethbackend/backend.hpp>
3032

3133
namespace silkrpc::http { class RequestHandler; }
3234

3335
namespace silkrpc::commands {
3436

3537
class Web3RpcApi {
3638
public:
37-
explicit Web3RpcApi(std::unique_ptr<ethdb::Database>& database) : database_(database) {}
39+
explicit Web3RpcApi(Context& context) : database_(context.database), backend_(context.backend) {}
3840
virtual ~Web3RpcApi() {}
3941

4042
Web3RpcApi(const Web3RpcApi&) = delete;
@@ -46,6 +48,7 @@ class Web3RpcApi {
4648

4749
private:
4850
std::unique_ptr<ethdb::Database>& database_;
51+
std::unique_ptr<ethbackend::BackEnd>& backend_;
4952

5053
friend class silkrpc::http::RequestHandler;
5154
};

silkrpc/common/constants.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ constexpr const char* kDefaultLocal{"localhost:8545"};
4949
constexpr const char* kDefaultTarget{"localhost:9090"};
5050
constexpr const std::chrono::milliseconds kDefaultTimeout{10000};
5151

52-
constexpr const char* kNodeName{"TurboGeth"};
53-
constexpr const char* kVersion{"2021.05.3-alpha"};
54-
const std::string kEthereumNodeName = boost::str(boost::format("%1%/v%2%/%3%") % kNodeName % kVersion % kOsName); // NOLINT
55-
56-
constexpr uint64_t kETH66{66};
57-
5852
} // namespace silkrpc::common
5953

6054
#endif // SILKRPC_COMMON_CONSTANTS_HPP_

silkrpc/ethbackend/backend.hpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SILKRPC_ETHBACKEND_BACKEND_HPP_
1919

2020
#include <memory>
21+
#include <string>
2122

2223
#include <silkrpc/config.hpp>
2324

@@ -52,13 +53,35 @@ using ProtocolVersionClient = AsyncUnaryClient<
5253
&::remote::ETHBACKEND::Stub::PrepareAsyncProtocolVersion
5354
>;
5455

56+
using NetVersionClient = AsyncUnaryClient<
57+
::remote::ETHBACKEND::Stub,
58+
::remote::ETHBACKEND::NewStub,
59+
::remote::NetVersionRequest,
60+
::remote::NetVersionReply,
61+
&::remote::ETHBACKEND::Stub::PrepareAsyncNetVersion
62+
>;
63+
64+
using ClientVersionClient = AsyncUnaryClient<
65+
::remote::ETHBACKEND::Stub,
66+
::remote::ETHBACKEND::NewStub,
67+
::remote::ClientVersionRequest,
68+
::remote::ClientVersionReply,
69+
&::remote::ETHBACKEND::Stub::PrepareAsyncClientVersion
70+
>;
71+
72+
5573
using EtherbaseAwaitable = unary_awaitable<asio::io_context::executor_type, EtherbaseClient, ::remote::EtherbaseReply>;
5674
using ProtocolVersionAwaitable = unary_awaitable<asio::io_context::executor_type, ProtocolVersionClient, ::remote::ProtocolVersionReply>;
75+
using NetVersionAwaitable = unary_awaitable<asio::io_context::executor_type, NetVersionClient, ::remote::NetVersionReply>;
76+
using ClientVersionAwaitable = unary_awaitable<asio::io_context::executor_type, ClientVersionClient, ::remote::ClientVersionReply>;
5777

5878
class BackEnd final {
5979
public:
6080
explicit BackEnd(asio::io_context& context, std::shared_ptr<grpc::Channel> channel, grpc::CompletionQueue* queue)
61-
: eb_awaitable_{context.get_executor(), channel, queue}, pv_awaitable_{context.get_executor(), channel, queue} {
81+
: eb_awaitable_{context.get_executor(), channel, queue},
82+
pv_awaitable_{context.get_executor(), channel, queue},
83+
nv_awaitable_{context.get_executor(), channel, queue},
84+
cv_awaitable_{context.get_executor(), channel, queue} {
6285
SILKRPC_TRACE << "BackEnd::ctor " << this << "\n";
6386
}
6487

@@ -83,6 +106,24 @@ class BackEnd final {
83106
co_return pv;
84107
}
85108

109+
asio::awaitable<uint64_t> get_net_version() {
110+
const auto start_time = clock_time::now();
111+
const auto reply = co_await nv_awaitable_.async_call(asio::use_awaitable);
112+
const auto nv = reply.id();
113+
SILKRPC_DEBUG << "BackEnd::get_net_version version=" << nv << " t=" << clock_time::since(start_time) << "\n";
114+
co_return nv;
115+
}
116+
117+
asio::awaitable<std::string> get_client_version() {
118+
const auto start_time = clock_time::now();
119+
const auto reply = co_await cv_awaitable_.async_call(asio::use_awaitable);
120+
const auto cv = reply.nodename();
121+
SILKRPC_DEBUG << "BackEnd::get_client_version version=" << cv << " t=" << clock_time::since(start_time) << "\n";
122+
co_return cv;
123+
}
124+
125+
126+
86127
private:
87128
evmc::address address_from_H160(const types::H160& h160) {
88129
uint64_t hi_hi = h160.hi().hi();
@@ -97,6 +138,8 @@ class BackEnd final {
97138

98139
EtherbaseAwaitable eb_awaitable_;
99140
ProtocolVersionAwaitable pv_awaitable_;
141+
NetVersionAwaitable nv_awaitable_;
142+
ClientVersionAwaitable cv_awaitable_;
100143
};
101144

102145
} // namespace silkrpc::ethbackend

tests/integration/jsonrpc_commands_goerli.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"response":{
1010
"jsonrpc":"2.0",
1111
"id":1,
12-
"result":"TurboGeth/v2021.05.3-alpha/Linux"
12+
"result":null
1313
}
1414
},
1515
{
@@ -61,7 +61,7 @@
6161
"response":{
6262
"jsonrpc":"2.0",
6363
"id":1,
64-
"result":5
64+
"result":"5"
6565
}
6666
},
6767
{

0 commit comments

Comments
 (0)