Skip to content

Commit 6c219e8

Browse files
authored
Merge pull request #761 from evoskuil/master
Stub in get_unspent().
2 parents 29c43ab + cf23e3f commit 6c219e8

5 files changed

Lines changed: 103 additions & 32 deletions

File tree

include/bitcoin/database/impl/query/address/address_outpoints.ipp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ code CLASS::parallel_outpoint_transform(const stopper& cancel, bool turbo,
131131
stopper fail{};
132132

133133
out.clear();
134-
std::vector<outpoint> outpoints(links.size());
134+
out.resize(links.size());
135135

136-
std::transform(policy, links.cbegin(), links.cend(), outpoints.begin(),
136+
std::transform(policy, links.cbegin(), links.cend(), out.begin(),
137137
[&functor, &cancel, &fail](const auto& link) NOEXCEPT
138138
{
139139
return functor(link, cancel, fail);
@@ -145,17 +145,16 @@ code CLASS::parallel_outpoint_transform(const stopper& cancel, bool turbo,
145145
if (cancel)
146146
return error::canceled;
147147

148-
// TODO: change outpoints to vector and avoid copy.
149-
for (auto& outpoint: outpoints)
150-
{
151-
if (cancel)
152-
return error::canceled;
153-
154-
// Filter out non-failures.
155-
if (!outpoint.point().is_null())
156-
out.insert(std::move(outpoint));
157-
}
148+
// Remove default/null points.
149+
out.erase(std::remove_if(out.begin(), out.end(),
150+
[](const auto& outpoint) NOEXCEPT
151+
{
152+
return outpoint.point().is_null();
153+
}), out.end());
158154

155+
// Sort (arbitrary - by index and then binary hash) and remove duplicates.
156+
std::sort(out.begin(), out.end());
157+
out.erase(std::unique(out.begin(), out.end()), out.end());
159158
return error::success;
160159
}
161160

include/bitcoin/database/impl/query/address/address_unspent.ipp

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,100 @@ namespace database {
3434

3535
// ununsed
3636
TEMPLATE
37-
code CLASS::get_unconfirmed_unspent(const stopper& , unspents& ,
38-
const hash_digest& , bool ) const NOEXCEPT
37+
code CLASS::get_unconfirmed_unspent(const stopper& cancel, unspents& out,
38+
const hash_digest& key, bool turbo) const NOEXCEPT
3939
{
40-
return {};
40+
output_links outs{};
41+
if (const auto ec = to_address_outputs(cancel, outs, key))
42+
return ec;
43+
44+
out.clear();
45+
out.resize(outs.size());
46+
return parallel_unspent_transform(cancel, turbo, out, outs,
47+
[this](const auto& , auto& cancel, auto& fail) NOEXCEPT -> unspent
48+
{
49+
if (cancel || fail)
50+
return {};
51+
52+
// TODO: return unconfirmed unspent outputs for address key.
53+
return {};
54+
});
4155
}
4256

4357
// ununsed
4458
TEMPLATE
45-
code CLASS::get_confirmed_unspent(const stopper& , unspents& ,
46-
const hash_digest& , bool ) const NOEXCEPT
59+
code CLASS::get_confirmed_unspent(const stopper& cancel, unspents& out,
60+
const hash_digest& key, bool turbo) const NOEXCEPT
4761
{
48-
return {};
62+
output_links outs{};
63+
if (const auto ec = to_address_outputs(cancel, outs, key))
64+
return ec;
65+
66+
out.clear();
67+
out.resize(outs.size());
68+
return parallel_unspent_transform(cancel, turbo, out, outs,
69+
[this](const auto& , auto& cancel, auto& fail) NOEXCEPT -> unspent
70+
{
71+
if (cancel || fail)
72+
return {};
73+
74+
// TODO: return confirmed unspent outputs for address key.
75+
return {};
76+
});
4977
}
5078

5179
// server/electrum
5280
TEMPLATE
53-
code CLASS::get_unspent(const stopper& , unspents& ,
54-
const hash_digest& , bool ) const NOEXCEPT
81+
code CLASS::get_unspent(const stopper& cancel, unspents& out,
82+
const hash_digest& key, bool turbo) const NOEXCEPT
5583
{
56-
return {};
84+
output_links outs{};
85+
if (const auto ec = to_address_outputs(cancel, outs, key))
86+
return ec;
87+
88+
out.clear();
89+
out.resize(outs.size());
90+
return parallel_unspent_transform(cancel, turbo, out, outs,
91+
[this](const auto& , auto& cancel, auto& fail) NOEXCEPT -> unspent
92+
{
93+
if (cancel || fail)
94+
return {};
95+
96+
// TODO: return unspent outputs for address key.
97+
return {};
98+
});
5799
}
58100

59-
// turbos
101+
// utilities
60102
// ----------------------------------------------------------------------------
61-
// protected
103+
// private/static
104+
105+
TEMPLATE
106+
template <typename Functor>
107+
code CLASS::parallel_unspent_transform(const stopper& cancel, bool turbo,
108+
unspents& out, const output_links& outs, Functor&& functor) NOEXCEPT
109+
{
110+
const auto policy = poolstl::execution::par_if(turbo);
111+
stopper fail{};
112+
113+
out.clear();
114+
out.resize(outs.size());
115+
std::transform(policy, outs.cbegin(), outs.cend(), out.begin(),
116+
[&functor, &cancel, &fail](const auto& output) NOEXCEPT
117+
{
118+
return functor(output, cancel, fail);
119+
});
120+
121+
if (fail)
122+
return error::integrity;
123+
124+
if (cancel)
125+
return error::canceled;
126+
127+
unspent::sort_and_dedup(out);
128+
return error::success;
129+
}
130+
62131

63132
} // namespace database
64133
} // namespace libbitcoin

include/bitcoin/database/impl/query/archive/chain_reader.ipp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef LIBBITCOIN_DATABASE_QUERY_ARCHIVE_CHAIN_READER_IPP
2020
#define LIBBITCOIN_DATABASE_QUERY_ARCHIVE_CHAIN_READER_IPP
2121

22+
#include <algorithm>
2223
#include <utility>
2324
#include <bitcoin/database/define.hpp>
2425

@@ -368,9 +369,11 @@ inpoints CLASS::get_spenders(const point& point) const NOEXCEPT
368369
{
369370
inpoints ins{};
370371
for (const auto& point_fk: to_spenders(point))
371-
ins.insert(get_spender(point_fk));
372+
ins.push_back(get_spender(point_fk));
372373

373-
// std::set (lexically sorted/deduped).
374+
// Sort (arbitrary - by index and then binary hash) and remove duplicates.
375+
std::sort(ins.begin(), ins.end());
376+
ins.erase(std::unique(ins.begin(), ins.end()), ins.end());
374377
return ins;
375378
}
376379

include/bitcoin/database/query.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,9 @@ class query
826826
template <typename Functor>
827827
static code parallel_history_transform(const stopper& cancel, bool turbo,
828828
histories& out, const tx_links& links, Functor&& functor) NOEXCEPT;
829+
template <typename Functor>
830+
static code parallel_unspent_transform(const stopper& cancel, bool turbo,
831+
unspents& out, const output_links& outs, Functor&& functor) NOEXCEPT;
829832

830833
static point::cptr make_point(hash_digest&& hash,
831834
uint32_t index) NOEXCEPT;

include/bitcoin/database/types.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include <atomic>
2323
#include <optional>
24-
#include <set>
2524
#include <utility>
2625
#include <bitcoin/database/define.hpp>
2726
#include <bitcoin/database/tables/tables.hpp>
@@ -70,13 +69,11 @@ using data_chunk = system::data_chunk;
7069
/// Common system::chain aliases.
7170
/// ---------------------------------------------------------------------------
7271

73-
using checkpoint = system::chain::checkpoint;
74-
using outpoint = system::chain::outpoint;
7572
using inpoint = system::chain::point;
76-
77-
/// Sorted and deduped.
78-
using outpoints = std::set<outpoint>;
79-
using inpoints = std::set<inpoint>;
73+
using outpoint = system::chain::outpoint;
74+
using inpoints = std::vector<inpoint>;
75+
using outpoints = std::vector<outpoint>;
76+
using checkpoint = system::chain::checkpoint;
8077

8178
/// Common carriers.
8279
/// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)