From 5af619d109a971b9dfa0ff5a9cb35c3715428081 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Tue, 24 Nov 2026 16:20:59 -0500 Subject: [PATCH 1/2] Query the tops of the branches of weak headers. --- .../impl/query/consensus/consensus_forks.ipp | 58 +++++++++++++++ include/bitcoin/database/query.hpp | 1 + test/query/confirmed.cpp | 72 +++++++++++++++++++ 3 files changed, 131 insertions(+) diff --git a/include/bitcoin/database/impl/query/consensus/consensus_forks.ipp b/include/bitcoin/database/impl/query/consensus/consensus_forks.ipp index 9f0d1590d..d50b5e790 100644 --- a/include/bitcoin/database/impl/query/consensus/consensus_forks.ipp +++ b/include/bitcoin/database/impl/query/consensus/consensus_forks.ipp @@ -56,6 +56,64 @@ size_t CLASS::get_fork() const NOEXCEPT /////////////////////////////////////////////////////////////////////////// } +// server/native +TEMPLATE +header_links CLASS::get_branch_tops() const NOEXCEPT +{ + using namespace system; + using link_t = header_link::integer; + const auto top = std::max(get_top_candidate(), get_top_confirmed()); + const auto records = header_records(); + + // A height of more than one header is forked, as is any above the tops. + size_t height{}; + std::vector seen(add1(top), false); + std::vector forked(add1(top), false); + for (size_t record{}; record < records; ++record) + { + if (!get_height(height, possible_narrow_cast(record)) || + height > top) + continue; + + if (seen.at(height)) + forked.at(height) = true; + else + seen.at(height) = true; + } + + std::unordered_set forks{}; + for (size_t record{}; record < records; ++record) + { + const auto link = possible_narrow_cast(record); + if (get_height(height, link) && (height > top || forked.at(height))) + forks.insert(link); + } + + // A child follows its parent, so the last link of a branch is its top. + header_links tops{}; + for (auto record = records; is_nonzero(record) && !forks.empty();) + { + const auto link = possible_narrow_cast(--record); + if (!forks.contains(link)) + continue; + + tops.push_back(link); + for (auto next = link; is_nonzero(forks.erase(next));) + next = to_parent(next).value; + } + + /////////////////////////////////////////////////////////////////////////// + std::shared_lock interlock1{ candidate_reorganization_mutex_ }; + std::shared_lock interlock2{ confirmed_reorganization_mutex_ }; + std::erase_if(tops, [&](const auto& link) NOEXCEPT + { + return is_candidate_header(link) || is_confirmed_block(link); + }); + /////////////////////////////////////////////////////////////////////////// + + return tops; +} + // node/organizer TEMPLATE header_links CLASS::get_candidate_fork(size_t& fork_point) const NOEXCEPT diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 68caa1192..5483c23ca 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -330,6 +330,7 @@ class query inline size_t get_top_candidate() const NOEXCEPT; inline size_t get_top_confirmed() const NOEXCEPT; size_t get_fork() const NOEXCEPT; + header_links get_branch_tops() const NOEXCEPT; size_t get_top_associated() const NOEXCEPT; size_t get_top_associated_from(size_t height) const NOEXCEPT; associations get_all_unassociated() const NOEXCEPT; diff --git a/test/query/confirmed.cpp b/test/query/confirmed.cpp index 133b57fc7..d49b1895b 100644 --- a/test/query/confirmed.cpp +++ b/test/query/confirmed.cpp @@ -373,4 +373,76 @@ BC_AUTO_TEST_CASE(query_confirmed__is_confirmed_all_prevouts__prevouts_confirmed BC_REQUIRE(query.is_confirmed_all_prevouts(2)); } +// get_branch_tops + +BC_AUTO_TEST_CASE(query_confirmed__get_branch_tops__genesis__empty) +{ + 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_branch_tops().empty()); +} + +BC_AUTO_TEST_CASE(query_confirmed__get_branch_tops__candidates__empty) +{ + 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(test::block1.header(), context{ 0, 1, 0 }, {}, false)); + BC_REQUIRE(query.set(test::block2.header(), context{ 0, 2, 0 }, {}, false)); + BC_REQUIRE(query.push_candidate(1)); + BC_REQUIRE(query.push_candidate(2)); + BC_REQUIRE(query.get_branch_tops().empty()); +} + +BC_AUTO_TEST_CASE(query_confirmed__get_branch_tops__weak_header__top) +{ + 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(test::block1.header(), context{ 0, 1, 0 }, {}, false)); + BC_REQUIRE(query.set(test::block1a.header(), context{ 0, 1, 0 }, {}, false)); + BC_REQUIRE(query.push_candidate(1)); + BC_REQUIRE_EQUAL(query.get_branch_tops(), header_links{ 2 }); +} + +BC_AUTO_TEST_CASE(query_confirmed__get_branch_tops__weak_branch__top_only) +{ + 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(test::block1.header(), context{ 0, 1, 0 }, {}, false)); + BC_REQUIRE(query.set(test::block1a.header(), context{ 0, 1, 0 }, {}, false)); + BC_REQUIRE(query.set(test::block2a.header(), context{ 0, 2, 0 }, {}, false)); + BC_REQUIRE(query.push_candidate(1)); + BC_REQUIRE_EQUAL(query.get_branch_tops(), header_links{ 3 }); +} + +BC_AUTO_TEST_CASE(query_confirmed__get_branch_tops__confirmed_above_fork__empty) +{ + 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(test::block1, context{ 0, 1, 0 }, {}, false, false)); + BC_REQUIRE(query.set(test::block1a.header(), context{ 0, 1, 0 }, {}, false)); + BC_REQUIRE(query.push_confirmed(1, false)); + BC_REQUIRE(query.push_candidate(2)); + BC_REQUIRE(query.get_branch_tops().empty()); +} + BC_AUTO_TEST_SUITE_END() From fef98a96ee42e9be42c44d392642e6def2bea545 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Tue, 24 Nov 2026 17:26:15 -0500 Subject: [PATCH 2/2] Store pool totals as weight, drop pool on migration to 4.0.35. --- .../impl/query/pool/pool_confirmable.ipp | 4 +- .../impl/query/pool/pool_coverage.ipp | 14 ++--- .../impl/query/pool/pool_estimate.ipp | 4 +- .../database/impl/query/pool/pool_reader.ipp | 10 ++-- .../database/impl/query/pool/pool_writer.ipp | 2 +- .../database/impl/store/store_open.ipp | 36 +++++++++-- include/bitcoin/database/query.hpp | 4 +- include/bitcoin/database/store.hpp | 6 ++ .../bitcoin/database/tables/caches/pool.hpp | 10 ++-- include/bitcoin/database/tables/envelope.hpp | 3 + include/bitcoin/database/tables/schema.hpp | 3 +- src/tables/envelope.cpp | 10 ++++ test/mocks/chunk_store.hpp | 4 +- test/mocks/pooling.hpp | 2 +- test/query/amounts.cpp | 2 +- test/query/pool/pool_confirmable.cpp | 2 +- test/query/pool/pool_estimate.cpp | 60 +++++++++---------- test/query/pool/pool_reader.cpp | 2 +- test/query/pool/pool_suffice.cpp | 2 +- test/query/pool/pool_template.cpp | 8 +-- test/store/store_open.cpp | 34 +++++++++++ test/tables/envelope.cpp | 17 +++++- 22 files changed, 168 insertions(+), 71 deletions(-) diff --git a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp index 5d8b45c76..007af0c73 100644 --- a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp @@ -227,7 +227,9 @@ code CLASS::get_confirmable_fees(fee_rates& out) const NOEXCEPT const auto totals = algorithm::from_column(columns.totals[row]); const size_t height = algorithm::from_column(columns.heights[row]); - out.push_back({ totals.size, totals.fee, height }); + const auto bytes = ceilinged_divide(totals.weight, + chain::light_weight_factor); + out.push_back({ bytes, totals.fee, height }); } return error::success; diff --git a/include/bitcoin/database/impl/query/pool/pool_coverage.ipp b/include/bitcoin/database/impl/query/pool/pool_coverage.ipp index 29675b642..26b1102e6 100644 --- a/include/bitcoin/database/impl/query/pool/pool_coverage.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_coverage.ipp @@ -87,7 +87,7 @@ code CLASS::get_pool_candidate(pool_graph& out, const transaction& tx, out.totals.push_back(algorithm::to_column(algorithm::totals_t { tx.fee(), - possible_narrow_cast(tx.virtual_size()), + possible_narrow_cast(tx.weight()), possible_narrow_cast(sigops) })); @@ -236,18 +236,18 @@ code CLASS::get_pool_coverage(bool& out, const pool_graph& graph, return error::pool_limit; const auto best = algorithm::evaluate(masks, component, g); - const auto bytes = algorithm::chargeable(component, g, bytes_per_sigop); - each = each && algorithm::covered(best, bytes, rate); + const auto weight = algorithm::chargeable(component, g, bytes_per_sigop); + each = each && algorithm::covered(best, weight, rate); components.push_back(component); } // Pooled parent's surplus pays for its child (parent pays for child), in a // family within a block, beyond which each component pays its own. - uint64_t fee{}, bytes{}; - const auto bounded = algorithm::family(fee, bytes, g, measured, - limit.size, rate, bytes_per_sigop, maximum_outcomes); + uint64_t fee{}, weight{}; + const auto bounded = algorithm::family(fee, weight, g, measured, + limit.weight, rate, bytes_per_sigop, maximum_outcomes); - out = bounded ? algorithm::covered(fee, bytes, rate) : each; + out = bounded ? algorithm::covered(fee, weight, rate) : each; return error::success; } diff --git a/include/bitcoin/database/impl/query/pool/pool_estimate.ipp b/include/bitcoin/database/impl/query/pool/pool_estimate.ipp index b3d56c7bd..d17d029c3 100644 --- a/include/bitcoin/database/impl/query/pool/pool_estimate.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_estimate.ipp @@ -109,8 +109,8 @@ code CLASS::get_pool_arrivals(pool_arrivals& out, size_t top, size_t count, const auto totals = algorithm::from_column(columns.totals[at]); const auto rate = algorithm::fee_rate(totals, bytes_per_sigop); - const auto size = algorithm::chargeable(totals, bytes_per_sigop); - out.at(sub1(top - height)).push_back({ rate, size }); + const auto weight = algorithm::chargeable(totals, bytes_per_sigop); + out.at(sub1(top - height)).push_back({ rate, weight }); } return error::success; diff --git a/include/bitcoin/database/impl/query/pool/pool_reader.ipp b/include/bitcoin/database/impl/query/pool/pool_reader.ipp index 85fad0971..4ce396dfb 100644 --- a/include/bitcoin/database/impl/query/pool/pool_reader.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_reader.ipp @@ -123,7 +123,7 @@ bool CLASS::get_pooled_totals(pool_totals& out, if (!store_.pool.totals.get(fk, total)) return false; - out = { total.fee, total.bytes, total.sigops }; + out = { total.fee, total.weight, total.sigops }; return true; } @@ -138,12 +138,12 @@ TEMPLATE bool CLASS::get_pooled_fee(uint64_t& fee, size_t& height, const tx_link& link) const NOEXCEPT { - size_t size{}; - return get_pooled_fee(fee, size, height, link); + size_t weight{}; + return get_pooled_fee(fee, weight, height, link); } TEMPLATE -bool CLASS::get_pooled_fee(uint64_t& fee, size_t& size, size_t& height, +bool CLASS::get_pooled_fee(uint64_t& fee, size_t& weight, size_t& height, const tx_link& link) const NOEXCEPT { if (!store_.pool.enabled()) @@ -160,7 +160,7 @@ bool CLASS::get_pooled_fee(uint64_t& fee, size_t& size, size_t& height, return false; fee = total.fee; - size = total.bytes; + weight = total.weight; height = pooled.value; return true; } diff --git a/include/bitcoin/database/impl/query/pool/pool_writer.ipp b/include/bitcoin/database/impl/query/pool/pool_writer.ipp index 4073c7ccd..f3060a463 100644 --- a/include/bitcoin/database/impl/query/pool/pool_writer.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_writer.ipp @@ -147,7 +147,7 @@ bool CLASS::set_pool_row(const tx_link& link, const transaction& tx, { {}, tx.fee(), - possible_narrow_cast(tx.virtual_size()), + possible_narrow_cast(tx.weight()), possible_narrow_cast(sigops) }; const table::pool_edge edge diff --git a/include/bitcoin/database/impl/store/store_open.ipp b/include/bitcoin/database/impl/store/store_open.ipp index 49b5b6722..d624b7e78 100644 --- a/include/bitcoin/database/impl/store/store_open.ipp +++ b/include/bitcoin/database/impl/store/store_open.ipp @@ -263,6 +263,11 @@ code CLASS::migrate(system::config::version& original, { schema = { 4, 0, 34, 0 }; } + else if (schema == system::config::version{ 4, 0, 34, 0 }) + { + ec = migrate_4_0_34_to_4_0_35(stored, configured, handler); + schema = { 4, 0, 35, 0 }; + } else { ec = error::schema_version; @@ -327,11 +332,34 @@ code CLASS::purge_snapshots() NOEXCEPT return error::success; } -// The pool tables are discarded and created empty at configured sizing. TEMPLATE code CLASS::migrate_4_0_30_to_4_0_31(database::envelope& stored, const database::envelope& configured, const event_handler& handler) NOEXCEPT +{ + const auto obsolete = body(configuration_.path, + std::string{ schema::caches::pool } + "_context"); + + if (const auto ec = file::remove_ex(obsolete)) + return ec; + + return recreate_pool(stored, configured, handler); +} + +TEMPLATE +code CLASS::migrate_4_0_34_to_4_0_35(database::envelope& stored, + const database::envelope& configured, + const event_handler& handler) NOEXCEPT +{ + return recreate_pool(stored, configured, handler); +} + +// The pool tables are discarded and created empty at configured sizing, and +// the dropped pool has no top. +TEMPLATE +code CLASS::recreate_pool(database::envelope& stored, + const database::envelope& configured, + const event_handler& handler) NOEXCEPT { using namespace system; const auto pool_buckets = configured.pool_buckets; @@ -396,10 +424,7 @@ code CLASS::migrate_4_0_30_to_4_0_31(database::envelope& stored, if (const auto closed = file.close(); !ec) ec = closed; }; - const auto obsolete = body(configuration_.path, - std::string{ schema::caches::pool } + "_context"); - - auto ec = file::remove_ex(obsolete); + code ec{}; create(ec, pool_head_, table_t::pool_head); create(ec, pool_body_, table_t::pool_body); create(ec, spends_head_, table_t::spends_head); @@ -458,6 +483,7 @@ code CLASS::migrate_4_0_30_to_4_0_31(database::envelope& stored, stored.wtxid_buckets = wtxid_buckets; stored.pool_k = possible_narrow_cast(pool_k); stored.wtxid_k = possible_narrow_cast(wtxid_k); + stored.pool_top = schema::header::link::terminal; return error::success; } diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 5483c23ca..21f0eaf71 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -898,7 +898,7 @@ class query 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. + /// Rate and weight 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; @@ -1265,7 +1265,7 @@ class query bool get_pooled_fee(uint64_t& out, const tx_link& link) const NOEXCEPT; bool get_pooled_fee(uint64_t& fee, size_t& height, const tx_link& link) const NOEXCEPT; - bool get_pooled_fee(uint64_t& fee, size_t& size, size_t& height, + bool get_pooled_fee(uint64_t& fee, size_t& weight, size_t& height, const tx_link& link) const NOEXCEPT; hash_digest get_pooled_wtxid(const table::pool::link& link) const NOEXCEPT; diff --git a/include/bitcoin/database/store.hpp b/include/bitcoin/database/store.hpp index d9a88f913..a3a7eb862 100644 --- a/include/bitcoin/database/store.hpp +++ b/include/bitcoin/database/store.hpp @@ -188,6 +188,12 @@ class store code migrate_4_0_31_to_4_0_32(database::envelope& stored, const database::envelope& configured) NOEXCEPT; code migrate_4_0_32_to_4_0_33(const event_handler& handler) NOEXCEPT; + code migrate_4_0_34_to_4_0_35(database::envelope& stored, + const database::envelope& configured, + const event_handler& handler) NOEXCEPT; + code recreate_pool(database::envelope& stored, + const database::envelope& configured, + const event_handler& handler) NOEXCEPT; code get_pruned(bool& pruned, const table::transaction::record& record) const NOEXCEPT; diff --git a/include/bitcoin/database/tables/caches/pool.hpp b/include/bitcoin/database/tables/caches/pool.hpp index ee47235e7..297a65db9 100644 --- a/include/bitcoin/database/tables/caches/pool.hpp +++ b/include/bitcoin/database/tables/caches/pool.hpp @@ -66,7 +66,7 @@ struct pool_word lane word{}; }; -/// Fee, virtual size and sigops of the tx (totals column). +/// Fee, weight and sigops of the tx (totals column). struct pool_total : public schema::pool_totals { @@ -78,7 +78,7 @@ struct pool_total inline bool from_data(reader& source) NOEXCEPT { fee = source.read_little_endian(); - bytes = source.read_little_endian(); + weight = source.read_little_endian(); sigops = source.read_little_endian(); BC_ASSERT(!source || source.get_read_position() == minrow); return source; @@ -87,7 +87,7 @@ struct pool_total inline bool to_data(flipper& sink) const NOEXCEPT { sink.write_little_endian(fee); - sink.write_little_endian(bytes); + sink.write_little_endian(weight); sink.write_little_endian(sigops); BC_ASSERT(!sink || sink.get_write_position() == minrow); return sink; @@ -96,7 +96,7 @@ struct pool_total inline bool operator==(const pool_total&) const NOEXCEPT = default; uint64_t fee{}; - uint32_t bytes{}; + uint32_t weight{}; uint32_t sigops{}; }; @@ -186,7 +186,7 @@ struct pool_id3 using no_map::nomap; }; -/// Scan columns (fee, size, sigops and edges), words aligned for lanes. +/// Scan columns (fee, weight, sigops and edges), words aligned for lanes. struct pool_totals : public no_map { diff --git a/include/bitcoin/database/tables/envelope.hpp b/include/bitcoin/database/tables/envelope.hpp index 5813346c5..03d11a0d0 100644 --- a/include/bitcoin/database/tables/envelope.hpp +++ b/include/bitcoin/database/tables/envelope.hpp @@ -66,6 +66,9 @@ struct BCD_API envelope /// 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 a serialized 4.0.34 envelope to 4.0.35. + static bool migrate_4_0_34_to_4_0_35(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, diff --git a/include/bitcoin/database/tables/schema.hpp b/include/bitcoin/database/tables/schema.hpp index 231040732..255c6ac1b 100644 --- a/include/bitcoin/database/tables/schema.hpp +++ b/include/bitcoin/database/tables/schema.hpp @@ -78,7 +78,8 @@ namespace schema { /// 4.0.32.0 Require witness in the envelope. /// 4.0.33.0 Pruned flags merged into the tx and txs heavy sizes. /// 4.0.34.0 Pool top in the envelope. -constexpr std::array version{ 4, 0, 34, 0 }; +/// 4.0.35.0 Pool totals weight in place of virtual size. +constexpr std::array version{ 4, 0, 35, 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 4929b7a7d..9d2c08251 100644 --- a/src/tables/envelope.cpp +++ b/src/tables/envelope.cpp @@ -120,6 +120,13 @@ bool envelope::migrate_4_0_33_to_4_0_34(data_chunk& data) NOEXCEPT return true; } +// The envelope layout is unchanged. +bool envelope::migrate_4_0_34_to_4_0_35(data_chunk& data) NOEXCEPT +{ + set_schema(data, { 4, 0, 35, 0 }); + return true; +} + bool envelope::migrate_step(data_chunk& data, const envelope& configured) NOEXCEPT { @@ -139,6 +146,9 @@ bool envelope::migrate_step(data_chunk& data, if (schema == config::version{ 4, 0, 33, 0 }) return migrate_4_0_33_to_4_0_34(data); + if (schema == config::version{ 4, 0, 34, 0 }) + return migrate_4_0_34_to_4_0_35(data); + return false; } diff --git a/test/mocks/chunk_store.hpp b/test/mocks/chunk_store.hpp index 4e6dfdb76..df0bc3014 100644 --- a/test/mocks/chunk_store.hpp +++ b/test/mocks/chunk_store.hpp @@ -286,10 +286,10 @@ class chunk_store using query_accessor = query>; inline bool put_pooled(chunk_store& store, const tx_link& tx, uint64_t fee, - size_t height, uint32_t bytes=0) NOEXCEPT + size_t height, uint32_t weight=0) NOEXCEPT { const auto link = store.pool.allocate(1); - const table::pool_total total{ {}, fee, bytes, 0 }; + const table::pool_total total{ {}, fee, weight, 0 }; const auto high = system::possible_narrow_cast(height); auto guard = store.pool.get_memory(); if (!guard || !store.pool.totals.put(link, total) || diff --git a/test/mocks/pooling.hpp b/test/mocks/pooling.hpp index c12421cab..a631eef53 100644 --- a/test/mocks/pooling.hpp +++ b/test/mocks/pooling.hpp @@ -29,7 +29,7 @@ using pool_totals = system::pool::algorithm::totals_t; using pool_projection = system::pool::algorithm::projection_t; using pool_arrivals = std::vector; constexpr auto bip113 = system::chain::flags::bip113_rule; -constexpr pool_totals block_limit{ 0, 1'000'000, 80'000 }; +constexpr pool_totals block_limit{ 0, 4'000'000, 80'000 }; constexpr pool_totals tight_limit{ 0, 10, 80'000 }; /// A tx of one output spending the first output of the hash. diff --git a/test/query/amounts.cpp b/test/query/amounts.cpp index 8f65cf85e..dba270f53 100644 --- a/test/query/amounts.cpp +++ b/test/query/amounts.cpp @@ -107,7 +107,7 @@ BC_AUTO_TEST_CASE(query_amounts__get_tx_fee__pooled_missing_prevouts__pooled_fee constexpr size_t expected_height = 7; size_t bytes{}; BC_CHECK(query.get_tx_virtual_size(bytes, 3)); - BC_CHECK(test::put_pooled(store, tx_link{ 3 }, expected_fee, expected_height, system::possible_narrow_cast(bytes))); + BC_CHECK(test::put_pooled(store, tx_link{ 3 }, expected_fee, expected_height)); BC_CHECK(!query.get_tx_value(out, 3)); BC_CHECK(query.get_tx_fee(out, 3)); BC_CHECK_EQUAL(out, expected_fee); diff --git a/test/query/pool/pool_confirmable.cpp b/test/query/pool/pool_confirmable.cpp index a2c7b00f4..28fa1fd9b 100644 --- a/test/query/pool/pool_confirmable.cpp +++ b/test/query/pool/pool_confirmable.cpp @@ -454,7 +454,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pooled_ancestry__pooled_chain__rows_by_row) test::pool_totals totals{}; BC_REQUIRE(query.get_pooled_totals(totals, grandchild_link)); BC_REQUIRE_EQUAL(out[2].totals.fee, totals.fee); - BC_REQUIRE_EQUAL(out[2].totals.size, totals.size); + BC_REQUIRE_EQUAL(out[2].totals.weight, totals.weight); } BC_AUTO_TEST_CASE(query_pool__get_pooled_ancestry__confirmed_parent__child_only) diff --git a/test/query/pool/pool_estimate.cpp b/test/query/pool/pool_estimate.cpp index 426706308..ff4615749 100644 --- a/test/query/pool/pool_estimate.cpp +++ b/test/query/pool/pool_estimate.cpp @@ -66,20 +66,20 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_projection__chain_over_budget__block_each 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, child); pool_projection projection{}; - const auto ec = query.get_pool_projection(projection, 2, { 0, size, 80'000 }, 25, 0, 20, 1'024); + const auto ec = query.get_pool_projection(projection, 2, { 0, weight, 80'000 }, 25, 0, 20, 1'024); BC_REQUIRE(pooled); BC_REQUIRE_EQUAL(ec, error::success); BC_REQUIRE_EQUAL(projection.blocks.size(), 2u); BC_REQUIRE_EQUAL(projection.blocks[0].size(), 1u); - BC_REQUIRE_EQUAL(projection.blocks[0][0].rate, 1'000'000u / size); - BC_REQUIRE_EQUAL(projection.blocks[0][0].size, size); + BC_REQUIRE_EQUAL(projection.blocks[0][0].rate, 4'000'000u / weight); + BC_REQUIRE_EQUAL(projection.blocks[0][0].weight, weight); BC_REQUIRE_EQUAL(projection.blocks[1].size(), 1u); - BC_REQUIRE_EQUAL(projection.blocks[1][0].rate, 9'000'000u / size); - BC_REQUIRE_EQUAL(projection.blocks[1][0].size, size); + BC_REQUIRE_EQUAL(projection.blocks[1][0].rate, 36'000'000u / weight); + BC_REQUIRE_EQUAL(projection.blocks[1][0].weight, weight); BC_REQUIRE(projection.exhausted); } @@ -95,10 +95,10 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_projection__one_block_of_two__not_exhaust 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, child); pool_projection projection{}; - const auto ec = query.get_pool_projection(projection, 1, { 0, size, 80'000 }, 25, 0, 20, 1'024); + const auto ec = query.get_pool_projection(projection, 1, { 0, weight, 80'000 }, 25, 0, 20, 1'024); BC_REQUIRE(pooled); BC_REQUIRE_EQUAL(ec, error::success); @@ -118,11 +118,11 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_projection__after_template__continued) 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, child); tx_links out{}; pool_projection projection{}; - const pool_totals budget{ 0, size, 80'000 }; + const pool_totals budget{ 0, weight, 80'000 }; const auto template_ec = query.get_pool_template(out, projection, budget, 25, 0, 20, 1'024); const auto ec = query.get_pool_projection(projection, 2, budget, 25, 0, 20, 1'024); @@ -130,7 +130,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_projection__after_template__continued) BC_REQUIRE_EQUAL(template_ec, 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_EQUAL(projection.blocks[1][0].rate, 36'000'000u / weight); BC_REQUIRE(projection.exhausted); } @@ -146,18 +146,18 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_projection__marks_of_other_rows__restarte 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, child); pool_projection projection{}; projection.marks.resize(42); projection.blocks.resize(3); - const auto ec = query.get_pool_projection(projection, 1, { 0, size, 80'000 }, 25, 0, 20, 1'024); + const auto ec = query.get_pool_projection(projection, 1, { 0, weight, 80'000 }, 25, 0, 20, 1'024); BC_REQUIRE(pooled); BC_REQUIRE_EQUAL(ec, error::success); BC_REQUIRE_EQUAL(projection.marks.size(), 2u); BC_REQUIRE_EQUAL(projection.blocks.size(), 1u); - BC_REQUIRE_EQUAL(projection.blocks[0][0].rate, 1'000'000u / size); + BC_REQUIRE_EQUAL(projection.blocks[0][0].rate, 4'000'000u / weight); } // get_pool_continuation @@ -175,10 +175,10 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_continuation__more_blocks__continued) 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, child); pool_projection projection{}; - const pool_totals budget{ 0, size, 80'000 }; + const pool_totals budget{ 0, weight, 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); @@ -186,7 +186,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_continuation__more_blocks__continued) 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_EQUAL(projection.blocks[1][0].rate, 36'000'000u / weight); BC_REQUIRE(projection.exhausted); } @@ -202,10 +202,10 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_continuation__later_row__left_out) 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled_parent = pool_populated(query, parent); pool_projection projection{}; - const pool_totals budget{ 0, size, 80'000 }; + const pool_totals budget{ 0, weight, 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); @@ -270,7 +270,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__two_heights__top_first) 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent, 8) && pool_populated(query, child, 9); pool_arrivals out{}; const auto ec = query.get_pool_arrivals(out, 10, 3, 20); @@ -279,11 +279,11 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__two_heights__top_first) BC_REQUIRE_EQUAL(ec, error::success); BC_REQUIRE_EQUAL(out.size(), 3u); BC_REQUIRE_EQUAL(out[0].size(), 1u); - BC_REQUIRE_EQUAL(out[0][0].rate, 9'000'000u / size); - BC_REQUIRE_EQUAL(out[0][0].size, size); + BC_REQUIRE_EQUAL(out[0][0].rate, 36'000'000u / weight); + BC_REQUIRE_EQUAL(out[0][0].weight, weight); BC_REQUIRE_EQUAL(out[1].size(), 1u); - BC_REQUIRE_EQUAL(out[1][0].rate, 1'000'000u / size); - BC_REQUIRE_EQUAL(out[1][0].size, size); + BC_REQUIRE_EQUAL(out[1][0].rate, 4'000'000u / weight); + BC_REQUIRE_EQUAL(out[1][0].weight, weight); BC_REQUIRE(out[2].empty()); } @@ -299,7 +299,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__top_height__excluded) 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent, 8) && pool_populated(query, child, 9); pool_arrivals out{}; const auto ec = query.get_pool_arrivals(out, 9, 2, 20); @@ -308,7 +308,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__top_height__excluded) BC_REQUIRE_EQUAL(ec, error::success); BC_REQUIRE_EQUAL(out.size(), 2u); BC_REQUIRE_EQUAL(out[0].size(), 1u); - BC_REQUIRE_EQUAL(out[0][0].rate, 1'000'000u / size); + BC_REQUIRE_EQUAL(out[0][0].rate, 4'000'000u / weight); BC_REQUIRE(out[1].empty()); } @@ -324,7 +324,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__below_count__excluded) 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent, 8) && pool_populated(query, child, 9); pool_arrivals out{}; const auto ec = query.get_pool_arrivals(out, 10, 1, 20); @@ -333,7 +333,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__below_count__excluded) BC_REQUIRE_EQUAL(ec, error::success); BC_REQUIRE_EQUAL(out.size(), 1u); BC_REQUIRE_EQUAL(out[0].size(), 1u); - BC_REQUIRE_EQUAL(out[0][0].rate, 9'000'000u / size); + BC_REQUIRE_EQUAL(out[0][0].rate, 36'000'000u / weight); } BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__confirmed_rival__confirmed_only) @@ -348,7 +348,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__confirmed_rival__confirmed_only store.set_pooling(); const auto parent = to_spend(genesis_coinbase(), 4'999'999'000); const auto rival = to_spend(genesis_coinbase(), 4'999'998'500); - const auto size = system::possible_narrow_cast(rival.virtual_size()); + const auto weight = system::possible_narrow_cast(rival.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, rival); const auto confirmed = !archive_confirm(query, rival).is_terminal(); pool_arrivals out{}; @@ -358,7 +358,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__confirmed_rival__confirmed_only BC_REQUIRE(confirmed); BC_REQUIRE_EQUAL(ec, error::success); BC_REQUIRE_EQUAL(out[0].size(), 1u); - BC_REQUIRE_EQUAL(out[0][0].rate, 1'500'000u / size); + BC_REQUIRE_EQUAL(out[0][0].rate, 6'000'000u / weight); } BC_AUTO_TEST_CASE(query_pool__get_pool_arrivals__zero_count__empty) diff --git a/test/query/pool/pool_reader.cpp b/test/query/pool/pool_reader.cpp index d9e2869d1..ec357c282 100644 --- a/test/query/pool/pool_reader.cpp +++ b/test/query/pool/pool_reader.cpp @@ -216,7 +216,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pooled_totals__pooled__fee_size_and_sigops) BC_REQUIRE(pooled); BC_REQUIRE(found); BC_REQUIRE_EQUAL(out.fee, 1'000u); - BC_REQUIRE_EQUAL(out.size, tx.virtual_size()); + BC_REQUIRE_EQUAL(out.weight, tx.weight()); } BC_AUTO_TEST_SUITE_END() diff --git a/test/query/pool/pool_suffice.cpp b/test/query/pool/pool_suffice.cpp index aa41baf05..703fc1787 100644 --- a/test/query/pool/pool_suffice.cpp +++ b/test/query/pool/pool_suffice.cpp @@ -490,7 +490,7 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled_context__sigops__stored_with_fee_and_si BC_REQUIRE(set); BC_REQUIRE(second); BC_REQUIRE_EQUAL(after.fee, before.fee); - BC_REQUIRE_EQUAL(after.size, before.size); + BC_REQUIRE_EQUAL(after.weight, before.weight); BC_REQUIRE_EQUAL(after.sigops, 42u); } diff --git a/test/query/pool/pool_template.cpp b/test/query/pool/pool_template.cpp index 3d7a8dbe7..5420245f6 100644 --- a/test/query/pool/pool_template.cpp +++ b/test/query/pool/pool_template.cpp @@ -154,11 +154,11 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_template__chain_over_budget__leading_tx) 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, child); tx_links out{}; pool_projection projection{}; - const auto ec = query.get_pool_template(out, projection, { 0, size, 80'000 }, 25, 0, 20, 1'024); + const auto ec = query.get_pool_template(out, projection, { 0, weight, 80'000 }, 25, 0, 20, 1'024); BC_REQUIRE(pooled); BC_REQUIRE_EQUAL(ec, error::success); @@ -178,11 +178,11 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_template__chain_over_budget__first_block_ 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 weight = system::possible_narrow_cast(parent.weight()); const auto pooled = pool_populated(query, parent) && pool_populated(query, child); tx_links out{}; pool_projection projection{}; - const auto ec = query.get_pool_template(out, projection, { 0, size, 80'000 }, 25, 0, 20, 1'024); + const auto ec = query.get_pool_template(out, projection, { 0, weight, 80'000 }, 25, 0, 20, 1'024); BC_REQUIRE(pooled); BC_REQUIRE_EQUAL(ec, error::success); diff --git a/test/store/store_open.cpp b/test/store/store_open.cpp index dc880fafd..f271bfd77 100644 --- a/test/store/store_open.cpp +++ b/test/store/store_open.cpp @@ -297,6 +297,40 @@ BC_AUTO_TEST_CASE(store__migrate__4_0_33__pool_top_terminal_activation_kept) BC_REQUIRE_EQUAL(stored.activations.back().link, 42u); } +BC_AUTO_TEST_CASE(store__migrate__4_0_34__pool_top_terminal) +{ + 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)); + + // Pool top link precedes the 224 bytes of activations. + database::envelope configured{}; + auto image = test::envelope_4_0_31; + BC_REQUIRE(envelope::migrate_step(image, configured)); + BC_REQUIRE(envelope::migrate_step(image, configured)); + BC_REQUIRE(envelope::migrate_step(image, configured)); + image.at(image.size() - 227u) = 42; + image.at(image.size() - 226u) = 0; + image.at(image.size() - 225u) = 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, 34, 0)); + BC_REQUIRE_EQUAL(test::size(instance.envelope_head_file()), image.size()); + 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); +} + // 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 { diff --git a/test/tables/envelope.cpp b/test/tables/envelope.cpp index 301a1d1f0..ddd8672f4 100644 --- a/test/tables/envelope.cpp +++ b/test/tables/envelope.cpp @@ -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, 34, 0)); + BC_REQUIRE_EQUAL(instance.schema, system::config::version(4, 0, 35, 0)); BC_REQUIRE_EQUAL(to_chunk(instance).front(), schema::version.front()); } @@ -447,6 +447,11 @@ BC_AUTO_TEST_CASE(envelope__migrate_step__4_0_31__stepwise_expected) 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); + // 4.0.34 -> 4.0.35 + BC_REQUIRE(envelope::migrate_step(data, configured)); + BC_REQUIRE(envelope::get_schema(data) == system::config::version(4, 0, 35, 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)); @@ -502,6 +507,16 @@ BC_AUTO_TEST_CASE(envelope__migrate_4_0_33_to_4_0_34__short__false) BC_REQUIRE(!envelope::migrate_4_0_33_to_4_0_34(data)); } +BC_AUTO_TEST_CASE(envelope__migrate_4_0_34_to_4_0_35__schema_only) +{ + auto data = test::envelope_4_0_31; + data.at(8) = 34; + auto expected = data; + expected.at(8) = 35; + BC_REQUIRE(envelope::migrate_4_0_34_to_4_0_35(data)); + BC_REQUIRE(data == expected); +} + BC_AUTO_TEST_CASE(envelope__migrate__4_0_30__compiled) { envelope configured{};