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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builds/gnu/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\pooled_tx.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\position.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\span.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\touch.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\type.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\types.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\unspent_coin.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\span.hpp">
<Filter>include\bitcoin\database\types</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\touch.hpp">
<Filter>include\bitcoin\database\types</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\database\types\type.hpp">
<Filter>include\bitcoin\database\types</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
#include <bitcoin/database/types/pooled_tx.hpp>
#include <bitcoin/database/types/position.hpp>
#include <bitcoin/database/types/span.hpp>
#include <bitcoin/database/types/touch.hpp>
#include <bitcoin/database/types/type.hpp>
#include <bitcoin/database/types/types.hpp>
#include <bitcoin/database/types/unspent_coin.hpp>
Expand Down
92 changes: 92 additions & 0 deletions include/bitcoin/database/impl/query/address/address_history.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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<touches> 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
// ----------------------------------------------------------------------------

Expand Down
11 changes: 11 additions & 0 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
93 changes: 93 additions & 0 deletions include/bitcoin/database/types/touch.hpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_DATABASE_TYPES_TOUCH_HPP
#define LIBBITCOIN_DATABASE_TYPES_TOUCH_HPP

#include <algorithm>
#include <iterator>
#include <vector>
#include <bitcoin/database/define.hpp>
#include <bitcoin/database/types/type.hpp>

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<touch>& 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<touch>;

} // namespace database
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/database/types/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <bitcoin/database/types/pooled_tx.hpp>
#include <bitcoin/database/types/position.hpp>
#include <bitcoin/database/types/span.hpp>
#include <bitcoin/database/types/touch.hpp>
#include <bitcoin/database/types/type.hpp>
#include <bitcoin/database/types/block_amounts.hpp>
#include <bitcoin/database/types/unspent_output.hpp>
Expand Down
Loading
Loading