diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index 1c8f2eb2..9fc2f7d8 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -202,7 +202,7 @@ class BCN_API chaser_transaction std::vector sigops_of{}; std::unordered_set held{}; query::pool_graph graph{}; - uint64_t bytes{}; + uint64_t weight{}; uint64_t sigops{}; size_t given{}; size_t first{}; @@ -215,6 +215,10 @@ class BCN_API chaser_transaction static size_t to_rate(const system::chain::transaction& tx) NOEXCEPT; system::pool::algorithm::totals_t block_limit() const NOEXCEPT; + /// Chain state of top, the held state advanced by top, else from store. + system::chain::chain_state::cptr top_state(const database::header_link& top, + size_t height) NOEXCEPT; + /// Submission steps. void hold(submission& set) NOEXCEPT; bool spends_held(const submission& set, @@ -313,6 +317,7 @@ class BCN_API chaser_transaction // These are protected by strand. pool_subscriber subscriber_{}; system::chain::context pool_{}; + system::chain::chain_state::cptr state_{}; system::hash_digest previous_{}; database::header_link top_{}; size_t floor_{}; diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp index 7075ab51..c059ec6a 100644 --- a/src/chasers/chaser_transaction.cpp +++ b/src/chasers/chaser_transaction.cpp @@ -204,9 +204,7 @@ void chaser_transaction::do_bump() NOEXCEPT return; } - const auto state = query.get_confirmed_chain_state(system_settings(), - top, height); - + const auto state = top_state(top, height); if (!state) { fault(error::transaction1); @@ -216,7 +214,7 @@ void chaser_transaction::do_bump() NOEXCEPT // The context of the next block, in which a pool tx would confirm. const auto previous = pool_; pool_ = chain_state{ *state, system_settings() }.context(); - previous_ = query.get_header_key(top); + previous_ = state->hash(); // Row holds unless a flag changes or height or median time past falls. due_ = due_ || pool_.flags != previous.flags || @@ -265,6 +263,30 @@ void chaser_transaction::do_bump() NOEXCEPT start_scan(true); } +// private +chain_state::cptr chaser_transaction::top_state( + const database::header_link& top, size_t height) NOEXCEPT +{ + BC_ASSERT(stranded()); + const auto& query = archive(); + if (state_ && height == state_->height() && + query.get_header_key(top) == state_->hash()) + return state_; + + if (state_ && height == add1(state_->height())) + { + const auto header = query.get_header(top); + if (header && header->previous_block_hash() == state_->hash()) + { + state_ = to_shared(*state_, *header, system_settings()); + return state_; + } + } + + state_ = query.get_confirmed_chain_state(system_settings(), top, height); + return state_; +} + bool chaser_transaction::is_indexing() const NOEXCEPT { return network_settings().enable_relay && archive().is_pooling(); @@ -715,7 +737,7 @@ pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT return { {}, - possible_narrow_cast(weight / light_weight_factor), + possible_narrow_cast(weight), possible_narrow_cast(sigops) }; } diff --git a/src/chasers/chaser_transaction_query.cpp b/src/chasers/chaser_transaction_query.cpp index 0dc29fd6..9ce31e39 100644 --- a/src/chasers/chaser_transaction_query.cpp +++ b/src/chasers/chaser_transaction_query.cpp @@ -237,9 +237,11 @@ void chaser_transaction::do_estimate(size_t target, estimate_mode mode, }; using algorithm = pool::algorithm; - const auto space = limit.size; - const auto rate = algorithm::estimate(projection_, count, space, - settings.estimate_room, arrive); + const auto space = limit.weight; + const auto room = ceilinged_multiply(settings.estimate_room, + light_weight_factor); + const auto rate = algorithm::estimate(projection_, count, space, room, + arrive); if (rate == max_uint64) { handler(error::estimate_false, {}); diff --git a/src/chasers/chaser_transaction_relay.cpp b/src/chasers/chaser_transaction_relay.cpp index 87bf7eaf..ef52373b 100644 --- a/src/chasers/chaser_transaction_relay.cpp +++ b/src/chasers/chaser_transaction_relay.cpp @@ -229,10 +229,10 @@ bool chaser_transaction::relay(const database::tx_links& links, if (archive().get_pooled_ancestry(rows, links)) return false; - const auto limit = block_limit().size; + const auto limit = block_limit().weight; const auto bytes_per_sigop = node_settings().pool.bytes_per_sigop; std::array fees{}; - std::array sizes{}; + std::array weights{}; for (const auto& row: rows) { auto& mark = marks(row.row); @@ -240,25 +240,23 @@ bool chaser_transaction::relay(const database::tx_links& links, set_right_into(mark, origin); const auto lacking = bit_and(slots, bit_not(mark)); - const auto size = algorithm::chargeable(row.totals, bytes_per_sigop); + const auto weight = algorithm::chargeable(row.totals, bytes_per_sigop); for (size_t slot{}; slot < relay_slots; ++slot) { if (get_right(lacking, slot)) { fees.at(slot) = ceilinged_add(fees.at(slot), row.totals.fee); - sizes.at(slot) = ceilinged_add(sizes.at(slot), size); + weights.at(slot) = ceilinged_add(weights.at(slot), weight); } } } - // Compared in satoshis per virtual kilobyte. uint64_t clears{}; for (size_t slot{}; slot < relay_slots; ++slot) { - const auto size = sizes.at(slot); - const auto paid = ceilinged_multiply(fees.at(slot), 1'000_u64); - const auto owed = ceilinged_multiply(rates_.at(slot), size); - if (!is_zero(size) && size <= limit && paid >= owed) + const auto weight = weights.at(slot); + if (!is_zero(weight) && weight <= limit && + algorithm::covered(fees.at(slot), weight, rates_.at(slot))) set_right_into(clears, slot); } diff --git a/src/chasers/chaser_transaction_submit.cpp b/src/chasers/chaser_transaction_submit.cpp index ba8a9405..e5b09363 100644 --- a/src/chasers/chaser_transaction_submit.cpp +++ b/src/chasers/chaser_transaction_submit.cpp @@ -96,7 +96,7 @@ static std::string describe_limit(const node::query& query, out << "candidate " << at << (ancestry ? " outcomes " : " ancestry ") << "members " << component.count << " contests " << component.contests << " outcomes " - << outcomes << " size " << sum.size << " sigops " + << outcomes << " weight " << sum.weight << " sigops " << sum.sigops << ":" << members(component); return out.str(); } @@ -300,7 +300,7 @@ void chaser_transaction::count(submission& set, size_t index) const NOEXCEPT using algorithm = pool::algorithm; const auto totals = algorithm::from_column(set.graph.totals.back()); set.sigops_of.at(index) = totals.sigops; - set.bytes = ceilinged_add(set.bytes, totals.size); + set.weight = ceilinged_add(set.weight, totals.weight); set.sigops = ceilinged_add(set.sigops, totals.sigops); } @@ -323,7 +323,7 @@ code chaser_transaction::extend(bool& covered, submission& set, count(set, index); const auto limit = block_limit(); - if (set.bytes > limit.size || set.sigops > limit.sigops) + if (set.weight > limit.weight || set.sigops > limit.sigops) { LOGN("Pool limit [" << encode_hash(tx.get_hash(false)) << "] running set over a block"); @@ -361,7 +361,7 @@ code chaser_transaction::restore(submission& set, size_t index) NOEXCEPT auto& query = archive(); const auto ctx = database::context::from(pool_); set.graph = {}; - set.bytes = set.sigops = zero; + set.weight = set.sigops = zero; set.running = false; for (auto at = set.first; at < index; ++at) { @@ -426,7 +426,7 @@ code chaser_transaction::clear(size_t& at, submission& set, } set.running = false; - set.bytes = set.sigops = zero; + set.weight = set.sigops = zero; return error::success; }