Skip to content

Commit 97d1939

Browse files
authored
Merge pull request #906 from evoskuil/master
Generalize inpoints/outputs, push get_spenders to database.
2 parents 383ef5a + 8aaad5a commit 97d1939

File tree

2 files changed

+37
-50
lines changed

2 files changed

+37
-50
lines changed

include/bitcoin/node/protocols/protocol_explore.hpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@
2222
#include <atomic>
2323
#include <memory>
2424
#include <optional>
25-
#include <set>
2625
#include <bitcoin/node/define.hpp>
2726
#include <bitcoin/node/protocols/protocol_html.hpp>
2827

2928
namespace libbitcoin {
3029
namespace node {
3130

32-
// TODO: establish a place for endpoint types.
33-
using point_set = std::set<system::chain::point>;
34-
using outpoint_set = std::set<system::chain::outpoint>;
35-
3631
class BCN_API protocol_explore
3732
: public node::protocol_html,
3833
protected network::tracker<protocol_explore>
@@ -152,7 +147,8 @@ class BCN_API protocol_explore
152147

153148
private:
154149
using balance_handler = std::function<void(code, uint8_t, uint64_t)>;
155-
using address_handler = std::function<void(code, uint8_t, outpoint_set&&)>;
150+
using address_handler = std::function<void(code, uint8_t,
151+
database::outpoints&&)>;
156152

157153
void do_get_address(uint8_t media, const system::hash_cptr& hash,
158154
const address_handler& handler) NOEXCEPT;
@@ -161,9 +157,8 @@ class BCN_API protocol_explore
161157
void do_get_address_unconfirmed(uint8_t media,
162158
const system::hash_cptr& hash,
163159
const address_handler& handler) NOEXCEPT;
164-
165160
void complete_get_address(const code& ec, uint8_t media,
166-
const outpoint_set& set) NOEXCEPT;
161+
const database::outpoints& set) NOEXCEPT;
167162

168163
void do_get_address_balance(uint8_t media, const system::hash_cptr& hash,
169164
const balance_handler& handler) NOEXCEPT;

src/protocols/protocol_explore.cpp

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,25 @@ using namespace network::messages::peer;
4040
using namespace std::placeholders;
4141
using namespace boost::json;
4242

43-
DEFINE_JSON_TO_TAG(point_set)
43+
using inpoint = database::inpoint;
44+
using outpoint = database::outpoint;
45+
using inpoints = database::inpoints;
46+
using outpoints = database::outpoints;
47+
48+
DEFINE_JSON_TO_TAG(inpoints)
4449
{
45-
point_set out{};
46-
for (const auto& point : value.as_array())
47-
out.insert(value_to<chain::point>(point));
50+
inpoints out{};
51+
for (const auto& point: value.as_array())
52+
out.insert(value_to<inpoint>(point));
4853

4954
return out;
5055
}
5156

52-
DEFINE_JSON_TO_TAG(outpoint_set)
57+
DEFINE_JSON_TO_TAG(outpoints)
5358
{
54-
outpoint_set out{};
55-
for (const auto& outpoint : value.as_array())
56-
out.insert(value_to<chain::outpoint>(outpoint));
59+
outpoints out{};
60+
for (const auto& point: value.as_array())
61+
out.insert(value_to<outpoint>(point));
5762

5863
return out;
5964
}
@@ -869,17 +874,10 @@ bool protocol_explore::handle_get_output_spender(const code& ec,
869874

870875
const auto& query = archive();
871876
const chain::point spent{ *hash, index };
872-
const auto link = query.to_confirmed_spender(spent);
873-
if (link.is_terminal())
874-
{
875-
send_not_found();
876-
return true;
877-
}
878-
879-
const auto spender = query.get_spender(link);
877+
const auto spender = query.get_spender(query.to_confirmed_spender(spent));
880878
if (spender.index() == chain::point::null_index)
881879
{
882-
send_internal_server_error(database::error::integrity);
880+
send_not_found();
883881
return true;
884882
}
885883

@@ -908,29 +906,24 @@ bool protocol_explore::handle_get_output_spenders(const code& ec,
908906
if (stopped(ec))
909907
return false;
910908

911-
const auto& query = archive();
912-
const auto points = query.to_spenders(*hash, index);
913-
if (points.empty())
909+
const auto ins = archive().get_spenders({ *hash, index });
910+
if (ins.empty())
914911
{
915912
send_not_found();
916913
return true;
917914
}
918915

919-
point_set out{};
920-
for (const auto& point: points)
921-
out.insert(query.get_spender(point));
922-
923-
const auto size = out.size() * chain::point::serialized_size();
916+
const auto size = ins.size() * inpoint::serialized_size();
924917
switch (media)
925918
{
926919
case data:
927-
send_chunk(to_bin_array(out, size));
920+
send_chunk(to_bin_array(ins, size));
928921
return true;
929922
case text:
930-
send_text(to_hex_array(out, size));
923+
send_text(to_hex_array(ins, size));
931924
return true;
932925
case json:
933-
send_json(value_from(out), two * size);
926+
send_json(value_from(ins), two * size);
934927
return true;
935928
}
936929

@@ -962,29 +955,28 @@ bool protocol_explore::handle_get_address(const code& ec, interface::address,
962955
void protocol_explore::do_get_address(uint8_t media, const hash_cptr& hash,
963956
const address_handler& handler) NOEXCEPT
964957
{
965-
// Not stranded, query is threadsafe.
966958
const auto& query = archive();
967959

968-
// TODO: push into database as single call, generalize outpoint_set.
960+
// TODO: push into database as single call.
969961
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
970962
// TODO: change query to return code to differentiate cancel vs. integrity.
971-
database::output_links outputs{};
972-
if (!query.to_address_outputs(stopping_, outputs, *hash))
963+
database::output_links links{};
964+
if (!query.to_address_outputs(stopping_, links, *hash))
973965
{
974966
handler(network::error::operation_canceled, {}, {});
975967
return;
976968
}
977969

978-
outpoint_set set{};
979-
for (const auto& output: outputs)
970+
outpoints set{};
971+
for (const auto& link: links)
980972
{
981973
if (stopping_)
982974
{
983975
handler(network::error::operation_canceled, {}, {});
984976
return;
985977
}
986978

987-
set.insert(query.get_spent(output));
979+
set.insert(query.get_spent(link));
988980
}
989981
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
990982

@@ -993,7 +985,7 @@ void protocol_explore::do_get_address(uint8_t media, const hash_cptr& hash,
993985

994986
// This is shared by the tree get_address.. methods.
995987
void protocol_explore::complete_get_address(const code& ec, uint8_t media,
996-
const outpoint_set& set) NOEXCEPT
988+
const outpoints& set) NOEXCEPT
997989
{
998990
BC_ASSERT(stranded());
999991

@@ -1056,26 +1048,26 @@ void protocol_explore::do_get_address_confirmed(uint8_t media,
10561048
{
10571049
const auto& query = archive();
10581050

1059-
// TODO: push into database as single call, generalize outpoint_set.
1051+
// TODO: push into database as single call.
10601052
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10611053
// TODO: change query to return code to differentiate cancel vs. integrity.
1062-
database::output_links outputs{};
1063-
if (!query.to_confirmed_unspent_outputs(stopping_, outputs, *hash))
1054+
database::output_links links{};
1055+
if (!query.to_confirmed_unspent_outputs(stopping_, links, *hash))
10641056
{
10651057
handler(network::error::operation_canceled, {}, {});
10661058
return;
10671059
}
10681060

1069-
outpoint_set set{};
1070-
for (const auto& output : outputs)
1061+
outpoints set{};
1062+
for (const auto& link: links)
10711063
{
10721064
if (stopping_)
10731065
{
10741066
handler(network::error::operation_canceled, {}, {});
10751067
return;
10761068
}
10771069

1078-
set.insert(query.get_spent(output));
1070+
set.insert(query.get_spent(link));
10791071
}
10801072
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10811073

0 commit comments

Comments
 (0)