rippled
Loading...
Searching...
No Matches
JSONRPCClient.cpp
1#include <test/jtx/JSONRPCClient.h>
2
3#include <xrpl/json/json_reader.h>
4#include <xrpl/json/to_string.h>
5#include <xrpl/protocol/jss.h>
6#include <xrpl/server/Port.h>
7
8#include <boost/asio.hpp>
9#include <boost/beast/http/dynamic_body.hpp>
10#include <boost/beast/http/message.hpp>
11#include <boost/beast/http/read.hpp>
12#include <boost/beast/http/string_body.hpp>
13#include <boost/beast/http/write.hpp>
14
15#include <string>
16
17namespace xrpl {
18namespace test {
19
21{
22 static boost::asio::ip::tcp::endpoint
24 {
25 auto& log = std::cerr;
26 ParsedPort common;
27 parse_Port(common, cfg["server"], log);
28 for (auto const& name : cfg.section("server").values())
29 {
30 if (!cfg.exists(name))
31 continue;
32 ParsedPort pp;
33 parse_Port(pp, cfg[name], log);
34 if (pp.protocol.count("http") == 0)
35 continue;
36 using namespace boost::asio::ip;
37 if (pp.ip && pp.ip->is_unspecified())
38 *pp.ip = pp.ip->is_v6() ? address{address_v6::loopback()} : address{address_v4::loopback()};
39
40 if (!pp.port)
41 Throw<std::runtime_error>("Use fixConfigPorts with auto ports");
42
43 return {*pp.ip, *pp.port};
44 }
45 Throw<std::runtime_error>("Missing HTTP port");
46 return {}; // Silence compiler control paths return value warning
47 }
48
49 template <class ConstBufferSequence>
50 static std::string
51 buffer_string(ConstBufferSequence const& b)
52 {
53 using namespace boost::asio;
55 s.resize(buffer_size(b));
56 buffer_copy(buffer(&s[0], s.size()), b);
57 return s;
58 }
59
60 boost::asio::ip::tcp::endpoint ep_;
61 boost::asio::io_context ios_;
62 boost::asio::ip::tcp::socket stream_;
63 boost::beast::multi_buffer bin_;
64 boost::beast::multi_buffer bout_;
65 unsigned rpc_version_;
66
67public:
68 explicit JSONRPCClient(Config const& cfg, unsigned rpc_version)
69 : ep_(getEndpoint(cfg)), stream_(ios_), rpc_version_(rpc_version)
70 {
71 stream_.connect(ep_);
72 }
73
74 ~JSONRPCClient() override
75 {
76 // stream_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
77 // stream_.close();
78 }
79
80 /*
81 Return value is an Object type with up to three keys:
82 status
83 error
84 result
85 */
87 invoke(std::string const& cmd, Json::Value const& params) override
88 {
89 using namespace boost::beast::http;
90 using namespace boost::asio;
91 using namespace std::string_literals;
92
93 request<string_body> req;
94 req.method(boost::beast::http::verb::post);
95 req.target("/");
96 req.version(11);
97 req.insert("Content-Type", "application/json; charset=UTF-8");
98 {
100 ostr << ep_;
101 req.insert("Host", ostr.str());
102 }
103 {
104 Json::Value jr;
105 jr[jss::method] = cmd;
106 if (rpc_version_ == 2)
107 {
108 jr[jss::jsonrpc] = "2.0";
109 jr[jss::ripplerpc] = "2.0";
110 jr[jss::id] = 5;
111 }
112 if (params)
113 {
114 Json::Value& ja = jr[jss::params] = Json::arrayValue;
115 ja.append(params);
116 }
117 req.body() = to_string(jr);
118 }
119 req.prepare_payload();
120 write(stream_, req);
121
122 response<dynamic_body> res;
123 read(stream_, bin_, res);
124
125 Json::Reader jr;
126 Json::Value jv;
127 jr.parse(buffer_string(res.body().data()), jv);
128 if (jv["result"].isMember("error"))
129 jv["error"] = jv["result"]["error"];
130 if (jv["result"].isMember("status"))
131 jv["status"] = jv["result"]["status"];
132 return jv;
133 }
134
135 unsigned
136 version() const override
137 {
138 return rpc_version_;
139 }
140};
141
143makeJSONRPCClient(Config const& cfg, unsigned rpc_version)
144{
145 return std::make_unique<JSONRPCClient>(cfg, rpc_version);
146}
147
148} // namespace test
149} // namespace xrpl
Unserialize a JSON document into a Value.
Definition json_reader.h:18
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
Holds unparsed configuration information.
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Section & section(std::string const &name)
Returns the section with the given name.
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:59
static std::string buffer_string(ConstBufferSequence const &b)
unsigned version() const override
Get RPC 1.0 or RPC 2.0.
boost::asio::ip::tcp::endpoint ep_
boost::asio::ip::tcp::socket stream_
boost::beast::multi_buffer bout_
JSONRPCClient(Config const &cfg, unsigned rpc_version)
boost::beast::multi_buffer bin_
boost::asio::io_context ios_
static boost::asio::ip::tcp::endpoint getEndpoint(BasicConfig const &cfg)
Json::Value invoke(std::string const &cmd, Json::Value const &params) override
Submit a command synchronously.
T count(T... args)
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:26
std::unique_ptr< AbstractClient > makeJSONRPCClient(Config const &cfg, unsigned rpc_version)
Returns a client using JSON-RPC over HTTP/S.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
void parse_Port(ParsedPort &port, Section const &section, std::ostream &log)
Definition Port.cpp:191
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
T resize(T... args)
T size(T... args)
T str(T... args)
std::set< std::string, boost::beast::iless > protocol
Definition Port.h:83
std::optional< boost::asio::ip::address > ip
Definition Port.h:96
std::optional< std::uint16_t > port
Definition Port.h:97