diff --git a/builds/gnu/Makefile.am b/builds/gnu/Makefile.am index 5922f7a6c..5269db47c 100644 --- a/builds/gnu/Makefile.am +++ b/builds/gnu/Makefile.am @@ -391,6 +391,7 @@ include_bitcoin_database_types_HEADERS = \ ${srcdir}/../../include/bitcoin/database/types/pooled_tx.hpp \ ${srcdir}/../../include/bitcoin/database/types/position.hpp \ ${srcdir}/../../include/bitcoin/database/types/span.hpp \ + ${srcdir}/../../include/bitcoin/database/types/touch.hpp \ ${srcdir}/../../include/bitcoin/database/types/type.hpp \ ${srcdir}/../../include/bitcoin/database/types/types.hpp \ ${srcdir}/../../include/bitcoin/database/types/unspent_coin.hpp \ diff --git a/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj b/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj index 31ef8ccd0..c4ed35e09 100644 --- a/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj @@ -225,6 +225,7 @@ + diff --git a/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj.filters index 50412ef44..caaaa7099 100644 --- a/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-database/libbitcoin-database.vcxproj.filters @@ -428,6 +428,9 @@ include\bitcoin\database\types + + include\bitcoin\database\types + include\bitcoin\database\types diff --git a/include/bitcoin/database.hpp b/include/bitcoin/database.hpp index b8010bdf0..d2fecec71 100644 --- a/include/bitcoin/database.hpp +++ b/include/bitcoin/database.hpp @@ -106,6 +106,7 @@ #include #include #include +#include #include #include #include diff --git a/include/bitcoin/database/impl/query/address/address_history.ipp b/include/bitcoin/database/impl/query/address/address_history.ipp index 1727cb66f..88ac4ecbf 100644 --- a/include/bitcoin/database/impl/query/address/address_history.ipp +++ b/include/bitcoin/database/impl/query/address/address_history.ipp @@ -247,6 +247,98 @@ histories CLASS::get_spenders_history(const hash_digest& key, return get_spenders_history({ key , index }); } +// get_confirmed_touches +// ---------------------------------------------------------------------------- + +// server/btcd +TEMPLATE +code CLASS::get_confirmed_touches(const stopper& cancel, touches& out, + const hash_digest& key, size_t start, size_t end, size_t limit, + bool turbo) 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; + + const auto policy = poolstl::execution::par_if(turbo, parallel_floor); + stopper fail{}; + + // Each output resolves to its parent tx followed by its spending txs. + std::vector touched(outs.size()); + std::transform(policy, outs.cbegin(), outs.cend(), parents.cbegin(), + touched.begin(), [this, start, end, &cancel, &fail]( + const output_link& output, const tx_link& parent) NOEXCEPT + { + touches txs{}; + if (cancel || fail) + return txs; + + if (!push_confirmed_touch(txs, parent, start, end, true)) + fail = true; + + for (const auto& input: to_spenders(parent, output)) + { + const auto spender = to_input_tx(input); + if (!push_confirmed_touch(txs, spender, start, end, false)) + fail = true; + } + + return txs; + }); + + if (fail) + return error::integrity; + + if (cancel) + return error::query_canceled; + + for (const auto& txs: touched) + out.insert(out.end(), txs.begin(), txs.end()); + + touch::sort_and_merge(out); + return error::success; +} + +// server/btcd +TEMPLATE +code CLASS::get_confirmed_touches(touches& out, const point& prevout, + size_t start, size_t end) const NOEXCEPT +{ + out.clear(); + for (const auto& input: to_spenders(prevout)) + if (!push_confirmed_touch(out, to_input_tx(input), start, end, false)) + return error::integrity; + + touch::sort_and_merge(out); + return error::success; +} + +// protected +TEMPLATE +bool CLASS::push_confirmed_touch(touches& out, const tx_link& link, + size_t start, size_t end, bool funds) const NOEXCEPT +{ + if (link.is_terminal()) + return false; + + tx_link strong{}; + const auto block = find_strong(strong, link); + const auto at = get_confirmed_height(block); + if (at.is_terminal() || system::is_limited(at.value, start, end)) + return true; + + size_t position{}; + if (!get_tx_position(position, strong, block)) + return false; + + out.emplace_back(at.value, position, strong, funds, !funds); + return true; +} + // utilities // ---------------------------------------------------------------------------- diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 21f0eaf71..a75dc1c2b 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -1169,6 +1169,15 @@ class query histories get_spenders_history(const hash_digest& key, uint32_t index) const NOEXCEPT; + /// Confirmed txs at heights [start, end] that fund or spend key's outputs. + code get_confirmed_touches(const stopper& cancel, touches& out, + const hash_digest& key, size_t start=zero, size_t end=max_size_t, + size_t limit=max_size_t, bool turbo=false) const NOEXCEPT; + + /// Confirmed txs at heights [start, end] that spend the prevout. + code get_confirmed_touches(touches& out, const point& prevout, + size_t start=zero, size_t end=max_size_t) const NOEXCEPT; + /// Unspent queries. unspent_output get_tx_unspent(const output_link& link) const NOEXCEPT; unspent_output get_tx_confirmed_unspent(const output_link& link) const NOEXCEPT; @@ -1565,6 +1574,8 @@ class query const tx_link& link) const NOEXCEPT; code get_address_txs(const stopper& cancel, tx_links& out, const hash_digest& key, size_t limit, bool turbo) const NOEXCEPT; + bool push_confirmed_touch(touches& out, const tx_link& link, size_t start, + size_t end, bool funds) const NOEXCEPT; private: // This value should never be read, but may be useful in debugging. diff --git a/include/bitcoin/database/types/touch.hpp b/include/bitcoin/database/types/touch.hpp new file mode 100644 index 000000000..80a8e053d --- /dev/null +++ b/include/bitcoin/database/types/touch.hpp @@ -0,0 +1,93 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_DATABASE_TYPES_TOUCH_HPP +#define LIBBITCOIN_DATABASE_TYPES_TOUCH_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace database { + +/// Confirmed tx that funds and/or spends a watched output. +struct touch +{ + /// Sort by block order and merge elements of the same tx. + static inline void sort_and_merge(std::vector& out) NOEXCEPT + { + if (out.empty()) + return; + + std::sort(out.begin(), out.end()); + auto last = out.begin(); + for (auto it = std::next(last); it != out.end(); ++it) + { + if (*it == *last) + { + last->funds = last->funds || it->funds; + last->spends = last->spends || it->spends; + } + else + { + ++last; + *last = *it; + } + } + + out.erase(std::next(last), out.end()); + } + + /// Block order (height then position). + inline bool operator<(const touch& other) const NOEXCEPT + { + return (height == other.height) ? (position < other.position) : + (height < other.height); + } + + /// Same confirmed tx (flags are not considered). + inline bool operator==(const touch& other) const NOEXCEPT + { + return height == other.height && position == other.position; + } + + /// Confirmed block height. + size_t height{}; + + /// Tx position in the confirmed block. + size_t position{}; + + /// Strong tx. + tx_link link{}; + + /// Tx has a watched output. + bool funds{}; + + /// Tx spends a watched output. + bool spends{}; +}; + +using touches = std::vector; + +} // namespace database +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/database/types/types.hpp b/include/bitcoin/database/types/types.hpp index 895737a89..573279dab 100644 --- a/include/bitcoin/database/types/types.hpp +++ b/include/bitcoin/database/types/types.hpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include diff --git a/test/query/address/address_history.cpp b/test/query/address/address_history.cpp index 5c7573908..bb6df6114 100644 --- a/test/query/address/address_history.cpp +++ b/test/query/address/address_history.cpp @@ -647,4 +647,149 @@ BC_AUTO_TEST_CASE(query_address__get_history__duplicate_instances__deduplicated) BC_REQUIRE_EQUAL(confirmed.at(1).tx.hash(), test::tx4.hash(false)); } +// get_confirmed_touches + +BC_AUTO_TEST_CASE(query_address__get_confirmed_touches__block1a_address0__block_order_flagged) +{ + settings settings{ test::database_settings() }; + 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)); + + touches out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE_EQUAL(query.get_confirmed_touches(cancel, out, test::block1a_address0), error::success); + + // tx1 funds, tx2 spends tx1 and funds, tx3 funds, tx6 spends tx1 and funds. + BC_REQUIRE_EQUAL(out.size(), 4u); + BC_CHECK_EQUAL(out.at(0).height, 1u); + BC_CHECK_EQUAL(out.at(0).position, 0u); + BC_CHECK_EQUAL(query.get_tx_key(out.at(0).link), test::block1a.transactions_ptr()->at(0)->hash(false)); + BC_CHECK(out.at(0).funds); + BC_CHECK(!out.at(0).spends); + BC_CHECK_EQUAL(out.at(1).height, 2u); + BC_CHECK_EQUAL(out.at(1).position, 0u); + BC_CHECK_EQUAL(query.get_tx_key(out.at(1).link), test::block2a.transactions_ptr()->at(0)->hash(false)); + BC_CHECK(out.at(1).funds); + BC_CHECK(out.at(1).spends); + BC_CHECK_EQUAL(out.at(2).height, 2u); + BC_CHECK_EQUAL(out.at(2).position, 1u); + BC_CHECK_EQUAL(query.get_tx_key(out.at(2).link), test::block2a.transactions_ptr()->at(1)->hash(false)); + BC_CHECK(out.at(2).funds); + BC_CHECK(!out.at(2).spends); + BC_CHECK_EQUAL(out.at(3).height, 3u); + BC_CHECK_EQUAL(out.at(3).position, 0u); + BC_CHECK_EQUAL(query.get_tx_key(out.at(3).link), test::block3a.transactions_ptr()->at(0)->hash(false)); + BC_CHECK(out.at(3).funds); + BC_CHECK(out.at(3).spends); +} + +BC_AUTO_TEST_CASE(query_address__get_confirmed_touches__turbo__same) +{ + settings settings{ test::database_settings() }; + 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)); + + touches out{}; + touches turbo{}; + const std::atomic_bool cancel{}; + BC_REQUIRE_EQUAL(query.get_confirmed_touches(cancel, out, test::block1a_address0), error::success); + BC_REQUIRE_EQUAL(query.get_confirmed_touches(cancel, turbo, test::block1a_address0, zero, max_size_t, max_size_t, true), error::success); + BC_REQUIRE_EQUAL(turbo.size(), 4u); + BC_CHECK(turbo == out); +} + +BC_AUTO_TEST_CASE(query_address__get_confirmed_touches__height_range__excludes_outside) +{ + settings settings{ test::database_settings() }; + 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)); + + touches out{}; + const std::atomic_bool cancel{}; + BC_REQUIRE_EQUAL(query.get_confirmed_touches(cancel, out, test::block1a_address0, 2, 2), error::success); + BC_REQUIRE_EQUAL(out.size(), 2u); + BC_CHECK_EQUAL(out.at(0).height, 2u); + BC_CHECK_EQUAL(out.at(0).position, 0u); + BC_CHECK_EQUAL(out.at(1).height, 2u); + BC_CHECK_EQUAL(out.at(1).position, 1u); +} + +BC_AUTO_TEST_CASE(query_address__get_confirmed_touches__limited__depth_limited_empty) +{ + settings settings{ test::database_settings() }; + 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)); + + touches out{}; + const std::atomic_bool cancel{}; + BC_CHECK_EQUAL(query.get_confirmed_touches(cancel, out, test::block1a_address0, zero, max_size_t, 8), error::depth_limited); + BC_CHECK(out.empty()); +} + +BC_AUTO_TEST_CASE(query_address__get_confirmed_touches__canceled__query_canceled) +{ + settings settings{ test::database_settings() }; + 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)); + + touches out{}; + const std::atomic_bool cancel{ true }; + BC_CHECK_EQUAL(query.get_confirmed_touches(cancel, out, test::block1a_address0), error::query_canceled); + BC_CHECK(out.empty()); +} + +BC_AUTO_TEST_CASE(query_address__get_confirmed_touches__prevout__confirmed_spenders) +{ + settings settings{ test::database_settings() }; + 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)); + + // tx2 (block2a) and tx6 (block3a) spend tx1:0. + touches out{}; + const system::chain::point prevout{ test::block1a.transactions_ptr()->at(0)->hash(false), 0 }; + BC_REQUIRE_EQUAL(query.get_confirmed_touches(out, prevout), error::success); + BC_REQUIRE_EQUAL(out.size(), 2u); + BC_CHECK_EQUAL(out.at(0).height, 2u); + BC_CHECK_EQUAL(out.at(0).position, 0u); + BC_CHECK(!out.at(0).funds); + BC_CHECK(out.at(0).spends); + BC_CHECK_EQUAL(out.at(1).height, 3u); + BC_CHECK_EQUAL(out.at(1).position, 0u); + BC_CHECK(out.at(1).spends); +} + +BC_AUTO_TEST_CASE(query_address__get_confirmed_touches__prevout_height_range__excludes_outside) +{ + settings settings{ test::database_settings() }; + 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)); + + touches out{}; + const system::chain::point prevout{ test::block1a.transactions_ptr()->at(0)->hash(false), 0 }; + BC_REQUIRE_EQUAL(query.get_confirmed_touches(out, prevout, 3, 3), error::success); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_CHECK_EQUAL(out.at(0).height, 3u); +} + BC_AUTO_TEST_SUITE_END()