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
13 changes: 12 additions & 1 deletion include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ class BCN_API chaser_transaction
const system::data_chunk& passed,
const system::chain::context& ctx) NOEXCEPT;

// Override base class strand because it sits on the network thread pool.
network::io::strand& strand() NOEXCEPT override;
bool stranded() const NOEXCEPT override;

private:
/// A set under submission, its running set and outcome.
struct submission
Expand Down Expand Up @@ -211,7 +215,8 @@ class BCN_API chaser_transaction

code check(const system::chain::transaction_cptrs& txs,
const std::vector<bool>& stored, bool announced) NOEXCEPT;
code accept(const system::chain::transaction& tx) NOEXCEPT;
code accept(const system::chain::transaction& tx,
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,
Expand Down Expand Up @@ -261,7 +266,11 @@ class BCN_API chaser_transaction
/// Fire the event with the microseconds since the start.
void fire_span(events event, const time_point& start) NOEXCEPT;

// This is not thread safe.
network::threadpool transaction_threadpool_;

// These are thread safe.
network::io::strand transaction_strand_;
network::threadpool scan_threadpool_;
network::threadpool revalidation_threadpool_;
stopper stopping_{};
Expand All @@ -278,7 +287,9 @@ class BCN_API chaser_transaction
bool due_{ true };
bool scanning_{};
bool revalidating_{};
bool unvalidated_{};
uint32_t revalidated_flags_{};
system::chain::context retried_{};
std::vector<uint64_t> marks_{};
std::array<uint64_t, relay_slots> rates_{};
};
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/node/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ class BCN_API settings
uint32_t bytes_per_sigop;
uint32_t coinbase_weight;
uint32_t maximum_chunk_depth;
bool thread_priority;

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

/// The network of the defaults.
Expand Down
63 changes: 54 additions & 9 deletions src/chasers/chaser_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ using namespace std::placeholders;
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)

// Independent threadpool and strand (base class strand uses network pool).
chaser_transaction::chaser_transaction(full_node& node) NOEXCEPT
: chaser(node),
transaction_threadpool_(one, node.node_settings().pool.thread_priority_()),
transaction_strand_(transaction_threadpool_.service().get_executor()),
scan_threadpool_(one, node.node_settings().thread_priority_()),
revalidation_threadpool_(one, node.node_settings().thread_priority_()),
outbound_excluded_(unmask_right<uint64_t>(
Expand All @@ -61,13 +64,15 @@ void chaser_transaction::stopping(const code& ec) NOEXCEPT
stopping_.store(true);
scan_threadpool_.stop();
revalidation_threadpool_.stop();
transaction_threadpool_.stop();
chaser::stopping(ec);
}

// The strand is no longer running when the chaser is stopped.
void chaser_transaction::stop() NOEXCEPT
{
if (!scan_threadpool_.join() || !revalidation_threadpool_.join())
if (!scan_threadpool_.join() || !revalidation_threadpool_.join() ||
!transaction_threadpool_.join())
{
BC_ASSERT_MSG(false, "failed to join threadpool");
std::abort();
Expand Down Expand Up @@ -249,23 +254,35 @@ bool chaser_transaction::sweep() NOEXCEPT
return applied;
}

// Rows left out by flags are revalidated once per change of flags, so a row
// that fails is not retried until the flags change again.
// 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.
void chaser_transaction::start_revalidation() NOEXCEPT
{
BC_ASSERT(stranded());
if (revalidating_ || pool_.flags == revalidated_flags_)
if (revalidating_)
return;

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

if (!flagged && !moved)
return;

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

revalidated_flags_ = pool_.flags;
retried_ = pool_;
unvalidated_ = false;
if (links.empty())
return;

Expand All @@ -274,6 +291,8 @@ void chaser_transaction::start_revalidation() NOEXCEPT
BIND(do_revalidate, std::move(links), pool_));
}

// Revalidation is admission under the context, as confirmation of what a row
// spends may differ from when it was pooled.
void chaser_transaction::do_revalidate(const database::tx_links& links,
const chain::context& ctx) NOEXCEPT
{
Expand All @@ -288,13 +307,15 @@ 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 && !validate(*tx, ctx));
passed.at(at) = to_int<uint8_t>(tx && !accept(*tx, ctx) &&
!tx->connect(ctx));
});

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

// The pool context may have moved under the revalidation.
// 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
{
Expand All @@ -303,9 +324,14 @@ void chaser_transaction::do_revalidated(const database::tx_links& links,
if (closed())
return;

if (!is_sufficient(ctx))
if (unvalidated_ || !is_sufficient(ctx))
{
revalidated_flags_ = {};
unvalidated_ = true;
due_ = true;
if (!sweep())
fault(error::transaction11);

return;
}

Expand Down Expand Up @@ -418,14 +444,20 @@ void chaser_transaction::do_reorganized(header_t link) NOEXCEPT
auto& query = archive();
if (is_indexing())
{
size_t count{};
const auto start = now();
const auto applied = query.set_pool_block(link, false);
const auto applied = query.set_pool_block(link, false) &&
query.set_pool_unvalidated(count, link);

report(pool_scan::block_out, start);
if (!applied)
{
fault(error::transaction10);
return;
}

unvalidated_ = unvalidated_ || is_nonzero(count);
due_ = due_ || is_nonzero(count);
}

do_bump();
Expand Down Expand Up @@ -604,6 +636,19 @@ pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT
};
}

// Overrides due to independent priority thread pool
// ----------------------------------------------------------------------------

network::io::strand& chaser_transaction::strand() NOEXCEPT
{
return transaction_strand_;
}

bool chaser_transaction::stranded() const NOEXCEPT
{
return transaction_strand_.running_in_this_thread();
}

BC_POP_WARNING()
BC_POP_WARNING()

Expand Down
5 changes: 3 additions & 2 deletions src/chasers/chaser_transaction_compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ void chaser_transaction::do_archive_block(
}

// The stored tx, or validated and archived, pooled under the block context.
// Each block archives its own coinbase.
code chaser_transaction::fill(database::tx_link& out,
const chain::transaction& tx, const chain::context& ctx) NOEXCEPT
{
auto& query = archive();
out = to_stored(tx);
out = tx.is_coinbase() ? database::tx_link{} : to_stored(tx);
if (!out.is_terminal())
{
if (tx.is_coinbase() || !query.is_pooling() || !reusable(ctx) ||
if (!query.is_pooling() || !reusable(ctx) ||
query.is_pooled(out) || query.is_confirmed_tx(out))
return error::success;

Expand Down
15 changes: 8 additions & 7 deletions src/chasers/chaser_transaction_submit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void chaser_transaction::do_submit(const transactions_cptr& txs, bool test,
continue;

const auto& tx = *txs->at(index);
auto ec = accept(tx);
auto ec = accept(tx, pool_);
if (!ec && !spends_held(set, tx))
ec = system::error::missing_previous_output;

Expand Down Expand Up @@ -458,23 +458,24 @@ code chaser_transaction::admit(database::tx_link& link,
return error::success;
}

code chaser_transaction::accept(const chain::transaction& tx) NOEXCEPT
code chaser_transaction::accept(const chain::transaction& tx,
const chain::context& ctx) NOEXCEPT
{
code ec{};

// Ensure tx does not violate tx consensus rules.
// Guards ensure tx does not violate presumed block consensus rules.
// These are DoS guards when validating a tx outside of a block.
if (!ec) ec = tx.check();
if (!ec) ec = tx.check(pool_);
if (!ec) ec = tx.check(ctx);
if (!ec) ec = tx.check_guard();
if (!ec) ec = tx.check_guard(pool_);
if (!ec) ec = tx.check_guard(ctx);

// Store population follows all checks that do not require it.
if (!ec) archive().populate_with_metadata(tx, true, true);
if (!ec) ec = tx.accept(pool_);
if (!ec) ec = tx.accept_guard(pool_);
if (!ec) ec = tx.confirm_guard(pool_);
if (!ec) ec = tx.accept(ctx);
if (!ec) ec = tx.accept_guard(ctx);
if (!ec) ec = tx.confirm_guard(ctx);

// An unconfirmed conflict is admitted by coverage.
return ec == system::error::double_spend ? error::success : ec;
Expand Down
10 changes: 9 additions & 1 deletion src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ settings::transaction_pool::transaction_pool() NOEXCEPT
maximum_outcomes{ 1'024 },
bytes_per_sigop{ 20 },
coinbase_weight{ 2'000 },
maximum_chunk_depth{ 25 }
maximum_chunk_depth{ 25 },
thread_priority{ true }
{
}

Expand All @@ -52,6 +53,13 @@ uint64_t settings::transaction_pool::cutoff_rate_() const NOEXCEPT
chain::satoshi_per_bitcoin);
}

network::processing_priority
settings::transaction_pool::thread_priority_() const NOEXCEPT
{
return thread_priority ? network::processing_priority::high :
network::processing_priority::medium;
}

settings::settings() NOEXCEPT
: network{ chain::selection::none },
pool{},
Expand Down
39 changes: 39 additions & 0 deletions test/chasers/chaser_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,45 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__reorganized__unpooled_block_tx__pooled,
BC_REQUIRE_EQUAL(record.time, block.header().timestamp());
}

BC_FIXTURE_TEST_CASE(chaser_transaction__reorganized__relative_spender_of_reconfirmed_parent__revalidated, chaser_transaction_relative_setup_fixture)
{
constexpr auto insufficient = bit_or(pool::algorithm::absent, pool::algorithm::insufficient);
const auto block2 = confirm();
const auto raised = bumped(3);
const auto tx = spend_relative(parent().hash(false), parent_value - 1'000, 2);
const auto result = submit(package({ tx }));
const auto link = query_.to_tx(tx->hash(false));
const auto before = cell(link);
const auto popped2 = query_.pop_confirmed();
node_.notify(node::error::success, chases::reorganized{ block2 });
const auto lowered2 = bumped(2);
const auto block1 = query_.to_header(parent_block().hash());
const auto popped1 = query_.pop_confirmed();
node_.notify(node::error::success, chases::reorganized{ block1 });
const auto lowered1 = bumped(1);
const auto other1 = confirm();
const auto other2 = confirm(to_shared<chain::transaction>(parent()));
const auto regained = bumped(3);
const auto locked = cell(link);
const auto other3 = confirm();
const auto unlocked = celled(link, pool::algorithm::unlabeled);

BC_REQUIRE(!block2.is_terminal());
BC_REQUIRE(raised);
BC_REQUIRE_EQUAL(result.first, node::error::success);
BC_REQUIRE(pool::algorithm::is_present(before));
BC_REQUIRE(popped2);
BC_REQUIRE(lowered2);
BC_REQUIRE(popped1);
BC_REQUIRE(lowered1);
BC_REQUIRE(!other1.is_terminal());
BC_REQUIRE(!other2.is_terminal());
BC_REQUIRE(regained);
BC_REQUIRE_EQUAL(locked, insufficient);
BC_REQUIRE(!other3.is_terminal());
BC_REQUIRE(unlocked);
}

BC_AUTO_TEST_SUITE_END()

BC_AUTO_TEST_SUITE(chaser_transaction_scan_tests)
Expand Down
15 changes: 15 additions & 0 deletions test/chasers/chaser_transaction_compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__stored_unpooled_tx__poo
BC_REQUIRE_EQUAL(query_.to_tx(tx->hash(false)), stored);
}

BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__stored_coinbase__archived_again, chaser_transaction_header_setup_fixture)
{
const auto& block1 = p2p_compact_setup_fixture::block1();
const auto link = query_.to_header(block1.hash());
const auto coinbase = block1.transactions_ptr()->front();
BC_REQUIRE(query_.set(*coinbase));

const auto stored = query_.to_tx(coinbase->hash(false));
const auto result = submit_compact(package({ coinbase }), { database::tx_link::terminal }, link);
BC_REQUIRE_EQUAL(result.first, node::error::success);
BC_REQUIRE(query_.is_associated(link));
BC_REQUIRE_EQUAL(query_.to_duplicates(coinbase->hash(false)).size(), 2u);
BC_REQUIRE(query_.to_tx(coinbase->hash(false)) != stored);
}

BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__not_pooling__pooling_disabled, chaser_transaction_closed_header_setup_fixture)
{
const auto link = query_.to_header(p2p_compact_setup_fixture::block1().hash());
Expand Down
9 changes: 9 additions & 0 deletions test/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ BC_AUTO_TEST_CASE(settings__pool__default__expected)
BC_REQUIRE_EQUAL(pool.bytes_per_sigop, 20_u32);
BC_REQUIRE_EQUAL(pool.coinbase_weight, 2'000_u32);
BC_REQUIRE_EQUAL(pool.maximum_chunk_depth, 25_u32);
BC_REQUIRE(pool.thread_priority);
BC_REQUIRE(pool.thread_priority_() == network::processing_priority::high);
}

BC_AUTO_TEST_CASE(settings__pool__thread_priority_false__medium)
{
node::settings::transaction_pool pool{};
pool.thread_priority = false;
BC_REQUIRE(pool.thread_priority_() == network::processing_priority::medium);
}

BC_AUTO_TEST_SUITE_END()
Loading