diff --git a/include/bitcoin/node/chasers/chaser_header.hpp b/include/bitcoin/node/chasers/chaser_header.hpp index 0a5d3638..c55fb611 100644 --- a/include/bitcoin/node/chasers/chaser_header.hpp +++ b/include/bitcoin/node/chasers/chaser_header.hpp @@ -60,9 +60,10 @@ class BCN_API chaser_header virtual void organize_compact(const system::chain::header::cptr& header, organize_handler&& handler) NOEXCEPT; - /// Organize a submitted (proven) header, prioritized (a tie wins). + /// Organize a submitted (proven) header, prioritized (a tie wins), + /// milestone set if trusted. virtual void organize_submitted(const system::chain::header::cptr& header, - organize_handler&& handler) NOEXCEPT; + bool milestone, organize_handler&& handler) NOEXCEPT; /// Reorganize to the branch of an archived block of at least equal work. virtual void prioritize(const system::hash_digest& hash, diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index bc890cd1..e2a48fe2 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -84,6 +84,10 @@ class BCN_API chaser_transaction virtual void get_confirmable_fees( confirmable_fees_handler&& handler) NOEXCEPT; + /// Least and greatest value of receipts not spent, over conflict outcomes. + virtual void get_unconfirmed_range(const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT; + /// Count of changes to the pool (thread safe). uint64_t pool_generation() const NOEXCEPT override; @@ -141,6 +145,9 @@ class BCN_API chaser_transaction const confirmable_txs_handler& handler) NOEXCEPT; virtual void do_get_confirmable_fees( const confirmable_fees_handler& handler) NOEXCEPT; + virtual void do_get_unconfirmed_range( + const database::pool_receipts& receipts, + const unconfirmed_range_handler& handler) NOEXCEPT; /// Recompute pool context and sweep index by it if due, closing pool if not /// current or context not reusable, and map scan starts: built once diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index 5216297a..ca0cd3d7 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -71,9 +71,10 @@ class BCN_API full_node virtual void organize_compact(const system::chain::header::cptr& header, organize_handler&& handler) NOEXCEPT; - /// Organize a submitted (proven) header, prioritized (a tie wins). + /// Organize a submitted (proven) header, prioritized (a tie wins), + /// milestone set if trusted. virtual void organize_submitted(const system::chain::header::cptr& header, - organize_handler&& handler) NOEXCEPT; + bool milestone, organize_handler&& handler) NOEXCEPT; /// Reorganize to the branch of an archived block of at least equal work. virtual void prioritize(const system::hash_digest& hash, @@ -235,6 +236,10 @@ class BCN_API full_node /// Fee rate tuples of the confirmable txs, in pool order. void get_confirmable_fees(confirmable_fees_handler&& handler) NOEXCEPT; + /// Least and greatest value of receipts not spent, over conflict outcomes. + void get_unconfirmed_range(const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT; + /// Handle performance, base returns false (implied terminate). virtual void performance(object_key channel, uint64_t speed, result_handler&& handler) NOEXCEPT; diff --git a/include/bitcoin/node/protocols/protocol.hpp b/include/bitcoin/node/protocols/protocol.hpp index 8e7407d8..1cf7e8f5 100644 --- a/include/bitcoin/node/protocols/protocol.hpp +++ b/include/bitcoin/node/protocols/protocol.hpp @@ -113,6 +113,10 @@ class BCN_API protocol /// Fee rate tuples of the confirmable txs, in pool order. void get_confirmable_fees(confirmable_fees_handler&& handler) NOEXCEPT; + /// Least and greatest value of receipts not spent, over conflict outcomes. + void get_unconfirmed_range(const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT; + /// Suspensions. /// ----------------------------------------------------------------------- @@ -135,9 +139,10 @@ class BCN_API protocol virtual void organize_compact(const system::chain::header::cptr& header, organize_handler&& handler) NOEXCEPT; - /// Organize a submitted (proven) header, prioritized (a tie wins). + /// Organize a submitted (proven) header, prioritized (a tie wins), + /// milestone set if trusted. virtual void organize_submitted(const system::chain::header::cptr& header, - organize_handler&& handler) NOEXCEPT; + bool milestone, organize_handler&& handler) NOEXCEPT; /// Reorganize to the branch of an archived block of at least equal work. virtual void prioritize(const system::hash_digest& hash, diff --git a/include/bitcoin/node/sessions/session.hpp b/include/bitcoin/node/sessions/session.hpp index cc5da4df..6968ee87 100644 --- a/include/bitcoin/node/sessions/session.hpp +++ b/include/bitcoin/node/sessions/session.hpp @@ -49,9 +49,10 @@ class BCN_API session virtual void organize_compact(const system::chain::header::cptr& header, organize_handler&& handler) NOEXCEPT; - /// Organize a submitted (proven) header, prioritized (a tie wins). + /// Organize a submitted (proven) header, prioritized (a tie wins), + /// milestone set if trusted. virtual void organize_submitted(const system::chain::header::cptr& header, - organize_handler&& handler) NOEXCEPT; + bool milestone, organize_handler&& handler) NOEXCEPT; /// Reorganize to the branch of an archived block of at least equal work. virtual void prioritize(const system::hash_digest& hash, @@ -164,6 +165,10 @@ class BCN_API session /// Fee rate tuples of the confirmable txs, in pool order. void get_confirmable_fees(confirmable_fees_handler&& handler) NOEXCEPT; + /// Least and greatest value of receipts not spent, over conflict outcomes. + void get_unconfirmed_range(const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT; + /// Handle performance, base returns false (implied terminate). virtual void performance(object_key channel, uint64_t speed, network::result_handler&& handler) NOEXCEPT; diff --git a/include/bitcoin/node/types/type.hpp b/include/bitcoin/node/types/type.hpp index 5f6e9a11..b57641fd 100644 --- a/include/bitcoin/node/types/type.hpp +++ b/include/bitcoin/node/types/type.hpp @@ -54,6 +54,8 @@ typedef std::function confirmable_txs_handler; typedef std::function confirmable_fees_handler; +typedef std::function unconfirmed_range_handler; /// Event desubscriber. typedef network::desubscriber event_subscriber; diff --git a/src/chasers/chaser_header.cpp b/src/chasers/chaser_header.cpp index 0a93e056..88b7d98d 100644 --- a/src/chasers/chaser_header.cpp +++ b/src/chasers/chaser_header.cpp @@ -120,12 +120,13 @@ void chaser_header::organize_compact(const header::cptr& header, } void chaser_header::organize_submitted(const header::cptr& header, - organize_handler&& handler) NOEXCEPT + bool milestone, organize_handler&& handler) NOEXCEPT { if (closed()) return; - POST(do_organize, header, true, false, true, false, std::move(handler)); + POST(do_organize, header, true, milestone, true, false, + std::move(handler)); } void chaser_header::prioritize(const hash_digest& hash, diff --git a/src/chasers/chaser_transaction_query.cpp b/src/chasers/chaser_transaction_query.cpp index fa02c2d2..25387173 100644 --- a/src/chasers/chaser_transaction_query.cpp +++ b/src/chasers/chaser_transaction_query.cpp @@ -122,6 +122,16 @@ void chaser_transaction::get_confirmable_fees( POST(do_get_confirmable_fees, std::move(handler)); } +void chaser_transaction::get_unconfirmed_range( + const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT +{ + if (closed()) + return; + + POST(do_get_unconfirmed_range, receipts, std::move(handler)); +} + // private void chaser_transaction::do_build_template( const template_handler& handler) NOEXCEPT @@ -302,6 +312,27 @@ void chaser_transaction::do_get_confirmable_fees( handler(ec, rates); } +// private +void chaser_transaction::do_get_unconfirmed_range( + const database::pool_receipts& receipts, + const unconfirmed_range_handler& handler) NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (closed()) + { + handler(network::error::service_stopped, {}, {}); + return; + } + + uint64_t worst{}; + uint64_t best{}; + const auto& settings = node_settings().pool; + const auto ec = archive().get_unconfirmed_range(worst, best, receipts, + settings.maximum_outcomes); + handler(ec, worst, best); +} + BC_POP_WARNING() BC_POP_WARNING() diff --git a/src/full_node.cpp b/src/full_node.cpp index 33e6ec63..701baf04 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -178,9 +178,9 @@ void full_node::organize_compact(const system::chain::header::cptr& header, } void full_node::organize_submitted(const system::chain::header::cptr& header, - organize_handler&& handler) NOEXCEPT + bool milestone, organize_handler&& handler) NOEXCEPT { - chaser_header_.organize_submitted(header, std::move(handler)); + chaser_header_.organize_submitted(header, milestone, std::move(handler)); } void full_node::prioritize(const system::hash_digest& hash, @@ -605,6 +605,12 @@ void full_node::get_confirmable_fees( chaser_transaction_.get_confirmable_fees(std::move(handler)); } +void full_node::get_unconfirmed_range(const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT +{ + chaser_transaction_.get_unconfirmed_range(receipts, std::move(handler)); +} + void full_node::performance(object_key key, uint64_t speed, result_handler&& handler) NOEXCEPT { diff --git a/src/protocols/protocol.cpp b/src/protocols/protocol.cpp index 75e34aa9..5b277197 100644 --- a/src/protocols/protocol.cpp +++ b/src/protocols/protocol.cpp @@ -126,6 +126,12 @@ void protocol::get_confirmable_fees(confirmable_fees_handler&& handler) NOEXCEPT session_->get_confirmable_fees(std::move(handler)); } +void protocol::get_unconfirmed_range(const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT +{ + session_->get_unconfirmed_range(receipts, std::move(handler)); +} + // Events subscription. // ---------------------------------------------------------------------------- @@ -182,9 +188,9 @@ void protocol::organize_compact(const system::chain::header::cptr& header, } void protocol::organize_submitted(const system::chain::header::cptr& header, - organize_handler&& handler) NOEXCEPT + bool milestone, organize_handler&& handler) NOEXCEPT { - session_->organize_submitted(header, std::move(handler)); + session_->organize_submitted(header, milestone, std::move(handler)); } void protocol::prioritize(const system::hash_digest& hash, diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 14d6367f..e8472f0a 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -58,9 +58,9 @@ void session::organize_compact(const header::cptr& header, } void session::organize_submitted(const header::cptr& header, - organize_handler&& handler) NOEXCEPT + bool milestone, organize_handler&& handler) NOEXCEPT { - node_.organize_submitted(header, std::move(handler)); + node_.organize_submitted(header, milestone, std::move(handler)); } void session::prioritize(const hash_digest& hash, @@ -233,6 +233,12 @@ void session::get_confirmable_fees(confirmable_fees_handler&& handler) NOEXCEPT node_.get_confirmable_fees(std::move(handler)); } +void session::get_unconfirmed_range(const database::pool_receipts& receipts, + unconfirmed_range_handler&& handler) NOEXCEPT +{ + node_.get_unconfirmed_range(receipts, std::move(handler)); +} + void session::performance(object_key key, uint64_t speed, result_handler&& handler) NOEXCEPT { diff --git a/test/chasers/chaser_header.cpp b/test/chasers/chaser_header.cpp index de71f9cf..94925f1d 100644 --- a/test/chasers/chaser_header.cpp +++ b/test/chasers/chaser_header.cpp @@ -278,12 +278,13 @@ struct chaser_header_setup_fixture return future.get(); } - /// Organize a submitted header. - result organize_submitted(const system::chain::header::cptr& header) + /// Organize a submitted header, milestone set if trusted. + result organize_submitted(const system::chain::header::cptr& header, + bool milestone=false) { const auto promise = std::make_shared>(); auto future = promise->get_future(); - header_.organize_submitted(header, [promise](const code& ec, + header_.organize_submitted(header, milestone, [promise](const code& ec, size_t height) NOEXCEPT { promise->set_value({ ec, height }); @@ -933,6 +934,17 @@ BC_FIXTURE_TEST_CASE(chaser_header__organize_submitted__block1__compact_candidat BC_REQUIRE_EQUAL(result.second, 1u); BC_REQUIRE(candidate(1, header->hash())); BC_REQUIRE(query_.is_compact(query_.to_header(header->hash()))); + BC_REQUIRE(!query_.is_milestone(query_.to_header(header->hash()))); +} + +BC_FIXTURE_TEST_CASE(chaser_header__organize_submitted__trusted_block1__archived_milestone, chaser_header_setup_fixture) +{ + const auto header = block1(); + const auto result = organize_submitted(header, true); + BC_REQUIRE_EQUAL(result.first, error::success); + BC_REQUIRE_EQUAL(result.second, 1u); + BC_REQUIRE(candidate(1, header->hash())); + BC_REQUIRE(query_.is_milestone(query_.to_header(header->hash()))); } BC_FIXTURE_TEST_CASE(chaser_header__organize__tied_block1__cached, chaser_header_setup_fixture) diff --git a/test/chasers/chaser_transaction_query.cpp b/test/chasers/chaser_transaction_query.cpp index 6e556c98..07a5b4bb 100644 --- a/test/chasers/chaser_transaction_query.cpp +++ b/test/chasers/chaser_transaction_query.cpp @@ -165,6 +165,36 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__get_confirmable_fees__relay_off_pooled_ BC_REQUIRE(rates.empty()); } +// get_unconfirmed_range + +BC_FIXTURE_TEST_CASE(chaser_transaction__get_unconfirmed_range__no_receipts__zeros, chaser_transaction_pooling_setup_fixture) +{ + const auto [ec, worst, best] = get_unconfirmed_range({}); + BC_REQUIRE_EQUAL(ec, database::error::success); + BC_REQUIRE_EQUAL(worst, 0u); + BC_REQUIRE_EQUAL(best, 0u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__get_unconfirmed_range__pooled_tx__its_value, chaser_transaction_pooling_setup_fixture) +{ + const auto tx = spend(parent_value - 1'000); + const auto result = submit(package({ tx })); + const auto row = query_.to_pool_row(query_.to_tx(tx->hash(false))); + const auto [ec, worst, best] = get_unconfirmed_range({ { row.value, 42u, {} } }); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE_EQUAL(ec, database::error::success); + BC_REQUIRE_EQUAL(worst, 42u); + BC_REQUIRE_EQUAL(best, 42u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__get_unconfirmed_range__row_not_pooled__zeros, chaser_transaction_pooling_setup_fixture) +{ + const auto [ec, worst, best] = get_unconfirmed_range({ { 0u, 42u, {} } }); + BC_REQUIRE_EQUAL(ec, database::error::success); + BC_REQUIRE_EQUAL(worst, 0u); + BC_REQUIRE_EQUAL(best, 0u); +} + // estimate BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__relay_disabled__estimate_disabled, chaser_transaction_setup_fixture) diff --git a/test/chasers/chaser_transaction_setup_fixture.hpp b/test/chasers/chaser_transaction_setup_fixture.hpp index 6b65c85a..98d5746f 100644 --- a/test/chasers/chaser_transaction_setup_fixture.hpp +++ b/test/chasers/chaser_transaction_setup_fixture.hpp @@ -526,6 +526,19 @@ struct chaser_transaction_setup_fixture return promise.get_future().get(); } + using ranged = std::tuple; + ranged get_unconfirmed_range(const database::pool_receipts& receipts) + { + std::promise promise{}; + node_.get_unconfirmed_range(receipts, [&](const code& ec, + uint64_t worst, uint64_t best) NOEXCEPT + { + promise.set_value({ ec, worst, best }); + }); + + return promise.get_future().get(); + } + // Confirm a block of a coinbase, and the tx if any, and notify of it. database::header_link confirm(const chain::transaction::cptr& tx={}) {