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 ripple {
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()}
39 : address{address_v4::loopback()};
40
41 if (!pp.port)
42 Throw<std::runtime_error>("Use fixConfigPorts with auto ports");
43
44 return {*pp.ip, *pp.port};
45 }
46 Throw<std::runtime_error>("Missing HTTP port");
47 return {}; // Silence compiler control paths return value warning
48 }
49
50 template <class ConstBufferSequence>
51 static std::string
52 buffer_string(ConstBufferSequence const& b)
53 {
54 using namespace boost::asio;
56 s.resize(buffer_size(b));
57 buffer_copy(buffer(&s[0], s.size()), b);
58 return s;
59 }
60
61 boost::asio::ip::tcp::endpoint ep_;
62 boost::asio::io_context ios_;
63 boost::asio::ip::tcp::socket stream_;
64 boost::beast::multi_buffer bin_;
65 boost::beast::multi_buffer bout_;
66 unsigned rpc_version_;
67
68public:
69 explicit JSONRPCClient(Config const& cfg, unsigned rpc_version)
70 : ep_(getEndpoint(cfg)), stream_(ios_), rpc_version_(rpc_version)
71 {
72 stream_.connect(ep_);
73 }
74
75 ~JSONRPCClient() override
76 {
77 // stream_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
78 // stream_.close();
79 }
80
81 /*
82 Return value is an Object type with up to three keys:
83 status
84 error
85 result
86 */
88 invoke(std::string const& cmd, Json::Value const& params) override
89 {
90 using namespace boost::beast::http;
91 using namespace boost::asio;
92 using namespace std::string_literals;
93
94 request<string_body> req;
95 req.method(boost::beast::http::verb::post);
96 req.target("/");
97 req.version(11);
98 req.insert("Content-Type", "application/json; charset=UTF-8");
99 {
101 ostr << ep_;
102 req.insert("Host", ostr.str());
103 }
104 {
105 Json::Value jr;
106 jr[jss::method] = cmd;
107 if (rpc_version_ == 2)
108 {
109 jr[jss::jsonrpc] = "2.0";
110 jr[jss::ripplerpc] = "2.0";
111 jr[jss::id] = 5;
112 }
113 if (params)
114 {
115 Json::Value& ja = jr[jss::params] = Json::arrayValue;
116 ja.append(params);
117 }
118 req.body() = to_string(jr);
119 }
120 req.prepare_payload();
121 write(stream_, req);
122
123 response<dynamic_body> res;
124 read(stream_, bin_, res);
125
126 Json::Reader jr;
127 Json::Value jv;
128 jr.parse(buffer_string(res.body().data()), jv);
129 if (jv["result"].isMember("error"))
130 jv["error"] = jv["result"]["error"];
131 if (jv["result"].isMember("status"))
132 jv["status"] = jv["result"]["status"];
133 return jv;
134 }
135
136 unsigned
137 version() const override
138 {
139 return rpc_version_;
140 }
141};
142
144makeJSONRPCClient(Config const& cfg, unsigned rpc_version)
145{
146 return std::make_unique<JSONRPCClient>(cfg, rpc_version);
147}
148
149} // namespace test
150} // namespace ripple
Unserialize a JSON document into a Value.
Definition json_reader.h:20
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Represents a JSON value.
Definition json_value.h:130
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:60
static boost::asio::ip::tcp::endpoint getEndpoint(BasicConfig const &cfg)
unsigned version() const override
Get RPC 1.0 or RPC 2.0.
boost::asio::io_context ios_
boost::asio::ip::tcp::socket stream_
boost::asio::ip::tcp::endpoint ep_
boost::beast::multi_buffer bout_
JSONRPCClient(Config const &cfg, unsigned rpc_version)
Json::Value invoke(std::string const &cmd, Json::Value const &params) override
Submit a command synchronously.
static std::string buffer_string(ConstBufferSequence const &b)
boost::beast::multi_buffer bin_
T count(T... args)
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:25
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:195
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
T resize(T... args)
T size(T... args)
T str(T... args)
std::optional< std::uint16_t > port
Definition Port.h:97
std::set< std::string, boost::beast::iless > protocol
Definition Port.h:83
std::optional< boost::asio::ip::address > ip
Definition Port.h:96