Skip to content

Commit c8d1436

Browse files
committed
Make request an optional last param for http senders.
1 parent 045ccb4 commit c8d1436

File tree

3 files changed

+104
-78
lines changed

3 files changed

+104
-78
lines changed

include/bitcoin/node/protocols/protocol_html.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,21 @@ class BCN_API protocol_html
6060
const network::http::request& request) NOEXCEPT;
6161

6262
/// Senders.
63-
virtual void send_json(const network::http::request& request,
64-
boost::json::value&& model, size_t size_hint) NOEXCEPT;
65-
virtual void send_text(const network::http::request& request,
66-
std::string&& hexidecimal) NOEXCEPT;
67-
virtual void send_chunk(const network::http::request& request,
68-
system::data_chunk&& bytes) NOEXCEPT;
69-
virtual void send_file(const network::http::request& request,
70-
network::http::file&& file, network::http::media_type type) NOEXCEPT;
71-
virtual void send_span(const network::http::request& request,
72-
network::http::span_body::value_type&& span,
73-
network::http::media_type type) NOEXCEPT;
74-
virtual void send_buffer(const network::http::request& request,
75-
network::http::buffer_body::value_type&& buffer,
76-
network::http::media_type type) NOEXCEPT;
63+
virtual void send_json(boost::json::value&& model, size_t size_hint,
64+
const network::http::request& request={}) NOEXCEPT;
65+
virtual void send_text(std::string&& hexidecimal,
66+
const network::http::request& request={}) NOEXCEPT;
67+
virtual void send_chunk(system::data_chunk&& bytes,
68+
const network::http::request& request={}) NOEXCEPT;
69+
virtual void send_file(network::http::file&& file,
70+
network::http::media_type type,
71+
const network::http::request& request={}) NOEXCEPT;
72+
virtual void send_span(network::http::span_body::value_type&& span,
73+
network::http::media_type type,
74+
const network::http::request& request={}) NOEXCEPT;
75+
virtual void send_buffer(network::http::buffer_body::value_type&& buffer,
76+
network::http::media_type type,
77+
const network::http::request& request={}) NOEXCEPT;
7778

7879
/// Utilities.
7980
bool is_allowed_origin(const network::http::fields& fields,

src/protocols/protocol_explore.cpp

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ bool protocol_explore::try_dispatch_object(const request& request) NOEXCEPT
111111
}
112112

113113
if (const auto ec = dispatcher_.notify(model))
114-
send_internal_server_error(request, ec);
114+
send_internal_server_error(ec, request);
115115

116116
return true;
117117
}
@@ -132,29 +132,29 @@ bool protocol_explore::handle_get_block(const code& ec, interface::block,
132132

133133
if (const auto block = archive().get_block(to_header(height, hash), witness))
134134
{
135+
const auto size = block->serialized_size(witness);
135136
switch (media)
136137
{
137138
case data:
138139
{
139-
data_chunk out(block->serialized_size(witness));
140+
data_chunk out(size);
140141
stream::out::fast sink{ out };
141142
write::bytes::fast writer{ sink };
142143
block->to_data(writer);
143-
send_chunk({}, std::move(out));
144+
send_chunk(std::move(out));
144145
return true;
145146
}
146147
case text:
147148
{
148-
std::string out(two * block->serialized_size(witness), '\0');
149+
std::string out(two * size, '\0');
149150
stream::out::fast sink{ out };
150151
write::base16::fast writer{ sink };
151152
block->to_data(writer);
152-
send_text({}, std::move(out));
153+
send_text(std::move(out));
153154
return true;
154155
}
155156
case json:
156-
send_json({}, value_from(block),
157-
two * block->serialized_size(witness));
157+
send_json(value_from(block), two * size);
158158
return true;
159159
}
160160
}
@@ -179,7 +179,7 @@ bool protocol_explore::handle_get_header(const code& ec, interface::header,
179179
send_wire(media, header->to_data());
180180
return true;
181181
case json:
182-
send_json({}, value_from(header),
182+
send_json(value_from(header),
183183
two * chain::header::serialized_size());
184184
return true;
185185
}
@@ -201,7 +201,6 @@ bool protocol_explore::handle_get_block_txs(const code& ec,
201201
!hashes.empty())
202202
{
203203
const auto size = hashes.size() * hash_size;
204-
205204
switch (media)
206205
{
207206
case data:
@@ -215,8 +214,8 @@ bool protocol_explore::handle_get_block_txs(const code& ec,
215214
{
216215
array out(hashes.size());
217216
std::ranges::transform(hashes, out.begin(),
218-
[](const auto& hash) { return encode_base16(hash); });
219-
send_json({}, out, two * size);
217+
[](const auto& hash) { return encode_hash(hash); });
218+
send_json(out, two * size);
220219
return true;
221220
}
222221
}
@@ -237,15 +236,29 @@ bool protocol_explore::handle_get_block_tx(const code& ec, interface::block_tx,
237236
if (const auto tx = query.get_transaction(query.to_transaction(
238237
to_header(height, hash), position), witness))
239238
{
239+
const auto size = tx->serialized_size(witness);
240240
switch (media)
241241
{
242242
case data:
243+
{
244+
data_chunk out(size);
245+
stream::out::fast sink{ out };
246+
write::bytes::fast writer{ sink };
247+
tx->to_data(writer);
248+
send_chunk(std::move(out));
249+
return true;
250+
}
243251
case text:
244-
send_wire(media, tx->to_data(witness));
252+
{
253+
std::string out(two * size, '\0');
254+
stream::out::fast sink{ out };
255+
write::base16::fast writer{ sink };
256+
tx->to_data(writer);
257+
send_text(std::move(out));
245258
return true;
259+
}
246260
case json:
247-
send_json({}, value_from(tx),
248-
two * tx->serialized_size(witness));
261+
send_json(value_from(tx), two * size);
249262
return true;
250263
}
251264
}
@@ -264,15 +277,29 @@ bool protocol_explore::handle_get_transaction(const code& ec,
264277
const auto& query = archive();
265278
if (const auto tx = query.get_transaction(query.to_tx(*hash), witness))
266279
{
280+
const auto size = tx->serialized_size(witness);
267281
switch (media)
268282
{
269283
case data:
284+
{
285+
data_chunk out(size);
286+
stream::out::fast sink{ out };
287+
write::bytes::fast writer{ sink };
288+
tx->to_data(writer);
289+
send_chunk(std::move(out));
290+
return true;
291+
}
270292
case text:
271-
send_wire(media, tx->to_data(witness));
293+
{
294+
std::string out(two * size, '\0');
295+
stream::out::fast sink{ out };
296+
write::base16::fast writer{ sink };
297+
tx->to_data(writer);
298+
send_text(std::move(out));
272299
return true;
300+
}
273301
case json:
274-
send_json({}, value_from(tx),
275-
two * tx->serialized_size(witness));
302+
send_json(value_from(tx), two * size);
276303
return true;
277304
}
278305
}
@@ -288,7 +315,7 @@ bool protocol_explore::handle_get_tx_block(const code& ec, interface::tx_block,
288315
return false;
289316

290317
const auto& query = archive();
291-
const auto block = query.to_block(query.to_tx(*hash));
318+
const auto block = query.to_strong(*hash);
292319
if (block.is_terminal())
293320
{
294321
send_not_found({});
@@ -298,7 +325,7 @@ bool protocol_explore::handle_get_tx_block(const code& ec, interface::tx_block,
298325
const auto key = query.get_header_key(block);
299326
if (key == null_hash)
300327
{
301-
send_internal_server_error({}, database::error::integrity);
328+
send_internal_server_error(database::error::integrity);
302329
return true;
303330
}
304331

@@ -309,7 +336,7 @@ bool protocol_explore::handle_get_tx_block(const code& ec, interface::tx_block,
309336
send_wire(media, to_chunk(key));
310337
return true;
311338
case json:
312-
send_json({}, value_from(encode_base16(key)), two * hash_size);
339+
send_json(value_from(encode_hash(key)), two * hash_size);
313340
return true;
314341
}
315342

@@ -334,7 +361,7 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
334361
const auto inputs = query.get_inputs(tx, witness);
335362
if (!inputs || inputs->empty())
336363
{
337-
send_internal_server_error({}, database::error::integrity);
364+
send_internal_server_error(database::error::integrity);
338365
return true;
339366
}
340367

@@ -353,7 +380,7 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
353380
for (const auto& output: *inputs)
354381
output->to_data(writer);
355382

356-
send_chunk({}, std::move(out));
383+
send_chunk(std::move(out));
357384
return true;
358385
}
359386
case text:
@@ -364,13 +391,13 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
364391
for (const auto& output: *inputs)
365392
output->to_data(writer);
366393

367-
send_text({}, std::move(out));
394+
send_text(std::move(out));
368395
return true;
369396
}
370397
case json:
371398
{
372399
// Json input serialization includes witness (if queried).
373-
send_json({}, value_from(*inputs), two * size);
400+
send_json(value_from(*inputs), two * size);
374401
return true;
375402
}
376403
}
@@ -396,7 +423,7 @@ bool protocol_explore::handle_get_input(const code& ec, interface::input,
396423
send_wire(media, input->to_data());
397424
return true;
398425
case json:
399-
send_json({}, value_from(input),
426+
send_json(value_from(input),
400427
two * input->serialized_size(witness));
401428
return true;
402429
}
@@ -424,7 +451,7 @@ bool protocol_explore::handle_get_input_script(const code& ec,
424451
send_wire(media, script->to_data(false));
425452
return true;
426453
case json:
427-
send_json({}, value_from(script),
454+
send_json(value_from(script),
428455
two * script->serialized_size(false));
429456
return true;
430457
}
@@ -452,7 +479,7 @@ bool protocol_explore::handle_get_input_witness(const code& ec,
452479
send_wire(media, witness->to_data(false));
453480
return true;
454481
case json:
455-
send_json({}, value_from(witness),
482+
send_json(value_from(witness),
456483
two * witness->serialized_size(false));
457484
return true;
458485
}
@@ -479,7 +506,7 @@ bool protocol_explore::handle_get_outputs(const code& ec, interface::outputs,
479506
const auto outputs = query.get_outputs(tx);
480507
if (!outputs || outputs->empty())
481508
{
482-
send_internal_server_error({}, database::error::integrity);
509+
send_internal_server_error(database::error::integrity);
483510
return true;
484511
}
485512

@@ -498,7 +525,7 @@ bool protocol_explore::handle_get_outputs(const code& ec, interface::outputs,
498525
for (const auto& output: *outputs)
499526
output->to_data(writer);
500527

501-
send_chunk({}, std::move(out));
528+
send_chunk(std::move(out));
502529
return true;
503530
}
504531
case text:
@@ -510,12 +537,12 @@ bool protocol_explore::handle_get_outputs(const code& ec, interface::outputs,
510537
for (const auto& output: *outputs)
511538
output->to_data(writer);
512539

513-
send_text({}, std::move(out));
540+
send_text(std::move(out));
514541
return true;
515542
}
516543
case json:
517544
{
518-
send_json({}, value_from(*outputs), two * size);
545+
send_json(value_from(*outputs), two * size);
519546
return true;
520547
}
521548
}
@@ -541,7 +568,7 @@ bool protocol_explore::handle_get_output(const code& ec, interface::output,
541568
send_wire(media, output->to_data());
542569
return true;
543570
case json:
544-
send_json({}, value_from(output),
571+
send_json(value_from(output),
545572
two * output->serialized_size());
546573
return true;
547574
}
@@ -569,7 +596,7 @@ bool protocol_explore::handle_get_output_script(const code& ec,
569596
send_wire(media, script->to_data(false));
570597
return true;
571598
case json:
572-
send_json({}, value_from(script),
599+
send_json(value_from(script),
573600
two * script->serialized_size(false));
574601
return true;
575602
}
@@ -605,7 +632,7 @@ bool protocol_explore::handle_get_output_spender(const code& ec,
605632
send_wire(media, point.to_data());
606633
return true;
607634
case json:
608-
send_json({}, value_from(point),
635+
send_json(value_from(point),
609636
two * chain::point::serialized_size());
610637
return true;
611638
}
@@ -642,7 +669,7 @@ bool protocol_explore::handle_get_output_spenders(const code& ec,
642669
send_wire(media, {});
643670
return true;
644671
case json:
645-
send_json({}, value_from(point), {});
672+
send_json(value_from(point), {});
646673
return true;
647674
}
648675
}
@@ -668,7 +695,7 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
668695
database::output_links outputs{};
669696
if (!query.to_address_outputs(outputs, *hash))
670697
{
671-
send_internal_server_error({}, database::error::integrity);
698+
send_internal_server_error(database::error::integrity);
672699
return true;
673700
}
674701

@@ -688,7 +715,7 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
688715
send_wire(media, {});
689716
return true;
690717
case json:
691-
send_json({}, value_from(*ptr), {});
718+
send_json(value_from(*ptr), {});
692719
return true;
693720
}
694721
}
@@ -727,7 +754,7 @@ bool protocol_explore::handle_get_filter(const code& ec, interface::filter,
727754
send_wire(media, std::move(filter));
728755
return true;
729756
case json:
730-
send_json({}, value_from(encode_base16(filter)),
757+
send_json(value_from(encode_base16(filter)),
731758
two * filter.size());
732759
return true;
733760
}
@@ -768,8 +795,7 @@ bool protocol_explore::handle_get_filter_hash(const code& ec,
768795
send_wire(media, std::move(chunk));
769796
return true;
770797
case json:
771-
send_json({}, value_from(encode_base16(chunk)),
772-
two * chunk.size());
798+
send_json(value_from(encode_hash(chunk)), two * chunk.size());
773799
return true;
774800
}
775801
}
@@ -809,8 +835,7 @@ bool protocol_explore::handle_get_filter_header(const code& ec,
809835
send_wire(media, std::move(chunk));
810836
return true;
811837
case json:
812-
const auto base16 = encode_base16(chunk);
813-
send_json({}, value_from(base16), base16.size());
838+
send_json(value_from(encode_hash(chunk)), two * chunk.size());
814839
return true;
815840
}
816841
}
@@ -825,9 +850,9 @@ bool protocol_explore::handle_get_filter_header(const code& ec,
825850
void protocol_explore::send_wire(uint8_t media, data_chunk&& chunk) NOEXCEPT
826851
{
827852
if (media == data)
828-
send_chunk({}, std::move(chunk));
853+
send_chunk(std::move(chunk));
829854
else
830-
send_text({}, encode_base16(chunk));
855+
send_text(encode_base16(chunk));
831856
}
832857

833858
database::header_link protocol_explore::to_header(

0 commit comments

Comments
 (0)