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
15 changes: 15 additions & 0 deletions include/bitcoin/database/impl/query/archive/chain_reader.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ typename CLASS::point CLASS::get_point(
return { point.hash, point.index };
}

// ins_link->sequence
// ----------------------------------------------------------------------------

TEMPLATE
bool CLASS::get_input_sequence(uint32_t& out,
const ins_link& link) const NOEXCEPT
{
table::ins_sequence::get_input ins{};
if (!store_.ins.sequence.get(link, ins))
return false;

out = ins.sequence;
return true;
}

// ins_link->witness
// ----------------------------------------------------------------------------

Expand Down
9 changes: 7 additions & 2 deletions include/bitcoin/database/impl/query/archive/chain_writer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,15 @@ code CLASS::set_code(const block& block, const header_link& key,
if (fk.is_terminal())
return error::tx_tx_allocate;

// A coinbase is never pruned, so neither is a block of only its coinbase.
code ec{};
auto it = links.begin();
for (const auto& tx: *block.transactions_ptr())
{
if (*it == tx_link::terminal)
{
if ((ec = set_code(fk, *tx, bypass, prune)))
const auto pruned = prune && !tx->is_coinbase();
if ((ec = set_code(fk, *tx, bypass, pruned)))
return ec;

*it = fk++;
Expand All @@ -471,8 +473,11 @@ code CLASS::set_code(const block& block, const header_link& key,
++it;
}

const auto& all = *block.transactions_ptr();
const auto pruned = prune && std::any_of(all.begin(), all.end(),
[](const auto& tx) NOEXCEPT { return !tx->is_coinbase(); });
return set_txs(key, std::move(links), light, heavy, std::move(interval),
strong, prune);
strong, pruned);
// ========================================================================
}

Expand Down
11 changes: 7 additions & 4 deletions include/bitcoin/database/impl/query/archive/wire_writer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ code CLASS::set_code(const block_view& block, const header_link& key,

points += tx.inputs();
outputs += tx.outputs();
input_bytes += tx.input_table_size(prune);
input_bytes += tx.input_table_size(prune && !tx.is_coinbase());
output_bytes += tx.outputs() * tx_link::size + tx.output_table_size();
}

Expand Down Expand Up @@ -332,10 +332,11 @@ code CLASS::set_code(const block_view& block, const header_link& key,
{
if (*it == tx_link::terminal)
{
if ((ec = set_code(twins, ptrs, fks, tx, bypass, prune)))
const auto pruned = prune && !tx.is_coinbase();
if ((ec = set_code(twins, ptrs, fks, tx, bypass, pruned)))
return ec;

const auto in_bytes = tx.input_table_size(prune);
const auto in_bytes = tx.input_table_size(pruned);

// Output rows are parent fk prefixed (see table::output::put_view).
const auto out_bytes = tx.outputs() * tx_link::size +
Expand Down Expand Up @@ -365,8 +366,10 @@ code CLASS::set_code(const block_view& block, const header_link& key,
if (!store_.duplicate.put(twin, table::duplicate::record{}))
return error::tx_duplicate_put;

const auto pruned = prune && std::any_of(views.begin(), views.end(),
[](const auto& tx) NOEXCEPT { return !tx.is_coinbase(); });
return set_txs(key, std::move(links), light, heavy, std::move(interval),
strong, prune);
strong, pruned);
// ========================================================================
}

Expand Down
64 changes: 59 additions & 5 deletions include/bitcoin/database/impl/query/pool/pool_confirmable.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,69 @@ code CLASS::get_confirmable_txs(tx_links& out) const NOEXCEPT
if (!algorithm::is_present(cell))
continue;

const auto tx = to_pool_tx(pool_link{ row });
if (!tx.is_terminal())
out.push_back(tx);
out.push_back(tx_link{ store_.pool.get_key(pool_link{ row }) });
}

return error::success;
}

TEMPLATE
code CLASS::get_pooled_ancestry(pooled_rows& out,
const tx_links& links) const NOEXCEPT
{
using namespace system;
using algorithm = system::pool::algorithm;
using link_t = algorithm::link_t;
out.clear();
if (!store_.pool.enabled())
return error::success;

pool_guards guards{};
algorithm::columns_t columns{};
if (!get_pool_columns(columns, guards))
return error::integrity;

std::vector<link_t> pending{};
pending.reserve(links.size());
for (const tx_link link: links)
{
const auto row = store_.pool.first(link);
if (row.is_terminal())
return error::integrity;

pending.push_back(possible_narrow_cast<link_t>(row.value));
}

std::unordered_set<link_t> seen{};
while (!pending.empty())
{
const auto row = pending.back();
pending.pop_back();
if (!seen.insert(row).second)
continue;

const auto edges = algorithm::from_column(columns.edges[row]);
const auto end = edges.first + edges.count;
for (auto input = edges.first; input < end; ++input)
{
const auto parent = algorithm::from_column(columns.rows[input]);
if (parent == algorithm::none)
continue;

const auto cell = algorithm::from_column(columns.cells[parent]);
if (!algorithm::is_boundary(cell))
pending.push_back(parent);
}

const tx_link tx{ store_.pool.get_key(pool_link{ row }) };
out.push_back({ row, tx.value,
algorithm::from_column(columns.totals[row]) });
}

std::ranges::sort(out, {}, &pooled_row::row);
return error::success;
}

TEMPLATE
code CLASS::get_confirmable_fees(fee_rates& out) const NOEXCEPT
{
Expand All @@ -167,8 +222,7 @@ code CLASS::get_confirmable_fees(fee_rates& out) const NOEXCEPT
for (link_t row{}; row < rows; ++row)
{
const auto cell = algorithm::from_column(columns.cells[row]);
if (!algorithm::is_present(cell) ||
to_pool_tx(pool_link{ row }).is_terminal())
if (!algorithm::is_present(cell))
continue;

const auto totals = algorithm::from_column(columns.totals[row]);
Expand Down
12 changes: 5 additions & 7 deletions include/bitcoin/database/impl/query/pool/pool_coverage.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,12 @@ code CLASS::get_pool_coverage(bool& out, const pool_graph& graph,
!algorithm::ancestry(sum, g, component, limit))
return error::pool_limit;

const auto outcomes = algorithm::outcomes(component);
if (outcomes > maximum_outcomes)
std::vector<mask_t> masks{};
algorithm::leaves(masks, component, g, maximum_outcomes, rate,
bytes_per_sigop);
if (masks.empty())
return error::pool_limit;

std::vector<mask_t> masks(outcomes);
for (size_t index{}; index < outcomes; ++index)
masks[index] = algorithm::outcome(component, index);

const auto best = algorithm::evaluate(masks, component, g);
const auto bytes = algorithm::chargeable(component, g, bytes_per_sigop);
each = each && algorithm::covered(best, bytes, rate);
Expand All @@ -247,7 +245,7 @@ code CLASS::get_pool_coverage(bool& out, const pool_graph& graph,
// family within a block, beyond which each component pays its own.
uint64_t fee{}, bytes{};
const auto bounded = algorithm::family(fee, bytes, g, measured,
limit.size, bytes_per_sigop, maximum_outcomes);
limit.size, rate, bytes_per_sigop, maximum_outcomes);

out = bounded ? algorithm::covered(fee, bytes, rate) : each;

Expand Down
43 changes: 43 additions & 0 deletions include/bitcoin/database/impl/query/pool/pool_template.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,49 @@ code CLASS::get_pool_template(tx_links& out, pool_projection& projection,
return error::success;
}

TEMPLATE
code CLASS::get_template_txs(template_txs& out,
const tx_links& links) const NOEXCEPT
{
using prevout = table::prevout::slab_get;
out.clear();
out.reserve(links.size());
std::unordered_map<tx_link::integer, size_t> positions{};
positions.reserve(links.size());
for (const tx_link link: links)
{
table::pool_edge edge{};
table::pool_total total{};
const auto fk = store_.pool.first(link);
if (fk.is_terminal() || !store_.pool.totals.get(fk, total) ||
!store_.pool.edges.get(fk, edge))
return error::integrity;

table::spends::get_refs::parents parents(edge.inputs);
table::spends::get_refs spent{ {}, parents };
if (!store_.spends.get(edge.spends_fk, spent))
return error::integrity;

template_tx tx{ get_tx_key(link), get_wtxid_key(fk), total.fee,
total.sigops, {} };
for (const auto merged: parents)
{
const tx_link parent{ prevout::output_tx_fk(merged) };
const auto it = positions.find(parent.value);
if (it == positions.end())
continue;

if (!system::contains(tx.parents, it->second))
tx.parents.push_back(it->second);
}

positions.emplace(link.value, out.size());
out.push_back(std::move(tx));
}

return error::success;
}

// protected
TEMPLATE
void CLASS::set_pool_resolution(pool_projection& projection,
Expand Down
28 changes: 21 additions & 7 deletions include/bitcoin/database/impl/query/pool/pool_writer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,29 @@ bool CLASS::set_pooled(const tx_link& link, const transaction& tx,
TEMPLATE
bool CLASS::set_pooled(size_t& repooled, const tx_link& link,
const transaction& tx, const context& ctx, uint32_t time) NOEXCEPT
{
using namespace system::chain;
const auto bip16 = ctx.is_enabled(flags::bip16_rule);
const auto bip141 = ctx.is_enabled(flags::bip141_rule);
const auto sigops = tx.signature_operations(bip16, bip141);
return set_pooled(repooled, link, tx, ctx, time, sigops, true);
}

TEMPLATE
bool CLASS::set_pooled(size_t& repooled, const tx_link& link,
const transaction& tx, const context& ctx, uint32_t time, size_t sigops,
bool archived) NOEXCEPT
{
repooled = zero;
if (!store_.pool.enabled())
return true;

if (!set_pool_row(link, tx, ctx, time, sigops))
return false;

tx_links purged{};
if (!set_pool_row(link, tx, ctx, time) || !set_pool_purged(purged, link) ||
!set_pool_repooled(purged))
if (archived && (!set_pool_purged(purged, link) ||
!set_pool_repooled(purged)))
return false;

repooled = purged.size();
Expand All @@ -61,7 +76,7 @@ bool CLASS::set_pooled(size_t& repooled, const tx_link& link,
// protected
TEMPLATE
bool CLASS::set_pool_row(const tx_link& link, const transaction& tx,
const context& ctx, uint32_t time) NOEXCEPT
const context& ctx, uint32_t time, size_t sigops) NOEXCEPT
{
using namespace system;
using algorithm = system::pool::algorithm;
Expand Down Expand Up @@ -103,9 +118,6 @@ bool CLASS::set_pool_row(const tx_link& link, const transaction& tx,
conflicted |= conflict;
}

const auto bip16 = ctx.is_enabled(chain::flags::bip16_rule);
const auto bip141 = ctx.is_enabled(chain::flags::bip141_rule);
const auto sigops = tx.signature_operations(bip16, bip141);
using lane_t = schema::pool::witness_lane;
const auto hash = tx.get_hash(true);
const auto words = from_little_endians(array_cast<lane_t>(hash));
Expand Down Expand Up @@ -254,9 +266,11 @@ bool CLASS::set_pool_repooled(const tx_links& purged) NOEXCEPT
{
const tx_link link{ value };
table::pool::record record{};
table::pool_total total{};
table::pool_scalar flags{}, height{}, median{};
const auto row = store_.pool.first(link);
if (!store_.pool.get(row, record) ||
!store_.pool.totals.get(row, total) ||
!store_.pool.flags.get(row, flags) ||
!store_.pool.heights.get(row, height) ||
!store_.pool.medians.get(row, median))
Expand All @@ -267,7 +281,7 @@ bool CLASS::set_pool_repooled(const tx_links& purged) NOEXCEPT
return false;

const context ctx{ flags.value, height.value, median.value };
if (!set_pool_row(link, *tx, ctx, record.time))
if (!set_pool_row(link, *tx, ctx, record.time, total.sigops))
return false;
}

Expand Down
16 changes: 15 additions & 1 deletion include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ class query
bool witness) const NOEXCEPT;

point get_point(const ins_link& link) const NOEXCEPT;
bool get_input_sequence(uint32_t& out, const ins_link& link) const NOEXCEPT;
witness::cptr get_witness(const ins_link& link) const NOEXCEPT;
script::cptr get_input_script(const ins_link& link) const NOEXCEPT;
input::cptr get_input(const ins_link& link, bool witness) const NOEXCEPT;
Expand Down Expand Up @@ -761,6 +762,10 @@ class query
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;
Expand Down Expand Up @@ -791,6 +796,11 @@ class query
bool set_pooled(size_t& repooled, const tx_link& link,
const transaction& tx, const context& ctx, uint32_t time) NOEXCEPT;

/// As above with sigops counted, descendants only if archived before.
bool set_pooled(size_t& repooled, const tx_link& link,
const transaction& tx, const context& ctx, uint32_t time,
size_t sigops, bool archived) NOEXCEPT;

/// Rebuild pool index heads from pool (after compaction).
bool set_pool_index() NOEXCEPT;

Expand Down Expand Up @@ -866,6 +876,10 @@ 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.
/// -----------------------------------------------------------------------

Expand Down Expand Up @@ -1269,7 +1283,7 @@ class query

/// Pool tx at a new row, its spenders not yet considered.
bool set_pool_row(const tx_link& link, const transaction& tx,
const context& ctx, uint32_t time) NOEXCEPT;
const context& ctx, uint32_t time, size_t sigops) NOEXCEPT;

/// Purge pooled spenders of tx and theirs, ascending by row.
bool set_pool_purged(tx_links& out, const tx_link& link) NOEXCEPT;
Expand Down
22 changes: 22 additions & 0 deletions include/bitcoin/database/types/pooled_tx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ struct pooled_tx
std::vector<prevout> prevouts{};
};

/// Pooled tx of template, ids and totals with positions of its parents.
struct template_tx
{
system::hash_digest txid{};
system::hash_digest wtxid{};
uint64_t fee{};
size_t sigops{};
std::vector<size_t> parents{};
};

using template_txs = std::vector<template_tx>;

/// Pool row of tx, with its totals.
struct pooled_row
{
table::pool::link::integer row{};
table::transaction::link::integer tx{};
system::pool::algorithm::totals_t totals{};
};

using pooled_rows = std::vector<pooled_row>;

} // namespace database
} // namespace libbitcoin

Expand Down
Loading
Loading