Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class BCN_API chaser_transaction
/// Reserve a free slot of the tier, relay_slots if none (thread safe).
virtual size_t reserve_relay(relay_tier tier) NOEXCEPT;

/// Start relay to the slot, once its channel is notified of relay.
virtual void activate_relay(size_t slot) NOEXCEPT;

/// Release the relay slot, free once cleared from the marks.
virtual void release_relay(size_t slot) NOEXCEPT;

Expand Down Expand Up @@ -139,12 +142,16 @@ class BCN_API chaser_transaction
virtual void do_subscribe_pool(const pool_notifier& handler) NOEXCEPT;

/// Relay slot management.
virtual void do_activate_relay(size_t slot) NOEXCEPT;
virtual void do_release_relay(size_t slot) NOEXCEPT;
virtual void do_set_relay_rate(size_t slot, uint64_t rate) NOEXCEPT;
virtual void do_set_relayed(size_t slot,
const database::tx_links& links) NOEXCEPT;
virtual void do_relay_pool(size_t slot) NOEXCEPT;

/// Sweep the pool to one slot owed a sweep, then post for the next.
virtual void do_sweep_relay() NOEXCEPT;

/// Apply the organized block to the pool index, then bump.
virtual void do_organized(header_t link) NOEXCEPT;

Expand All @@ -170,7 +177,7 @@ class BCN_API chaser_transaction

/// Restore the pooled txs that passed, if the context still holds.
virtual void do_revalidated(const database::tx_links& links,
const system::data_chunk& passed,
const system::data_chunk& passed, const std::vector<size_t>& sigops,
const system::chain::context& ctx) NOEXCEPT;

// Override base class strand because it sits on the network thread pool.
Expand Down Expand Up @@ -203,9 +210,11 @@ class BCN_API chaser_transaction
system::pool::algorithm::totals_t block_limit() const NOEXCEPT;

/// Submission steps.
void hold(submission& set) const NOEXCEPT;
void hold(submission& set) NOEXCEPT;
bool spends_held(const submission& set,
const system::chain::transaction& tx) const NOEXCEPT;
bool spends_held(const system::chain::transaction& tx) const NOEXCEPT;
bool is_held(const system::chain::input& in) const NOEXCEPT;
void pass_over(submission& set, size_t index, const code& ec) NOEXCEPT;
void count(submission& set,
const system::chain::transaction& tx) const NOEXCEPT;
Expand All @@ -219,12 +228,15 @@ class BCN_API chaser_transaction
const system::chain::context& ctx) NOEXCEPT;
code admit(database::tx_link& out, const system::chain::transaction& tx,
uint32_t time) NOEXCEPT;
code validate(const system::chain::transaction& tx,
code validate(bool& guarded, const system::chain::transaction& tx,
const system::chain::context& ctx) NOEXCEPT;
database::tx_link to_stored(
const system::chain::transaction& tx) const NOEXCEPT;
code fill(database::tx_link& out, const system::chain::transaction& tx,
const system::chain::context& ctx) NOEXCEPT;
bool set_pooled(const database::tx_link& link,
const system::chain::transaction& tx,
const system::chain::context& ctx) NOEXCEPT;
code unconfirmable(const database::header_link& link, size_t height,
const code& ec) NOEXCEPT;
void broadcast(const system::chain::transaction& tx) const NOEXCEPT;
Expand All @@ -240,6 +252,13 @@ class BCN_API chaser_transaction
/// slot lacks, where that clears the slot's rate.
bool sweep_relay(size_t slot) NOEXCEPT;

/// Owe the slot a sweep, made at once unless it was swept since the last
/// block, and otherwise after the next.
void owe_relay(size_t slot) NOEXCEPT;

/// The relaying slots owed a sweep and not swept since the last block.
uint64_t sweepable() const NOEXCEPT;

/// The marks of the pool row, grown with the table.
uint64_t& marks(size_t row) NOEXCEPT;

Expand Down Expand Up @@ -283,15 +302,24 @@ class BCN_API chaser_transaction
// These are protected by strand.
pool_subscriber subscriber_{};
system::chain::context pool_{};
database::header_link top_{};
size_t floor_{};
bool due_{ true };
bool scanning_{};
bool revalidating_{};
bool unvalidated_{};
uint32_t revalidated_flags_{};
bool estimating_{};
bool arrived_{};
time_point estimated_{};
query::pool_projection projection_{};
query::pool_arrivals arrivals_{};
system::chain::context retried_{};
std::vector<uint64_t> marks_{};
std::array<uint64_t, relay_slots> rates_{};
uint64_t relaying_{};
uint64_t owed_{};
uint64_t swept_{};
bool sweeping_{};
};

} // namespace node
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ class BCN_API full_node
virtual bool reserve_compact() NOEXCEPT;
virtual void release_compact() NOEXCEPT;

/// Reserve (relay_slots if none) or release a relay slot.
/// Reserve (relay_slots if none), activate or release a relay slot.
virtual size_t reserve_relay(relay_tier tier) NOEXCEPT;
virtual void activate_relay(size_t slot) NOEXCEPT;
virtual void release_relay(size_t slot) NOEXCEPT;

/// Set the fee rate of the relay slot's peer.
Expand Down
4 changes: 3 additions & 1 deletion include/bitcoin/node/protocols/protocol_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ class BCN_API protocol_peer
/// The memory pool of the peer is requested once relaying.
virtual bool request_mempool() const NOEXCEPT;

/// Reserve or release the channel's relay slot, false if none is free.
/// Reserve, activate or release the channel's relay slot, reserve false
/// if none is free.
virtual bool reserve_relay() NOEXCEPT;
virtual void activate_relay() NOEXCEPT;
virtual void release_relay() NOEXCEPT;

/// The channel's relay slot, relay_slots if none.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class BCN_API protocol_transaction_out_106

/// Handle chaser events.
virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT;
void subscribed(const code& ec, object_key key) NOEXCEPT override;

/// Announce the relayed tx if the channel's slot is in the word.
virtual bool do_announce(transaction_t link, uint64_t slots) NOEXCEPT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class BCN_API protocol_transaction_out_70013
protected:
/// Handle chaser events.
bool handle_chase(const code& ec, event_value value) NOEXCEPT override;
void subscribed(const code& ec, object_key key) NOEXCEPT override;

/// Capture the peer's minimum fee rate for announcements.
virtual bool handle_receive_fee_filter(const code& ec,
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ class BCN_API session
/// The relay slot tier of the session's peers.
virtual relay_tier relay_tier_() const NOEXCEPT;

/// Reserve (relay_slots if none) or release a relay slot.
/// Reserve (relay_slots if none), activate or release a relay slot.
virtual size_t reserve_relay() NOEXCEPT;
virtual void activate_relay(size_t slot) NOEXCEPT;
virtual void release_relay(size_t slot) NOEXCEPT;

/// Set the fee rate of the relay slot's peer.
Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/node/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ class BCN_API settings
uint32_t maximum_outcomes;
uint32_t bytes_per_sigop;
uint32_t coinbase_weight;
uint32_t coinbase_sigops;
uint32_t maximum_chunk_depth;
uint16_t estimate_cache_seconds;
bool thread_priority;

/// Helpers.
virtual uint64_t minimum_fee_rate_() const NOEXCEPT;
virtual uint64_t cutoff_rate_() const NOEXCEPT;
virtual network::steady_clock::duration estimate_cache() const NOEXCEPT;
virtual network::processing_priority thread_priority_() const NOEXCEPT;
};

Expand Down
71 changes: 49 additions & 22 deletions src/chasers/chaser_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ bool chaser_transaction::handle_chase(const code&, event_value value) NOEXCEPT
void chaser_transaction::do_bump() NOEXCEPT
{
BC_ASSERT(stranded());
estimating_ = false;
const auto opened = pooling_.load();
pooling_ = false;
if (closed() || !is_current_chain(true))
Expand All @@ -154,8 +155,10 @@ void chaser_transaction::do_bump() NOEXCEPT
auto& query = archive();
if (network_settings().enable_relay)
{
// The context is of the block the index was last applied through, as
// the store's top may be above it.
size_t height{};
const auto top = query.to_top_confirmed();
const auto top = top_.is_terminal() ? query.to_top_confirmed() : top_;
if (!query.get_height(height, top))
{
fault(error::transaction9);
Expand Down Expand Up @@ -195,6 +198,11 @@ void chaser_transaction::do_bump() NOEXCEPT
// Rows that arrived while the pool was closed are not in it.
if (!opened)
floor_ = std::max(floor_, pool_.height);

// Sweeps deferred to the block are now made.
swept_ = {};
if (!sweeping_)
do_sweep_relay();
}
}

Expand Down Expand Up @@ -255,15 +263,15 @@ bool chaser_transaction::sweep() NOEXCEPT
}

// Rows left out by flags are revalidated once per change of flags, and rows
// without a context once per change of the pool context, so a row that fails
// is retried only under another context.
// not validated once per change of the pool context, so a row that fails is
// retried only under another context.
void chaser_transaction::start_revalidation() NOEXCEPT
{
BC_ASSERT(stranded());
if (revalidating_)
return;

const auto flagged = pool_.flags != revalidated_flags_;
const auto flagged = pool_.flags != retried_.flags;
const auto moved = unvalidated_ || pool_.height != retried_.height ||
pool_.median_time_past != retried_.median_time_past;

Expand All @@ -272,15 +280,23 @@ void chaser_transaction::start_revalidation() NOEXCEPT

auto& query = archive();
database::tx_links links{};
database::tx_links unvalidated{};
const auto ctx = database::context::from(pool_);
if (const auto ec = flagged ? query.get_pool_revalidation(links, ctx) :
query.get_pool_unvalidated(links))
code ec{};
if (flagged)
ec = query.get_pool_revalidation(links, ctx);

if (!ec)
ec = query.get_pool_unvalidated(unvalidated);

if (ec)
{
fault(ec);
return;
}

revalidated_flags_ = pool_.flags;
links.insert(links.end(), unvalidated.begin(), unvalidated.end());

retried_ = pool_;
unvalidated_ = false;
if (links.empty())
Expand All @@ -292,12 +308,16 @@ void chaser_transaction::start_revalidation() NOEXCEPT
}

// Revalidation is admission under the context, as confirmation of what a row
// spends may differ from when it was pooled.
// spends may differ from when it was pooled. Sigops are counted under the
// flags of the context, so a tx that passes is counted again.
void chaser_transaction::do_revalidate(const database::tx_links& links,
const chain::context& ctx) NOEXCEPT
{
const auto& query = archive();
const auto bip16 = ctx.is_enabled(flags::bip16_rule);
const auto bip141 = ctx.is_enabled(flags::bip141_rule);
data_chunk passed(links.size());
std::vector<size_t> sigops(links.size());
std::for_each(poolstl::execution::par, links.cbegin(), links.cend(),
[&](const auto& link) NOEXCEPT
{
Expand All @@ -307,17 +327,22 @@ void chaser_transaction::do_revalidate(const database::tx_links& links,
const auto offset = std::distance(links.data(), &link);
const auto at = sign_cast<size_t>(offset);
const auto tx = query.get_transaction(link, true);
passed.at(at) = to_int<uint8_t>(tx && !accept(*tx, ctx) &&
!tx->connect(ctx));
if (!tx || accept(*tx, ctx) || !spends_held(*tx) ||
tx->connect(ctx))
return;

sigops.at(at) = tx->signature_operations(bip16, bip141);
passed.at(at) = one;
});

POST(do_revalidated, links, std::move(passed), ctx);
POST(do_revalidated, links, std::move(passed), std::move(sigops), ctx);
}

// The pool context may have moved under the revalidation, and a reorganization
// under it may have unvalidated a row that it passed.
void chaser_transaction::do_revalidated(const database::tx_links& links,
const data_chunk& passed, const chain::context& ctx) NOEXCEPT
const data_chunk& passed, const std::vector<size_t>& sigops,
const chain::context& ctx) NOEXCEPT
{
BC_ASSERT(stranded());
revalidating_ = false;
Expand All @@ -326,7 +351,7 @@ void chaser_transaction::do_revalidated(const database::tx_links& links,

if (unvalidated_ || !is_sufficient(ctx))
{
revalidated_flags_ = {};
retried_.flags = {};
unvalidated_ = true;
due_ = true;
if (!sweep())
Expand All @@ -343,7 +368,8 @@ void chaser_transaction::do_revalidated(const database::tx_links& links,
if (is_zero(passed.at(at)))
continue;

if (!query.set_pooled_context(links.at(at), revalidated))
if (!query.set_pooled_context(links.at(at), revalidated,
sigops.at(at)))
{
fault(error::transaction2);
return;
Expand Down Expand Up @@ -419,11 +445,12 @@ void chaser_transaction::do_organized(header_t link) NOEXCEPT
fire(events::pool_rows, query.pool_records());
}

top_ = link;
do_bump();
}

// The block was organized before it was announced, so its transition has
// been applied, and its prevout cache is no longer required by the pool.
// been applied.
void chaser_transaction::do_block(header_t link) NOEXCEPT
{
BC_ASSERT(stranded());
Expand Down Expand Up @@ -460,6 +487,7 @@ void chaser_transaction::do_reorganized(header_t link) NOEXCEPT
due_ = due_ || is_nonzero(count);
}

top_ = query.to_parent(link);
do_bump();
if (!pooling_)
return;
Expand Down Expand Up @@ -490,16 +518,12 @@ void chaser_transaction::do_reorganized(header_t link) NOEXCEPT
}

// Block txs are already silent indexed.
const auto admit = now();
const auto pooled = query.set_pooled(fk, *tx, ctx, ctx.timestamp);
report(pool_scan::admission, admit);
if (!pooled)
if (!set_pooled(fk, *tx, ctx))
{
fault(error::transaction2);
return;
}

due_ = due_ || !is_sufficient(ctx);
notify(error::success, chases::transaction{ fk });
broadcast(*tx);
}
Expand All @@ -523,6 +547,7 @@ void chaser_transaction::do_pruned(header_t link) NOEXCEPT
}

floor_ = std::max(floor_, add1(height));
estimating_ = false;
}

// utility
Expand Down Expand Up @@ -617,7 +642,8 @@ void chaser_transaction::broadcast(const chain::transaction& tx) const NOEXCEPT
broadcast(emplace_shared<const network::wire_tx>(view));
}

// A block less its header, largest tx count and declared coinbase weight.
// A block less its header, largest tx count and declared coinbase weight and
// sigops.
pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT
{
constexpr auto count = variable_size(max_block_size / min_transaction_size);
Expand All @@ -627,7 +653,8 @@ pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT
const auto reserved = ceilinged_add(fixed, settings.coinbase_weight);
const auto weight = floored_subtract(max_block_weight, reserved);
const auto bip141 = pool_.is_enabled(flags::bip141_rule);
const auto sigops = bip141 ? max_fast_sigops : max_block_sigops;
const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
const auto sigops = floored_subtract(limit, settings.coinbase_sigops);
return
{
{},
Expand Down
Loading
Loading