Skip to content

Commit e276482

Browse files
Begin adding JSON-RPC formatting support.
1 parent 4b0c6c9 commit e276482

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

include/bitcoin/protocol/web/json_string.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,21 @@ namespace http {
3535
//-----------------------------------------------------------------------------
3636

3737
BCP_API std::string to_json(const boost::property_tree::ptree& tree);
38-
BCP_API std::string to_json(uint64_t height, uint32_t id);
39-
BCP_API std::string to_json(const system::code& code, uint32_t id);
40-
BCP_API std::string to_json(const system::chain::header& header, uint32_t id);
41-
BCP_API std::string to_json(const system::chain::block& block, uint32_t id);
38+
39+
// The rpc distinction is for formatting the json to conform to
40+
// libbitcoin json, or rpc json.
41+
BCP_API std::string to_json(uint64_t height, uint32_t id, bool rpc);
42+
BCP_API std::string to_json(const system::hash_digest& hash, uint32_t id,
43+
bool rpc);
44+
BCP_API std::string to_json(const system::code& code, uint32_t id, bool rpc);
45+
BCP_API std::string to_json(const system::chain::header& header, uint32_t id,
46+
bool rpc);
47+
BCP_API std::string to_json(const system::chain::block& block, uint32_t id,
48+
bool rpc);
4249
BCP_API std::string to_json(const system::chain::block& block, uint32_t height,
43-
uint32_t id);
50+
uint32_t id, bool rpc);
4451
BCP_API std::string to_json(const system::chain::transaction& transaction,
45-
uint32_t id);
52+
uint32_t id, bool rpc);
4653

4754
} // namespace http
4855
} // namespace protocol

src/web/json_string.cpp

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,58 +47,88 @@ std::string to_json(const ptree& tree)
4747
return json_stream.str();
4848
}
4949

50-
std::string to_json(const ptree& tree, uint32_t id)
50+
std::string to_json(const ptree& tree, uint32_t id, bool /* rpc */)
5151
{
5252
ptree result_tree;
5353
result_tree.add_child("result", tree);
5454
result_tree.put("id", id);
5555
return to_json(result_tree);
5656
}
5757

58-
std::string to_json(uint64_t height, uint32_t id)
58+
std::string to_json(uint64_t height, uint32_t id, bool rpc)
5959
{
60+
if (!rpc)
61+
return to_json(property_tree(height, id));
62+
6063
ptree tree;
6164
tree.put("result", height);
6265
tree.put("id", id);
6366
return to_json(tree);
64-
65-
// TODO: The bc::property_tree call works fine, but the format is
66-
// different than expected for json_rpc so eventually we need to
67-
// separate out property_tree and json_rpc::property_tree, or
68-
// something along the lines to make this a clear distinction.
69-
//// return to_json(property_tree(height, id));
7067
}
7168

72-
std::string to_json(const code& code, uint32_t id)
69+
std::string to_json(const code& code, uint32_t id, bool rpc)
7370
{
71+
if (!rpc)
72+
return to_json(property_tree(code, id));
73+
7474
ptree tree;
7575
ptree error_tree;
7676
error_tree.put("code", code.value());
7777
error_tree.put("message", code.message());
7878
tree.add_child("error", error_tree);
7979
tree.put("id", id);
8080
return to_json(tree);
81-
//// return to_json(property_tree(code, id));
8281
}
8382

84-
std::string to_json(const chain::header& header, uint32_t id)
83+
std::string to_json(const hash_digest& hash, uint32_t id, bool rpc)
84+
{
85+
ptree tree;
86+
if (rpc)
87+
tree.put("result", encode_hash(hash));
88+
else
89+
tree = property_tree(hash);
90+
tree.put("id", id);
91+
return to_json(tree);
92+
}
93+
94+
std::string to_json(const chain::header& header, uint32_t id, bool rpc)
8595
{
86-
return to_json(property_tree(config::header(header)), id);
96+
if (!rpc)
97+
return to_json(property_tree(config::header(header)), id, rpc);
98+
99+
auto hex = [](uint32_t value)
100+
{
101+
std::stringstream hex_value;
102+
hex_value << std::setfill('0') << std::setw(sizeof(uint32_t) * 2);
103+
hex_value << std::hex << value;
104+
return hex_value.str();
105+
};
106+
107+
ptree tree;
108+
tree.put("hash", encode_hash(header.hash()));
109+
tree.put("version", header.version());
110+
tree.put("versionHex", hex(header.version()));
111+
tree.put("merkleroot", encode_hash(header.merkle()));
112+
tree.put("time", header.timestamp());
113+
tree.put("nonce", header.nonce());
114+
tree.put("bits", hex(header.bits()));
115+
return to_json(tree, id, rpc);
87116
}
88117

89-
std::string to_json(const chain::block& block, uint32_t id)
118+
std::string to_json(const chain::block& block, uint32_t id, bool rpc)
90119
{
91-
return to_json(property_tree(block, true), id);
120+
return to_json(property_tree(block, true), id, rpc);
92121
}
93122

94-
std::string to_json(const chain::block& block, uint32_t, uint32_t id)
123+
std::string to_json(const chain::block& block, uint32_t, uint32_t id, bool rpc)
95124
{
96-
return to_json(property_tree(config::header(block.header())), id);
125+
return to_json(property_tree(config::header(block.header())), id, rpc);
97126
}
98127

99-
std::string to_json(const chain::transaction& transaction, uint32_t id)
128+
std::string to_json(const chain::transaction& transaction, uint32_t id,
129+
bool rpc)
100130
{
101-
return to_json(property_tree(config::transaction(transaction), true), id);
131+
return to_json(property_tree(config::transaction(transaction), true), id, rpc);
102132
}
103133

104134
} // namespace http

src/web/socket.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ class query_response_task_sender
150150
const auto ec = source.read_error_code();
151151
if (ec)
152152
{
153-
work.connection->write(to_json(ec, id));
153+
work.connection->write(to_json(ec, id, connection->json_rpc()));
154154
return true;
155155
}
156156

157157
const auto handler = handlers_.find(work.command);
158158
if (handler == handlers_.end())
159159
{
160160
static constexpr auto error = system::error::not_implemented;
161-
work.connection->write(to_json(error, id));
161+
work.connection->write(to_json(error, id, connection->json_rpc()));
162162
return true;
163163
}
164164

@@ -543,7 +543,7 @@ void socket::notify_query_work(connection_ptr connection,
543543
const auto send_error_reply = [=](protocol_status status, const code& ec)
544544
{
545545
http_reply reply;
546-
const auto error = to_json(ec, id);
546+
const auto error = to_json(ec, id, connection->json_rpc());
547547
const auto response = reply.generate(status, {}, error.size(), false);
548548
LOG_VERBOSE(LOG_PROTOCOL) << response + error;
549549
connection->write(response + error);

0 commit comments

Comments
 (0)