Skip to content

Commit f21376e

Browse files
committed
Implement handle_get_output_spenders().
1 parent 46106b3 commit f21376e

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/protocols/protocol_explore.cpp

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,10 @@ bool protocol_explore::handle_get_block(const code& ec, interface::block,
155155
return true;
156156
}
157157
case json:
158+
{
158159
send_json(value_from(block), two * size);
159160
return true;
161+
}
160162
}
161163
}
162164

@@ -259,8 +261,10 @@ bool protocol_explore::handle_get_block_tx(const code& ec, interface::block_tx,
259261
return true;
260262
}
261263
case json:
264+
{
262265
send_json(value_from(tx), two * size);
263266
return true;
267+
}
264268
}
265269
}
266270

@@ -300,8 +304,10 @@ bool protocol_explore::handle_get_transaction(const code& ec,
300304
return true;
301305
}
302306
case json:
307+
{
303308
send_json(value_from(tx), two * size);
304309
return true;
310+
}
305311
}
306312
}
307313

@@ -531,8 +537,7 @@ bool protocol_explore::handle_get_outputs(const code& ec, interface::outputs,
531537
}
532538
case text:
533539
{
534-
std::string out{};
535-
out.resize(two * size);
540+
std::string out(two * size, '\0');
536541
stream::out::fast sink{ out };
537542
write::base16::fast writer{ sink };
538543
for (const auto& output: *outputs)
@@ -652,28 +657,51 @@ bool protocol_explore::handle_get_output_spenders(const code& ec,
652657
if (stopped(ec))
653658
return false;
654659

655-
// TODO: iterate, sort by height/position/index.
656660
const auto& query = archive();
657-
const auto spenders = query.to_spenders(*hash, index);
658-
if (spenders.empty())
661+
const auto links = query.to_spenders(*hash, index);
662+
if (links.empty())
659663
{
660664
send_not_found();
661665
return true;
662666
}
663667

664-
// TODO: iterate, sort in query.
665-
if (const auto point = query.get_point(spenders.front());
666-
point.hash() != null_hash)
668+
const auto size = links.size() * chain::point::serialized_size();
669+
chain::points points(links.size());
670+
std::ranges::transform(links, points.begin(),
671+
[&](const auto& link) NOEXCEPT { return query.get_point(link); });
672+
673+
// TODO: dedup and sort by height/position/index in query.
674+
if (true)
667675
{
668676
switch (media)
669677
{
670678
case data:
679+
{
680+
data_chunk out(size);
681+
stream::out::fast sink{ out };
682+
write::bytes::fast writer{ sink };
683+
for (const auto& point: points)
684+
point.to_data(writer);
685+
686+
send_chunk(std::move(out));
687+
return true;
688+
}
671689
case text:
672-
send_wire(media, {});
690+
{
691+
std::string out(two * size, '\0');
692+
stream::out::fast sink{ out };
693+
write::base16::fast writer{ sink };
694+
for (const auto& point: points)
695+
point.to_data(writer);
696+
697+
send_text(std::move(out));
673698
return true;
699+
}
674700
case json:
675-
send_json(value_from(point), {});
701+
{
702+
send_json(value_from(points), two * size);
676703
return true;
704+
}
677705
}
678706
}
679707

@@ -694,7 +722,6 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
694722
return true;
695723
}
696724

697-
// TODO: iterate, sort by height/position/index.
698725
database::output_links outputs{};
699726
if (!query.to_address_outputs(outputs, *hash))
700727
{
@@ -708,7 +735,7 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
708735
return true;
709736
}
710737

711-
// TODO: iterate, sort in query.
738+
// TODO: dedup and sort by height/position/index in query.
712739
if (const auto ptr = query.get_output(outputs.front()))
713740
{
714741
switch (media)

0 commit comments

Comments
 (0)