Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 73 additions & 16 deletions include/bitcoin/database/impl/query/address/address_balance.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ namespace database {

// Address balance
// ----------------------------------------------------------------------------
// Balance queries (universal, unconfirmed conflict resolution arbitrary).
// Balance queries (unconfirmed is confirmable outputs not spent).

// unused
TEMPLATE
code CLASS::get_unconfirmed_balance(const stopper& , uint64_t& out,
const hash_digest& , size_t , bool ) const NOEXCEPT
code CLASS::get_unconfirmed_balance(const stopper& cancel, uint64_t& out,
const hash_digest& key, size_t limit, bool turbo) const NOEXCEPT
{
// While duplicates are easily filtered out, conflict resolution is murky.
// Output may have multiple directly or indirectly conflicting
// spends, and other spends and receives may not be visible.
// Unconfirmed balance is therefore inherently ambiguous.
// Given lack of tx pooling, returns zero unconfirmed.
out = zero;
outpoints outs{};
if (const auto ec = get_unconfirmed_unspent_outpoints(cancel, outs, key,
limit, turbo))
{
out = zero;
return ec;
}

// Conflicting confirmable outputs are each included.
out = std::accumulate(outs.cbegin(), outs.cend(), 0_u64,
[](uint64_t total, const outpoint& out) NOEXCEPT
{
return system::ceilinged_add(total, out.value());
});

return error::success;
}

Expand Down Expand Up @@ -72,11 +80,60 @@ code CLASS::get_balance(const stopper& cancel, uint64_t& confirmed,
uint64_t& unconfirmed, const hash_digest& key, size_t limit,
bool turbo) const NOEXCEPT
{
// See notes on get_unconfirmed_balance().
const auto ec = get_confirmed_balance(cancel, confirmed, key, limit,
turbo);
unconfirmed = zero;
return ec;
if (const auto ec = get_confirmed_balance(cancel, confirmed, key, limit,
turbo))
{
unconfirmed = zero;
return ec;
}

return get_unconfirmed_balance(cancel, unconfirmed, key, limit, turbo);
}

// Output spent by a confirmed tx is not a receipt.
TEMPLATE
code CLASS::get_unconfirmed_receipts(const stopper& cancel,
pool_receipts& out, const hash_digest& key, size_t limit) const NOEXCEPT
{
out.clear();
output_links outs{};
tx_links parents{};
outs_link cursor{};
if (const auto ec = to_address_outputs(cancel, cursor, outs, parents, key,
limit))
return ec;

for (size_t index{}; index < outs.size(); ++index)
{
if (cancel)
return error::query_canceled;

const auto& link = outs.at(index);
const auto& parent = parents.at(index);
if (!is_confirmable_tx(parent))
continue;

auto spent = false;
pool_receipt receipt{ to_pool_row(parent).value };
for (const auto& in: to_spenders(link))
{
const auto spender = to_input_tx(in);
const auto state = get_tx_state(spender);
spent |= (state == tx_state::confirmed);
if (state == tx_state::confirmable)
receipt.spenders.push_back(to_pool_row(spender).value);
}

if (spent)
continue;

if (!get_value(receipt.value, link))
return error::integrity;

out.push_back(std::move(receipt));
}

return error::success;
}

// server/esplora
Expand Down Expand Up @@ -133,7 +190,7 @@ code CLASS::get_address_totals(const stopper& cancel,
else if (is_spent(out))
spend(unconfirmed, value);
}
else
else if (is_confirmable_tx(parent))
{
fund(unconfirmed, value);
if (is_spent(out))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace database {
// root txs (height:zero) sorted before transitive (height:max) txs.
// tied-height transactions sorted by base16 txid (not converted).
// All confirmed txs are root, unconfirmed may or may not be root.
// A tx neither confirmed nor confirmable is excluded.

// server/electrum
TEMPLATE
Expand Down Expand Up @@ -140,6 +141,10 @@ history CLASS::get_tx_history(hash_digest&& key, const tx_link& link,
const auto at = get_confirmed_height(block);
if (at.is_terminal())
{
// Invalid with unconfirmed_position signals no fault.
if (!is_confirmable_tx(link))
return { .position = history::unconfirmed_position };

if (!get_tx_fee(fee, link))
fee = history::missing_prevout;

Expand Down Expand Up @@ -209,7 +214,7 @@ history CLASS::get_tx_unconfirmed_history(hash_digest&& key,
return {};

// Invalid with unconfirmed_position signals no fault.
if (!get_confirmed_height(find_strong(link)).is_terminal())
if (!is_confirmable_tx(link))
return { .position = history::unconfirmed_position };

uint64_t fee{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace database {
// Address table is populated during transaction archival.

// server/native
// Only unconfirmed outputs that are not spent (by an unconfirmed tx).
// Only confirmable outputs that are not spent.
TEMPLATE
code CLASS::get_unconfirmed_unspent_outpoints(const stopper& cancel,
outpoints& out, const hash_digest& key, size_t limit,
Expand All @@ -45,7 +45,8 @@ code CLASS::get_unconfirmed_unspent_outpoints(const stopper& cancel,
return parallel_outpoint_transform(cancel, turbo, out, links,
[this](const output_link& link, auto& cancel, auto& fail) NOEXCEPT
{
if (cancel || fail || is_confirmed_output(link) || is_spent(link))
if (cancel || fail || !is_confirmable_tx(to_output_tx(link)) ||
is_spent(link))
return outpoint{};

const auto point = get_outpoint(link);
Expand Down Expand Up @@ -122,6 +123,7 @@ code CLASS::get_minimum_unspent_outpoints(const stopper& cancel,
}

// server/native
// Confirmed and confirmable outputs.
TEMPLATE
code CLASS::get_address_outpoints(const stopper& cancel, outpoints& out,
const hash_digest& key, size_t limit, bool turbo) const NOEXCEPT
Expand All @@ -138,6 +140,11 @@ code CLASS::get_address_outpoints(const stopper& cancel, outpoints& out,
if (cancel || fail)
return outpoint{};

const auto state = get_tx_state(to_output_tx(link));
if (state != tx_state::confirmed &&
state != tx_state::confirmable)
return outpoint{};

const auto point = get_outpoint(link);
if (!point.point().is_valid())
fail = true;
Expand Down
15 changes: 8 additions & 7 deletions include/bitcoin/database/impl/query/address/address_unspent.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace database {
// Unconfirmed unspent are included at end of list in consistent order.

// ununsed
// Only unconfirmed outputs that are not spent (by an unconfirmed tx).
// Only confirmable outputs that are not spent.
TEMPLATE
code CLASS::get_unconfirmed_unspent(const stopper& cancel, unspent_outputs& out,
const hash_digest& key, size_t limit, bool turbo) const NOEXCEPT
Expand All @@ -46,9 +46,6 @@ code CLASS::get_unconfirmed_unspent(const stopper& cancel, unspent_outputs& out,
return parallel_unspent_transform(cancel, turbo, out, outs,
[this](const output_link& link, auto& cancel, auto& fail) NOEXCEPT
{
// Exclude if spent by any tx. Given output must be confirmed this
// is only unconfirmeds, but checking both via is_spent() is much
// faster than calling is_unconfirmed_spent().
if (cancel || fail || is_spent(link))
return unspent_output{};

Expand Down Expand Up @@ -85,7 +82,7 @@ code CLASS::get_confirmed_unspent(const stopper& cancel, unspent_outputs& out,
}

// server/electrum
// All outputs that are not spent by any tx (confirmed or unconfirmed).
// Confirmed and confirmable outputs that are not spent.
TEMPLATE
code CLASS::get_unspent(const stopper& cancel, unspent_outputs& out,
const hash_digest& key, size_t limit, bool turbo) const NOEXCEPT
Expand All @@ -100,7 +97,6 @@ code CLASS::get_unspent(const stopper& cancel, unspent_outputs& out,
return parallel_unspent_transform(cancel, turbo, out, outs,
[this](const output_link& link, auto& cancel, auto& fail) NOEXCEPT
{
// Exclude if spent by any tx, confirmed or unconfirmed.
if (cancel || fail || is_spent(link))
return unspent_output{};

Expand Down Expand Up @@ -138,6 +134,11 @@ unspent_output CLASS::get_tx_unspent(const output_link& link) const NOEXCEPT
if (!get_tx_position(position, strong, block))
return {};
}
else if (!is_confirmable_tx(tx))
{
// Invalid with unconfirmed_position signals no fault.
return { .position = unspent_output::unconfirmed_position };
}

return { { { std::move(hash), index }, out.value }, height, position };
}
Expand Down Expand Up @@ -180,7 +181,7 @@ unspent_output CLASS::get_tx_unconfirmed_unspent(const output_link& link) const
const auto tx = out.parent_fk;

// Invalid with unconfirmed_position signals no fault.
if (!get_confirmed_height(find_strong(tx)).is_terminal())
if (!is_confirmable_tx(tx))
return { .position = unspent_output::unconfirmed_position };

auto hash = get_tx_key(tx);
Expand Down
15 changes: 9 additions & 6 deletions include/bitcoin/database/impl/query/batch/silent.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ bool CLASS::scan_silent(const stopper& cancel,
return !cancel;
}

// Confirmation is strong association, otherwise height is zero (unconfirmed).
// Tx archived only on stale branch reports as unconfirmed, and one also
// archived there before store began pooling may report twice (benign).
// Confirmation is strong association, otherwise confirmable at height zero.
TEMPLATE
bool CLASS::get_silent_matches(silent_matches& out, const stopper& cancel,
const system::scan::batch::receiver& keys, size_t first, size_t last,
Expand Down Expand Up @@ -242,10 +240,15 @@ bool CLASS::get_silent_matches(silent_matches& out, const stopper& cancel,
for (const auto& [link, tweak]: found)
{
size_t height{};
if (!get_tx_height(height, link))
if (get_tx_height(height, link))
{
if (!is_silent_cut(link, height, start, stop, time))
out.emplace_back(height, get_tx_key(link), tweak);
}
else if (is_confirmable_tx(link))
{
out.emplace_back(zero, get_tx_key(link), tweak);
else if (!is_silent_cut(link, height, start, stop, time))
out.emplace_back(height, get_tx_key(link), tweak);
}
}

std::sort(out.begin(), out.end(),
Expand Down
22 changes: 7 additions & 15 deletions include/bitcoin/database/impl/query/confirmed.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,6 @@ bool CLASS::is_confirmed_spent(const output_link& link) const NOEXCEPT
});
}

TEMPLATE
bool CLASS::is_unconfirmed_spent(const output_link& link) const NOEXCEPT
{
// At least one spender and no spender is in a confirmed block (by height).
const auto ins = to_spenders(link);
return !ins.empty() && std::none_of(ins.cbegin(), ins.cend(),
[&](const auto& in) NOEXCEPT
{
return is_confirmed_input(in);
});
}

TEMPLATE
bool CLASS::is_spent(const output_link& link) const NOEXCEPT
{
Expand All @@ -175,9 +163,13 @@ bool CLASS::is_spent(const output_link& link) const NOEXCEPT
TEMPLATE
bool CLASS::is_spent(const point& prevout) const NOEXCEPT
{
// *Any* tx spends the point. Could even be a tx in conflict with another
// long-confirmed tx, or valid tx in invalid block.
return store_.ins.exists(prevout);
const auto ins = to_spenders(prevout);
return std::any_of(ins.cbegin(), ins.cend(), [&](const auto& in) NOEXCEPT
{
const auto state = get_tx_state(to_input_tx(in));
return state == tx_state::confirmed ||
state == tx_state::confirmable;
});
}

} // namespace database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool CLASS::populate_with_metadata(const input& input, bool chain,
bool pool) const NOEXCEPT
{
// Null point would return nullptr and be interpreted as missing.
BC_ASSERT(!input.point().is_null());
////BC_ASSERT(!input.point().is_null());

// If read via store for store confirmation, input.metadata.point_link is
// set earlier in get_input(), so this assertion does not hold.
Expand Down Expand Up @@ -120,7 +120,7 @@ bool CLASS::populate_with_metadata(const input& input, bool chain,

// Genesis coinbase is unspendable, so no confirmed spender is at zero,
// which therefore distinguishes unconfirmed spender.
if (pool && !is_spent(input.point()))
if (pool && !store_.ins.exists(input.point()))
{
// Any spender (including duplicate tx) not found.
metadata.spender_height = max_uint32;
Expand Down Expand Up @@ -194,7 +194,7 @@ bool CLASS::populate_without_metadata(const block& block) const NOEXCEPT
TEMPLATE
bool CLASS::populate_without_metadata(const transaction& tx) const NOEXCEPT
{
BC_ASSERT(!tx.is_coinbase());
////BC_ASSERT(!tx.is_coinbase());

const auto& ins = tx.inputs_ptr();
return std::all_of(ins->begin(), ins->end(),
Expand All @@ -208,7 +208,7 @@ TEMPLATE
bool CLASS::populate_without_metadata(const input& input) const NOEXCEPT
{
// Null point would return nullptr and be interpreted as missing.
BC_ASSERT(!input.point().is_null());
////BC_ASSERT(!input.point().is_null());

if (input.prevout)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,25 @@ bool CLASS::get_block_prevouts(data_chunk& prevouts, prevout_spends& spends,
// ----------------------------------------------------------------------------
// protected

// Each instance of a tx archives its own points, so spenders of the point
// include every tx with the hash of any of them.
TEMPLATE
bool CLASS::get_doubles(tx_links& out, const point& point) const NOEXCEPT
{
if (!store_.duplicate.exists(point))
return true;

// Get the [tx.hash:index] of each spender of the point (index unused).
const auto spenders = get_spenders(point);
bool found{};

// Inpoints are deduped by the std::set.
for (const auto& spender: spenders)
const auto spenders = to_spenders(point);
for (const auto& in: spenders)
{
// Exhaustive enumeration of all txs with spender hash.
for (auto it = store_.tx.it(spender.hash()); it != it.end(); ++it)
{
found = true;
out.push_back(*it);
}
const auto tx = to_input_tx(in);
if (tx.is_terminal())
return false;

out.push_back(tx.value);
}

return found;
return !spenders.empty();
}

TEMPLATE
Expand Down
Loading
Loading