Skip to content

Commit 2a90e1d

Browse files
authored
Merge pull request #877 from evoskuil/master
Add size hints to json serialization.
2 parents a2f1ebb + 7284667 commit 2a90e1d

File tree

3 files changed

+80
-88
lines changed

3 files changed

+80
-88
lines changed

include/bitcoin/node/protocols/protocol_explore.hpp

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -48,81 +48,9 @@ class BCN_API protocol_explore
4848
}
4949

5050
protected:
51-
52-
/// TODO: move to own source.
53-
/// Pagination and filtering are via query string.
54-
enum targets
55-
{
56-
/// /v[]/block/hash/[bkhash] {1}
57-
/// /v[]/block/height/[height] {1}
58-
block,
59-
60-
/// /v[]/block/hash/[bkhash]/filter {1}
61-
/// /v[]/block/height/[height]/filter {1}
62-
filter,
63-
64-
/// /v[]/block/hash/[bkhash]/header {1}
65-
/// /v[]/block/height/[height]/header {1}
66-
header,
67-
68-
/// /v[]/transaction/hash/[txhash] {1}
69-
/// /v[]/block/hash/[bkhash]/transaction/position/[position] {1}
70-
/// /v[]/block/height/[height]/transaction/position/[position] {1}
71-
transaction,
72-
73-
/// /v[]/block/hash/[bkhash]/transactions {all txs in the block}
74-
/// /v[]/block/height/[height]/transactions {all txs in the block}
75-
transactions,
76-
77-
// --------------------------------------------------------------------
78-
79-
/// /v[]/input/[txhash]/[index] {1}
80-
input,
81-
82-
/// /v[]/inputs/[txhash] {all inputs in the tx}
83-
inputs,
84-
85-
/// /v[]/input/[txhash]/[index]/script {1}
86-
input_script,
87-
88-
/// /v[]/input/[txhash]/scripts {all input scripts in the tx}
89-
input_scripts,
90-
91-
/// /v[]/input/[txhash]/[index]/witness {1}
92-
input_witness,
93-
94-
/// /v[]/input/[txhash]/witnesses {all witnesses in the tx}
95-
input_witnesses,
96-
97-
// --------------------------------------------------------------------
98-
99-
/// /v[]/output/[txhash]/[index] {1}
100-
output,
101-
102-
/// /v[]/outputs/[txhash] {all outputs in the tx}
103-
outputs,
104-
105-
/// /v[]/output/[txhash]/[index]/script {1}
106-
output_script,
107-
108-
/// /v[]/output/[txhash]/scripts {all output scripts in the tx}
109-
output_scripts,
110-
111-
/// /v[]/output/[txhash]/[index]/spender {1 - confirmed}
112-
output_spender,
113-
114-
/// /v[]/output/[txhash]/spenders {all}
115-
output_spenders,
116-
117-
// --------------------------------------------------------------------
118-
119-
/// /v[]/address/[output-script-hash] {all}
120-
address
121-
};
122-
12351
/// Senders.
12452
void send_json(const network::http::request& request,
125-
boost::json::value&& model) NOEXCEPT;
53+
boost::json::value&& model, size_t size_hint) NOEXCEPT;
12654
void send_text(const network::http::request& request,
12755
std::string&& hexidecimal) NOEXCEPT;
12856
void send_data(const network::http::request& request,

src/protocols/protocol_explore.cpp

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,81 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
4040
// ----------------------------------------------------------------------------
4141
// TODO: performance timing header.
4242
// TODO: formatted error responses.
43-
// TODO: priority sort and dispatch.
43+
// TODO: formatted error responses.
44+
// TODO: priority accept media type sort and dispatch.
4445
// TODO: URI path parse (see API doc).
4546

47+
/// TODO: move to own source.
48+
/// Pagination and filtering are via query string.
49+
enum targets
50+
{
51+
/// /v[]/block/hash/[bkhash] {1}
52+
/// /v[]/block/height/[height] {1}
53+
block,
54+
55+
/// /v[]/block/hash/[bkhash]/filter {1}
56+
/// /v[]/block/height/[height]/filter {1}
57+
filter,
58+
59+
/// /v[]/block/hash/[bkhash]/header {1}
60+
/// /v[]/block/height/[height]/header {1}
61+
header,
62+
63+
/// /v[]/transaction/hash/[txhash] {1}
64+
/// /v[]/block/hash/[bkhash]/transaction/position/[position] {1}
65+
/// /v[]/block/height/[height]/transaction/position/[position] {1}
66+
transaction,
67+
68+
/// /v[]/block/hash/[bkhash]/transactions {all txs in the block}
69+
/// /v[]/block/height/[height]/transactions {all txs in the block}
70+
transactions,
71+
72+
// --------------------------------------------------------------------
73+
74+
/// /v[]/input/[txhash]/[index] {1}
75+
input,
76+
77+
/// /v[]/inputs/[txhash] {all inputs in the tx}
78+
inputs,
79+
80+
/// /v[]/input/[txhash]/[index]/script {1}
81+
input_script,
82+
83+
/// /v[]/input/[txhash]/scripts {all input scripts in the tx}
84+
input_scripts,
85+
86+
/// /v[]/input/[txhash]/[index]/witness {1}
87+
input_witness,
88+
89+
/// /v[]/input/[txhash]/witnesses {all witnesses in the tx}
90+
input_witnesses,
91+
92+
// --------------------------------------------------------------------
93+
94+
/// /v[]/output/[txhash]/[index] {1}
95+
output,
96+
97+
/// /v[]/outputs/[txhash] {all outputs in the tx}
98+
outputs,
99+
100+
/// /v[]/output/[txhash]/[index]/script {1}
101+
output_script,
102+
103+
/// /v[]/output/[txhash]/scripts {all output scripts in the tx}
104+
output_scripts,
105+
106+
/// /v[]/output/[txhash]/[index]/spender {1 - confirmed}
107+
output_spender,
108+
109+
/// /v[]/output/[txhash]/spenders {all}
110+
output_spenders,
111+
112+
// --------------------------------------------------------------------
113+
114+
/// /v[]/address/[output-script-hash] {all}
115+
address
116+
};
117+
46118
void protocol_explore::handle_receive_get(const code& ec,
47119
const method::get& request) NOEXCEPT
48120
{
@@ -80,12 +152,7 @@ void protocol_explore::handle_receive_get(const code& ec,
80152
}
81153

82154
if (const auto parts = split(uri.path(), "/");
83-
!parts.empty() && parts.size() != two)
84-
{
85-
send_bad_target(*request);
86-
return;
87-
}
88-
else
155+
parts.size() == two)
89156
{
90157
const auto hd = parts.front() == "header" || parts.front() == "hd";
91158
const auto bk = parts.front() == "block" || parts.front() == "bk";
@@ -120,23 +187,23 @@ void protocol_explore::handle_receive_get(const code& ec,
120187
{
121188
if (const auto ptr = query.get_header(query.to_header(hash)))
122189
{
123-
send_json(*request, value_from(ptr));
190+
send_json(*request, value_from(ptr), ptr->serialized_size());
124191
return;
125192
}
126193
}
127194
else if (bk)
128195
{
129196
if (const auto ptr = query.get_block(query.to_header(hash), wit))
130197
{
131-
send_json(*request, value_from(ptr));
198+
send_json(*request, value_from(ptr), ptr->serialized_size(wit));
132199
return;
133200
}
134201
}
135202
else
136203
{
137204
if (const auto ptr = query.get_transaction(query.to_tx(hash), wit))
138205
{
139-
send_json(*request, value_from(ptr));
206+
send_json(*request, value_from(ptr), ptr->serialized_size(wit));
140207
return;
141208
}
142209
}
@@ -241,13 +308,13 @@ void protocol_explore::handle_receive_get(const code& ec,
241308
// TODO: buffer should be reused, so set at the channel.
242309
// json_value is not a sized body, so this sets chunked encoding.
243310
void protocol_explore::send_json(const request& request,
244-
boost::json::value&& model) NOEXCEPT
311+
boost::json::value&& model, size_t size_hint) NOEXCEPT
245312
{
246313
BC_ASSERT_MSG(stranded(), "strand");
247314
response response{ status::ok, request.version() };
248315
add_common_headers(response, request);
249316
response.set(field::content_type, from_mime_type(json));
250-
response.body() = { std::move(model), std::make_shared<flat_buffer>(10 * 1024 * 1024) };
317+
response.body() = { std::move(model), size_hint };
251318
response.prepare_payload();
252319
SEND(std::move(response), handle_complete, _1, error::success);
253320
}

src/protocols/protocol_html.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,11 @@ void protocol_html::send_file(const request& request, file&& file,
8484
{
8585
BC_ASSERT_MSG(stranded(), "strand");
8686
BC_ASSERT_MSG(file.is_open(), "sending closed file handle");
87-
8887
response response{ status::ok, request.version() };
8988
add_common_headers(response, request);
90-
9189
response.set(field::content_type, from_mime_type(type));
9290
response.body() = std::move(file);
9391
response.prepare_payload();
94-
9592
SEND(std::move(response), handle_complete, _1, error::success);
9693
}
9794

0 commit comments

Comments
 (0)