From 16db309ed1f4d35bf450f58e3de9b1a362298ccd Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 16:06:49 -0500 Subject: [PATCH 1/4] Test the repool order of a descendant reached by two paths. --- test/query/pool/pool_index.cpp | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/query/pool/pool_index.cpp b/test/query/pool/pool_index.cpp index 12768256b..6cbd16d08 100644 --- a/test/query/pool/pool_index.cpp +++ b/test/query/pool/pool_index.cpp @@ -281,6 +281,43 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled__reorganized_parent__descendants_repool BC_REQUIRE(out == expected); } +BC_AUTO_TEST_CASE(query_pool__set_pooled__reorganized_parent__descendant_by_two_paths_repooled_after_both) +{ + 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)); + + // Rows: 0 child, 1 grandchild, 2 joined (spends parent and grandchild). + store.set_pooling(); + const transaction parent{ 1u, inputs{ input{ point{ genesis_coinbase(), 0 }, script{}, max_uint32 } }, outputs{ output{ 2'499'999'000, script{} }, output{ 2'499'999'000, script{} } }, 0u }; + const auto child = to_spend(parent.hash(false), 2'499'998'000); + const auto grandchild = to_spend(child.hash(false), 2'499'997'000); + const transaction joined{ 1u, inputs{ input{ point{ parent.hash(false), 1 }, script{}, max_uint32 }, input{ point{ grandchild.hash(false), 0 }, script{}, max_uint32 } }, outputs{ output{ 4'999'995'000, script{} } }, 0u }; + const auto confirmed = !archive_confirm(query, parent).is_terminal(); + const auto pooled = pool_populated(query, child) && pool_populated(query, grandchild) && pool_populated(query, joined); + const auto popped = query.pop_confirmed(); + const auto link = query.to_tx(parent.hash(false)); + size_t count{}; + const auto repooled = query.set_pooled(count, link, parent, context{ bip113, 8, 9 }, 0); + + BC_REQUIRE(confirmed); + BC_REQUIRE(pooled); + BC_REQUIRE(popped); + BC_REQUIRE(repooled); + BC_REQUIRE_EQUAL(count, 3u); + BC_REQUIRE_EQUAL(query.to_pool_row(link).value, 3u); + BC_REQUIRE_EQUAL(query.to_pool_row(query.to_tx(child.hash(false))).value, 4u); + BC_REQUIRE_EQUAL(query.to_pool_row(query.to_tx(grandchild.hash(false))).value, 5u); + BC_REQUIRE_EQUAL(query.to_pool_row(query.to_tx(joined.hash(false))).value, 6u); + BC_REQUIRE_EQUAL(store.spends_index.rows.at(5u), 3u); + BC_REQUIRE_EQUAL(store.spends_index.rows.at(6u), 4u); + BC_REQUIRE_EQUAL(store.spends_index.rows.at(7u), 3u); + BC_REQUIRE_EQUAL(store.spends_index.rows.at(8u), 5u); +} + BC_AUTO_TEST_CASE(query_pool__set_pool_index__repooled_child__purged_row_kept_purged) { settings settings{ test::database_settings() }; From 5fa1e4c21130306a25dcd3de3629821eb352e449 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 16:49:41 -0500 Subject: [PATCH 2/4] Store the last block applied to the pool in the envelope. --- .../database/impl/query/envelope_state.ipp | 16 +++ .../database/impl/query/pool/pool_index.ipp | 4 +- .../database/impl/query/pool/pool_suffice.ipp | 2 +- include/bitcoin/database/impl/store/store.ipp | 19 +++ .../database/impl/store/store_open.ipp | 5 + .../database/impl/store/store_restore.ipp | 4 + include/bitcoin/database/query.hpp | 8 +- include/bitcoin/database/store.hpp | 5 + .../database/tables/caches/envelope.hpp | 20 +++ include/bitcoin/database/tables/envelope.hpp | 6 + include/bitcoin/database/tables/schema.hpp | 3 +- src/tables/envelope.cpp | 28 +++- test/query/pool/pool_index.cpp | 120 ++++++++++++++++++ test/store/store.cpp | 49 +++++++ test/store/store_open.cpp | 82 +++++++++++- test/tables/envelope.cpp | 63 ++++++++- 16 files changed, 423 insertions(+), 11 deletions(-) diff --git a/include/bitcoin/database/impl/query/envelope_state.ipp b/include/bitcoin/database/impl/query/envelope_state.ipp index d50a63d92..8202e6236 100644 --- a/include/bitcoin/database/impl/query/envelope_state.ipp +++ b/include/bitcoin/database/impl/query/envelope_state.ipp @@ -48,6 +48,22 @@ size_t CLASS::get_latch_tx() const NOEXCEPT return store_.get_latch_tx(); } +TEMPLATE +header_link CLASS::get_pool_top() const NOEXCEPT +{ + return store_.get_pool_top(); +} + +TEMPLATE +bool CLASS::set_pool_top(const header_link& link) NOEXCEPT +{ + // ======================================================================== + const auto scope = get_transactor(); + + return store_.set_pool_top(link); + // ======================================================================== +} + TEMPLATE bool CLASS::get_activation(size_t& out, uint32_t flag) const NOEXCEPT { diff --git a/include/bitcoin/database/impl/query/pool/pool_index.ipp b/include/bitcoin/database/impl/query/pool/pool_index.ipp index ccc24ede4..63c0991a5 100644 --- a/include/bitcoin/database/impl/query/pool/pool_index.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_index.ipp @@ -376,7 +376,7 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT const auto count = store_.pool.count().value; if (is_zero(count)) - return true; + return !confirmed || store_.set_pool_top(link); table::txs::get_txs txs{}; if (!store_.txs.at(to_txs(link), txs) || txs.tx_fks.empty()) @@ -436,7 +436,7 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT if (!complete) system::pool::label(columns); - return true; + return !confirmed || store_.set_pool_top(link); // ======================================================================== } diff --git a/include/bitcoin/database/impl/query/pool/pool_suffice.ipp b/include/bitcoin/database/impl/query/pool/pool_suffice.ipp index aeffd104b..c01b21d6c 100644 --- a/include/bitcoin/database/impl/query/pool/pool_suffice.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_suffice.ipp @@ -167,7 +167,7 @@ bool CLASS::set_pool_unvalidated(size_t& out, } } - return true; + return store_.set_pool_top(to_parent(link)); // ======================================================================== } diff --git a/include/bitcoin/database/impl/store/store.ipp b/include/bitcoin/database/impl/store/store.ipp index 11504fc1b..03737d53c 100644 --- a/include/bitcoin/database/impl/store/store.ipp +++ b/include/bitcoin/database/impl/store/store.ipp @@ -307,6 +307,25 @@ void CLASS::set_pooling() NOEXCEPT set_dirty(); } +TEMPLATE +header_link CLASS::get_pool_top() const NOEXCEPT +{ + return pool_top_.load(std::memory_order_relaxed); +} + +TEMPLATE +bool CLASS::set_pool_top(const header_link& link) NOEXCEPT +{ + envelope_.pool_top = link.value; + pool_top_.store(link.value, std::memory_order_relaxed); + + using integer = table::envelope::link::integer; + const auto position = envelope_.pool_top_offset(); + const auto offset = system::possible_narrow_cast(position); + const table::envelope::put_pool_top record{ link.value }; + return envelope.put(offset, record); +} + TEMPLATE database::envelope::activation CLASS::get_activation( size_t index) const NOEXCEPT diff --git a/include/bitcoin/database/impl/store/store_open.ipp b/include/bitcoin/database/impl/store/store_open.ipp index fdaf63152..49b5b6722 100644 --- a/include/bitcoin/database/impl/store/store_open.ipp +++ b/include/bitcoin/database/impl/store/store_open.ipp @@ -259,6 +259,10 @@ code CLASS::migrate(system::config::version& original, ec = migrate_4_0_32_to_4_0_33(handler); schema = { 4, 0, 33, 0 }; } + else if (schema == system::config::version{ 4, 0, 33, 0 }) + { + schema = { 4, 0, 34, 0 }; + } else { ec = error::schema_version; @@ -803,6 +807,7 @@ code CLASS::load_envelope() NOEXCEPT // The stored latch governs, it is one-way and survives the session. scan_frontier_.store(envelope_.scan_frontier, std::memory_order_relaxed); latch_tx_.store(envelope_.latch_tx, std::memory_order_relaxed); + pool_top_.store(envelope_.pool_top, std::memory_order_relaxed); for (size_t index{}; index < activation_count; ++index) { const auto& activation = envelope_.activations.at(index); diff --git a/include/bitcoin/database/impl/store/store_restore.ipp b/include/bitcoin/database/impl/store/store_restore.ipp index 42c88e292..c776c9973 100644 --- a/include/bitcoin/database/impl/store/store_restore.ipp +++ b/include/bitcoin/database/impl/store/store_restore.ipp @@ -181,6 +181,10 @@ code CLASS::restore(const event_handler& handler) NOEXCEPT dropped(ec, spends_index, table_t::spends_index_table); dropped(ec, wtxid, table_t::wtxid_table); + // Dropped pool has no top. + if (!ec && !set_pool_top(header_link::terminal)) + ec = error::restore_table; + restore(ec, filter_bk, table_t::filter_bk_table); restore(ec, filter_tx, table_t::filter_tx_table); diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index fc988b81c..a07ed65c6 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -131,6 +131,10 @@ class query size_t get_scan_frontier() const NOEXCEPT; size_t get_latch_tx() const NOEXCEPT; + /// Last block applied to pool, terminal if none. + header_link get_pool_top() const NOEXCEPT; + bool set_pool_top(const header_link& link) NOEXCEPT; + /// Height at which forks of flag activated, if confirmed active. bool get_activation(size_t& out, uint32_t flag) const NOEXCEPT; @@ -790,7 +794,7 @@ class query /// Rebuild pool index heads from pool (after compaction). bool set_pool_index() NOEXCEPT; - /// Apply confirmation (or reorganization) of block to index. + /// Apply confirmation (pool top) or reorganization of block to index. bool set_pool_block(const header_link& link, bool confirmed) NOEXCEPT; /// Apply pool context to index, out rows it does not satisfy. @@ -800,7 +804,7 @@ class query bool set_pooled_context(const tx_link& link, const context& ctx, size_t sigops) NOEXCEPT; - /// Set each pooled spender of block's txs not validated, out count. + /// Set pooled spenders of block's txs not validated, parent pool top. bool set_pool_unvalidated(size_t& out, const header_link& link) NOEXCEPT; /// Coverage. diff --git a/include/bitcoin/database/store.hpp b/include/bitcoin/database/store.hpp index 421bbf235..d9a88f913 100644 --- a/include/bitcoin/database/store.hpp +++ b/include/bitcoin/database/store.hpp @@ -123,6 +123,10 @@ class store size_t get_scan_frontier() const NOEXCEPT; size_t get_latch_tx() const NOEXCEPT; + /// Last block applied to pool, terminal if none (set under transactor). + header_link get_pool_top() const NOEXCEPT; + bool set_pool_top(const header_link& link) NOEXCEPT; + /// Fork activation slot, flags activated together and their header. database::envelope::activation get_activation(size_t index) const NOEXCEPT; bool set_activation(size_t index, uint32_t flags, @@ -356,6 +360,7 @@ class store stopper pooling_{ false }; std::atomic scan_frontier_{}; std::atomic latch_tx_{}; + std::atomic pool_top_{ header_link::terminal }; std::array, activation_count> activation_flags_{}; std::array activation_links_{}; diff --git a/include/bitcoin/database/tables/caches/envelope.hpp b/include/bitcoin/database/tables/caches/envelope.hpp index 4e86e7d63..a5466819b 100644 --- a/include/bitcoin/database/tables/caches/envelope.hpp +++ b/include/bitcoin/database/tables/caches/envelope.hpp @@ -105,6 +105,26 @@ struct envelope uint32_t latch_tx{}; }; + struct put_pool_top + { + static constexpr size_t size = max_size_t; + + inline link count() const NOEXCEPT + { + return system::possible_narrow_cast( + database::envelope::pool_top_size); + } + + inline bool to_data(flipper& sink) const NOEXCEPT + { + using header = schema::header::link; + sink.write_little_endian(header_fk); + return sink; + } + + schema::header::link::integer header_fk{}; + }; + struct put_activation { static constexpr size_t size = max_size_t; diff --git a/include/bitcoin/database/tables/envelope.hpp b/include/bitcoin/database/tables/envelope.hpp index d4bc538ef..5813346c5 100644 --- a/include/bitcoin/database/tables/envelope.hpp +++ b/include/bitcoin/database/tables/envelope.hpp @@ -63,6 +63,9 @@ struct BCD_API envelope /// Migrate a serialized 4.0.32 envelope to 4.0.33. static bool migrate_4_0_32_to_4_0_33(system::data_chunk& data) NOEXCEPT; + /// Migrate a serialized 4.0.33 envelope to 4.0.34. + static bool migrate_4_0_33_to_4_0_34(system::data_chunk& data) NOEXCEPT; + /// Migrate serialized migratable envelope to next schema, taking values /// prior schema does not store from configured. static bool migrate_step(system::data_chunk& data, @@ -77,6 +80,7 @@ struct BCD_API envelope schema::header::pk; static constexpr size_t pooling_size = sizeof(uint8_t) + (2 * sizeof(uint32_t)); + static constexpr size_t pool_top_size = schema::header::pk; envelope() NOEXCEPT; envelope(const system::settings& bitcoin, const settings& database, @@ -96,6 +100,7 @@ struct BCD_API envelope size_t serialized_size() const NOEXCEPT; size_t pooling_offset() const NOEXCEPT; + size_t pool_top_offset() const NOEXCEPT; size_t activation_offset(size_t index) const NOEXCEPT; bool operator==(const envelope& other) const NOEXCEPT = default; @@ -169,6 +174,7 @@ struct BCD_API envelope bool pooling{}; uint32_t scan_frontier{}; uint32_t latch_tx{}; + schema::header::link::integer pool_top{ schema::header::link::terminal }; std::array activations{}; }; diff --git a/include/bitcoin/database/tables/schema.hpp b/include/bitcoin/database/tables/schema.hpp index e4b6f6943..231040732 100644 --- a/include/bitcoin/database/tables/schema.hpp +++ b/include/bitcoin/database/tables/schema.hpp @@ -77,7 +77,8 @@ namespace schema { /// 4.0.31.0 Pool flags, heights and medians columns (from the pool record). /// 4.0.32.0 Require witness in the envelope. /// 4.0.33.0 Pruned flags merged into the tx and txs heavy sizes. -constexpr std::array version{ 4, 0, 33, 0 }; +/// 4.0.34.0 Pool top in the envelope. +constexpr std::array version{ 4, 0, 34, 0 }; /// The oldest store schema migrated to version. constexpr std::array migratable{ 4, 0, 30, 0 }; diff --git a/src/tables/envelope.cpp b/src/tables/envelope.cpp index cf2cebe11..4929b7a7d 100644 --- a/src/tables/envelope.cpp +++ b/src/tables/envelope.cpp @@ -105,6 +105,21 @@ bool envelope::migrate_4_0_32_to_4_0_33(data_chunk& data) NOEXCEPT return true; } +// pool_top precedes activations. +bool envelope::migrate_4_0_33_to_4_0_34(data_chunk& data) NOEXCEPT +{ + constexpr size_t link = 3; + constexpr size_t activations = 32 * (sizeof(uint32_t) + link); + constexpr data_array terminal{ 0xff, 0xff, 0x7f }; + if (data.size() < activations) + return false; + + const auto at = std::prev(data.end(), activations); + data.insert(at, terminal.begin(), terminal.end()); + set_schema(data, { 4, 0, 34, 0 }); + return true; +} + bool envelope::migrate_step(data_chunk& data, const envelope& configured) NOEXCEPT { @@ -121,6 +136,9 @@ bool envelope::migrate_step(data_chunk& data, if (schema == config::version{ 4, 0, 32, 0 }) return migrate_4_0_32_to_4_0_33(data); + if (schema == config::version{ 4, 0, 33, 0 }) + return migrate_4_0_33_to_4_0_34(data); + return false; } @@ -401,6 +419,7 @@ bool envelope::from_data(reader& source) NOEXCEPT using integer = schema::header::link::integer; constexpr auto size = schema::header::link::size; + pool_top = source.read_little_endian(); for (auto& activation: activations) { activation.flags = source.read_little_endian(); @@ -528,6 +547,7 @@ bool envelope::to_data(flipper& sink) const NOEXCEPT using integer = schema::header::link::integer; constexpr auto size = schema::header::link::size; + sink.write_little_endian(pool_top); for (const auto& activation: activations) { sink.write_little_endian(activation.flags); @@ -538,9 +558,14 @@ bool envelope::to_data(flipper& sink) const NOEXCEPT } size_t envelope::pooling_offset() const NOEXCEPT +{ + return pool_top_offset() - pooling_size; +} + +size_t envelope::pool_top_offset() const NOEXCEPT { constexpr auto activations_size = activation_count * activation_size; - return serialized_size() - pooling_size - activations_size; + return serialized_size() - pool_top_size - activations_size; } size_t envelope::activation_offset(size_t index) const NOEXCEPT @@ -557,6 +582,7 @@ size_t envelope::serialized_size() const NOEXCEPT forks_count + ( 1 * sizeof(uint64_t)) + (18 * sizeof(uint32_t)) + + pool_top_size + (activation_count * activation_size); const auto checkpoint_size = [](const chain::checkpoint& in) NOEXCEPT diff --git a/test/query/pool/pool_index.cpp b/test/query/pool/pool_index.cpp index 6cbd16d08..66cfb4dc4 100644 --- a/test/query/pool/pool_index.cpp +++ b/test/query/pool/pool_index.cpp @@ -349,4 +349,124 @@ BC_AUTO_TEST_CASE(query_pool__set_pool_index__repooled_child__purged_row_kept_pu BC_REQUIRE_EQUAL(pool_cell(query, store, child), system::pool::algorithm::unlabeled); } +// pool top +// ---------------------------------------------------------------------------- + +BC_AUTO_TEST_CASE(query_pool__get_pool_top__initialized__terminal) +{ + 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)); + BC_REQUIRE(query.get_pool_top().is_terminal()); +} + +BC_AUTO_TEST_CASE(query_pool__set_pool_top__link__stored) +{ + 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)); + BC_REQUIRE(query.set_pool_top(header_link{ 42 })); + BC_REQUIRE_EQUAL(query.get_pool_top().value, 42u); +} + +BC_AUTO_TEST_CASE(query_pool__set_pool_block__confirmed_empty_pool__pool_top_block) +{ + 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 link = archive_confirm(query, to_spend(genesis_coinbase(), 4'999'999'000)); + + BC_REQUIRE(!link.is_terminal()); + BC_REQUIRE_EQUAL(query.pool_records(), zero); + BC_REQUIRE_EQUAL(query.get_pool_top().value, 1u); +} + +BC_AUTO_TEST_CASE(query_pool__set_pool_block__confirmed__pool_top_block) +{ + 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 pooled = pool_populated(query, to_spend(genesis_coinbase(), 4'999'998'500)); + const auto link = archive_confirm(query, to_spend(genesis_coinbase(), 4'999'999'000)); + + BC_REQUIRE(pooled); + BC_REQUIRE(!link.is_terminal()); + BC_REQUIRE_EQUAL(query.get_pool_top().value, 1u); +} + +BC_AUTO_TEST_CASE(query_pool__set_pool_block__reorganized__pool_top_unchanged) +{ + 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 pooled = pool_populated(query, to_spend(genesis_coinbase(), 4'999'998'500)); + const auto link = archive_confirm(query, to_spend(genesis_coinbase(), 4'999'999'000)); + const auto popped = query.pop_confirmed(); + const auto reverted = query.set_pool_block(link, false); + + BC_REQUIRE(pooled); + BC_REQUIRE(!link.is_terminal()); + BC_REQUIRE(popped); + BC_REQUIRE(reverted); + BC_REQUIRE_EQUAL(query.get_pool_top().value, 1u); +} + +BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__block__pool_top_parent) +{ + 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 link = archive_confirm(query, to_spend(genesis_coinbase(), 4'999'999'000)); + const auto popped = query.pop_confirmed(); + size_t marked{}; + const auto unvalidated = query.set_pool_unvalidated(marked, link); + + BC_REQUIRE(!link.is_terminal()); + BC_REQUIRE(popped); + BC_REQUIRE(unvalidated); + BC_REQUIRE_EQUAL(query.get_pool_top().value, 0u); +} + +BC_AUTO_TEST_CASE(query_pool__set_pool_block__disabled__pool_top_terminal) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + settings.pool.buckets = 0; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_REQUIRE(!store.create(test::events_handler)); + BC_REQUIRE(query.initialize(test::genesis)); + + size_t marked{}; + BC_REQUIRE(query.set_pool_block(query.to_header(test::genesis.hash()), true)); + BC_REQUIRE(query.set_pool_unvalidated(marked, query.to_header(test::genesis.hash()))); + BC_REQUIRE(query.get_pool_top().is_terminal()); +} + BC_AUTO_TEST_SUITE_END() diff --git a/test/store/store.cpp b/test/store/store.cpp index 88bcc9307..a5c8b5cdb 100644 --- a/test/store/store.cpp +++ b/test/store/store.cpp @@ -306,6 +306,55 @@ BC_AUTO_TEST_CASE(store__set_activation__then_pooling__both_persisted) BC_REQUIRE(!instance.close(test::events)); } +// set_pool_top +// ---------------------------------------------------------------------------- + +BC_AUTO_TEST_CASE(store__get_pool_top__default__terminal) +{ + const settings configuration{ test::database_settings() }; + test::map_store instance{ configuration }; + BC_REQUIRE(instance.get_pool_top().is_terminal()); +} + +BC_AUTO_TEST_CASE(store__set_pool_top__link__expected) +{ + settings configuration{ test::database_settings() }; + configuration.path = TEST_DIRECTORY; + store instance{ configuration }; + query> query_{ instance }; + BC_REQUIRE(!instance.create(test::events)); + BC_REQUIRE(query_.initialize(test::genesis)); + + BC_REQUIRE(instance.set_pool_top(42)); + BC_REQUIRE_EQUAL(instance.get_pool_top().value, 42u); + BC_REQUIRE(!instance.close(test::events)); +} + +BC_AUTO_TEST_CASE(store__set_pool_top__between_pooling_and_activation__all_persisted) +{ + settings configuration{ test::database_settings() }; + configuration.path = TEST_DIRECTORY; + store instance{ configuration }; + query> query_{ instance }; + BC_REQUIRE(!instance.create(test::events)); + BC_REQUIRE(query_.initialize(test::genesis)); + + BC_REQUIRE(instance.set_activation(0, 0x01, 42)); + instance.set_pooling(); + const auto latch = instance.get_latch_tx(); + BC_REQUIRE(instance.set_pool_top(43)); + BC_REQUIRE(!instance.close(test::events)); + BC_REQUIRE(!instance.open(test::events)); + + const auto first = instance.get_activation(0); + BC_REQUIRE(instance.is_pooling()); + BC_REQUIRE_EQUAL(instance.get_latch_tx(), latch); + BC_REQUIRE_EQUAL(instance.get_pool_top().value, 43u); + BC_REQUIRE_EQUAL(first.flags, 0x01u); + BC_REQUIRE_EQUAL(first.link, 42u); + BC_REQUIRE(!instance.close(test::events)); +} + // set_current // ---------------------------------------------------------------------------- diff --git a/test/store/store_open.cpp b/test/store/store_open.cpp index 711278dcd..457fde6e1 100644 --- a/test/store/store_open.cpp +++ b/test/store/store_open.cpp @@ -219,7 +219,7 @@ BC_AUTO_TEST_CASE(store__migrate__4_0_30__compiled) BC_REQUIRE(!test::exists(obsolete)); BC_REQUIRE_GT(test::size(instance.pool_head_file()), pool_head); BC_REQUIRE_GT(test::size(instance.wtxid_head_file()), wtxid_head); - BC_REQUIRE_EQUAL(test::size(instance.envelope_head_file()), add1(image.size())); + BC_REQUIRE_EQUAL(test::size(instance.envelope_head_file()), image.size() + 4u); BC_REQUIRE(!test::exists(instance.fault_lock_file())); BC_REQUIRE(!instance.open(test::events)); BC_REQUIRE(!instance.close(test::events)); @@ -248,7 +248,7 @@ BC_AUTO_TEST_CASE(store__migrate__4_0_31__compiled) system::config::version original{}; BC_REQUIRE(!instance.migrate(original, configured, test::events)); BC_REQUIRE(original == system::config::version(4, 0, 31, 0)); - BC_REQUIRE_EQUAL(test::size(instance.envelope_head_file()), add1(image.size())); + BC_REQUIRE_EQUAL(test::size(instance.envelope_head_file()), image.size() + 4u); BC_REQUIRE(!test::exists(instance.fault_lock_file())); BC_REQUIRE(!test::exists(instance.flush_lock_file())); BC_REQUIRE(!instance.open(test::events)); @@ -263,6 +263,40 @@ BC_AUTO_TEST_CASE(store__migrate__4_0_31__compiled) BC_REQUIRE_EQUAL(stored.silent_start_height, 42u); } +BC_AUTO_TEST_CASE(store__migrate__4_0_33__pool_top_terminal_activation_kept) +{ + settings configuration{ test::database_settings() }; + configuration.path = TEST_DIRECTORY; + test::map_store instance{ configuration }; + BC_REQUIRE(!instance.create(test::events)); + BC_REQUIRE(!instance.close(test::events)); + + // Last activation link is the last three bytes. + database::envelope configured{}; + auto image = test::envelope_4_0_31; + BC_REQUIRE(envelope::migrate_step(image, configured)); + BC_REQUIRE(envelope::migrate_step(image, configured)); + image.at(image.size() - 3u) = 42; + image.at(image.size() - 2u) = 0; + image.at(image.size() - 1u) = 0; + BC_REQUIRE(test::create(instance.envelope_head_file(), image)); + + system::config::version original{}; + BC_REQUIRE(!instance.migrate(original, configured, test::events)); + BC_REQUIRE(original == system::config::version(4, 0, 33, 0)); + BC_REQUIRE_EQUAL(test::size(instance.envelope_head_file()), image.size() + 3u); + BC_REQUIRE(!test::exists(instance.fault_lock_file())); + BC_REQUIRE(!instance.open(test::events)); + BC_REQUIRE_EQUAL(instance.get_pool_top().value, schema::header::link::terminal); + BC_REQUIRE(!instance.close(test::events)); + + database::envelope stored{}; + BC_REQUIRE(!instance.read_envelope(stored)); + BC_REQUIRE(stored.schema == envelope::compiled); + BC_REQUIRE_EQUAL(stored.pool_top, schema::header::link::terminal); + BC_REQUIRE_EQUAL(stored.activations.back().link, 42u); +} + // Archive a pruned block and clear its flags, as stored before 4.0.33. static bool set_unflagged_pruned_block(test::map_store& store) NOEXCEPT { @@ -983,6 +1017,50 @@ BC_AUTO_TEST_CASE(store__open__compacted__pooled_after) BC_REQUIRE(!instance.close(test::events)); } +BC_AUTO_TEST_CASE(store__open__pool_top__retained) +{ + settings configuration{ test::database_settings() }; + configuration.path = TEST_DIRECTORY; + store instance{ configuration }; + query> query_{ instance }; + BC_REQUIRE(!instance.create(test::events)); + BC_REQUIRE(query_.initialize(test::genesis)); + instance.set_pooling(); + + BC_REQUIRE(confirm(query_, spend(genesis_coinbase(), 1u))); + BC_REQUIRE(query_.set_pool_block(query_.to_confirmed(1), true)); + BC_REQUIRE(!instance.close(test::events)); + BC_REQUIRE(!instance.open(test::events)); + BC_REQUIRE_EQUAL(query_.get_pool_top().value, 1u); + BC_REQUIRE(!instance.close(test::events)); +} + +BC_AUTO_TEST_CASE(store__restore__pool_top__terminal) +{ + settings configuration{ test::database_settings() }; + configuration.path = TEST_DIRECTORY; + store instance{ configuration }; + query> query_{ instance }; + BC_REQUIRE(!instance.create(test::events)); + BC_REQUIRE(query_.initialize(test::genesis)); + instance.set_pooling(); + + BC_REQUIRE(query_.set_pool_top(query_.to_confirmed(0))); + BC_REQUIRE(!instance.close(test::events)); + BC_REQUIRE(!instance.open(test::events)); + BC_REQUIRE(!query_.get_pool_top().is_terminal()); + BC_REQUIRE(!instance.snapshot(test::events)); + BC_REQUIRE(!instance.close(test::events)); + + BC_REQUIRE(test::create(test::flush_lock_file(configuration.path))); + BC_REQUIRE(!instance.restore(test::events)); + BC_REQUIRE(query_.get_pool_top().is_terminal()); + BC_REQUIRE(!instance.close(test::events)); + BC_REQUIRE(!instance.open(test::events)); + BC_REQUIRE(query_.get_pool_top().is_terminal()); + BC_REQUIRE(!instance.close(test::events)); +} + BC_AUTO_TEST_CASE(store__open__retained_snapshot_faulted_restore__pool_empty) { settings configuration{ test::database_settings() }; diff --git a/test/tables/envelope.cpp b/test/tables/envelope.cpp index fac268d07..584f6b20c 100644 --- a/test/tables/envelope.cpp +++ b/test/tables/envelope.cpp @@ -22,7 +22,7 @@ BC_AUTO_TEST_SUITE(envelope_tests) using namespace system; -constexpr auto default_size = 615_size; +constexpr auto default_size = 618_size; static data_chunk to_chunk(const envelope& instance) { @@ -52,7 +52,7 @@ BC_AUTO_TEST_CASE(envelope__serialized_size__default__expected) BC_AUTO_TEST_CASE(envelope__to_data__default__expected_version) { const envelope instance{}; - BC_REQUIRE_EQUAL(instance.schema, system::config::version(4, 0, 33, 0)); + BC_REQUIRE_EQUAL(instance.schema, system::config::version(4, 0, 34, 0)); BC_REQUIRE_EQUAL(to_chunk(instance).front(), schema::version.front()); } @@ -102,6 +102,38 @@ BC_AUTO_TEST_CASE(envelope__pooling_offset__serialized__expected) BC_REQUIRE_EQUAL(system::unsafe_from_little_endian(&data.at(offset + 5u)), 0x05060708u); } +BC_AUTO_TEST_CASE(envelope__construct__default__pool_top_terminal) +{ + const envelope instance{}; + BC_REQUIRE_EQUAL(instance.pool_top, schema::header::link::terminal); +} + +BC_AUTO_TEST_CASE(envelope__from_data__pool_top__round_trip) +{ + envelope instance{}; + instance.pool_top = 709632; + auto data = to_chunk(instance); + BC_REQUIRE_EQUAL(data.size(), default_size); + + envelope out{}; + BC_REQUIRE(from_chunk(out, data)); + BC_REQUIRE_EQUAL(out.pool_top, 709632u); + BC_REQUIRE(out == instance); +} + +BC_AUTO_TEST_CASE(envelope__pool_top_offset__serialized__expected) +{ + envelope instance{}; + instance.pool_top = 0x030201; + const auto data = to_chunk(instance); + const auto offset = instance.pool_top_offset(); + BC_REQUIRE_EQUAL(data.at(offset), 0x01u); + BC_REQUIRE_EQUAL(data.at(offset + 1u), 0x02u); + BC_REQUIRE_EQUAL(data.at(offset + 2u), 0x03u); + BC_REQUIRE_EQUAL(instance.pooling_offset() + envelope::pooling_size, offset); + BC_REQUIRE_EQUAL(offset + envelope::pool_top_size, instance.activation_offset(0)); +} + BC_AUTO_TEST_CASE(envelope__activation_offset__serialized__expected) { envelope instance{}; @@ -410,6 +442,11 @@ BC_AUTO_TEST_CASE(envelope__migrate_step__4_0_31__stepwise_expected) BC_REQUIRE(envelope::get_schema(data) == system::config::version(4, 0, 33, 0)); BC_REQUIRE_EQUAL(data.size(), add1(test::envelope_4_0_31.size())); + // 4.0.33 -> 4.0.34 + BC_REQUIRE(envelope::migrate_step(data, configured)); + BC_REQUIRE(envelope::get_schema(data) == system::config::version(4, 0, 34, 0)); + BC_REQUIRE_EQUAL(data.size(), test::envelope_4_0_31.size() + 4u); + // compiled BC_REQUIRE(envelope::get_schema(data) == envelope::compiled); BC_REQUIRE(!envelope::migrate_step(data, configured)); @@ -421,6 +458,7 @@ BC_AUTO_TEST_CASE(envelope__migrate_step__4_0_31__stepwise_expected) BC_REQUIRE(out.provide_filters); BC_REQUIRE(!out.limited_blocks); BC_REQUIRE_EQUAL(out.silent_start_height, 42u); + BC_REQUIRE_EQUAL(out.pool_top, schema::header::link::terminal); } BC_AUTO_TEST_CASE(envelope__migrate_4_0_30_to_4_0_31__schema_only) @@ -442,6 +480,27 @@ BC_AUTO_TEST_CASE(envelope__migrate_4_0_32_to_4_0_33__schema_only) BC_REQUIRE(data == expected); } +BC_AUTO_TEST_CASE(envelope__migrate_4_0_33_to_4_0_34__terminal_before_activations) +{ + // Activations are the last 224 bytes. + auto image = test::envelope_4_0_31; + image.at(8) = 33; + image.back() = 0x42; + auto data = image; + BC_REQUIRE(envelope::migrate_4_0_33_to_4_0_34(data)); + BC_REQUIRE(envelope::get_schema(data) == system::config::version(4, 0, 34, 0)); + BC_REQUIRE_EQUAL(data.size(), image.size() + 3u); + BC_REQUIRE(data_chunk(std::prev(data.end(), 227), std::prev(data.end(), 224)) == data_chunk{ 0xff, 0xff, 0x7f }); + BC_REQUIRE(data_chunk(std::prev(data.end(), 224), data.end()) == data_chunk(std::prev(image.end(), 224), image.end())); + BC_REQUIRE(data_chunk(std::next(data.begin(), 16), std::prev(data.end(), 227)) == data_chunk(std::next(image.begin(), 16), std::prev(image.end(), 224))); +} + +BC_AUTO_TEST_CASE(envelope__migrate_4_0_33_to_4_0_34__short__false) +{ + data_chunk data(223); + BC_REQUIRE(!envelope::migrate_4_0_33_to_4_0_34(data)); +} + BC_AUTO_TEST_CASE(envelope__migrate__4_0_30__compiled) { envelope configured{}; From 52062ef66621a1db5b5d50c9a01cc5c351da1d6e Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 17:02:11 -0500 Subject: [PATCH 3/4] Name the terminal link bytes in the pool top migration test. --- test/tables/envelope.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/tables/envelope.cpp b/test/tables/envelope.cpp index 584f6b20c..301a1d1f0 100644 --- a/test/tables/envelope.cpp +++ b/test/tables/envelope.cpp @@ -487,10 +487,11 @@ BC_AUTO_TEST_CASE(envelope__migrate_4_0_33_to_4_0_34__terminal_before_activation image.at(8) = 33; image.back() = 0x42; auto data = image; + const data_chunk terminal{ 0xff, 0xff, 0x7f }; BC_REQUIRE(envelope::migrate_4_0_33_to_4_0_34(data)); BC_REQUIRE(envelope::get_schema(data) == system::config::version(4, 0, 34, 0)); BC_REQUIRE_EQUAL(data.size(), image.size() + 3u); - BC_REQUIRE(data_chunk(std::prev(data.end(), 227), std::prev(data.end(), 224)) == data_chunk{ 0xff, 0xff, 0x7f }); + BC_REQUIRE(data_chunk(std::prev(data.end(), 227), std::prev(data.end(), 224)) == terminal); BC_REQUIRE(data_chunk(std::prev(data.end(), 224), data.end()) == data_chunk(std::prev(image.end(), 224), image.end())); BC_REQUIRE(data_chunk(std::next(data.begin(), 16), std::prev(data.end(), 227)) == data_chunk(std::next(image.begin(), 16), std::prev(image.end(), 224))); } From 9d06c5594ec31fe7571c4eb396a1499cebd51bdc Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 19:48:00 -0500 Subject: [PATCH 4/4] Ignore in-tree gnu build outputs. --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d8d63fb55..ba654f97d 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ # gnu .deps +.dirstamp Makefile Makefile.in .libs @@ -28,7 +29,10 @@ builds/gnu/build-aux builds/gnu/config.* builds/gnu/configure builds/gnu/libtool -builds/gnu/.dirstamp +builds/gnu/console +builds/gnu/test +builds/gnu/*.log +builds/gnu/*.trs # pkg-config pc file from .pc.in builds/gnu/*.pc