Skip to content

Commit e42ae4a

Browse files
committed
Ensure balance accumulation uses de-duplicated outpoints.
1 parent f1ef7d7 commit e42ae4a

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

include/bitcoin/database/impl/query/optional.ipp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,21 @@ code CLASS::to_minimum_unspent_outputs(const std::atomic_bool& cancel,
106106

107107
TEMPLATE
108108
code CLASS::get_confirmed_balance(const std::atomic_bool& cancel,
109-
uint64_t& out, const hash_digest& key) const NOEXCEPT
109+
uint64_t& balance, const hash_digest& key) const NOEXCEPT
110110
{
111-
out = zero;
112-
for (auto it = store_.address.it(key); it; ++it)
111+
outpoints outs{};
112+
if (code ec = to_confirmed_unspent_outputs(cancel, outs, key))
113113
{
114-
if (cancel)
115-
return error::canceled;
116-
117-
table::address::record address{};
118-
if (!store_.address.get(it, address))
119-
return error::integrity;
114+
balance = zero;
115+
return ec;
116+
}
120117

121-
if (is_confirmed_unspent(address.output_fk))
118+
// Use of to_confirmed_unspent_outputs() provides necessary deduplication.
119+
balance = std::accumulate(outs.begin(), outs.end(), zero,
120+
[](size_t total, const outpoint& out) NOEXCEPT
122121
{
123-
uint64_t value{};
124-
if (!get_value(value, address.output_fk))
125-
return error::integrity;
126-
127-
out = system::ceilinged_add(value, out);
128-
}
129-
}
122+
return system::ceilinged_add(total, out.value());
123+
});
130124

131125
return error::success;
132126
}

include/bitcoin/database/query.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ class query
556556
code to_minimum_unspent_outputs(const std::atomic_bool& cancel,
557557
outpoints& out, const hash_digest& key, uint64_t value) const NOEXCEPT;
558558
code get_confirmed_balance(const std::atomic_bool& cancel,
559-
uint64_t& out, const hash_digest& key) const NOEXCEPT;
559+
uint64_t& balance, const hash_digest& key) const NOEXCEPT;
560560

561561
bool is_filtered_body(const header_link& link) const NOEXCEPT;
562562
bool get_filter_body(filter& out, const header_link& link) const NOEXCEPT;

0 commit comments

Comments
 (0)