Skip to content

Commit 045ccb4

Browse files
committed
Explore protocol style, optimize block.
1 parent 7b5d77a commit 045ccb4

File tree

1 file changed

+56
-45
lines changed

1 file changed

+56
-45
lines changed

src/protocols/protocol_explore.cpp

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,31 @@ bool protocol_explore::handle_get_block(const code& ec, interface::block,
130130
if (stopped(ec))
131131
return false;
132132

133-
if (const auto ptr = archive().get_block(to_header(height, hash), witness))
133+
if (const auto block = archive().get_block(to_header(height, hash), witness))
134134
{
135135
switch (media)
136136
{
137137
case data:
138+
{
139+
data_chunk out(block->serialized_size(witness));
140+
stream::out::fast sink{ out };
141+
write::bytes::fast writer{ sink };
142+
block->to_data(writer);
143+
send_chunk({}, std::move(out));
144+
return true;
145+
}
138146
case text:
139-
send_wire(media, ptr->to_data(witness));
147+
{
148+
std::string out(two * block->serialized_size(witness), '\0');
149+
stream::out::fast sink{ out };
150+
write::base16::fast writer{ sink };
151+
block->to_data(writer);
152+
send_text({}, std::move(out));
140153
return true;
154+
}
141155
case json:
142-
send_json({}, value_from(ptr),
143-
two * ptr->serialized_size(witness));
156+
send_json({}, value_from(block),
157+
two * block->serialized_size(witness));
144158
return true;
145159
}
146160
}
@@ -156,16 +170,16 @@ bool protocol_explore::handle_get_header(const code& ec, interface::header,
156170
if (stopped(ec))
157171
return false;
158172

159-
if (const auto ptr = archive().get_header(to_header(height, hash)))
173+
if (const auto header = archive().get_header(to_header(height, hash)))
160174
{
161175
switch (media)
162176
{
163177
case data:
164178
case text:
165-
send_wire(media, ptr->to_data());
179+
send_wire(media, header->to_data());
166180
return true;
167181
case json:
168-
send_json({}, value_from(ptr),
182+
send_json({}, value_from(header),
169183
two * chain::header::serialized_size());
170184
return true;
171185
}
@@ -220,18 +234,18 @@ bool protocol_explore::handle_get_block_tx(const code& ec, interface::block_tx,
220234
return false;
221235

222236
const auto& query = archive();
223-
if (const auto ptr = query.get_transaction(query.to_transaction(
237+
if (const auto tx = query.get_transaction(query.to_transaction(
224238
to_header(height, hash), position), witness))
225239
{
226240
switch (media)
227241
{
228242
case data:
229243
case text:
230-
send_wire(media, ptr->to_data(witness));
244+
send_wire(media, tx->to_data(witness));
231245
return true;
232246
case json:
233-
send_json({}, value_from(ptr),
234-
two * ptr->serialized_size(witness));
247+
send_json({}, value_from(tx),
248+
two * tx->serialized_size(witness));
235249
return true;
236250
}
237251
}
@@ -248,17 +262,17 @@ bool protocol_explore::handle_get_transaction(const code& ec,
248262
return false;
249263

250264
const auto& query = archive();
251-
if (const auto ptr = query.get_transaction(query.to_tx(*hash), witness))
265+
if (const auto tx = query.get_transaction(query.to_tx(*hash), witness))
252266
{
253267
switch (media)
254268
{
255269
case data:
256270
case text:
257-
send_wire(media, ptr->to_data(witness));
271+
send_wire(media, tx->to_data(witness));
258272
return true;
259273
case json:
260-
send_json({}, value_from(ptr),
261-
two * ptr->serialized_size(witness));
274+
send_json({}, value_from(tx),
275+
two * tx->serialized_size(witness));
262276
return true;
263277
}
264278
}
@@ -324,10 +338,10 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
324338
return true;
325339
}
326340

327-
// Wire serialization size of inputs set.
341+
// Wire serialization size of inputs set (no witness in wire inputs).
328342
const auto size = std::accumulate(inputs->begin(), inputs->end(), zero,
329343
[&witness](size_t total, const auto& output) NOEXCEPT
330-
{ return total + output->serialized_size(witness); });
344+
{ return total + output->serialized_size(false); });
331345

332346
switch (media)
333347
{
@@ -344,8 +358,7 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
344358
}
345359
case text:
346360
{
347-
std::string out{};
348-
out.resize(two * size);
361+
std::string out(two * size, '\0');
349362
stream::out::fast sink{ out };
350363
write::base16::fast writer{ sink };
351364
for (const auto& output: *inputs)
@@ -356,6 +369,7 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
356369
}
357370
case json:
358371
{
372+
// Json input serialization includes witness (if queried).
359373
send_json({}, value_from(*inputs), two * size);
360374
return true;
361375
}
@@ -373,17 +387,17 @@ bool protocol_explore::handle_get_input(const code& ec, interface::input,
373387
return false;
374388

375389
const auto& query = archive();
376-
if (const auto ptr = query.get_input(query.to_tx(*hash), index, witness))
390+
if (const auto input = query.get_input(query.to_tx(*hash), index, witness))
377391
{
378392
switch (media)
379393
{
380394
case data:
381395
case text:
382-
send_wire(media, ptr->to_data());
396+
send_wire(media, input->to_data());
383397
return true;
384398
case json:
385-
send_json({}, value_from(ptr),
386-
two * ptr->serialized_size(witness));
399+
send_json({}, value_from(input),
400+
two * input->serialized_size(witness));
387401
return true;
388402
}
389403
}
@@ -400,18 +414,18 @@ bool protocol_explore::handle_get_input_script(const code& ec,
400414
return false;
401415

402416
const auto& query = archive();
403-
if (const auto ptr = query.get_input_script(query.to_point(
417+
if (const auto script = query.get_input_script(query.to_point(
404418
query.to_tx(*hash), index)))
405419
{
406420
switch (media)
407421
{
408422
case data:
409423
case text:
410-
send_wire(media, ptr->to_data(false));
424+
send_wire(media, script->to_data(false));
411425
return true;
412426
case json:
413-
send_json({}, value_from(ptr),
414-
two * ptr->serialized_size(false));
427+
send_json({}, value_from(script),
428+
two * script->serialized_size(false));
415429
return true;
416430
}
417431
}
@@ -428,18 +442,18 @@ bool protocol_explore::handle_get_input_witness(const code& ec,
428442
return false;
429443

430444
const auto& query = archive();
431-
if (const auto ptr = query.get_witness(query.to_point(
445+
if (const auto witness = query.get_witness(query.to_point(
432446
query.to_tx(*hash), index)))
433447
{
434448
switch (media)
435449
{
436450
case data:
437451
case text:
438-
send_wire(media, ptr->to_data(false));
452+
send_wire(media, witness->to_data(false));
439453
return true;
440454
case json:
441-
send_json({}, value_from(ptr),
442-
two * ptr->serialized_size(false));
455+
send_json({}, value_from(witness),
456+
two * witness->serialized_size(false));
443457
return true;
444458
}
445459
}
@@ -518,17 +532,17 @@ bool protocol_explore::handle_get_output(const code& ec, interface::output,
518532
return false;
519533

520534
const auto& query = archive();
521-
if (const auto ptr = query.get_output(query.to_tx(*hash), index))
535+
if (const auto output = query.get_output(query.to_tx(*hash), index))
522536
{
523537
switch (media)
524538
{
525539
case data:
526540
case text:
527-
send_wire(media, ptr->to_data());
541+
send_wire(media, output->to_data());
528542
return true;
529543
case json:
530-
send_json({}, value_from(ptr),
531-
two * ptr->serialized_size());
544+
send_json({}, value_from(output),
545+
two * output->serialized_size());
532546
return true;
533547
}
534548
}
@@ -545,18 +559,18 @@ bool protocol_explore::handle_get_output_script(const code& ec,
545559
return false;
546560

547561
const auto& query = archive();
548-
if (const auto ptr = query.get_output_script(query.to_output(
562+
if (const auto script = query.get_output_script(query.to_output(
549563
query.to_tx(*hash), index)))
550564
{
551565
switch (media)
552566
{
553567
case data:
554568
case text:
555-
send_wire(media, ptr->to_data(false));
569+
send_wire(media, script->to_data(false));
556570
return true;
557571
case json:
558-
send_json({}, value_from(ptr),
559-
two * ptr->serialized_size(false));
572+
send_json({}, value_from(script),
573+
two * script->serialized_size(false));
560574
return true;
561575
}
562576
}
@@ -684,8 +698,7 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
684698
}
685699

686700
bool protocol_explore::handle_get_filter(const code& ec, interface::filter,
687-
uint8_t, uint8_t media, uint8_t type,
688-
std::optional<system::hash_cptr> hash,
701+
uint8_t, uint8_t media, uint8_t type, std::optional<hash_cptr> hash,
689702
std::optional<uint32_t> height) NOEXCEPT
690703
{
691704
if (stopped(ec))
@@ -726,8 +739,7 @@ bool protocol_explore::handle_get_filter(const code& ec, interface::filter,
726739

727740
bool protocol_explore::handle_get_filter_hash(const code& ec,
728741
interface::filter_hash, uint8_t, uint8_t media, uint8_t type,
729-
std::optional<system::hash_cptr> hash,
730-
std::optional<uint32_t> height) NOEXCEPT
742+
std::optional<hash_cptr> hash, std::optional<uint32_t> height) NOEXCEPT
731743
{
732744
if (stopped(ec))
733745
return false;
@@ -768,8 +780,7 @@ bool protocol_explore::handle_get_filter_hash(const code& ec,
768780

769781
bool protocol_explore::handle_get_filter_header(const code& ec,
770782
interface::filter_header, uint8_t, uint8_t media, uint8_t type,
771-
std::optional<system::hash_cptr> hash,
772-
std::optional<uint32_t> height) NOEXCEPT
783+
std::optional<hash_cptr> hash, std::optional<uint32_t> height) NOEXCEPT
773784
{
774785
if (stopped(ec))
775786
return false;

0 commit comments

Comments
 (0)