Skip to content

Commit

Permalink
Merge pull request #524 from evoskuil/master
Browse files Browse the repository at this point in the history
Add put counts and block to put translations.
  • Loading branch information
evoskuil authored Jan 1, 2025
2 parents 87351a6 + c8e9207 commit c4cec94
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 70 deletions.
4 changes: 2 additions & 2 deletions include/bitcoin/database/impl/query/archive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ typename CLASS::inputs_ptr CLASS::get_inputs(
{
// TODO: eliminate shared memory pointer reallocations.
using namespace system;
const auto fks = to_tx_spends(link);
const auto fks = to_spends(link);
if (fks.empty())
return {};

Expand All @@ -450,7 +450,7 @@ typename CLASS::outputs_ptr CLASS::get_outputs(
{
// TODO: eliminate shared memory pointer reallocations.
using namespace system;
const auto fks = to_tx_outputs(link);
const auto fks = to_outputs(link);
if (fks.empty())
return {};

Expand Down
21 changes: 21 additions & 0 deletions include/bitcoin/database/impl/query/extent.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef LIBBITCOIN_DATABASE_QUERY_EXTENT_IPP
#define LIBBITCOIN_DATABASE_QUERY_EXTENT_IPP

#include <numeric>
#include <bitcoin/system.hpp>
#include <bitcoin/database/define.hpp>

Expand Down Expand Up @@ -207,6 +208,26 @@ two_counts CLASS::put_counts(const tx_link& link) const NOEXCEPT
return { tx.ins_count, tx.outs_count };
}

TEMPLATE
size_t CLASS::input_count(const tx_links& txs) const NOEXCEPT
{
const auto fn = [this](auto tx) NOEXCEPT { return input_count(tx); };
return std_reduce(bc::par_unseq, txs.begin(), txs.end(), zero, fn);
}

TEMPLATE
size_t CLASS::output_count(const tx_links& txs) const NOEXCEPT
{
const auto fn = [this](auto tx) NOEXCEPT { return output_count(tx); };
return std_reduce(bc::par_unseq, txs.begin(), txs.end(), zero, fn);
}

TEMPLATE
two_counts CLASS::put_counts(const tx_links& txs) const NOEXCEPT
{
return { input_count(txs), output_count(txs) };
}

TEMPLATE
bool CLASS::address_enabled() const NOEXCEPT
{
Expand Down
136 changes: 92 additions & 44 deletions include/bitcoin/database/impl/query/translate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <iterator>
#include <numeric>
#include <unordered_map>
#include <utility>
#include <bitcoin/system.hpp>
Expand Down Expand Up @@ -326,7 +327,7 @@ uint32_t CLASS::to_spend_index(const tx_link& parent_fk,
const spend_link& spend_fk) const NOEXCEPT
{
uint32_t index{};
for (const auto& in_fk: to_tx_spends(parent_fk))
for (const auto& in_fk: to_spends(parent_fk))
{
if (in_fk == spend_fk) return index;
++index;
Expand All @@ -341,7 +342,7 @@ uint32_t CLASS::to_output_index(const tx_link& parent_fk,
const output_link& output_fk) const NOEXCEPT
{
uint32_t index{};
for (const auto& out_fk: to_tx_outputs(parent_fk))
for (const auto& out_fk: to_outputs(parent_fk))
{
if (out_fk == output_fk) return index;
++index;
Expand All @@ -356,7 +357,7 @@ spend_link CLASS::to_spender(const tx_link& link,
const foreign_point& point) const NOEXCEPT
{
table::spend::get_key spend{};
for (const auto& spend_fk: to_tx_spends(link))
for (const auto& spend_fk: to_spends(link))
if (store_.spend.get(spend_fk, spend) && (spend.key == point))
return spend_fk;

Expand Down Expand Up @@ -410,7 +411,7 @@ spend_links CLASS::to_spenders(const foreign_point& point) const NOEXCEPT
return fault;

auto found{ false };
for (const auto& spend_fk: to_tx_spends(spender.parent_fk))
for (const auto& spend_fk: to_spends(spender.parent_fk))
{
table::spend::get_key spend{};
if (!store_.spend.get(it, spend_fk, spend))
Expand All @@ -435,7 +436,22 @@ spend_links CLASS::to_spenders(const foreign_point& point) const NOEXCEPT
// ----------------------------------------------------------------------------

TEMPLATE
output_links CLASS::to_tx_outputs(const tx_link& link) const NOEXCEPT
spend_links CLASS::to_spends(const tx_link& link) const NOEXCEPT
{
table::transaction::get_puts tx{};
if (!store_.tx.get(link, tx))
return {};

table::puts::get_spends puts{};
puts.spend_fks.resize(tx.ins_count);
if (!store_.puts.get(tx.puts_fk, puts))
return {};

return std::move(puts.spend_fks);
}

TEMPLATE
output_links CLASS::to_outputs(const tx_link& link) const NOEXCEPT
{
table::transaction::get_puts tx{};
if (!store_.tx.get(link, tx))
Expand All @@ -450,18 +466,18 @@ output_links CLASS::to_tx_outputs(const tx_link& link) const NOEXCEPT
}

TEMPLATE
spend_links CLASS::to_tx_spends(const tx_link& link) const NOEXCEPT
output_links CLASS::to_prevouts(const tx_link& link) const NOEXCEPT
{
table::transaction::get_puts tx{};
if (!store_.tx.get(link, tx))
const auto spends = to_spends(link);
if (spends.empty())
return {};

table::puts::get_spends puts{};
puts.spend_fks.resize(tx.ins_count);
if (!store_.puts.get(tx.puts_fk, puts))
return {};
output_links prevouts{};
prevouts.reserve(spends.size());
for (const auto& spend: spends)
prevouts.push_back(to_prevout(spend));

return std::move(puts.spend_fks);
return prevouts;
}

// protected
Expand Down Expand Up @@ -502,29 +518,71 @@ spend_set CLASS::to_spend_set(const tx_link& link) const NOEXCEPT
return set;
}

// block to txs/puts (forward navigation)
// txs to puts (forward navigation)
// ----------------------------------------------------------------------------

TEMPLATE
tx_links CLASS::to_transactions(const header_link& link) const NOEXCEPT
spend_links CLASS::to_spends(const tx_links& txs) const NOEXCEPT
{
table::txs::get_txs txs{};
if (!store_.txs.find(link, txs))
return {};
spend_links spends{};
for (const auto& tx: txs)
{
const auto tx_spends = to_spends(tx);
spends.insert(spends.end(), tx_spends.begin(), tx_spends.end());
}

return std::move(txs.tx_fks);
return spends;
}

TEMPLATE
tx_links CLASS::to_spending_transactions(const header_link& link) const NOEXCEPT
output_links CLASS::to_outputs(const tx_links& txs) const NOEXCEPT
{
table::txs::get_spending_txs txs{};
if (!store_.txs.find(link, txs))
return {};
output_links outputs{};
for (const auto& tx: txs)
{
const auto tx_outputs = to_outputs(tx);
outputs.insert(outputs.end(), tx_outputs.begin(), tx_outputs.end());
}

return std::move(txs.tx_fks);
return outputs;
}

TEMPLATE
output_links CLASS::to_prevouts(const tx_links& txs) const NOEXCEPT
{
const auto ins = to_spends(txs);
output_links outs(ins.size());
const auto fn = [this](auto spend) NOEXCEPT{ return to_prevout(spend); };

// C++17 incomplete on GCC/CLang, so presently parallel only on MSVC++.
std_transform(bc::par_unseq, ins.begin(), ins.end(), outs.begin(), fn);
return outs;
}

// block to puts (forward navigation)
// ----------------------------------------------------------------------------

TEMPLATE
spend_links CLASS::to_block_spends(const header_link& link) const NOEXCEPT
{
return to_spends(to_spending_transactions(link));
}

TEMPLATE
output_links CLASS::to_block_outputs(const header_link& link) const NOEXCEPT
{
return to_outputs(to_transactions(link));
}

TEMPLATE
output_links CLASS::to_block_prevouts(const header_link& link) const NOEXCEPT
{
return to_prevouts(to_spending_transactions(link));
}

// block to txs (forward navigation)
// ----------------------------------------------------------------------------

TEMPLATE
tx_link CLASS::to_coinbase(const header_link& link) const NOEXCEPT
{
Expand All @@ -536,33 +594,23 @@ tx_link CLASS::to_coinbase(const header_link& link) const NOEXCEPT
}

TEMPLATE
spend_links CLASS::to_block_spends(const header_link& link) const NOEXCEPT
tx_links CLASS::to_transactions(const header_link& link) const NOEXCEPT
{
spend_links spends{};
const auto txs = to_transactions(link);

for (const auto& tx: txs)
{
const auto tx_spends = to_tx_spends(tx);
spends.insert(spends.end(), tx_spends.begin(), tx_spends.end());
}
table::txs::get_txs txs{};
if (!store_.txs.find(link, txs))
return {};

return spends;
return std::move(txs.tx_fks);
}

TEMPLATE
output_links CLASS::to_block_outputs(const header_link& link) const NOEXCEPT
tx_links CLASS::to_spending_transactions(const header_link& link) const NOEXCEPT
{
output_links outputs{};
const auto txs = to_transactions(link);

for (const auto& tx: txs)
{
const auto tx_outputs = to_tx_outputs(tx);
outputs.insert(outputs.end(), tx_outputs.begin(), tx_outputs.end());
}
table::txs::get_spending_txs txs{};
if (!store_.txs.find(link, txs))
return {};

return outputs;
return std::move(txs.tx_fks);
}

// hashmap enumeration
Expand Down
24 changes: 18 additions & 6 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ class query
size_t input_count(const tx_link& link) const NOEXCEPT;
size_t output_count(const tx_link& link) const NOEXCEPT;
two_counts put_counts(const tx_link& link) const NOEXCEPT;
size_t input_count(const tx_links& txs) const NOEXCEPT;
size_t output_count(const tx_links& txs) const NOEXCEPT;
two_counts put_counts(const tx_links& txs) const NOEXCEPT;

/// Optional table state.
bool address_enabled() const NOEXCEPT;
Expand Down Expand Up @@ -270,7 +273,7 @@ class query
uint32_t output_index) const NOEXCEPT;
output_link to_prevout(const spend_link& link) const NOEXCEPT;

/// block/tx to block/s (reverse navigation)
/// block/tx to block (reverse navigation)
header_link to_parent(const header_link& link) const NOEXCEPT;
header_link to_block(const tx_link& key) const NOEXCEPT;

Expand All @@ -282,15 +285,24 @@ class query
uint32_t output_index) const NOEXCEPT;

/// tx to puts (forward navigation)
output_links to_tx_outputs(const tx_link& link) const NOEXCEPT;
spend_links to_tx_spends(const tx_link& link) const NOEXCEPT;
spend_links to_spends(const tx_link& link) const NOEXCEPT;
output_links to_outputs(const tx_link& link) const NOEXCEPT;
output_links to_prevouts(const tx_link& link) const NOEXCEPT;

/// block to txs/puts (forward navigation)
/// txs to puts (forward navigation)
spend_links to_spends(const tx_links& txs) const NOEXCEPT;
output_links to_outputs(const tx_links& txs) const NOEXCEPT;
output_links to_prevouts(const tx_links& txs) const NOEXCEPT;

/// block to puts (forward navigation)
spend_links to_block_spends(const header_link& link) const NOEXCEPT;
output_links to_block_outputs(const header_link& link) const NOEXCEPT;
output_links to_block_prevouts(const header_link& link) const NOEXCEPT;

/// block to txs (forward navigation)
tx_link to_coinbase(const header_link& link) const NOEXCEPT;
tx_links to_transactions(const header_link& link) const NOEXCEPT;
tx_links to_spending_transactions(const header_link& link) const NOEXCEPT;
output_links to_block_outputs(const header_link& link) const NOEXCEPT;
spend_links to_block_spends(const header_link& link) const NOEXCEPT;

/// hashmap enumeration
header_link top_header(size_t bucket) const NOEXCEPT;
Expand Down
Loading

0 comments on commit c4cec94

Please sign in to comment.