From 50c39932a0c6901941f2e57c47788a05dae7abee Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sat, 28 Nov 2026 03:35:00 -0500 Subject: [PATCH 1/4] Disable populate asserts reachable from block data. --- .../database/impl/query/consensus/consensus_populate.ipp | 6 +++--- test/query/consensus/consensus_chain_state.cpp | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp b/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp index 5e04bd4ba..bae73a3d2 100644 --- a/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp +++ b/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp @@ -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. @@ -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(), @@ -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; diff --git a/test/query/consensus/consensus_chain_state.cpp b/test/query/consensus/consensus_chain_state.cpp index 7d291ff8a..d52f5662c 100644 --- a/test/query/consensus/consensus_chain_state.cpp +++ b/test/query/consensus/consensus_chain_state.cpp @@ -34,7 +34,8 @@ BC_AUTO_TEST_CASE(query_consensus__get_candidate_chain_state__genesis__expected) 0u, 0u, 0u, - test::genesis.header().timestamp() + test::genesis.header().timestamp(), + test::genesis.header().version() }; database::settings database_settings{ test::database_settings() }; @@ -68,7 +69,8 @@ BC_AUTO_TEST_CASE(query_consensus__get_candidate_chain_state__block1__expected) 0u, // minimum_block_version 486604799u, // work_required test::genesis.header().timestamp(), // previous timestamp - test::genesis.header().timestamp() // retarget timestamp + test::genesis.header().timestamp(), // retarget timestamp + test::block1.header().version() // version }; // Not actually contributory. From 7be7bd744e048c13bfe37573368c4322a659a116 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sat, 28 Nov 2026 05:01:06 -0500 Subject: [PATCH 2/4] Find conflicts of pooled block txs by rival ring. --- .../database/impl/query/pool/pool_index.ipp | 74 +++++++++++-------- include/bitcoin/database/query.hpp | 2 +- test/query/pool/pool_index.cpp | 34 +++++++++ 3 files changed, 79 insertions(+), 31 deletions(-) diff --git a/include/bitcoin/database/impl/query/pool/pool_index.ipp b/include/bitcoin/database/impl/query/pool/pool_index.ipp index 63c0991a5..0131279da 100644 --- a/include/bitcoin/database/impl/query/pool/pool_index.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_index.ipp @@ -312,26 +312,28 @@ bool CLASS::get_pooled_spender(system::pool::algorithm::link_t& out, } // protected -// First tx is coinbase, which spends nothing. +// Point archived with one spender is not in duplicate table. TEMPLATE bool CLASS::get_pool_conflicts(tx_links& out, const tx_links& txs) const NOEXCEPT { - out.clear(); - if (is_zero(store_.duplicate.body_size())) - return true; - - for (auto tx = std::next(txs.cbegin()); tx != txs.cend(); ++tx) + for (const auto tx: txs) { table::transaction::get_puts record{}; - if (!store_.tx.get(*tx, record)) + if (!store_.tx.get(tx, record)) return false; // Point links are contiguous (computed). const auto end = record.points_fk + record.ins_count; for (auto fk = record.points_fk; fk < end; ++fk) - if (!get_doubles(out, get_point_key(fk))) - return false; + { + const auto point = get_point_key(fk); + if (!store_.duplicate.exists(point)) + continue; + + for (const auto& in: to_spenders(point)) + out.push_back(to_input_tx(in).value); + } } return true; @@ -357,11 +359,11 @@ bool CLASS::is_pool_conflicted(bool& out, const tx_link& link, return true; } -// Conflicts of block's txs are archived spenders of its points -// as of transition, as a tx may be pooled after block's prevouts -// are cached. They include block's own txs, as each spends a -// conflicted point. Conflict is cleared at reorganization only -// if no other confirmed tx spends a point of row. +// Conflicts of block's txs are pooled spenders of its points as of +// transition, as a tx may be pooled after block's prevouts are cached: +// rivals of its pooled txs by ring, and archived spenders of points of +// its other txs. Conflict is cleared at reorganization only if no other +// confirmed tx spends a point of row. TEMPLATE bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT { @@ -382,17 +384,40 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT if (!store_.txs.at(to_txs(link), txs) || txs.tx_fks.empty()) return false; + pool_guards guards{}; + algorithm::columns_t columns{}; + if (!get_pool_columns(columns, guards)) + return false; + + const auto beyond = [&](link_t row) NOEXCEPT + { + return row >= columns.cells.size(); + }; + + // First tx is coinbase, which is not pooled and spends nothing. pool_links block{}; - for (const auto fk: txs.tx_fks) + tx_links unpooled{}; + for (auto fk = std::next(txs.tx_fks.cbegin()); fk != txs.tx_fks.cend(); + ++fk) { - const auto row = store_.pool.first(tx_link{ fk }); - if (!row.is_terminal() && row.value < count) + const auto row = store_.pool.first(tx_link{ *fk }); + if (row.is_terminal() || row.value >= count || beyond(row.value)) + unpooled.push_back(*fk); + else block.push_back(possible_narrow_cast(row.value)); } tx_links spenders{}; - if (!get_pool_conflicts(spenders, txs.tx_fks)) - return false; + if (!is_zero(store_.duplicate.body_size())) + { + pool_links rivals{}; + algorithm::rivals(rivals, columns, block); + for (const auto row: rivals) + spenders.push_back(tx_link{ store_.pool.get_key(row) }.value); + + if (!get_pool_conflicts(spenders, unpooled)) + return false; + } auto own = txs.tx_fks; std::ranges::sort(own); @@ -416,17 +441,6 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT conflicts.push_back(possible_narrow_cast(row.value)); } - pool_guards guards{}; - algorithm::columns_t columns{}; - if (!get_pool_columns(columns, guards)) - return false; - - const auto beyond = [&](link_t row) NOEXCEPT - { - return row >= columns.cells.size(); - }; - - std::erase_if(block, beyond); std::erase_if(conflicts, beyond); const auto complete = confirmed ? algorithm::confirm(columns, block, conflicts) : diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index a75dc1c2b..ede1358f4 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -1320,7 +1320,7 @@ class query system::pool::algorithm::link_t bound=system::pool::algorithm::none) const NOEXCEPT; - /// Pooled spenders of points spent by txs (their conflicts). + /// Archived spenders of points spent by txs (txs among them). bool get_pool_conflicts(tx_links& out, const tx_links& txs) const NOEXCEPT; /// A point of pooled tx is spent by a confirmed tx. diff --git a/test/query/pool/pool_index.cpp b/test/query/pool/pool_index.cpp index 66cfb4dc4..863aa5281 100644 --- a/test/query/pool/pool_index.cpp +++ b/test/query/pool/pool_index.cpp @@ -172,6 +172,40 @@ BC_AUTO_TEST_CASE(query_pool__set_pool_block__rival_pooled_after_prevout_cache__ BC_REQUIRE_EQUAL(pool_cell(query, store, rival), absent_conflicted); } +// The spender is pooled and its rival pooled after the block's prevouts are cached. +BC_AUTO_TEST_CASE(query_pool__set_pool_block__pooled_spender_rival_pooled_after_prevout_cache__excluded) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto spender = to_spend(genesis_coinbase(), 4'999'998'500); + const auto rival = to_spend(genesis_coinbase(), 4'999'999'000); + const auto pooled_spender = pool_populated(query, spender); + const auto block = test::to_block(spender); + const auto stored = query.set(block, context{ 0, 1, 0 }, {}, false, true); + const auto link = query.to_header(block.hash()); + const auto cached = query.populate_with_metadata(block) && query.set_prevouts(link, block); + const auto pooled_rival = pool_populated(query, rival); + const auto before = pool_cell(query, store, rival); + const auto pushed = query.push_candidate(link) && query.push_confirmed(link, true); + const auto applied = query.set_pool_block(link, true); + + BC_REQUIRE(pooled_spender); + BC_REQUIRE(stored); + BC_REQUIRE(cached); + BC_REQUIRE(pooled_rival); + BC_REQUIRE(system::pool::algorithm::is_present(before)); + BC_REQUIRE(pushed); + BC_REQUIRE(applied); + BC_REQUIRE_EQUAL(pool_cell(query, store, spender), system::pool::algorithm::boundary); + BC_REQUIRE_EQUAL(pool_cell(query, store, rival), system::bit_or(absent_rivaled, system::pool::algorithm::conflicted)); +} + BC_AUTO_TEST_CASE(query_pool__set_pool_block__unpooled_rival_no_prevout_cache__excluded_and_revived) { settings settings{ test::database_settings() }; From d4e45e3bf67618ea286abbb492c76ca1bb87bb96 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sat, 28 Nov 2026 05:35:19 -0500 Subject: [PATCH 3/4] Add tx existence classes and unconfirmed range. --- .../impl/query/address/address_balance.ipp | 89 +++++- .../impl/query/address/address_history.ipp | 7 +- .../impl/query/address/address_outpoints.ipp | 11 +- .../impl/query/address/address_unspent.ipp | 15 +- .../database/impl/query/batch/silent.ipp | 15 +- .../bitcoin/database/impl/query/confirmed.ipp | 22 +- .../query/consensus/consensus_populate.ipp | 2 +- .../impl/query/pool/pool_confirmable.ipp | 162 ++++++---- .../database/impl/query/pool/pool_reader.ipp | 27 ++ .../database/impl/store/store_prune.ipp | 12 +- include/bitcoin/database/query.hpp | 60 ++-- include/bitcoin/database/types/pooled_tx.hpp | 11 + include/bitcoin/database/types/type.hpp | 11 +- test/mocks/pooling.cpp | 13 + test/mocks/pooling.hpp | 7 + test/query/address/address_balance.cpp | 247 +++++++++++++++- test/query/address/address_history.cpp | 278 ++++++++++++------ test/query/address/address_outpoints.cpp | 95 +++++- test/query/address/address_unspent.cpp | 143 +++++---- test/query/batch/silent.cpp | 35 ++- 20 files changed, 971 insertions(+), 291 deletions(-) diff --git a/include/bitcoin/database/impl/query/address/address_balance.ipp b/include/bitcoin/database/impl/query/address/address_balance.ipp index 6702c5902..f515d1926 100644 --- a/include/bitcoin/database/impl/query/address/address_balance.ipp +++ b/include/bitcoin/database/impl/query/address/address_balance.ipp @@ -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; } @@ -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 @@ -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)) diff --git a/include/bitcoin/database/impl/query/address/address_history.ipp b/include/bitcoin/database/impl/query/address/address_history.ipp index 88ac4ecbf..d971e6877 100644 --- a/include/bitcoin/database/impl/query/address/address_history.ipp +++ b/include/bitcoin/database/impl/query/address/address_history.ipp @@ -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 @@ -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; @@ -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{}; diff --git a/include/bitcoin/database/impl/query/address/address_outpoints.ipp b/include/bitcoin/database/impl/query/address/address_outpoints.ipp index bbc27d6f5..2ea0eccc1 100644 --- a/include/bitcoin/database/impl/query/address/address_outpoints.ipp +++ b/include/bitcoin/database/impl/query/address/address_outpoints.ipp @@ -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, @@ -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); @@ -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 @@ -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; diff --git a/include/bitcoin/database/impl/query/address/address_unspent.ipp b/include/bitcoin/database/impl/query/address/address_unspent.ipp index 5fda2d81f..9be423cd9 100644 --- a/include/bitcoin/database/impl/query/address/address_unspent.ipp +++ b/include/bitcoin/database/impl/query/address/address_unspent.ipp @@ -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 @@ -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{}; @@ -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 @@ -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{}; @@ -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 }; } @@ -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); diff --git a/include/bitcoin/database/impl/query/batch/silent.ipp b/include/bitcoin/database/impl/query/batch/silent.ipp index 97f93a157..48aedce99 100644 --- a/include/bitcoin/database/impl/query/batch/silent.ipp +++ b/include/bitcoin/database/impl/query/batch/silent.ipp @@ -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, @@ -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(), diff --git a/include/bitcoin/database/impl/query/confirmed.ipp b/include/bitcoin/database/impl/query/confirmed.ipp index 9200fdced..7d97f7330 100644 --- a/include/bitcoin/database/impl/query/confirmed.ipp +++ b/include/bitcoin/database/impl/query/confirmed.ipp @@ -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 { @@ -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 diff --git a/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp b/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp index bae73a3d2..75826cd2d 100644 --- a/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp +++ b/include/bitcoin/database/impl/query/consensus/consensus_populate.ipp @@ -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; diff --git a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp index 007af0c73..79a44647c 100644 --- a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp @@ -24,22 +24,12 @@ namespace libbitcoin { namespace database { -TEMPLATE -bool CLASS::is_confirmable_tx(const tx_link& link) const NOEXCEPT -{ - confirmables memo{}; - return is_confirmable_tx(memo, link); -} - TEMPLATE bool CLASS::is_confirmable_spent(const output_link& link) const NOEXCEPT { for (const auto& in: to_spenders(link)) - { - const auto tx = to_input_tx(in); - if (is_pooled(tx) && is_confirmable_tx(tx)) + if (is_confirmable_tx(to_input_tx(in))) return true; - } return false; } @@ -48,11 +38,8 @@ TEMPLATE ins_link CLASS::find_confirmable_spender(const point& prevout) const NOEXCEPT { for (const auto& in: to_spenders(prevout)) - { - const auto tx = to_input_tx(in); - if (is_pooled(tx) && is_confirmable_tx(tx)) + if (is_confirmable_tx(to_input_tx(in))) return in; - } return {}; } @@ -65,8 +52,8 @@ tx_links CLASS::to_confirmable_parents(const tx_link& link) const NOEXCEPT for (const auto& point: to_points(link)) { const auto parent = to_tx(get_point_key(point).hash()); - if (!parent.is_terminal() && !is_confirmed_tx(parent) && - is_pooled(parent) && !system::contains(out, parent.value)) + if (!parent.is_terminal() && is_confirmable_tx(parent) && + !system::contains(out, parent.value)) out.push_back(parent.value); } @@ -82,8 +69,7 @@ tx_links CLASS::to_confirmable_children(const tx_link& link) const NOEXCEPT for (const auto& in: to_spenders(output)) { const auto child = to_input_tx(in); - if (!child.is_terminal() && is_pooled(child) && - is_confirmable_tx(child) && + if (!child.is_terminal() && is_confirmable_tx(child) && !system::contains(out, child.value)) out.push_back(child.value); } @@ -235,55 +221,131 @@ code CLASS::get_confirmable_fees(fee_rates& out) const NOEXCEPT return error::success; } -// A walk that finds no confirmed spend proves each tx visited confirmable. +// A receipt is counted in an outcome that includes its tx and no spender of it. +// Its spenders are rivals, so at most one is included, and the sum of an +// outcome is that of a weight of each member: what it receives less what it +// spends. Rival spenders are of one component, as is a row with its +// descendants, so a receipt is of at most one. Component over the limit counts +// none, or each. TEMPLATE -bool CLASS::is_confirmable_tx(confirmables& memo, - const tx_link& link) const NOEXCEPT +code CLASS::get_unconfirmed_range(uint64_t& worst, uint64_t& best, + const pool_receipts& receipts, size_t maximum_outcomes) const NOEXCEPT { - if (const auto it = memo.find(link.value); it != memo.cend()) - return it->second; + using namespace system; + using algorithm = system::pool::algorithm; + using link_t = algorithm::link_t; + constexpr auto none = algorithm::none; + worst = zero; + best = zero; + if (!store_.pool.enabled()) + return error::success; + + pool_guards guards{}; + algorithm::columns_t columns{}; + if (!get_pool_columns(columns, guards)) + return error::integrity; - const auto unconfirmable = [&]() NOEXCEPT + // Label of the row if present, labeled false if not in a component. + const auto rows = columns.cells.size(); + const auto present = [&](link_t& label, bool& labeled, + pool_link::integer row) NOEXCEPT { - memo.emplace(link.value, false); - return false; + if (row >= rows) + return false; + + label = algorithm::from_column(columns.cells[row]); + labeled = algorithm::is_labeled(label); + return algorithm::is_present(label); }; - if (is_confirmed_tx(link)) - return unconfirmable(); + struct item { link_t row; uint64_t value; pool_links spenders; }; + std::map> components{}; + for (const auto& receipt: receipts) + { + link_t label{}; + bool labeled{}; + if (!present(label, labeled, receipt.row)) + continue; - std::vector pending{ link.value }; - std::unordered_set visited{ link.value }; - while (!pending.empty()) + auto spent = false; + auto component = labeled ? label : none; + item next + { + labeled ? possible_narrow_cast(receipt.row) : none, + receipt.value, + {} + }; + + for (const auto spender: receipt.spenders) + { + if (!present(label, labeled, spender)) + continue; + + spent = true; + if (!labeled) + continue; + + component = label; + next.spenders.push_back(possible_narrow_cast(spender)); + } + + if (component != none) + { + components[component].push_back(std::move(next)); + } + else if (!spent) + { + worst = ceilinged_add(worst, receipt.value); + best = ceilinged_add(best, receipt.value); + } + } + + std::vector outcomes{}; + const algorithm::graph_t graph{ columns }; + for (const auto& [label, items]: components) { - const tx_link next{ pending.back() }; - pending.pop_back(); + algorithm::component_t component{}; + const auto exact = algorithm::collect(component, graph, label) && + algorithm::relate(component, graph) && + algorithm::outcomes(component) <= maximum_outcomes; - if (const auto it = memo.find(next.value); it != memo.cend()) + if (!exact) { - if (!it->second) - return unconfirmable(); + for (const auto& next: items) + best = ceilinged_add(best, next.value); continue; } - for (const auto& point: to_points(next)) + // Receipt of a row not in component is counted unless spent. + uint64_t base{}; + algorithm::weights_t weights{}; + const auto member = [&](link_t row) NOEXCEPT + { + const std::array one_row{ row }; + return right_zeros(algorithm::members(component, one_row)); + }; + + for (const auto& next: items) { - const auto prevout = get_point_key(point); - if (!find_confirmed_spender(prevout).is_terminal()) - return unconfirmable(); - - const auto parent = to_tx(prevout.hash()); - if (!parent.is_terminal() && !is_confirmed_tx(parent) && - visited.insert(parent.value).second) - pending.push_back(parent.value); + if (next.row == none) + base += next.value; + else + weights[member(next.row)] += next.value; + + for (const auto spender: next.spenders) + weights[member(spender)] -= next.value; } - } - for (const auto tx: visited) - memo.emplace(tx, true); + int64_t least{}; + int64_t greatest{}; + algorithm::leaves(outcomes, component, graph, maximum_outcomes, {}, {}); + algorithm::range(least, greatest, outcomes, component, weights); + worst = ceilinged_add(worst, base + to_unsigned(least)); + best = ceilinged_add(best, base + to_unsigned(greatest)); + } - return true; + return error::success; } TEMPLATE diff --git a/include/bitcoin/database/impl/query/pool/pool_reader.ipp b/include/bitcoin/database/impl/query/pool/pool_reader.ipp index 4ce396dfb..8ad6a3bc3 100644 --- a/include/bitcoin/database/impl/query/pool/pool_reader.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_reader.ipp @@ -36,6 +36,33 @@ pool_link CLASS::to_pool_row(const tx_link& link) const NOEXCEPT return store_.pool.enabled() ? store_.pool.first(link) : pool_link{}; } +TEMPLATE +tx_state CLASS::get_tx_state(const tx_link& link) const NOEXCEPT +{ + using algorithm = system::pool::algorithm; + if (is_confirmed_tx(link)) + return tx_state::confirmed; + + // A row is committed before it is indexed. + const auto row = to_pool_row(link); + const auto& index = store_.pool_index; + if (row.is_terminal() || row.value >= index.count().value) + return tx_state::unknown; + + const auto cell = index.cells.at(row.value); + if (algorithm::is_present(cell)) + return tx_state::confirmable; + + return algorithm::is_absent(cell) ? tx_state::unconfirmable : + tx_state::unknown; +} + +TEMPLATE +bool CLASS::is_confirmable_tx(const tx_link& link) const NOEXCEPT +{ + return get_tx_state(link) == tx_state::confirmable; +} + TEMPLATE bool CLASS::get_pooled_time(uint32_t& out, const tx_link& link) const NOEXCEPT { diff --git a/include/bitcoin/database/impl/store/store_prune.ipp b/include/bitcoin/database/impl/store/store_prune.ipp index 0ff1d8b14..de7066d5f 100644 --- a/include/bitcoin/database/impl/store/store_prune.ipp +++ b/include/bitcoin/database/impl/store/store_prune.ipp @@ -237,7 +237,8 @@ bool CLASS::retain_pool(retentions& out) NOEXCEPT } // protected -// Tx retained if confirmable and every unconfirmed ancestor is pooled. +// Tx retained if not confirmed, every unconfirmed ancestor is pooled, and no +// confirmed tx spends a point of it or of its unconfirmed ancestry. TEMPLATE bool CLASS::is_retained(bool& out, const tx_link& link, const retentions& kept) NOEXCEPT @@ -249,7 +250,7 @@ bool CLASS::is_retained(bool& out, const tx_link& link, std::unordered_set visited{ link }; out = false; - if (!reader.is_confirmable_tx(link)) + if (reader.is_confirmed_tx(link)) return true; while (!pending.empty()) @@ -261,6 +262,13 @@ bool CLASS::is_retained(bool& out, const tx_link& link, if (fk.is_terminal()) return true; + for (const auto& point: reader.to_points(next)) + { + const auto prevout = reader.get_point_key(point); + if (!reader.find_confirmed_spender(prevout).is_terminal()) + return true; + } + table::pool_edge edge{}; if (!pool.edges.get(fk, edge)) return false; diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index ede1358f4..991d98f0e 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -734,8 +734,9 @@ class query bool set_block_confirmable(const header_link& link) NOEXCEPT; bool set_block_unknown(const header_link& link) NOEXCEPT; - /// Pool. + /// Pool (thread safe). /// ----------------------------------------------------------------------- + /// Each reads a pool record or one cell of pool index, as of the read. inline bool is_sufficient(const context& current, const context& evaluated) const NOEXCEPT; @@ -747,26 +748,22 @@ class query code get_pooled(pooled_tx& out, const tx_link& link, const context& ctx) const NOEXCEPT; - /// Unconfirmed, and not conflicted by a confirmed tx (or via ancestry). + /// Confirmed (block), confirmable or unconfirmable (pool), else unknown. + tx_state get_tx_state(const tx_link& link) const NOEXCEPT; + + /// Pooled, not confirmed, and able to confirm. bool is_confirmable_tx(const tx_link& link) const NOEXCEPT; - /// Spent by a pooled and confirmable tx. + /// Spent by a confirmable tx. bool is_confirmable_spent(const output_link& link) const NOEXCEPT; - /// Spender of prevout in a pooled and confirmable tx, terminal if none. + /// Spender of prevout in a confirmable tx, terminal if none. ins_link find_confirmable_spender(const point& prevout) const NOEXCEPT; - /// Confirmable tx links, in pool order (requires pool). - code get_confirmable_txs(tx_links& out) const NOEXCEPT; - /// Confirmable txs spent by, and spending, tx (direct only). tx_links to_confirmable_parents(const tx_link& link) const NOEXCEPT; tx_links to_confirmable_children(const tx_link& link) const NOEXCEPT; - /// Rows of pooled txs and of their unconfirmed pooled ancestors, by row. - code get_pooled_ancestry(pooled_rows& out, - const tx_links& links) const NOEXCEPT; - /// Confirmable ancestors, or descendants, of tx (excluding tx). tx_links to_confirmable_ancestors(const tx_link& link) const NOEXCEPT; tx_links to_confirmable_descendants(const tx_link& link) const NOEXCEPT; @@ -777,9 +774,29 @@ class query bool get_descendant_totals(fee_totals& out, const tx_link& link) const NOEXCEPT; + /// Ids, totals and parent positions of each pooled tx of template. + code get_template_txs(template_txs& out, + const tx_links& links) const NOEXCEPT; + + /// Pool (not thread safe). + /// ----------------------------------------------------------------------- + /// Each walks or writes pool index, so caller serializes them, with those + /// of coverage, template and estimate. + + /// Confirmable tx links, in pool order (requires pool). + code get_confirmable_txs(tx_links& out) const NOEXCEPT; + + /// Rows of pooled txs and of their unconfirmed pooled ancestors, by row. + code get_pooled_ancestry(pooled_rows& out, + const tx_links& links) const NOEXCEPT; + /// Confirmable fee rate tuples, in pool order (requires pool). code get_confirmable_fees(fee_rates& out) const NOEXCEPT; + /// Least and greatest value of receipts not spent, over conflict outcomes. + code get_unconfirmed_range(uint64_t& worst, uint64_t& best, + const pool_receipts& receipts, size_t maximum_outcomes) const NOEXCEPT; + /// Get txs not validated under context, in pool order (requires pool). code get_pool_revalidation(tx_links& out, const context& ctx) const NOEXCEPT; @@ -881,10 +898,6 @@ class query const pool_totals& budget, size_t depth, uint64_t cutoff, uint32_t bytes_per_sigop, size_t maximum_outcomes) const NOEXCEPT; - /// Ids, totals and parent positions of each pooled tx of template. - code get_template_txs(template_txs& out, - const tx_links& links) const NOEXCEPT; - /// Estimate. /// ----------------------------------------------------------------------- @@ -959,7 +972,7 @@ class query /// Matches (height, tx hash, tweak) of rows [first, last) confirmed at /// heights (or mtps if time) [start, stop], ordered by height, then - /// unconfirmed (zero height), false implies cancel. Short height cut + /// confirmable (zero height), false implies cancel. Short height cut /// scanned from its floor scans only rows of its confirmed blocks. bool get_silent_matches(silent_matches& out, const stopper& cancel, const system::scan::batch::receiver& keys, size_t first, @@ -1031,8 +1044,9 @@ class query bool is_confirmed_input(const ins_link& link) const NOEXCEPT; bool is_confirmed_output(const output_link& link) const NOEXCEPT; bool is_confirmed_all_prevouts(const tx_link& link) const NOEXCEPT; - bool is_unconfirmed_spent(const output_link& link) const NOEXCEPT; bool is_confirmed_spent(const output_link& link) const NOEXCEPT; + + /// Spent by a confirmed or confirmable tx. bool is_spent(const output_link& link) const NOEXCEPT; bool is_spent(const point& prevout) const NOEXCEPT; @@ -1144,7 +1158,7 @@ class query code get_unspent(const stopper& cancel, unspent_outputs& out, const hash_digest& key, size_t limit, bool turbo=false) const NOEXCEPT; - /// Balance queries (universal, unconfirmed conflict resolution arbitrary). + /// Balance queries (unconfirmed is confirmable outputs not spent). code get_unconfirmed_balance(const stopper& cancel, uint64_t& out, const hash_digest& key, size_t limit, bool turbo=false) const NOEXCEPT; code get_confirmed_balance(const stopper& cancel, uint64_t& out, @@ -1153,7 +1167,11 @@ class query uint64_t& unconfirmed, const hash_digest& key, size_t limit, bool turbo=false) const NOEXCEPT; - /// Confirmed and unconfirmed totals (an unconfirmed spend is unconfirmed). + /// Address outputs of confirmable txs, each with its confirmable spenders. + code get_unconfirmed_receipts(const stopper& cancel, pool_receipts& out, + const hash_digest& key, size_t limit) const NOEXCEPT; + + /// Confirmed and confirmable totals (a confirmable spend is unconfirmed). code get_address_totals(const stopper& cancel, address_totals& confirmed, address_totals& unconfirmed, const hash_digest& key, size_t limit) const NOEXCEPT; @@ -1327,10 +1345,6 @@ class query bool is_pool_conflicted(bool& out, const tx_link& link, const pool_link& row) const NOEXCEPT; - /// Confirmability memoized across txs of a scan. - bool is_confirmable_tx(confirmables& memo, - const tx_link& link) const NOEXCEPT; - /// Confirmable ancestors or descendants of tx (excluding tx). tx_links to_confirmable_relatives(const tx_link& link, bool ancestors) const NOEXCEPT; diff --git a/include/bitcoin/database/types/pooled_tx.hpp b/include/bitcoin/database/types/pooled_tx.hpp index 3e916e5a0..b089bd58c 100644 --- a/include/bitcoin/database/types/pooled_tx.hpp +++ b/include/bitcoin/database/types/pooled_tx.hpp @@ -62,6 +62,17 @@ struct pooled_row using pooled_rows = std::vector; +/// Value of an output of a confirmable tx, with pool rows of the tx and of +/// its confirmable spenders. +struct pool_receipt +{ + table::pool::link::integer row{}; + uint64_t value{}; + std::vector spenders{}; +}; + +using pool_receipts = std::vector; + } // namespace database } // namespace libbitcoin diff --git a/include/bitcoin/database/types/type.hpp b/include/bitcoin/database/types/type.hpp index c4c1f9f0b..59efc9b47 100644 --- a/include/bitcoin/database/types/type.hpp +++ b/include/bitcoin/database/types/type.hpp @@ -81,6 +81,16 @@ using inpoints = std::vector; using outpoint = system::chain::outpoint; using outpoints = std::vector; +/// Existence of a tx relative to the confirmed chain: in a confirmed block, +/// pooled and able to confirm, pooled and not able to confirm, or neither. +enum class tx_state : uint8_t +{ + unknown, + confirmed, + confirmable, + unconfirmable +}; + /// Custom data carriers. /// --------------------------------------------------------------------------- /// Also: position, header_state, fee_rate, span, unspent, history. @@ -90,7 +100,6 @@ using sizes = std::pair; using counts = std::pair; using threshold = std::pair; using thresholds = std::vector; -using confirmables = std::unordered_map; using compact_matches = std::vector>; using silent_match = std::tuple; using silent_matches = std::vector; diff --git a/test/mocks/pooling.cpp b/test/mocks/pooling.cpp index 9391b6012..3312f232e 100644 --- a/test/mocks/pooling.cpp +++ b/test/mocks/pooling.cpp @@ -36,6 +36,19 @@ const hash_digest& genesis_coinbase() NOEXCEPT return hash; } +transaction to_pay(const hash_digest& hash, uint64_t value) NOEXCEPT +{ + const input input{ point{ hash, 0 }, script{}, max_uint32 }; + const script pay{ { { opcode::nop } } }; + return { 1u, inputs{ input }, outputs{ output{ value, pay } }, 0u }; +} + +const hash_digest& pay_address() NOEXCEPT +{ + static const auto hash = script{ { { opcode::nop } } }.hash(); + return hash; +} + bool archive_pool(query_t& query, const transaction& tx, uint32_t height) NOEXCEPT { diff --git a/test/mocks/pooling.hpp b/test/mocks/pooling.hpp index a631eef53..720b7057c 100644 --- a/test/mocks/pooling.hpp +++ b/test/mocks/pooling.hpp @@ -39,6 +39,13 @@ system::chain::transaction to_spend(const system::hash_digest& hash, /// The hash of the genesis coinbase. const system::hash_digest& genesis_coinbase() NOEXCEPT; +/// As to_spend, paying the value to the pay address. +system::chain::transaction to_pay(const system::hash_digest& hash, + uint64_t value) NOEXCEPT; + +/// Address of the to_pay output. +const system::hash_digest& pay_address() NOEXCEPT; + /// Archive the tx and pool it under a context of the height. bool archive_pool(query_t& query, const system::chain::transaction& tx, uint32_t height=8) NOEXCEPT; diff --git a/test/query/address/address_balance.cpp b/test/query/address/address_balance.cpp index 28610e80c..3db4baf4b 100644 --- a/test/query/address/address_balance.cpp +++ b/test/query/address/address_balance.cpp @@ -19,6 +19,7 @@ #include "../../test.hpp" #include "../../mocks/blocks.hpp" #include "../../mocks/chunk_store.hpp" +#include "../../mocks/pooling.hpp" // Unconfirmed balance is always zero in the current implementation. @@ -164,10 +165,8 @@ BC_AUTO_TEST_CASE(query_address__get_address_totals__confirmed_blocks__expected) BC_REQUIRE_EQUAL(confirmed.funded_value, 0x18u + 0x81u + 0x81u + 0x83u); BC_REQUIRE_EQUAL(confirmed.spent_count, 1u); BC_REQUIRE_EQUAL(confirmed.spent_value, 0x18u); - BC_REQUIRE_EQUAL(unconfirmed.funded_count, 5u); - BC_REQUIRE_EQUAL(unconfirmed.funded_value, 0x08u + 0x85u + 0xb1u + 0xb1u + 0xb2u); - BC_REQUIRE_EQUAL(unconfirmed.spent_count, 2u); - BC_REQUIRE_EQUAL(unconfirmed.spent_value, 0xb1u + 0xb1u); + BC_REQUIRE_EQUAL(unconfirmed.funded_count, 0u); + BC_REQUIRE_EQUAL(unconfirmed.spent_count, 0u); } BC_AUTO_TEST_CASE(query_address__get_address_totals__unconfirmed_blocks__expected) @@ -185,10 +184,111 @@ BC_AUTO_TEST_CASE(query_address__get_address_totals__unconfirmed_blocks__expecte BC_REQUIRE(!query.get_address_totals(cancel, confirmed, unconfirmed, test::block1a_address0, max_size_t)); BC_REQUIRE_EQUAL(confirmed.funded_count, 0u); BC_REQUIRE_EQUAL(confirmed.spent_count, 0u); - BC_REQUIRE_EQUAL(unconfirmed.funded_count, 6u); - BC_REQUIRE_EQUAL(unconfirmed.funded_value, 0x18u + 0x81u + 0x81u + 0x83u + 0x08u + 0x85u); + BC_REQUIRE_EQUAL(unconfirmed.funded_count, 0u); + BC_REQUIRE_EQUAL(unconfirmed.spent_count, 0u); +} + +BC_AUTO_TEST_CASE(query_address__get_address_totals__confirmable__unconfirmed) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + address_totals confirmed{}; + address_totals unconfirmed{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_address_totals(cancel, confirmed, unconfirmed, test::pay_address(), max_size_t)); + BC_REQUIRE_EQUAL(confirmed.funded_count, 0u); + BC_REQUIRE_EQUAL(unconfirmed.funded_count, 2u); + BC_REQUIRE_EQUAL(unconfirmed.funded_value, 3u); BC_REQUIRE_EQUAL(unconfirmed.spent_count, 1u); - BC_REQUIRE_EQUAL(unconfirmed.spent_value, 0x18u); + BC_REQUIRE_EQUAL(unconfirmed.spent_value, 2u); +} + +BC_AUTO_TEST_CASE(query_address__get_address_totals__unconfirmable__confirmed_only) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); + + address_totals confirmed{}; + address_totals unconfirmed{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_address_totals(cancel, confirmed, unconfirmed, test::pay_address(), max_size_t)); + BC_REQUIRE_EQUAL(confirmed.funded_count, 1u); + BC_REQUIRE_EQUAL(confirmed.funded_value, 3u); + BC_REQUIRE_EQUAL(confirmed.spent_count, 0u); + BC_REQUIRE_EQUAL(unconfirmed.funded_count, 0u); + BC_REQUIRE_EQUAL(unconfirmed.spent_count, 0u); +} + +BC_AUTO_TEST_CASE(query_address__get_balance__confirmable__unconfirmed) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + uint64_t confirmed{}; + uint64_t unconfirmed{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_balance(cancel, confirmed, unconfirmed, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(confirmed, 0u); + BC_REQUIRE_EQUAL(unconfirmed, 1u); + + BC_REQUIRE(!query.get_unconfirmed_balance(cancel, unconfirmed, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(unconfirmed, 1u); +} + +BC_AUTO_TEST_CASE(query_address__get_balance__unconfirmable__confirmed_only) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); + + uint64_t confirmed{}; + uint64_t unconfirmed{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_balance(cancel, confirmed, unconfirmed, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(confirmed, 3u); + BC_REQUIRE_EQUAL(unconfirmed, 0u); } BC_AUTO_TEST_CASE(query_address__get_address_totals__duplicate_instances__counted_once) @@ -236,4 +336,137 @@ BC_AUTO_TEST_CASE(query_address__get_address_totals__canceled__query_canceled) BC_REQUIRE_EQUAL(query.get_address_totals(cancel, confirmed, unconfirmed, test::block1a_address0, max_size_t), error::query_canceled); } +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_range__spent_parent__child) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + uint64_t worst{}; + uint64_t best{}; + database::pool_receipts receipts{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_receipts(cancel, receipts, test::pay_address(), max_size_t)); + BC_REQUIRE(!query.get_unconfirmed_range(worst, best, receipts, 8u)); + BC_REQUIRE_EQUAL(receipts.size(), 2u); + BC_REQUIRE_EQUAL(worst, 1u); + BC_REQUIRE_EQUAL(best, 1u); +} + +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_range__rivals__least_and_greatest) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto low = test::to_pay(test::genesis_coinbase(), 3u); + const auto high = test::to_pay(test::genesis_coinbase(), 5u); + BC_REQUIRE(test::archive_pool(query, low)); + BC_REQUIRE(test::archive_pool(query, high)); + + uint64_t worst{}; + uint64_t best{}; + database::pool_receipts receipts{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_receipts(cancel, receipts, test::pay_address(), max_size_t)); + BC_REQUIRE(!query.get_unconfirmed_range(worst, best, receipts, 8u)); + BC_REQUIRE_EQUAL(receipts.size(), 2u); + BC_REQUIRE_EQUAL(worst, 3u); + BC_REQUIRE_EQUAL(best, 5u); +} + +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_range__rivals_over_limit__none_and_each) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto low = test::to_pay(test::genesis_coinbase(), 3u); + const auto high = test::to_pay(test::genesis_coinbase(), 5u); + BC_REQUIRE(test::archive_pool(query, low)); + BC_REQUIRE(test::archive_pool(query, high)); + + uint64_t worst{}; + uint64_t best{}; + database::pool_receipts receipts{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_receipts(cancel, receipts, test::pay_address(), max_size_t)); + BC_REQUIRE(!query.get_unconfirmed_range(worst, best, receipts, 1u)); + BC_REQUIRE_EQUAL(worst, 0u); + BC_REQUIRE_EQUAL(best, 8u); +} + +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_range__rival_with_child__child_or_rival) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto low = test::to_pay(test::genesis_coinbase(), 3u); + const auto high = test::to_pay(test::genesis_coinbase(), 5u); + const auto child = test::to_pay(high.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, low)); + BC_REQUIRE(test::archive_pool(query, high)); + BC_REQUIRE(test::archive_pool(query, child)); + + uint64_t worst{}; + uint64_t best{}; + database::pool_receipts receipts{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_receipts(cancel, receipts, test::pay_address(), max_size_t)); + BC_REQUIRE(!query.get_unconfirmed_range(worst, best, receipts, 8u)); + BC_REQUIRE_EQUAL(receipts.size(), 3u); + BC_REQUIRE_EQUAL(worst, 1u); + BC_REQUIRE_EQUAL(best, 3u); +} + +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_range__rival_spenders__spent_or_change) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 4u); + const auto away = test::to_spend(parent.hash(false), 3u); + const auto change = test::to_pay(parent.hash(false), 2u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, away)); + BC_REQUIRE(test::archive_pool(query, change)); + + uint64_t worst{}; + uint64_t best{}; + database::pool_receipts receipts{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_receipts(cancel, receipts, test::pay_address(), max_size_t)); + BC_REQUIRE(!query.get_unconfirmed_range(worst, best, receipts, 8u)); + BC_REQUIRE_EQUAL(receipts.size(), 2u); + BC_REQUIRE_EQUAL(worst, 0u); + BC_REQUIRE_EQUAL(best, 2u); +} + BC_AUTO_TEST_SUITE_END() diff --git a/test/query/address/address_history.cpp b/test/query/address/address_history.cpp index bb6df6114..04e4133a0 100644 --- a/test/query/address/address_history.cpp +++ b/test/query/address/address_history.cpp @@ -19,6 +19,7 @@ #include "../../test.hpp" #include "../../mocks/blocks.hpp" #include "../../mocks/chunk_store.hpp" +#include "../../mocks/pooling.hpp" BC_FIXTURE_TEST_SUITE(query_address_tests, test::directory_setup_fixture) @@ -71,55 +72,31 @@ BC_AUTO_TEST_CASE(query_address__get_history__turbo_block1a_address0__expected) height_link cursor{}; const std::atomic_bool cancel{}; BC_CHECK(!query.get_history(cancel, cursor, out, test::block1a_address0, max_size_t, true)); - BC_REQUIRE_EQUAL(out.size(), 8u); + BC_REQUIRE_EQUAL(out.size(), 4u); // Identities (not part of sort). BC_CHECK_EQUAL(out.at(0).tx.hash(), test::block1a.transactions_ptr()->at(0)->hash(false)); // tx1/tx1 (same) BC_CHECK_EQUAL(out.at(1).tx.hash(), test::block2a.transactions_ptr()->at(0)->hash(false)); // tx2/tx2 (same) BC_CHECK_EQUAL(out.at(2).tx.hash(), test::block2a.transactions_ptr()->at(1)->hash(false)); // tx3/tx3 (same) BC_CHECK_EQUAL(out.at(3).tx.hash(), test::block3a.transactions_ptr()->at(0)->hash(false)); // tx6/tx6 (same) - BC_CHECK_EQUAL(out.at(4).tx.hash(), test::tx5.hash(false)); // tx5/tx4 - BC_CHECK_EQUAL(out.at(5).tx.hash(), test::tx4.hash(false)); // tx4/tx8 - BC_CHECK_EQUAL(out.at(6).tx.hash(), test::block2b.transactions_ptr()->at(0)->hash(false)); // tx8/tx5 - BC_CHECK_EQUAL(out.at(7).tx.hash(), test::block1b.transactions_ptr()->at(0)->hash(false)); // tx7/tx7 (same) - // Confirmed by height ascending. - // Unconfirmed rooted before unrooted. - // NOTE: at(5) is spend exceeds value, which is returned as unrooted_height in get_unconfirmed_history() - // NOTE: but as rooted_height in get_history(). This is a consequence of the optimized processing order - // NOTE: and does not affect valid confirmed/unconfirmed txs. + // Confirmed by height ascending, unknown excluded. BC_CHECK_EQUAL(out.at(0).tx.height(), 1u); // tx1 BC_CHECK_EQUAL(out.at(1).tx.height(), 2u); // tx2 BC_CHECK_EQUAL(out.at(2).tx.height(), 2u); // tx3 BC_CHECK_EQUAL(out.at(3).tx.height(), 3u); // tx6 - BC_CHECK_EQUAL(out.at(4).tx.height(), history::rooted_height); // tx5/tx4 - BC_CHECK_EQUAL(out.at(5).tx.height(), history::rooted_height); // tx4/tx8 - BC_CHECK_EQUAL(out.at(6).tx.height(), history::unrooted_height); // tx8/tx5 - BC_CHECK_EQUAL(out.at(7).tx.height(), history::unrooted_height); // tx7 // Confirmed height by block position. BC_CHECK_EQUAL(out.at(0).position, 0u); // tx1 BC_CHECK_EQUAL(out.at(1).position, 0u); // tx2 BC_CHECK_EQUAL(out.at(2).position, 1u); // tx3 BC_CHECK_EQUAL(out.at(3).position, 0u); // tx6 - BC_CHECK_EQUAL(out.at(4).position, history::unconfirmed_position); // tx5/tx4 - BC_CHECK_EQUAL(out.at(5).position, history::unconfirmed_position); // tx4/tx8 - BC_CHECK_EQUAL(out.at(6).position, history::unconfirmed_position); // tx8/tx5 - BC_CHECK_EQUAL(out.at(7).position, history::unconfirmed_position); // tx7 - - // Unconfirmed system::encode_hash(hash) lexically sorted. - using namespace system; - BC_CHECK(encode_hash(out.at(6).tx.hash()) < encode_hash(out.at(7).tx.hash())); // Fee (not part of sort). BC_CHECK_EQUAL(out.at(0).fee, history::missing_prevout); // tx1 BC_CHECK_EQUAL(out.at(1).fee, history::missing_prevout); // tx2 BC_CHECK_EQUAL(out.at(2).fee, history::missing_prevout); // tx3 BC_CHECK_EQUAL(out.at(3).fee, history::missing_prevout); // tx6 - BC_CHECK_EQUAL(out.at(4).fee, history::missing_prevout); // tx5/tx4 - BC_CHECK_EQUAL(out.at(5).fee, floored_subtract(0x18u + 0x2au, 0x08u)); // tx4/tx8 - BC_CHECK_EQUAL(out.at(6).fee, floored_subtract(0xb1u + 0xb1u, 0xb2u)); // tx8/tx5 - BC_CHECK_EQUAL(out.at(7).fee, history::missing_prevout); // tx7 } BC_AUTO_TEST_CASE(query_address__get_history__limited__depth_limited_empty) @@ -146,11 +123,11 @@ BC_AUTO_TEST_CASE(query_address__get_history__limited__depth_limited_empty) cursor = {}; BC_CHECK_EQUAL(query.get_history(cancel, cursor, out, hash, 9, true), error::success); - BC_CHECK_EQUAL(out.size(), 8u); + BC_CHECK_EQUAL(out.size(), 4u); cursor = {}; BC_CHECK_EQUAL(query.get_history(cancel, cursor, out, hash, 100, true), error::success); - BC_CHECK_EQUAL(out.size(), 8u); + BC_CHECK_EQUAL(out.size(), 4u); } BC_AUTO_TEST_CASE(query_address__get_history__start__excludes_prior) @@ -175,20 +152,20 @@ BC_AUTO_TEST_CASE(query_address__get_history__start__excludes_prior) BC_CHECK_EQUAL(query.get_history(cancel, cursor, out, hash, 8, true), error::depth_limited); BC_CHECK(out.empty()); - // Only unconfirmed. + // None at or above start. cursor = 42; BC_CHECK_EQUAL(query.get_history(cancel, cursor, out, hash, 9, true), error::success); - BC_CHECK_EQUAL(out.size(), 4u); + BC_CHECK(out.empty()); // Only excludes genesis. cursor = 1; BC_CHECK_EQUAL(query.get_history(cancel, cursor, out, hash, 100, true), error::success); - BC_CHECK_EQUAL(out.size(), 8u); + BC_CHECK_EQUAL(out.size(), 4u); // Excludes block1a which has one confirmed touching tx. cursor = 2; BC_CHECK_EQUAL(query.get_history(cancel, cursor, out, hash, 100, true), error::success); - BC_CHECK_EQUAL(out.size(), 7u); + BC_CHECK_EQUAL(out.size(), 3u); } // ---------------------------------------------------------------------------- @@ -220,7 +197,7 @@ BC_AUTO_TEST_CASE(query_address__get_history__start__excludes_prior) // get_unconfirmed_history -BC_AUTO_TEST_CASE(query_address__get_unconfirmed_history__turbo_block1a_address0__expected) +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_history__unknown__empty) { settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -232,38 +209,81 @@ BC_AUTO_TEST_CASE(query_address__get_unconfirmed_history__turbo_block1a_address0 histories out{}; const std::atomic_bool cancel{}; BC_CHECK(!query.get_unconfirmed_history(cancel, out, test::block1a_address0, max_size_t, true)); - BC_REQUIRE_EQUAL(out.size(), 4u); + BC_CHECK(out.empty()); +} - // Identities (not part of sort). - BC_CHECK_EQUAL(out.at(0).tx.hash(), test::tx5.hash(false)); // tx5 - BC_CHECK_EQUAL(out.at(1).tx.hash(), test::tx4.hash(false)); // tx4 - BC_CHECK_EQUAL(out.at(2).tx.hash(), test::block2b.transactions_ptr()->at(0)->hash(false)); // tx8 - BC_CHECK_EQUAL(out.at(3).tx.hash(), test::block1b.transactions_ptr()->at(0)->hash(false)); // tx7 - - // Confirmed by height ascending (not part of sort). - - // Unconfirmed rooted before unrooted. - BC_CHECK_EQUAL(out.at(0).tx.height(), history::rooted_height); // tx5 spends block1a - BC_CHECK_EQUAL(out.at(1).tx.height(), history::rooted_height); // tx4 spends block1a - BC_CHECK_EQUAL(out.at(2).tx.height(), history::unrooted_height); // block2b spends block1b - BC_CHECK_EQUAL(out.at(3).tx.height(), history::unrooted_height); // block1b missing prevouts - - // Confirmed height by block position (not part of sort). - BC_CHECK_EQUAL(out.at(0).position, history::unconfirmed_position); - BC_CHECK_EQUAL(out.at(1).position, history::unconfirmed_position); - BC_CHECK_EQUAL(out.at(2).position, history::unconfirmed_position); - BC_CHECK_EQUAL(out.at(3).position, history::unconfirmed_position); - - // Unconfirmed system::encode_hash(hash) lexically sorted. - using namespace system; - BC_CHECK(encode_hash(out.at(0).tx.hash()) < encode_hash(out.at(1).tx.hash())); - BC_CHECK(encode_hash(out.at(2).tx.hash()) < encode_hash(out.at(3).tx.hash())); +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_history__confirmable__rooted_before_unrooted) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); - // Fee (not part of sort). - BC_CHECK_EQUAL(out.at(0).fee, history::missing_prevout); // spend exceeds value (treated as missing prevout). - BC_CHECK_EQUAL(out.at(1).fee, floored_subtract(0x18u + 0x2au, 0x08u)); - BC_CHECK_EQUAL(out.at(2).fee, floored_subtract(0xb1u + 0xb1u, 0xb2u)); - BC_CHECK_EQUAL(out.at(3).fee, 0u); // coinbase (archived with null single point). + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + histories out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_history(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 2u); + BC_REQUIRE_EQUAL(out.at(0).tx.hash(), parent.hash(false)); + BC_REQUIRE_EQUAL(out.at(0).tx.height(), history::rooted_height); + BC_REQUIRE_EQUAL(out.at(0).position, history::unconfirmed_position); + BC_REQUIRE_EQUAL(out.at(1).tx.hash(), child.hash(false)); + BC_REQUIRE_EQUAL(out.at(1).tx.height(), history::unrooted_height); + BC_REQUIRE_EQUAL(out.at(1).position, history::unconfirmed_position); +} + +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_history__unconfirmable__empty) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); + + histories out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_history(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE(out.empty()); +} + +BC_AUTO_TEST_CASE(query_address__get_history__unconfirmable__confirmed_only) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); + + histories out{}; + height_link cursor{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_history(cancel, cursor, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_REQUIRE_EQUAL(out.front().tx.height(), 1u); + BC_REQUIRE_EQUAL(out.front().position, 1u); } BC_AUTO_TEST_CASE(query_address__get_unconfirmed_history__limited__depth_limited_empty) @@ -287,10 +307,10 @@ BC_AUTO_TEST_CASE(query_address__get_unconfirmed_history__limited__depth_limited BC_CHECK(out.empty()); BC_CHECK_EQUAL(query.get_unconfirmed_history(cancel, out, hash, 9, true), error::success); - BC_CHECK_EQUAL(out.size(), 4u); + BC_CHECK(out.empty()); BC_CHECK_EQUAL(query.get_unconfirmed_history(cancel, out, hash, 100, true), error::success); - BC_CHECK_EQUAL(out.size(), 4u); + BC_CHECK(out.empty()); } // get_confirmed_history @@ -456,7 +476,7 @@ BC_AUTO_TEST_CASE(query_address__get_tx_history__confirmed_missing_prevout__expe BC_CHECK_EQUAL(history.tx.hash(), hash); } -BC_AUTO_TEST_CASE(query_address__get_tx_history__rooted__expected) +BC_AUTO_TEST_CASE(query_address__get_tx_history__unknown__invalid) { settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -465,14 +485,31 @@ BC_AUTO_TEST_CASE(query_address__get_tx_history__rooted__expected) BC_CHECK(!store.create(test::events_handler)); BC_CHECK(test::setup_three_block_confirmed_address_store(query)); - using namespace system; - const auto hash = test::tx4.hash(false); - const auto history = query.get_tx_history(query.to_tx(hash)); - BC_CHECK(history.valid()); - BC_CHECK_EQUAL(history.fee, floored_subtract(0x18u + 0x2au, 0x08u)); + const auto history = query.get_tx_history(query.to_tx(test::tx4.hash(false))); + BC_CHECK(!history.valid()); BC_CHECK_EQUAL(history.position, history::unconfirmed_position); - BC_CHECK_EQUAL(history.tx.height(), history::rooted_height); - BC_CHECK_EQUAL(history.tx.hash(), hash); +} + +BC_AUTO_TEST_CASE(query_address__get_tx_history__rooted__expected) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + const auto history = query.get_tx_history(query.to_tx(parent.hash(false))); + BC_REQUIRE(history.valid()); + BC_REQUIRE_EQUAL(history.position, history::unconfirmed_position); + BC_REQUIRE_EQUAL(history.tx.height(), history::rooted_height); + BC_REQUIRE_EQUAL(history.tx.hash(), parent.hash(false)); } BC_AUTO_TEST_CASE(query_address__get_tx_history__unrooted__expected) @@ -481,17 +518,40 @@ BC_AUTO_TEST_CASE(query_address__get_tx_history__unrooted__expected) settings.path = TEST_DIRECTORY; test::chunk_store store{ settings }; test::query_accessor query{ store }; - BC_CHECK(!store.create(test::events_handler)); - BC_CHECK(test::setup_three_block_confirmed_address_store(query)); + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); - using namespace system; - const auto hash = test::block2b.transactions_ptr()->at(0)->hash(false); - const auto history = query.get_tx_history(query.to_tx(hash)); - BC_CHECK(history.valid()); - BC_CHECK_EQUAL(history.fee, floored_subtract(0xb1u + 0xb1u, 0xb2u)); - BC_CHECK_EQUAL(history.position, history::unconfirmed_position); - BC_CHECK_EQUAL(history.tx.height(), history::unrooted_height); - BC_CHECK_EQUAL(history.tx.hash(), hash); + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + const auto history = query.get_tx_history(query.to_tx(child.hash(false))); + BC_REQUIRE(history.valid()); + BC_REQUIRE_EQUAL(history.position, history::unconfirmed_position); + BC_REQUIRE_EQUAL(history.tx.height(), history::unrooted_height); + BC_REQUIRE_EQUAL(history.tx.hash(), child.hash(false)); +} + +BC_AUTO_TEST_CASE(query_address__get_tx_history__unconfirmable__invalid) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); + + BC_REQUIRE(!query.get_tx_history(query.to_tx(parent.hash(false))).valid()); + BC_REQUIRE(!query.get_tx_history(query.to_tx(child.hash(false))).valid()); } // get_spenders_history1 @@ -542,7 +602,7 @@ BC_AUTO_TEST_CASE(query_address__get_spenders_history__confirmed__expected) using namespace system; const auto hash = test::block1a.transactions_ptr()->at(0)->hash(false); const auto histories = query.get_spenders_history(hash, 0); - BC_CHECK_EQUAL(histories.size(), 4u); + BC_CHECK_EQUAL(histories.size(), 2u); BC_CHECK(histories.at(0).valid()); BC_CHECK_EQUAL(histories.at(0).fee, history::missing_prevout); // spend > value @@ -555,18 +615,46 @@ BC_AUTO_TEST_CASE(query_address__get_spenders_history__confirmed__expected) BC_CHECK_EQUAL(histories.at(1).position, 0u); BC_CHECK_EQUAL(histories.at(1).tx.height(), 3u); BC_CHECK_EQUAL(histories.at(1).tx.hash(), test::block3a.transactions_ptr()->at(0)->hash(false)); +} + +BC_AUTO_TEST_CASE(query_address__get_spenders_history__confirmable__expected) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + const auto histories = query.get_spenders_history(parent.hash(false), 0); + BC_REQUIRE_EQUAL(histories.size(), 1u); + BC_REQUIRE_EQUAL(histories.front().tx.hash(), child.hash(false)); + BC_REQUIRE_EQUAL(histories.front().position, history::unconfirmed_position); +} + +BC_AUTO_TEST_CASE(query_address__get_spenders_history__unconfirmable__empty) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); - BC_CHECK(histories.at(2).valid()); - BC_CHECK_EQUAL(histories.at(2).fee, history::missing_prevout); // spend > value - BC_CHECK_EQUAL(histories.at(2).position, history::unconfirmed_position); - BC_CHECK_EQUAL(histories.at(2).tx.height(), history::rooted_height); - BC_CHECK_EQUAL(histories.at(2).tx.hash(), test::tx5.hash(false)); - - BC_CHECK(histories.at(3).valid()); - BC_CHECK_EQUAL(histories.at(3).fee, floored_subtract(0x18u + 0x2au, 0x08u)); - BC_CHECK_EQUAL(histories.at(3).position, history::unconfirmed_position); - BC_CHECK_EQUAL(histories.at(3).tx.height(), history::rooted_height); - BC_CHECK_EQUAL(histories.at(3).tx.hash(), test::tx4.hash(false)); + BC_REQUIRE(query.get_spenders_history(parent.hash(false), 0).empty()); } // duplicate instances (one tx archived twice, only one instance associated) diff --git a/test/query/address/address_outpoints.cpp b/test/query/address/address_outpoints.cpp index e85aac610..570a3d24c 100644 --- a/test/query/address/address_outpoints.cpp +++ b/test/query/address/address_outpoints.cpp @@ -19,6 +19,7 @@ #include "../../test.hpp" #include "../../mocks/blocks.hpp" #include "../../mocks/chunk_store.hpp" +#include "../../mocks/pooling.hpp" BC_FIXTURE_TEST_SUITE(query_address_tests, test::directory_setup_fixture) @@ -39,7 +40,7 @@ BC_AUTO_TEST_CASE(query_address__get_unconfirmed_unspent_outpoints__turbo_genesi BC_REQUIRE(out.empty()); } -BC_AUTO_TEST_CASE(query_address__get_unconfirmed_unspent_outpoints__turbo_block1a_address0__expected) +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_unspent_outpoints__unknown__empty) { settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -48,19 +49,91 @@ BC_AUTO_TEST_CASE(query_address__get_unconfirmed_unspent_outpoints__turbo_block1 BC_REQUIRE(!store.create(test::events_handler)); BC_REQUIRE(test::setup_three_block_confirmed_address_store(query)); - // tx4/5 contain one address output each. - // block1b (unconfirmed) contains 2 address outputs, both spent by block2b (0 unspent). - // block2b (unconfirmed) contains 1 address output, so 3 total address outputs. outpoints out{}; const std::atomic_bool cancel{}; BC_REQUIRE(!query.get_unconfirmed_unspent_outpoints(cancel, out, test::block1a_address0, max_size_t, true)); - BC_REQUIRE_EQUAL(out.size(), 3u); - BC_REQUIRE_EQUAL(out.at(0).value(), 0x08u); - BC_REQUIRE_EQUAL(out.at(0).point().hash(), test::tx4.hash(false)); - BC_REQUIRE_EQUAL(out.at(1).value(), 0x85u); - BC_REQUIRE_EQUAL(out.at(1).point().hash(), test::tx5.hash(false)); - BC_REQUIRE_EQUAL(out.at(2).value(), 0xb2u); - BC_REQUIRE_EQUAL(out.at(2).point().hash(), test::block2b.transactions_ptr()->at(0)->hash(false)); + BC_REQUIRE(out.empty()); +} + +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_unspent_outpoints__confirmable__unspent_only) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + outpoints out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_unspent_outpoints(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_REQUIRE_EQUAL(out.front().value(), 1u); + BC_REQUIRE_EQUAL(out.front().point().hash(), child.hash(false)); +} + +BC_AUTO_TEST_CASE(query_address__get_unconfirmed_unspent_outpoints__unconfirmable__empty) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); + + outpoints out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unconfirmed_unspent_outpoints(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE(out.empty()); +} + +BC_AUTO_TEST_CASE(query_address__get_address_outpoints__unknown__excluded) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(test::setup_three_block_confirmed_address_store(query)); + + outpoints out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_address_outpoints(cancel, out, test::block1a_address0, max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 4u); +} + +BC_AUTO_TEST_CASE(query_address__get_address_outpoints__confirmable__included) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + outpoints out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_address_outpoints(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 2u); } BC_AUTO_TEST_CASE(query_address__get_confirmed_unspent_outpoints__turbo_genesis__expected) diff --git a/test/query/address/address_unspent.cpp b/test/query/address/address_unspent.cpp index 3aa35546f..c0a08f505 100644 --- a/test/query/address/address_unspent.cpp +++ b/test/query/address/address_unspent.cpp @@ -19,6 +19,7 @@ #include "../../test.hpp" #include "../../mocks/blocks.hpp" #include "../../mocks/chunk_store.hpp" +#include "../../mocks/pooling.hpp" BC_FIXTURE_TEST_SUITE(query_address_tests, test::directory_setup_fixture) @@ -72,32 +73,8 @@ BC_AUTO_TEST_CASE(query_address__get_unspent__turbo_genesis__expected) const std::atomic_bool cancel{}; BC_REQUIRE(!query.get_unconfirmed_unspent(cancel, out, test::block1a_address0, max_size_t, true)); - // tx4/5 contain one address output each. - // block1b (unconfirmed) contains 2 address outputs, both spent by block2b (0 unspent). - // block2b (unconfirmed) contains 1 address output, so 3 total address outputs. - BC_CHECK_EQUAL(out.size(), 3u); - - BC_CHECK_EQUAL(out.at(0).height, unspent_output::unused_height); - BC_CHECK_EQUAL(out.at(0).position, unspent_output::unconfirmed_position); - BC_CHECK_EQUAL(out.at(0).out.value(), 0x08u); - BC_CHECK_EQUAL(out.at(0).out.point().index(), 0u); - BC_CHECK_EQUAL(out.at(0).out.point().hash(), test::tx4.hash(false)); - - BC_CHECK_EQUAL(out.at(1).height, unspent_output::unused_height); - BC_CHECK_EQUAL(out.at(1).position, unspent_output::unconfirmed_position); - BC_CHECK_EQUAL(out.at(1).out.value(), 0x85u); - BC_CHECK_EQUAL(out.at(1).out.point().index(), 0u); - BC_CHECK_EQUAL(out.at(1).out.point().hash(), test::tx5.hash(false)); - - BC_CHECK_EQUAL(out.at(2).height, unspent_output::unused_height); - BC_CHECK_EQUAL(out.at(2).position, unspent_output::unconfirmed_position); - BC_CHECK_EQUAL(out.at(2).out.value(), 0xb2u); - BC_CHECK_EQUAL(out.at(2).out.point().index(), 0u); - BC_CHECK_EQUAL(out.at(2).out.point().hash(), test::block2b.transactions_ptr()->at(0)->hash(false)); - - // Arbitrary but consistent sort based on chain::point::operator<. - BC_CHECK(out.at(0).out < out.at(1).out); - BC_CHECK(out.at(1).out < out.at(2).out); + // Neither tx4/5 nor the block1b/2b txs are confirmed or pooled. + BC_CHECK(out.empty()); out.clear(); BC_REQUIRE(!query.get_confirmed_unspent(cancel, out, test::block1a_address0, max_size_t, true)); @@ -126,11 +103,8 @@ BC_AUTO_TEST_CASE(query_address__get_unspent__turbo_genesis__expected) out.clear(); BC_REQUIRE(!query.get_unspent(cancel, out, test::block1a_address0, max_size_t, true)); - // As get_unconfirmed_unspent() and get_confirmed_unspent() are mutually- - // exclusive, get_unspent() contains 3 + 3 = 6 address outputs. - BC_CHECK_EQUAL(out.size(), 6u); - - // same as get_unconfirmed_unspent() + // Same as get_confirmed_unspent(). + BC_CHECK_EQUAL(out.size(), 3u); BC_CHECK_EQUAL(out.at(0).height, 2u); BC_CHECK_EQUAL(out.at(0).position, 0u); @@ -149,26 +123,95 @@ BC_AUTO_TEST_CASE(query_address__get_unspent__turbo_genesis__expected) BC_CHECK_EQUAL(out.at(2).out.value(), 0x83u); BC_CHECK_EQUAL(out.at(2).out.point().index(), 0u); BC_CHECK_EQUAL(out.at(2).out.point().hash(), test::block3a.transactions_ptr()->at(0)->hash(false)); +} - // same as get_confirmed_unspent() - - BC_CHECK_EQUAL(out.at(3).height, unspent_output::unused_height); - BC_CHECK_EQUAL(out.at(3).position, unspent_output::unconfirmed_position); - BC_CHECK_EQUAL(out.at(3).out.value(), 0x08u); - BC_CHECK_EQUAL(out.at(3).out.point().index(), 0u); - BC_CHECK_EQUAL(out.at(3).out.point().hash(), test::tx4.hash(false)); - - BC_CHECK_EQUAL(out.at(4).height, unspent_output::unused_height); - BC_CHECK_EQUAL(out.at(4).position, unspent_output::unconfirmed_position); - BC_CHECK_EQUAL(out.at(4).out.value(), 0x85u); - BC_CHECK_EQUAL(out.at(4).out.point().index(), 0u); - BC_CHECK_EQUAL(out.at(4).out.point().hash(), test::tx5.hash(false)); - - BC_CHECK_EQUAL(out.at(5).height, unspent_output::unused_height); - BC_CHECK_EQUAL(out.at(5).position, unspent_output::unconfirmed_position); - BC_CHECK_EQUAL(out.at(5).out.value(), 0xb2u); - BC_CHECK_EQUAL(out.at(5).out.point().index(), 0u); - BC_CHECK_EQUAL(out.at(5).out.point().hash(), test::block2b.transactions_ptr()->at(0)->hash(false)); +BC_AUTO_TEST_CASE(query_address__get_unspent__confirmable__unspent_only) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + unspent_outputs out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unspent(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_REQUIRE_EQUAL(out.front().out.point().hash(), child.hash(false)); + BC_REQUIRE_EQUAL(out.front().position, unspent_output::unconfirmed_position); + + BC_REQUIRE(!query.get_unconfirmed_unspent(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_REQUIRE_EQUAL(out.front().out.point().hash(), child.hash(false)); +} + +BC_AUTO_TEST_CASE(query_address__get_unspent__unknown_spender__unspent) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + BC_REQUIRE(query.set(test::to_pay(test::genesis_coinbase(), 2u))); + + unspent_outputs out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unspent(cancel, out, test::genesis_address0, max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_REQUIRE_EQUAL(out.front().height, 0u); +} + +BC_AUTO_TEST_CASE(query_address__get_unspent__confirmable_spender__spent) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + + unspent_outputs out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unspent(cancel, out, test::genesis_address0, max_size_t, true)); + BC_REQUIRE(out.empty()); +} + +BC_AUTO_TEST_CASE(query_address__get_unspent__unconfirmable__confirmed_only) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = test::to_pay(test::genesis_coinbase(), 2u); + const auto child = test::to_pay(parent.hash(false), 1u); + BC_REQUIRE(test::archive_pool(query, parent)); + BC_REQUIRE(test::archive_pool(query, child)); + BC_REQUIRE(!test::archive_confirm(query, test::to_pay(test::genesis_coinbase(), 3u)).is_terminal()); + + unspent_outputs out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE(!query.get_unspent(cancel, out, test::pay_address(), max_size_t, true)); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_REQUIRE_EQUAL(out.front().height, 1u); + BC_REQUIRE_EQUAL(out.front().out.value(), 3u); } // duplicate instances (one tx archived twice, only one instance associated) diff --git a/test/query/batch/silent.cpp b/test/query/batch/silent.cpp index 1e9656d07..e611b95c0 100644 --- a/test/query/batch/silent.cpp +++ b/test/query/batch/silent.cpp @@ -20,6 +20,7 @@ #include "../../mocks/blocks.hpp" #include "../../mocks/chunk_store.hpp" #include "../../mocks/map_store.hpp" +#include "../../mocks/pooling.hpp" BC_FIXTURE_TEST_SUITE(query_batch_silent_tests, test::directory_setup_fixture) @@ -802,7 +803,7 @@ BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__short_cut_from_floor__ BC_REQUIRE_EQUAL(std::get<1>(out.front()), genesis); } -BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__short_cut_from_floor__unconfirmed_selected) +BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__short_cut_from_floor__confirmable_selected) { database::settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -810,16 +811,38 @@ BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__short_cut_from_floor__ test::query_accessor query{ store }; BC_REQUIRE(!store.create(test::events_handler)); BC_REQUIRE(query.initialize(test::genesis)); - BC_REQUIRE(query.set_silent(42, simple_send())); + + store.set_pooling(); + const auto tx = test::to_spend(test::genesis_coinbase(), 2u); + BC_REQUIRE(test::archive_pool(query, tx)); + BC_REQUIRE(query.set_silent(query.to_tx(tx.hash(false)), simple_send())); silent_matches out{}; const stopper cancel{}; BC_REQUIRE(query.get_silent_matches(out, cancel, get_keys(), 0, 1, 0, 0, false)); BC_REQUIRE_EQUAL(out.size(), 1u); BC_REQUIRE_EQUAL(std::get<0>(out.front()), 0u); + BC_REQUIRE_EQUAL(std::get<1>(out.front()), tx.hash(false)); +} + +BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__unknown__excluded) +{ + database::settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + BC_REQUIRE(query.set(test::to_spend(test::genesis_coinbase(), 2u))); + BC_REQUIRE(query.set_silent(1, simple_send())); + + silent_matches out{}; + const stopper cancel{}; + BC_REQUIRE(query.get_silent_matches(out, cancel, get_keys(), 0, 1, 0, 0, false)); + BC_REQUIRE(out.empty()); } -BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__above_floor__unconfirmed) +BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__above_floor__confirmable) { database::settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -827,8 +850,12 @@ BC_AUTO_TEST_CASE(query_batch_silent__get_silent_matches__above_floor__unconfirm test::query_accessor query{ store }; BC_REQUIRE(!store.create(test::events_handler)); BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto tx = test::to_spend(test::genesis_coinbase(), 2u); + BC_REQUIRE(test::archive_pool(query, tx)); BC_REQUIRE(query.set_silent(0, simple_send())); - BC_REQUIRE(query.set_silent(42, simple_send())); + BC_REQUIRE(query.set_silent(query.to_tx(tx.hash(false)), simple_send())); silent_matches out{}; const stopper cancel{}; From 4e2c9cb40ac2e4cb0b56d7ccc1c2621713ddd12f Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sat, 28 Nov 2026 05:42:49 -0500 Subject: [PATCH 4/4] Take doubles from the spenders of the point, not their hashes. --- .../query/consensus/consensus_prevouts.ipp | 23 +++++++-------- .../database/impl/query/pool/pool_index.ipp | 11 ++------ test/query/consensus/consensus_prevouts.cpp | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/include/bitcoin/database/impl/query/consensus/consensus_prevouts.ipp b/include/bitcoin/database/impl/query/consensus/consensus_prevouts.ipp index 08148e6fe..e919f45c6 100644 --- a/include/bitcoin/database/impl/query/consensus/consensus_prevouts.ipp +++ b/include/bitcoin/database/impl/query/consensus/consensus_prevouts.ipp @@ -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 diff --git a/include/bitcoin/database/impl/query/pool/pool_index.ipp b/include/bitcoin/database/impl/query/pool/pool_index.ipp index 0131279da..e5be5874d 100644 --- a/include/bitcoin/database/impl/query/pool/pool_index.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_index.ipp @@ -312,7 +312,6 @@ bool CLASS::get_pooled_spender(system::pool::algorithm::link_t& out, } // protected -// Point archived with one spender is not in duplicate table. TEMPLATE bool CLASS::get_pool_conflicts(tx_links& out, const tx_links& txs) const NOEXCEPT @@ -326,14 +325,8 @@ bool CLASS::get_pool_conflicts(tx_links& out, // Point links are contiguous (computed). const auto end = record.points_fk + record.ins_count; for (auto fk = record.points_fk; fk < end; ++fk) - { - const auto point = get_point_key(fk); - if (!store_.duplicate.exists(point)) - continue; - - for (const auto& in: to_spenders(point)) - out.push_back(to_input_tx(in).value); - } + if (!get_doubles(out, get_point_key(fk))) + return false; } return true; diff --git a/test/query/consensus/consensus_prevouts.cpp b/test/query/consensus/consensus_prevouts.cpp index c863131a8..8cafddc27 100644 --- a/test/query/consensus/consensus_prevouts.cpp +++ b/test/query/consensus/consensus_prevouts.cpp @@ -145,6 +145,34 @@ BC_AUTO_TEST_CASE(query_consensus__get_block_prevouts__spending_block__expected) BC_REQUIRE_EQUAL(cache.spends.at(1).second, spends.at(1).second); } +BC_AUTO_TEST_CASE(query_consensus__get_block_prevouts__double_spend__both_spenders) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + BC_REQUIRE(query.set(test::block1a, context{ 0, 1, 0 }, {}, false, false)); + BC_REQUIRE(query.set_strong(1)); + + // tx5 spends output 0 of the block1a transaction, tx4 spends outputs 0 and 1. + BC_REQUIRE(query.set(test::tx5)); + const block spending{ header{ 1, test::block1a.hash(), hash_digest{ 0x43 }, 0, 0, 0 }, transactions{ make_coinbase(), test::tx4 } }; + BC_REQUIRE(query.set(spending, context{ 0, 2, 0 }, {}, false, false)); + const auto tx4_fk = query.to_tx(test::tx4.hash(false)); + const auto tx5_fk = query.to_tx(test::tx5.hash(false)); + BC_REQUIRE(!tx4_fk.is_terminal()); + BC_REQUIRE(!tx5_fk.is_terminal()); + + system::data_chunk prevouts{}; + table::prevout::spends spends{}; + tx_links conflicts{}; + BC_REQUIRE(query.get_block_prevouts(prevouts, spends, conflicts, 2)); + std::ranges::sort(conflicts); + BC_REQUIRE(conflicts == (tx_links{ std::min(tx4_fk.value, tx5_fk.value), std::max(tx4_fk.value, tx5_fk.value) })); +} + BC_AUTO_TEST_CASE(query_consensus__get_block_prevouts__internal_spend__terminal) { settings settings{ test::database_settings() };