Skip to content

Commit caa6a15

Browse files
committed
Serialize inputs and outputs.
1 parent d464b59 commit caa6a15

File tree

1 file changed

+87
-31
lines changed

1 file changed

+87
-31
lines changed

src/protocols/protocol_explore.cpp

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ bool protocol_explore::handle_get_tx_block(const code& ec, interface::tx_block,
304304
}
305305

306306
bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
307-
uint8_t, uint8_t media, const hash_cptr& hash, bool) NOEXCEPT
307+
uint8_t, uint8_t media, const hash_cptr& hash, bool witness) NOEXCEPT
308308
{
309309
if (stopped(ec))
310310
return false;
@@ -317,24 +317,48 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
317317
return true;
318318
}
319319

320-
const auto ptr = query.get_inputs(tx, false);
321-
if (!ptr || ptr->empty())
320+
const auto inputs = query.get_inputs(tx, witness);
321+
if (!inputs || inputs->empty())
322322
{
323323
send_internal_server_error({}, database::error::integrity);
324324
return true;
325325
}
326326

327-
// TODO: iterate, naturally sorted by position.
327+
// Wire serialization size of inputs set.
328+
const auto size = std::accumulate(inputs->begin(), inputs->end(), zero,
329+
[&witness](size_t total, const auto& output) NOEXCEPT
330+
{ return total + output->serialized_size(witness); });
331+
328332
switch (media)
329333
{
330334
case data:
335+
{
336+
data_chunk out(size);
337+
stream::out::fast sink{ out };
338+
write::bytes::fast writer{ sink };
339+
for (const auto& output: *inputs)
340+
output->to_data(writer);
341+
342+
send_chunk({}, std::move(out));
343+
return true;
344+
}
331345
case text:
332-
send_wire(media, ptr->front()->to_data());
346+
{
347+
std::string out{};
348+
out.resize(two * size);
349+
stream::out::fast sink{ out };
350+
write::base16::fast writer{ sink };
351+
for (const auto& output: *inputs)
352+
output->to_data(writer);
353+
354+
send_text({}, std::move(out));
333355
return true;
356+
}
334357
case json:
335-
send_json({}, value_from(ptr->front()),
336-
two * ptr->front()->serialized_size(false));
358+
{
359+
send_json({}, value_from(*inputs), two * size);
337360
return true;
361+
}
338362
}
339363

340364
send_not_found({});
@@ -343,13 +367,13 @@ bool protocol_explore::handle_get_inputs(const code& ec, interface::inputs,
343367

344368
bool protocol_explore::handle_get_input(const code& ec, interface::input,
345369
uint8_t, uint8_t media, const hash_cptr& hash, uint32_t index,
346-
bool) NOEXCEPT
370+
bool witness) NOEXCEPT
347371
{
348372
if (stopped(ec))
349373
return false;
350374

351375
const auto& query = archive();
352-
if (const auto ptr = query.get_input(query.to_tx(*hash), index, false))
376+
if (const auto ptr = query.get_input(query.to_tx(*hash), index, witness))
353377
{
354378
switch (media)
355379
{
@@ -359,7 +383,7 @@ bool protocol_explore::handle_get_input(const code& ec, interface::input,
359383
return true;
360384
case json:
361385
send_json({}, value_from(ptr),
362-
two * ptr->serialized_size(false));
386+
two * ptr->serialized_size(witness));
363387
return true;
364388
}
365389
}
@@ -431,19 +455,53 @@ bool protocol_explore::handle_get_outputs(const code& ec, interface::outputs,
431455
return false;
432456

433457
const auto& query = archive();
458+
const auto tx = query.to_tx(*hash);
459+
if (tx.is_terminal())
460+
{
461+
send_not_found({});
462+
return true;
463+
}
434464

435-
// TODO: iterate, naturally sorted by position.
436-
if (const auto ptr = query.get_outputs(query.to_tx(*hash)))
465+
const auto outputs = query.get_outputs(tx);
466+
if (!outputs || outputs->empty())
437467
{
438-
switch (media)
468+
send_internal_server_error({}, database::error::integrity);
469+
return true;
470+
}
471+
472+
// Wire serialization size of outputs set.
473+
const auto size = std::accumulate(outputs->begin(), outputs->end(), zero,
474+
[](size_t total, const auto& output) NOEXCEPT
475+
{ return total + output->serialized_size(); });
476+
477+
switch (media)
478+
{
479+
case data:
439480
{
440-
case data:
441-
case text:
442-
send_wire(media, ptr->front()->to_data());
443-
return true;
444-
case json:
445-
send_json({}, value_from(ptr->front()),
446-
two * ptr->front()->serialized_size());
481+
data_chunk out(size);
482+
stream::out::fast sink{ out };
483+
write::bytes::fast writer{ sink };
484+
for (const auto& output: *outputs)
485+
output->to_data(writer);
486+
487+
send_chunk({}, std::move(out));
488+
return true;
489+
}
490+
case text:
491+
{
492+
std::string out{};
493+
out.resize(two * size);
494+
stream::out::fast sink{ out };
495+
write::base16::fast writer{ sink };
496+
for (const auto& output: *outputs)
497+
output->to_data(writer);
498+
499+
send_text({}, std::move(out));
500+
return true;
501+
}
502+
case json:
503+
{
504+
send_json({}, value_from(*outputs), two * size);
447505
return true;
448506
}
449507
}
@@ -534,7 +592,7 @@ bool protocol_explore::handle_get_output_spender(const code& ec,
534592
return true;
535593
case json:
536594
send_json({}, value_from(point),
537-
two * point.serialized_size());
595+
two * chain::point::serialized_size());
538596
return true;
539597
}
540598
}
@@ -567,11 +625,10 @@ bool protocol_explore::handle_get_output_spenders(const code& ec,
567625
{
568626
case data:
569627
case text:
570-
send_wire(media, point.to_data());
628+
send_wire(media, {});
571629
return true;
572630
case json:
573-
send_json({}, value_from(point),
574-
two * point.serialized_size());
631+
send_json({}, value_from(point), {});
575632
return true;
576633
}
577634
}
@@ -614,11 +671,10 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
614671
{
615672
case data:
616673
case text:
617-
send_wire(media, ptr->to_data());
674+
send_wire(media, {});
618675
return true;
619676
case json:
620-
send_json({}, value_from(ptr),
621-
two * ptr->serialized_size());
677+
send_json({}, value_from(*ptr), {});
622678
return true;
623679
}
624680
}
@@ -658,8 +714,8 @@ bool protocol_explore::handle_get_filter(const code& ec, interface::filter,
658714
send_wire(media, std::move(filter));
659715
return true;
660716
case json:
661-
const auto base16 = encode_base16(filter);
662-
send_json({}, value_from(base16), base16.size());
717+
send_json({}, value_from(encode_base16(filter)),
718+
two * filter.size());
663719
return true;
664720
}
665721
}
@@ -700,8 +756,8 @@ bool protocol_explore::handle_get_filter_hash(const code& ec,
700756
send_wire(media, std::move(chunk));
701757
return true;
702758
case json:
703-
const auto base16 = encode_base16(chunk);
704-
send_json({}, value_from(base16), base16.size());
759+
send_json({}, value_from(encode_base16(chunk)),
760+
two * chunk.size());
705761
return true;
706762
}
707763
}

0 commit comments

Comments
 (0)