diff --git a/include/bitcoin/database/impl/query/consensus/consensus_block.ipp b/include/bitcoin/database/impl/query/consensus/consensus_block.ipp index c0b073140..4cd7d9fe4 100644 --- a/include/bitcoin/database/impl/query/consensus/consensus_block.ipp +++ b/include/bitcoin/database/impl/query/consensus/consensus_block.ipp @@ -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()) @@ -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; // ======================================================================== @@ -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), @@ -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)) diff --git a/include/bitcoin/database/impl/query/pool/pool_estimate.ipp b/include/bitcoin/database/impl/query/pool/pool_estimate.ipp index 404c61bdd..7ad3ccae8 100644 --- a/include/bitcoin/database/impl/query/pool/pool_estimate.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_estimate.ipp @@ -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 diff --git a/include/bitcoin/database/impl/query/pool/pool_index.ipp b/include/bitcoin/database/impl/query/pool/pool_index.ipp index 7ca924f9b..5a2fcb43c 100644 --- a/include/bitcoin/database/impl/query/pool/pool_index.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_index.ipp @@ -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 { @@ -392,15 +391,14 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT block.push_back(possible_narrow_cast(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; diff --git a/include/bitcoin/database/impl/query/pool/pool_suffice.ipp b/include/bitcoin/database/impl/query/pool/pool_suffice.ipp index 6916653f0..bdd718f0d 100644 --- a/include/bitcoin/database/impl/query/pool/pool_suffice.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_suffice.ipp @@ -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 @@ -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 }); @@ -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()) @@ -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(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 }); @@ -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 }; @@ -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; @@ -149,6 +168,7 @@ bool CLASS::set_pool_unvalidated(size_t& out, } return true; + // ======================================================================== } TEMPLATE diff --git a/include/bitcoin/database/impl/query/pool/pool_writer.ipp b/include/bitcoin/database/impl/query/pool/pool_writer.ipp index 50836039c..fe9206cc0 100644 --- a/include/bitcoin/database/impl/query/pool/pool_writer.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_writer.ipp @@ -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 diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 48ed417fd..d7d230e4a 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -131,8 +131,7 @@ class query size_t get_scan_frontier() const NOEXCEPT; size_t get_latch_tx() const NOEXCEPT; - /// Height at which the forks of the flag activated, if active there in the - /// confirmed chain. + /// Height at which forks of flag activated, if confirmed active. bool get_activation(size_t& out, uint32_t flag) const NOEXCEPT; /// Candidate chain header at which the set of forks activated. @@ -537,7 +536,7 @@ class query data_chunk get_wire_block(const header_link& link, bool witness, bool turbo=false) const NOEXCEPT; - /// Write to a target sized to the encoding, in parallel with turbo. + /// Write to a target sized to the encoding. template bool get_wire_block(const system::data_slab& out, const header_link& link, bool witness, bool turbo=false) const NOEXCEPT; @@ -700,11 +699,10 @@ class query inline block_state get_block_state( const header_link& link) const NOEXCEPT; - /// Validate a block whose txs are pooled under a context sufficient for - /// the block, writing its prevouts. Unvalidated implies a tx is not - /// sufficiently pooled (block requires full validation). + /// Validate a block whose txs are pooled under a sufficient context. code 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; /// Pooled tx links by compact block short id (masked siphash of wtxid), /// terminal where not pooled or ambiguous. @@ -733,19 +731,15 @@ class query /// Pool. /// ----------------------------------------------------------------------- - /// A validation under the evaluated context holds under the current. inline bool is_sufficient(const context& current, const context& evaluated) const NOEXCEPT; bool is_pooled(const tx_link& link) const NOEXCEPT; pool_link to_pool_row(const tx_link& link) const NOEXCEPT; + bool get_pooled_time(uint32_t& out, const tx_link& link) const NOEXCEPT; + bool get_pooled_totals(pool_totals& out, const tx_link& link) const NOEXCEPT; code get_pooled(pooled_tx& out, const tx_link& link, const context& ctx) const NOEXCEPT; - bool get_pooled_time(uint32_t& out, const tx_link& link) const NOEXCEPT; - - /// The fee, virtual size and sigops of the pooled tx. - bool get_pooled_totals(pool_totals& out, - const tx_link& link) const NOEXCEPT; /// Unconfirmed, and not conflicted by a confirmed tx (or via ancestry). bool is_confirmable_tx(const tx_link& link) const NOEXCEPT; @@ -756,33 +750,31 @@ class query /// Spender of prevout in a pooled and confirmable tx, terminal if none. ins_link find_confirmable_spender(const point& prevout) const NOEXCEPT; - /// Confirmable tx links, in pool order (requires the pool index). + /// Confirmable tx links, in pool order (requires pool). code get_confirmable_txs(tx_links& out) const NOEXCEPT; - /// Confirmable txs spent by, and spending, the tx (direct only). + /// Confirmable txs spent by, and spending, tx (direct only). tx_links to_confirmable_parents(const tx_link& link) const NOEXCEPT; tx_links to_confirmable_children(const tx_link& link) const NOEXCEPT; - /// Confirmable ancestors, or descendants, of the tx (excluding the tx). + /// 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; - /// Totals of the tx with its confirmable ancestors, or descendants. + /// Totals of tx with its confirmable ancestors, or descendants. bool get_ancestor_totals(fee_totals& out, const tx_link& link) const NOEXCEPT; bool get_descendant_totals(fee_totals& out, const tx_link& link) const NOEXCEPT; - /// Confirmable fee rate tuples, in pool order (requires the pool index). + /// Confirmable fee rate tuples, in pool order (requires pool). code get_confirmable_fees(fee_rates& out) const NOEXCEPT; - /// Pooled txs out only for flags of the context they were not validated - /// under, in pool order (requires the pool index). + /// Get txs not validated under context, in pool order (requires pool). code get_pool_revalidation(tx_links& out, const context& ctx) const NOEXCEPT; - /// Pooled txs out without a validation context, in pool order (requires - /// the pool index). + /// Pooled txs out as not validated, in pool order (requires pool). code get_pool_unvalidated(tx_links& out) const NOEXCEPT; /// Setters. @@ -791,25 +783,30 @@ class query bool set_pooled(const tx_link& link, const transaction& tx, const chain_context& ctx, uint32_t time) NOEXCEPT; - /// Rebuild the pool index heads from the pool (after compaction). + /// Pool tx, out count of its pooled descendants pooled again after it. + bool set_pooled(size_t& repooled, const tx_link& link, + const transaction& tx, const context& ctx, uint32_t time) NOEXCEPT; + + /// Rebuild pool index heads from pool (after compaction). bool set_pool_index() NOEXCEPT; - /// Apply the confirmation (or reorganization) of the block to the index. + /// Apply confirmation (or reorganization) of block to index. bool set_pool_block(const header_link& link, bool confirmed) NOEXCEPT; - /// Apply the pool context to the index, out the rows it does not satisfy. + /// Apply pool context to index, out rows it does not satisfy. bool set_pool_context(size_t& out, const context& ctx) NOEXCEPT; - /// Set the context of the pooled tx, as validated under it. - bool set_pooled_context(const tx_link& link, const context& ctx) NOEXCEPT; + /// Set context of pooled tx, as validated under it. + bool set_pooled_context(const tx_link& link, const context& ctx, + size_t sigops) NOEXCEPT; - /// Clear the context of each pooled spender of the block's txs, out count. + /// Set each pooled spender of block's txs not validated, out count. bool set_pool_unvalidated(size_t& out, const header_link& link) NOEXCEPT; /// Coverage. /// ----------------------------------------------------------------------- - /// Accessors held across spans over the pool columns and index heads. + /// Accessors held across spans over pool columns and index heads. struct pool_guards { memory pool{}; @@ -817,7 +814,7 @@ class query memory spends_index{}; }; - /// The pool columns with txs appended as candidates (guards held). + /// Pool columns with txs appended as candidates. struct pool_graph { using algorithm = system::pool::algorithm; @@ -839,19 +836,19 @@ class query bool conflicted{}; }; - /// True if the fee the package adds to the pool covers it at the rate. + /// True if fee the package adds to pool covers it at rate. code get_pool_coverage(bool& out, const system::chain::transaction_cptrs& txs, const std::vector& stored, const context& ctx, uint64_t rate, uint32_t bytes_per_sigop, const pool_totals& limit, size_t maximum_outcomes) const NOEXCEPT; - /// Append the tx to the graph as a candidate (parents precede children). + /// Append tx to graph as a candidate (parents berfore children). code get_pool_candidate(pool_graph& out, const transaction& tx, const context& ctx) const NOEXCEPT; - /// True if the fee the candidates from first add to the pool, given those - /// before it, covers them at the rate. + /// True if fee candidates from first add to pool, given those before it, + /// covers them at rate. code get_pool_coverage(bool& out, const pool_graph& graph, size_t first, uint64_t rate, uint32_t bytes_per_sigop, const pool_totals& limit, size_t maximum_outcomes) const NOEXCEPT; @@ -859,8 +856,8 @@ class query /// Template. /// ----------------------------------------------------------------------- - /// The pooled txs of a block template within the budget, parents first, - /// leaves chosen at the cutoff rate. + /// Pooled txs of a block template within budget, parents first, leaves + /// chosen at cutoff rate. code get_pool_template(tx_links& out, pool_projection& projection, const pool_totals& budget, size_t depth, uint64_t cutoff, uint32_t bytes_per_sigop, size_t maximum_outcomes) const NOEXCEPT; @@ -868,12 +865,17 @@ class query /// Estimate. /// ----------------------------------------------------------------------- - /// The projection extended to the count of blocks, or to exhaustion. + /// Projection extended to count of blocks, or to exhaustion. code get_pool_projection(pool_projection& projection, size_t count, const pool_totals& budget, size_t depth, uint64_t cutoff, uint32_t bytes_per_sigop, size_t maximum_outcomes) const NOEXCEPT; - /// Rate and size of the rows pooled at each height below top, top first. + /// Projection extended over rows it holds, later rows left out. + code get_pool_continuation(pool_projection& projection, size_t count, + const pool_totals& budget, size_t depth, + uint32_t bytes_per_sigop) const NOEXCEPT; + + /// Rate and size of rows pooled at each height below top, top first. code get_pool_arrivals(pool_arrivals& out, size_t top, size_t count, uint32_t bytes_per_sigop) const NOEXCEPT; @@ -886,20 +888,20 @@ class query bool set_silent(const header_link& link, const block& block) NOEXCEPT; bool set_silent(const header_link& link, const block_view& block) NOEXCEPT; - /// Commit a block's silent payment batch rows and presilent link, with - /// rows the number committed (a block with no rows is set scanned). + /// Commit block's silent payment batch rows and presilent link, with rows + /// number committed (block with no rows is set scanned). bool set_silents(size_t& rows, const header_link& link, const block& block, bool bank) NOEXCEPT; bool set_silents(size_t& rows, const header_link& link, const block_view& block, bool bank) NOEXCEPT; - /// The wire prevouts of the spends of the block's silent payment eligible - /// txs in block order, with the selection of those txs. + /// Wire prevouts of spends of block's silent payment eligible txs in block + /// order, with selection of those txs. bool get_silent_prevouts(data_chunk& prevouts, std::vector& selected, const header_link& link, const block_view& block) const NOEXCEPT; - /// The wire prevouts of the spends of all of the block's txs in block - /// order, with the selection of those txs (all but the coinbase). + /// Wire prevouts of spends of all of block's txs in block order, with + /// selection of those txs (all but coinbase). bool get_block_prevouts(data_chunk& prevouts, std::vector& selected, const header_link& link, const block_view& block) const NOEXCEPT; @@ -934,32 +936,32 @@ class query /// Matches (height, tx hash, tweak) of rows [first, last) confirmed at /// heights (or mtps if time) [start, stop], ordered by height, then /// unconfirmed (zero height), false implies cancel. A short height cut - /// scanned from its floor scans only the rows of its confirmed blocks. + /// scanned from its floor scans only rows of its confirmed blocks. bool get_silent_matches(silent_matches& out, const stopper& cancel, const system::scan::batch::receiver& keys, size_t first, size_t last, size_t start, size_t stop, bool time) NOEXCEPT; - /// The scan fk from which a scan covers all blocks at or above start + /// Scan fk from which a scan covers all blocks at or above start /// (height, or mtp if time). size_t get_silent_floor(size_t start, bool time) const NOEXCEPT; - /// The first silent row at or above first that is not fully written. + /// First silent row at or above first that is not fully written. size_t get_silent_frontier(size_t first) const NOEXCEPT; - /// The scan fk that starts the header's scan range, terminal if unmapped. + /// Scan fk that starts header's scan range, terminal if unmapped. scan_link get_scan_start(const header_link& link) const NOEXCEPT; - /// Map the header to the scan fk that starts its scan range. + /// Map header to scan fk that starts its scan range. bool set_scan_start(const header_link& link, const scan_link& start) NOEXCEPT; - /// Map headers from top down to the first mapped ancestor, or to genesis - /// if remap, over rows below bound, false if canceled or unarchived. + /// Map headers from top down to first mapped ancestor, or to genesis if + ///remap, over rows below bound, false if canceled or unarchived. bool set_scan_starts(const stopper& cancel, const header_link& top, size_t bound, bool remap) NOEXCEPT; - /// The scan fk that starts the confirmed header at or below height (zero - /// if none mapped), a scan from it covers all blocks at or above height. + /// Scan fk that starts confirmed header at or below height (zero if none + /// mapped), a scan from it covers all blocks at or above height. scan_link get_scan_floor(size_t height) const NOEXCEPT; /// Verify all signatures in table, false implies cancel. @@ -1033,8 +1035,8 @@ class query bool get_strong_fork(bool& strong, const uint256_t& fork_work, size_t fork_point, bool tie=false) const NOEXCEPT; - /// Wire-encoded prevouts of each spend of the block (in block order), - /// with the spends and conflicts for set_prevouts. False if any missing. + /// Wire-encoded prevouts of each spend of block (in block order), with + /// spends and conflicts for set_prevouts. False if any missing. bool get_block_prevouts(data_chunk& prevouts, prevout_spends& spends, tx_links& conflicts, const header_link& link) const NOEXCEPT; @@ -1171,11 +1173,11 @@ class query /// Unspent. /// ----------------------------------------------------------------------- - /// Coin amounts moved by the block. + /// Coin amounts moved by block. code get_block_amounts(block_amounts& out, const header_link& link) const NOEXCEPT; - /// Unspent output totals over the branch (administrative scan). + /// Unspent output totals over branch (administrative scan). code get_unspent_totals(const stopper& cancel, unspent_totals& out, const header_links& branch, bool turbo=false) const NOEXCEPT; @@ -1190,12 +1192,12 @@ class query hash_digest& digest, const header_links& branch, bool turbo=false) const NOEXCEPT; - /// Unspent coins with scripts hashing to the keys, and the set size. + /// Unspent coins with scripts hashing to keys, and set size. code get_unspent_matches(const stopper& cancel, unspent_coins& out, size_t& txouts, const std::unordered_set& keys, const header_links& branch, bool turbo=false) const NOEXCEPT; - /// The block is a bip30 exception (duplicated coinbase, if configured). + /// Block is a bip30 exception (duplicated coinbase, if configured). bool is_bip30_exception(bool& out, const header_link& link) const NOEXCEPT; @@ -1242,57 +1244,56 @@ class query const tx_link& link) const NOEXCEPT; hash_digest get_pooled_wtxid(const table::pool::link& link) const NOEXCEPT; - /// The table columns and heads as spans (guards held). + /// Table columns and heads as spans (guards held). bool get_pool_columns(system::pool::algorithm::columns_t& out, pool_guards& guards) const NOEXCEPT; - /// A new projection, its leaves chosen at the cutoff rate. + /// A new projection, its leaves chosen at cutoff rate. void set_pool_resolution(pool_projection& projection, const system::pool::algorithm::columns_t& columns, uint64_t cutoff, uint32_t bytes_per_sigop, size_t maximum_outcomes) const NOEXCEPT; - /// The index of each pooled row in the range (cell and children head). + /// Index of each pooled row in range (cell and children head). bool set_pool_rows(system::pool::algorithm::link_t begin, system::pool::algorithm::link_t end) NOEXCEPT; - /// The index of a pooled row's inputs (parent rows and ring mates). + /// Index of a pooled row's inputs (parent rows and ring mates). bool set_pool_links(system::pool::algorithm::link_t start, const pool_links& parents, const pool_links& mates) NOEXCEPT; - /// Pool the tx at a new row, its spenders not yet considered. + /// 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; - /// Purge the pooled spenders of the tx and theirs, ascending by row. + /// Purge pooled spenders of tx and theirs, ascending by row. bool set_pool_purged(tx_links& out, const tx_link& link) NOEXCEPT; /// Pool again each purged tx, in order, under its stored context. bool set_pool_repooled(const tx_links& purged) NOEXCEPT; - /// The input of a pooled spender of the point below the bound, and if it is - /// confirmed spent. + /// Input of a pooled spender of point below bound, and if confirmed spent. bool get_pooled_spender(system::pool::algorithm::link_t& out, bool& conflicted, const point& point, const tx_link& self, bool archived, system::pool::algorithm::link_t bound=system::pool::algorithm::none) const NOEXCEPT; - /// The pooled spenders of the points spent by the txs (their conflicts). + /// Pooled spenders of points spent by txs (their conflicts). bool get_pool_conflicts(tx_links& out, const tx_links& txs) const NOEXCEPT; - /// A point of the pooled tx is spent by a confirmed tx. + /// A point of pooled tx is spent by a confirmed tx. bool is_pool_conflicted(bool& out, const tx_link& link, const pool_link& row) const NOEXCEPT; - /// Confirmability memoized across the txs of a scan. + /// Confirmability memoized across txs of a scan. bool is_confirmable_tx(confirmables& memo, const tx_link& link) const NOEXCEPT; - /// Confirmable ancestors or descendants of the tx (excluding the tx). + /// Confirmable ancestors or descendants of tx (excluding tx). tx_links to_confirmable_relatives(const tx_link& link, bool ancestors) const NOEXCEPT; - /// Totals of the tx with its confirmable ancestors or descendants. + /// Totals of tx with its confirmable ancestors or descendants. bool get_confirmable_totals(fee_totals& out, const tx_link& link, bool ancestors) const NOEXCEPT; @@ -1323,8 +1324,9 @@ class query const context& ctx) const NOEXCEPT; code check_pooled_block(pooled_block& block) const NOEXCEPT; code 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; /// Called by get_compact_links. bool get_compact_matches(compact_matches& out, @@ -1413,8 +1415,8 @@ class query size_t heavy, hash_option&& interval, bool strong, bool pruned) NOEXCEPT; - /// Advance row to the first row linked above floor, an unpublished row - /// lowering last to itself (the row at or past which rows are unknown). + /// Advance row to first row linked above floor, an unpublished row + /// lowering last to itself (row at or past which rows are unknown). void seek_scan_start(size_t& row, size_t& last, tx_link::integer floor) const NOEXCEPT; @@ -1426,37 +1428,35 @@ class query template bool set_silent_(const tx_link& link, const Transaction& tx) NOEXCEPT; - /// Write scan records, the tx fk of each by its position in links, caller - /// must hold transactor. + /// Write scan records, caller must hold transactor. bool put_scan_(const system::wallet::silent_payment::rows& rows, const std::span& links) NOEXCEPT; - /// A block's tx is indexed unless linked below its coinbase and pooled. + /// Block's tx is indexed unless linked below its coinbase and pooled. bool is_silent_selected(const tx_link& link, const tx_link& coinbase) const NOEXCEPT; - /// The tx confirmed at height is outside heights (or mtps if time) - /// [start, stop]. + /// Tx confirmed at height is outside heights/mtps [start, stop]. bool is_silent_cut(const tx_link& link, size_t height, size_t start, size_t stop, bool time) const NOEXCEPT; - /// The wire prevouts of the spends of the block's selected txs, where - /// silent selects the silent payment eligible txs and otherwise all. + /// Wire prevouts of spends of block's selected txs, where silent selects + /// silent payment eligible txs, otherwise all. bool get_prevouts_(data_chunk& prevouts, std::vector& selected, const header_link& link, const block_view& block, bool silent) const NOEXCEPT; - /// Mark the blocks of a bank's presilent table scanned. + /// Mark blocks of bank's presilent table scanned. bool set_scanned_(bool bank) NOEXCEPT; - /// Commit the silent payment batch rows of a block's selected txs. + /// Commit silent payment batch rows of a block's selected txs. template bool set_silents_(size_t& count, const header_link& link, const Block& block, size_t txs, bool bank) NOEXCEPT; /// Pooling. /// ----------------------------------------------------------------------- - /// The archived link of a pooled duplicate of the tx, or terminal. + /// Archived link of a pooled duplicate of tx, or terminal. tx_link to_pooled(const transaction& tx) const NOEXCEPT; tx_link to_pooled(const transaction_view& tx) const NOEXCEPT; bool is_size_match(table::transaction::record& out, const tx_link& link, @@ -1475,7 +1475,7 @@ class query memory tx; }; - /// Per-tx allocated table links (bases advanced by the block writer). + /// Per-tx allocated table links (bases advanced by block writer). struct allocation { tx_link tx_fk; @@ -1489,7 +1489,7 @@ class query const allocation& fks,const transaction_view& tx, bool bypass, bool prune) NOEXCEPT; - /// Wire reading (all accessors held across the txs read). + /// Wire reading (all accessors held across txs read). /// ----------------------------------------------------------------------- /// Per-table accessors, each holds a shared lock on its storage remap. @@ -1540,7 +1540,7 @@ class query // Minimum elements per parallel task for single-read transforms. static constexpr size_t parallel_floor = 32; - // Fewest pool records hashed by one task of the compact short id scan. + // Fewest pool records hashed by one task of compact short id scan. static constexpr size_t short_id_minimum_records = system::power2(10u); // Chain objects. diff --git a/test/mocks/pooling.cpp b/test/mocks/pooling.cpp index 1afa72d16..9391b6012 100644 --- a/test/mocks/pooling.cpp +++ b/test/mocks/pooling.cpp @@ -45,15 +45,20 @@ bool archive_pool(query_t& query, const transaction& tx, return stored && query.set_pooled(link, tx, ctx, 0); } -database::header_link archive_confirm(query_t& query, - const transaction& tx) NOEXCEPT +block to_block(const transaction& tx) NOEXCEPT { const point null_point{ null_hash, point::null_index }; const input null{ null_point, script{}, max_uint32 }; const output reward{ 42u, script{} }; const transaction coinbase{ 1u, inputs{ null }, outputs{ reward }, 0u }; const header header{ 1u, genesis.hash(), null_hash, 1u, 2u, 3u }; - const block block{ header, { coinbase, tx } }; + return { header, { coinbase, tx } }; +} + +database::header_link archive_confirm(query_t& query, + const transaction& tx) NOEXCEPT +{ + const auto block = to_block(tx); const database::context ctx{ 0, 1, 0 }; const auto stored = query.set(block, ctx, {}, false, true); const auto link = query.to_header(block.hash()); diff --git a/test/mocks/pooling.hpp b/test/mocks/pooling.hpp index 958b95e50..c12421cab 100644 --- a/test/mocks/pooling.hpp +++ b/test/mocks/pooling.hpp @@ -47,6 +47,9 @@ bool archive_pool(query_t& query, const system::chain::transaction& tx, database::header_link archive_confirm(query_t& query, const system::chain::transaction& tx) NOEXCEPT; +/// Block 1 of a coinbase and the tx. +system::chain::block to_block(const system::chain::transaction& tx) NOEXCEPT; + /// Populate the tx prevouts, then archive and pool it. bool pool_populated(query_t& query, const system::chain::transaction& tx, uint32_t height=8) NOEXCEPT; diff --git a/test/query/consensus/consensus_block.cpp b/test/query/consensus/consensus_block.cpp index 71942d7f4..3a51df333 100644 --- a/test/query/consensus/consensus_block.cpp +++ b/test/query/consensus/consensus_block.cpp @@ -395,7 +395,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__pooled__success_with_prevout BC_REQUIRE(pool_tx(query, child, 4'000'000'000, pooled_context)); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy), error::success); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), error::success); table::prevout::slab_get cache{}; cache.spends.resize(2); @@ -424,7 +424,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__insufficient__unvalidated) BC_REQUIRE(pool_tx(query, parent, 5'000'000'000, database::context{ pooled_flags, 9, 9 })); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy), error::unvalidated); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), error::unvalidated); } BC_AUTO_TEST_CASE(query_consensus__validate_pooled__not_pooled__unvalidated) @@ -440,7 +440,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__not_pooled__unvalidated) BC_REQUIRE(store_block(query, block)); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy), error::unvalidated); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), error::unvalidated); } BC_AUTO_TEST_CASE(query_consensus__validate_pooled__overclaim__coinbase_value_limit) @@ -461,7 +461,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__overclaim__coinbase_value_li BC_REQUIRE(pool_tx(query, parent, 5'000'000'000, pooled_context)); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy), system::error::coinbase_value_limit); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), system::error::coinbase_value_limit); } BC_AUTO_TEST_CASE(query_consensus__validate_pooled__forward_reference__forward_reference) @@ -483,7 +483,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__forward_reference__forward_r BC_REQUIRE(pool_tx(query, child, 4'000'000'000, pooled_context)); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy), system::error::forward_reference); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), system::error::forward_reference); } BC_AUTO_TEST_CASE(query_consensus__validate_pooled__internal_double_spend__block_internal_double_spend) @@ -506,7 +506,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__internal_double_spend__block BC_REQUIRE(pool_tx(query, spend2, 5'000'000'000, pooled_context)); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy), system::error::block_internal_double_spend); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), system::error::block_internal_double_spend); } BC_AUTO_TEST_CASE(query_consensus__validate_pooled__internal_coinbase_spend__coinbase_maturity) @@ -526,7 +526,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__internal_coinbase_spend__coi BC_REQUIRE(pool_tx(query, spend, 1, pooled_context)); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy), system::error::coinbase_maturity); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), system::error::coinbase_maturity); } BC_AUTO_TEST_CASE(query_consensus__validate_pooled__mature__block_confirmable) @@ -557,10 +557,131 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__mature__block_confirmable) BC_REQUIRE(pool_tx(query, child, 4'000'000'000, mature_context)); const auto link = query.to_header(block.hash()); - BC_REQUIRE_EQUAL(query.validate_pooled(link, mature_block, pooled_interval, pooled_subsidy), error::success); + BC_REQUIRE_EQUAL(query.validate_pooled(link, mature_block, pooled_interval, pooled_subsidy, {}), error::success); BC_REQUIRE_EQUAL(query.block_confirmable(link), error::success); } +constexpr auto signet_flags = pooled_flags | flags::signet_rule; +constexpr auto txsize_flags = pooled_flags | flags::bip54_txsize_rule; +constexpr auto coinbase_flags = pooled_flags | flags::bip54_coinbase_rule; + +static transaction make_coinbase(uint64_t value, uint32_t locktime, uint32_t sequence) NOEXCEPT +{ + const script nops{ { { opcode::nop }, { opcode::nop } } }; + return + { + 1, + inputs{ input{ point{}, nops, witness{}, sequence } }, + outputs{ output{ value, script{} } }, + locktime + }; +} + +BC_AUTO_TEST_CASE(query_consensus__validate_pooled__signet_unsolved__invalid_signet_solution) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + const auto parent = make_spend(genesis_hash(), 0, 4'000'000'000); + const auto block = make_block({ make_coinbase(1), parent }); + BC_REQUIRE(store_block(query, block)); + + parent.inputs_ptr()->front()->metadata.parent_tx = 0; + BC_REQUIRE(pool_tx(query, parent, 5'000'000'000, { signet_flags, 8, 9 })); + + const auto link = query.to_header(block.hash()); + const script challenge{ { { opcode::push_positive_1 } } }; + BC_REQUIRE_EQUAL(query.validate_pooled(link, { signet_flags, 0, 9, 8, 0, 0, 0 }, pooled_interval, pooled_subsidy, challenge), system::error::invalid_signet_solution); +} + +BC_AUTO_TEST_CASE(query_consensus__validate_pooled__bip54_two_leaf_tx__invalid_transaction_size) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + const script four{ { { opcode::nop }, { opcode::nop }, { opcode::nop }, { opcode::nop } } }; + const transaction parent{ 1, inputs{ input{ point{ genesis_hash(), 0 }, script{}, witness{}, max_uint32 } }, outputs{ output{ 4'000'000'000, four } }, 0 }; + BC_REQUIRE_EQUAL(parent.serialized_size(false), invalid_transaction_size); + + const auto block = make_block({ make_coinbase(1), parent }); + BC_REQUIRE(store_block(query, block)); + + parent.inputs_ptr()->front()->metadata.parent_tx = 0; + BC_REQUIRE(pool_tx(query, parent, 5'000'000'000, { txsize_flags, 8, 9 })); + + const auto link = query.to_header(block.hash()); + BC_REQUIRE_EQUAL(query.validate_pooled(link, { txsize_flags, 0, 9, 8, 0, 0, 0 }, pooled_interval, pooled_subsidy, {}), system::error::invalid_transaction_size); + BC_REQUIRE_EQUAL(query.validate_pooled(link, block_context, pooled_interval, pooled_subsidy, {}), error::success); +} + +BC_AUTO_TEST_CASE(query_consensus__validate_pooled__bip54_coinbase_locktime__coinbase_locktime_mismatch) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + const auto parent = make_spend(genesis_hash(), 0, 4'000'000'000); + const auto block = make_block({ make_coinbase(1, 6, 0), parent }); + BC_REQUIRE(store_block(query, block)); + + parent.inputs_ptr()->front()->metadata.parent_tx = 0; + BC_REQUIRE(pool_tx(query, parent, 5'000'000'000, { coinbase_flags, 8, 9 })); + + const auto link = query.to_header(block.hash()); + BC_REQUIRE_EQUAL(query.validate_pooled(link, { coinbase_flags, 0, 9, 8, 0, 0, 0 }, pooled_interval, pooled_subsidy, {}), system::error::coinbase_locktime_mismatch); +} + +BC_AUTO_TEST_CASE(query_consensus__validate_pooled__bip54_coinbase_sequence__coinbase_sequence_final) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + const auto parent = make_spend(genesis_hash(), 0, 4'000'000'000); + const auto block = make_block({ make_coinbase(1, 7, max_uint32), parent }); + BC_REQUIRE(store_block(query, block)); + + parent.inputs_ptr()->front()->metadata.parent_tx = 0; + BC_REQUIRE(pool_tx(query, parent, 5'000'000'000, { coinbase_flags, 8, 9 })); + + const auto link = query.to_header(block.hash()); + BC_REQUIRE_EQUAL(query.validate_pooled(link, { coinbase_flags, 0, 9, 8, 0, 0, 0 }, pooled_interval, pooled_subsidy, {}), system::error::coinbase_sequence_final); +} + +BC_AUTO_TEST_CASE(query_consensus__validate_pooled__bip54_coinbase__success) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + const auto parent = make_spend(genesis_hash(), 0, 4'000'000'000); + const auto block = make_block({ make_coinbase(1, 7, 0), parent }); + BC_REQUIRE(store_block(query, block)); + + parent.inputs_ptr()->front()->metadata.parent_tx = 0; + BC_REQUIRE(pool_tx(query, parent, 5'000'000'000, { coinbase_flags, 8, 9 })); + + const auto link = query.to_header(block.hash()); + BC_REQUIRE_EQUAL(query.validate_pooled(link, { coinbase_flags, 0, 9, 8, 0, 0, 0 }, pooled_interval, pooled_subsidy, {}), error::success); +} + BC_AUTO_TEST_CASE(query_consensus__validate_pooled__disabled__unvalidated) { settings settings{ test::database_settings() }; @@ -570,7 +691,7 @@ BC_AUTO_TEST_CASE(query_consensus__validate_pooled__disabled__unvalidated) test::query_accessor query{ store }; BC_REQUIRE(!store.create(test::events_handler)); BC_REQUIRE(query.initialize(test::genesis)); - BC_REQUIRE_EQUAL(query.validate_pooled(0, block_context, pooled_interval, pooled_subsidy), error::unvalidated); + BC_REQUIRE_EQUAL(query.validate_pooled(0, block_context, pooled_interval, pooled_subsidy, {}), error::unvalidated); } BC_AUTO_TEST_SUITE_END() diff --git a/test/query/pool/pool_estimate.cpp b/test/query/pool/pool_estimate.cpp index 935fad581..426706308 100644 --- a/test/query/pool/pool_estimate.cpp +++ b/test/query/pool/pool_estimate.cpp @@ -160,6 +160,81 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_projection__marks_of_other_rows__restarte BC_REQUIRE_EQUAL(projection.blocks[0][0].rate, 1'000'000u / size); } +// get_pool_continuation +// ---------------------------------------------------------------------------- + +BC_AUTO_TEST_CASE(query_pool__get_pool_continuation__more_blocks__continued) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = to_spend(genesis_coinbase(), 4'999'999'000); + const auto child = to_spend(parent.hash(false), 4'999'990'000); + const auto size = system::possible_narrow_cast(parent.virtual_size()); + const auto pooled = pool_populated(query, parent) && pool_populated(query, child); + pool_projection projection{}; + const pool_totals budget{ 0, size, 80'000 }; + const auto first = query.get_pool_projection(projection, 1, budget, 25, 0, 20, 1'024); + const auto ec = query.get_pool_continuation(projection, 2, budget, 25, 20); + + BC_REQUIRE(pooled); + BC_REQUIRE_EQUAL(first, error::success); + BC_REQUIRE_EQUAL(ec, error::success); + BC_REQUIRE_EQUAL(projection.blocks.size(), 2u); + BC_REQUIRE_EQUAL(projection.blocks[1][0].rate, 9'000'000u / size); + BC_REQUIRE(projection.exhausted); +} + +BC_AUTO_TEST_CASE(query_pool__get_pool_continuation__later_row__left_out) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto parent = to_spend(genesis_coinbase(), 4'999'999'000); + const auto child = to_spend(parent.hash(false), 4'999'990'000); + const auto size = system::possible_narrow_cast(parent.virtual_size()); + const auto pooled_parent = pool_populated(query, parent); + pool_projection projection{}; + const pool_totals budget{ 0, size, 80'000 }; + const auto first = query.get_pool_projection(projection, 1, budget, 25, 0, 20, 1'024); + const auto pooled_child = pool_populated(query, child); + const auto ec = query.get_pool_continuation(projection, 2, budget, 25, 20); + + BC_REQUIRE(pooled_parent); + BC_REQUIRE_EQUAL(first, error::success); + BC_REQUIRE(pooled_child); + BC_REQUIRE_EQUAL(ec, error::success); + BC_REQUIRE_EQUAL(projection.marks.size(), 2u); + BC_REQUIRE(projection.marks[1] == system::pool::algorithm::mark::excluded); + BC_REQUIRE_EQUAL(projection.blocks.size(), 1u); + BC_REQUIRE(projection.exhausted); +} + +BC_AUTO_TEST_CASE(query_pool__get_pool_continuation__marks_over_rows__integrity) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + pool_projection projection{}; + projection.marks.resize(42); + BC_REQUIRE_EQUAL(query.get_pool_continuation(projection, 1, block_limit, 25, 20), error::integrity); +} + // get_pool_arrivals // ---------------------------------------------------------------------------- diff --git a/test/query/pool/pool_index.cpp b/test/query/pool/pool_index.cpp index 195aeb0f9..12768256b 100644 --- a/test/query/pool/pool_index.cpp +++ b/test/query/pool/pool_index.cpp @@ -141,6 +141,37 @@ BC_AUTO_TEST_CASE(query_pool__push_confirmed__rival__index_unchanged) BC_REQUIRE_EQUAL(pool_cell(query, store, rival), before); } +// The rival is pooled after the block's prevouts and conflicts are cached. +BC_AUTO_TEST_CASE(query_pool__set_pool_block__rival_pooled_after_prevout_cache__excluded) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto spender = to_spend(genesis_coinbase(), 4'999'998'500); + const auto rival = to_spend(genesis_coinbase(), 4'999'999'000); + const auto block = test::to_block(spender); + const auto stored = query.set(block, context{ 0, 1, 0 }, {}, false, true); + const auto link = query.to_header(block.hash()); + const auto cached = query.populate_with_metadata(block) && query.set_prevouts(link, block); + const auto pooled = pool_populated(query, rival); + const auto before = pool_cell(query, store, rival); + const auto pushed = query.push_candidate(link) && query.push_confirmed(link, true); + const auto applied = query.set_pool_block(link, true); + + BC_REQUIRE(stored); + BC_REQUIRE(cached); + BC_REQUIRE(pooled); + BC_REQUIRE(system::pool::algorithm::is_present(before)); + BC_REQUIRE(pushed); + BC_REQUIRE(applied); + BC_REQUIRE_EQUAL(pool_cell(query, store, rival), absent_conflicted); +} + BC_AUTO_TEST_CASE(query_pool__set_pool_block__unpooled_rival_no_prevout_cache__excluded_and_revived) { settings settings{ test::database_settings() }; @@ -192,7 +223,8 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled__reorganized_parent__child_repooled_aft const auto pooled = pool_populated(query, child); const auto popped = query.pop_confirmed(); const auto link = query.to_tx(parent.hash(false)); - const auto repooled = query.set_pooled(link, parent, context{ bip113, 8, 9 }, 0); + size_t count{}; + const auto repooled = query.set_pooled(count, link, parent, context{ bip113, 8, 9 }, 0); const auto rivaled = pool_populated(query, to_spend(genesis_coinbase(), 4'999'998'500)); constexpr auto purged = system::bit_or(system::pool::algorithm::absent, system::pool::algorithm::purged); @@ -200,6 +232,7 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled__reorganized_parent__child_repooled_aft BC_REQUIRE(pooled); BC_REQUIRE(popped); BC_REQUIRE(repooled); + BC_REQUIRE_EQUAL(count, 1u); BC_REQUIRE(rivaled); BC_REQUIRE_EQUAL(query.to_pool_row(query.to_tx(child.hash(false))).value, 2u); BC_REQUIRE_EQUAL(store.pool_index.cells.at(0u), purged); diff --git a/test/query/pool/pool_suffice.cpp b/test/query/pool/pool_suffice.cpp index 2dacd4cd7..ed64a86ab 100644 --- a/test/query/pool/pool_suffice.cpp +++ b/test/query/pool/pool_suffice.cpp @@ -315,6 +315,37 @@ BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__coinbase_spender__unvalidate BC_REQUIRE_EQUAL(links.front(), query.to_tx(parent.hash(false))); } +BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__coinbase_spender__flags_and_median_kept) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const auto tx = to_spend(genesis_coinbase(), 4'999'999'000); + const auto stored = query.set(tx); + const auto pooled = query.set_pooled(query.to_tx(tx.hash(false)), tx, context{ bip113, 8, 9 }, 0); + size_t marked{}; + const auto unvalidated = query.set_pool_unvalidated(marked, query.to_header(test::genesis.hash())); + const auto row = store.pool.first(query.to_tx(tx.hash(false))); + table::pool_scalar flags{}; + table::pool_scalar height{}; + table::pool_scalar median{}; + const auto read = store.pool.flags.get(row, flags) && store.pool.heights.get(row, height) && store.pool.medians.get(row, median); + + BC_REQUIRE(stored); + BC_REQUIRE(pooled); + BC_REQUIRE(unvalidated); + BC_REQUIRE_EQUAL(marked, 1u); + BC_REQUIRE(read); + BC_REQUIRE_EQUAL(flags.value, bip113); + BC_REQUIRE_EQUAL(height.value, max_uint32); + BC_REQUIRE_EQUAL(median.value, 9u); +} + BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__revalidated__present_after_sweep) { settings settings{ test::database_settings() }; @@ -332,7 +363,7 @@ BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__revalidated__present_after_s const auto unvalidated = query.set_pool_unvalidated(marked, query.to_header(test::genesis.hash())); size_t excluded{}; const auto swept = query.set_pool_context(excluded, ctx); - const auto set = query.set_pooled_context(query.to_tx(tx.hash(false)), ctx); + const auto set = query.set_pooled_context(query.to_tx(tx.hash(false)), ctx, 0); size_t restored{ 42 }; const auto resumed = query.set_pool_context(restored, ctx); @@ -381,7 +412,7 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled_context__revalidated__present_after_swe size_t excluded{}; size_t restored{ 42 }; const auto swept = query.set_pool_context(excluded, more); - const auto set = query.set_pooled_context(query.to_tx(parent.hash(false)), more) && query.set_pooled_context(query.to_tx(child.hash(false)), more); + const auto set = query.set_pooled_context(query.to_tx(parent.hash(false)), more, 0) && query.set_pooled_context(query.to_tx(child.hash(false)), more, 0); const auto resumed = query.set_pool_context(restored, more); BC_REQUIRE(pooled); @@ -394,6 +425,35 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled_context__revalidated__present_after_swe BC_REQUIRE(system::pool::algorithm::is_present(pool_cell(query, store, child))); } +BC_AUTO_TEST_CASE(query_pool__set_pooled_context__sigops__stored_with_fee_and_size) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + store.set_pooling(); + const context more{ bip113 | flags::bip16_rule, 8, 9 }; + const auto tx = to_spend(genesis_coinbase(), 4'999'999'000); + const auto pooled = pool_populated(query, tx); + const auto link = query.to_tx(tx.hash(false)); + system::pool::algorithm::totals_t before{}; + system::pool::algorithm::totals_t after{}; + const auto first = query.get_pooled_totals(before, link); + const auto set = query.set_pooled_context(link, more, 42); + const auto second = query.get_pooled_totals(after, link); + + BC_REQUIRE(pooled); + BC_REQUIRE(first); + BC_REQUIRE(set); + BC_REQUIRE(second); + BC_REQUIRE_EQUAL(after.fee, before.fee); + BC_REQUIRE_EQUAL(after.size, before.size); + BC_REQUIRE_EQUAL(after.sigops, 42u); +} + BC_AUTO_TEST_CASE(query_pool__set_pooled_context__not_pooled__false) { settings settings{ test::database_settings() }; @@ -404,7 +464,7 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled_context__not_pooled__false) BC_REQUIRE(query.initialize(test::genesis)); store.set_pooling(); - BC_REQUIRE(!query.set_pooled_context(42, context{ bip113, 8, 9 })); + BC_REQUIRE(!query.set_pooled_context(42, context{ bip113, 8, 9 }, 0)); } BC_AUTO_TEST_SUITE_END()