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
43 changes: 37 additions & 6 deletions include/bitcoin/database/impl/query/consensus/consensus_block.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ code CLASS::block_confirmable(const header_link& link) const NOEXCEPT

TEMPLATE
code CLASS::validate_pooled(const header_link& link, const chain_context& ctx,
size_t subsidy_interval, uint64_t initial_subsidy) NOEXCEPT
size_t subsidy_interval, uint64_t initial_subsidy,
const system::chain::script& challenge) NOEXCEPT
{
using prevout = table::prevout;
if (!store_.pool.enabled())
Expand All @@ -130,8 +131,8 @@ code CLASS::validate_pooled(const header_link& link, const chain_context& ctx,
pooled_block block{};
if ((ec = get_pooled_block(block, link, context::from(ctx))) ||
(ec = check_pooled_block(block)) ||
(ec = accept_pooled_block(block, ctx, subsidy_interval,
initial_subsidy)))
(ec = accept_pooled_block(block, link, ctx, subsidy_interval,
initial_subsidy, challenge)))
return ec;

// ========================================================================
Expand Down Expand Up @@ -236,17 +237,23 @@ code CLASS::check_pooled_block(pooled_block& block) const NOEXCEPT
return error::success;
}

// Block check (context) and accept: weight, coinbase, hash limit, claim, sigops.
// Block check (context) and accept: weight, coinbase, tx size, signet solution,
// claim, sigops.
TEMPLATE
code CLASS::accept_pooled_block(const pooled_block& block,
const chain_context& ctx, size_t subsidy_interval,
uint64_t initial_subsidy) const NOEXCEPT
const header_link& link, const chain_context& ctx, size_t subsidy_interval,
uint64_t initial_subsidy,
const system::chain::script& challenge) const NOEXCEPT
{
using namespace system;
const auto bip16 = ctx.is_enabled(chain::flags::bip16_rule);
const auto bip34 = ctx.is_enabled(chain::flags::bip34_rule);
const auto bip42 = ctx.is_enabled(chain::flags::bip42_rule);
const auto bip141 = ctx.is_enabled(chain::flags::bip141_rule);
const auto bip54_txsize = ctx.is_enabled(chain::flags::bip54_txsize_rule);
const auto bip54_coinbase = ctx.is_enabled(
chain::flags::bip54_coinbase_rule);
const auto signet = ctx.is_enabled(chain::flags::signet_rule);

const auto weight = ceilinged_add(
ceilinged_multiply(chain::base_size_contribution, block.light),
Expand All @@ -270,6 +277,30 @@ code CLASS::accept_pooled_block(const pooled_block& block,
ctx.height))
return system::error::coinbase_height_mismatch;

if (bip54_txsize && std::ranges::any_of(block.records,
[](const auto& record) NOEXCEPT
{
return record.light == chain::invalid_transaction_size;
}))
return system::error::invalid_transaction_size;

if (bip54_coinbase && coinbase->locktime() != sub1(ctx.height))
return system::error::coinbase_locktime_mismatch;

if (bip54_coinbase && !ins.empty() && ins.front()->is_final())
return system::error::coinbase_sequence_final;

if (signet)
{
const auto header = get_header(link);
if (!header)
return error::integrity;

if (!chain::signet::verify(*header, *coinbase, get_tx_keys(link),
challenge))
return system::error::invalid_signet_solution;
}

const auto subsidy = chain::block::subsidy(ctx.height, subsidy_interval,
initial_subsidy, bip42);
if (coinbase->spend() > ceilinged_add(block.fees, subsidy))
Expand Down
28 changes: 28 additions & 0 deletions include/bitcoin/database/impl/query/pool/pool_estimate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ code CLASS::get_pool_projection(pool_projection& projection, size_t count,
return error::success;
}

// Rows pooled since the projection was resolved are not of its snapshot.
TEMPLATE
code CLASS::get_pool_continuation(pool_projection& projection, size_t count,
const pool_totals& budget, size_t depth,
uint32_t bytes_per_sigop) const NOEXCEPT
{
using algorithm = system::pool::algorithm;
if (!store_.pool.enabled())
return error::success;

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

if (projection.marks.size() > columns.cells.size())
return error::integrity;

projection.marks.resize(columns.cells.size(), algorithm::mark::excluded);

pool_links rows{};
while (!projection.exhausted && projection.blocks.size() < count)
algorithm::project(rows, projection, columns, budget, depth,
bytes_per_sigop);

return error::success;
}

// A row arrives at the height of its context, and counts while it can confirm
// or has confirmed. Rows of the top height are of the open interval.
TEMPLATE
Expand Down
18 changes: 8 additions & 10 deletions include/bitcoin/database/impl/query/pool/pool_index.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,11 @@ bool CLASS::is_pool_conflicted(bool& out, const tx_link& link,
return true;
}

// The conflicts of the block's txs are in its prevout cache, which the prune
// leaves in place until the block is settled, and otherwise are taken from
// the archive (a block that was not validated, or was pruned before a
// reorganization). They include the block's own txs, as each spends a
// conflicted point. A conflict is cleared at a reorganization only if no
// other confirmed tx spends a point of the row.
// The conflicts of the block's txs are the archived spenders of its points as
// of the transition, as a tx may be pooled after the block's prevouts are
// cached. They include the block's own txs, as each spends a conflicted point.
// A conflict is cleared at a reorganization only if no other confirmed tx
// spends a point of the row.
TEMPLATE
bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT
{
Expand Down Expand Up @@ -392,15 +391,14 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT
block.push_back(possible_narrow_cast<link_t>(row.value));
}

table::prevout::slab_get cache{};
if (!store_.prevout.at(to_prevout(link), cache) &&
!get_pool_conflicts(cache.conflicts, txs.tx_fks))
tx_links spenders{};
if (!get_pool_conflicts(spenders, txs.tx_fks))
return false;

auto own = txs.tx_fks;
std::ranges::sort(own);
pool_links conflicts{};
for (const auto conflict: cache.conflicts)
for (const auto conflict: spenders)
{
if (std::ranges::binary_search(own, conflict))
continue;
Expand Down
34 changes: 27 additions & 7 deletions include/bitcoin/database/impl/query/pool/pool_suffice.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
namespace libbitcoin {
namespace database {

// The context height of a row that is not validated, above every height.
constexpr uint32_t unvalidated_height = max_uint32;

TEMPLATE
code CLASS::get_pool_revalidation(tx_links& out,
const context& ctx) const NOEXCEPT
Expand Down Expand Up @@ -83,7 +86,7 @@ code CLASS::get_pool_unvalidated(tx_links& out) const NOEXCEPT
const auto cell = algorithm::from_column(columns.cells[row]);
if (!algorithm::is_absent(cell) ||
is_zero(bit_and(cell, algorithm::insufficient)) ||
is_nonzero(algorithm::from_column(columns.flags[row])))
algorithm::from_column(columns.heights[row]) != unvalidated_height)
continue;

const auto tx = to_pool_tx(pool_link{ row });
Expand All @@ -94,9 +97,10 @@ code CLASS::get_pool_unvalidated(tx_links& out) const NOEXCEPT
return error::success;
}

// The sigops of a tx are counted under the flags of its context.
TEMPLATE
bool CLASS::set_pooled_context(const tx_link& link,
const context& ctx) NOEXCEPT
bool CLASS::set_pooled_context(const tx_link& link, const context& ctx,
size_t sigops) NOEXCEPT
{
using scalar = table::pool_scalar;
if (!store_.pool.enabled())
Expand All @@ -110,8 +114,13 @@ bool CLASS::set_pooled_context(const tx_link& link,
if (fk.is_terminal())
return false;

table::pool_total total{};
auto guard = vtx.get_memory();
return guard &&
if (!guard || !vtx.totals.get(fk, total))
return false;

total.sigops = system::possible_narrow_cast<uint32_t>(sigops);
return vtx.totals.put(fk, total) &&
vtx.flags.put(fk, scalar{ {}, ctx.flags }) &&
vtx.heights.put(fk, scalar{ {}, ctx.height }) &&
vtx.medians.put(fk, scalar{ {}, ctx.mtp });
Expand All @@ -120,14 +129,20 @@ bool CLASS::set_pooled_context(const tx_link& link,

// A tx spending an output of the block (coinbase included) may depend on its
// confirmation, so it is not sufficient under any context until revalidated.
// Its flags are kept, as its sigops are counted under them.
TEMPLATE
bool CLASS::set_pool_unvalidated(size_t& out,
const header_link& link) NOEXCEPT
{
using scalar = table::pool_scalar;
out = zero;
if (!store_.pool.enabled())
return true;

// ========================================================================
const auto scope = get_transactor();

decltype(store_.pool.get_memory()) guard{};
for (const auto fk: to_transactions(link))
{
const tx_link tx{ fk };
Expand All @@ -136,11 +151,15 @@ bool CLASS::set_pool_unvalidated(size_t& out,
{
for (const auto& in: to_spenders(tx, index))
{
const auto spender = to_input_tx(in);
if (!is_pooled(spender))
const auto row = store_.pool.first(to_input_tx(in));
if (row.is_terminal())
continue;

if (!set_pooled_context(spender, {}))
if (!guard)
guard = store_.pool.get_memory();

if (!guard || !store_.pool.heights.put(row,
scalar{ {}, unvalidated_height }))
return false;

++out;
Expand All @@ -149,6 +168,7 @@ bool CLASS::set_pool_unvalidated(size_t& out,
}

return true;
// ========================================================================
}

TEMPLATE
Expand Down
21 changes: 17 additions & 4 deletions include/bitcoin/database/impl/query/pool/pool_writer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,31 @@ bool CLASS::set_pooled(const tx_link& link, const transaction& tx,
return set_pooled(link, tx, context::from(ctx), time);
}

// A pooled spender of the tx (pooled while the tx was confirmed) holds a lower
// row, so it and its pooled descendants are purged and pooled after the tx.
TEMPLATE
bool CLASS::set_pooled(const tx_link& link, const transaction& tx,
const context& ctx, uint32_t time) NOEXCEPT
{
size_t repooled{};
return set_pooled(repooled, link, tx, ctx, time);
}

// A pooled spender of the tx (pooled while the tx was confirmed) holds a lower
// row, so it and its pooled descendants are purged and pooled after the tx.
TEMPLATE
bool CLASS::set_pooled(size_t& repooled, const tx_link& link,
const transaction& tx, const context& ctx, uint32_t time) NOEXCEPT
{
repooled = zero;
if (!store_.pool.enabled())
return true;

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

repooled = purged.size();
return true;
}

// protected
Expand Down
Loading
Loading