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
16 changes: 9 additions & 7 deletions include/bitcoin/node/chasers/chaser_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ class BCN_API chaser_template

private:
void build() NOEXCEPT;
void handle_build(const code& ec, size_t height,
const database::tx_links& links, uint64_t generation,
size_t moves) NOEXCEPT;
void do_build(const code& ec, size_t height,
const database::tx_links& links, uint64_t generation,
size_t moves) NOEXCEPT;
code replace(size_t height, const database::tx_links& links) NOEXCEPT;
void handle_build(const code& ec, const system::chain::context& context,
const system::hash_digest& previous, const database::tx_links& links,
uint64_t generation, size_t moves) NOEXCEPT;
void do_build(const code& ec, const system::chain::context& context,
const system::hash_digest& previous, const database::tx_links& links,
uint64_t generation, size_t moves) NOEXCEPT;
code replace(const system::chain::context& context,
const system::hash_digest& previous,
const database::tx_links& links) NOEXCEPT;
void complete(const code& ec) NOEXCEPT;
void start_timer() NOEXCEPT;
void handle_timer(const code& ec) NOEXCEPT;
Expand Down
9 changes: 5 additions & 4 deletions include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class BCN_API chaser_transaction
confirmable_fees_handler&& handler) NOEXCEPT;

/// Count of changes to the pool (thread safe).
virtual uint64_t pool_generation() const NOEXCEPT;
uint64_t pool_generation() const NOEXCEPT override;

/// Subscribe to pool scans (notified on the strand).
virtual void subscribe_pool(pool_notifier&& handler) NOEXCEPT;
Expand Down Expand Up @@ -199,6 +199,7 @@ class BCN_API chaser_transaction
bool test;
bool announced;
std::vector<bool> stored{};
std::vector<size_t> sigops_of{};
std::unordered_set<system::hash_digest> held{};
query::pool_graph graph{};
uint64_t bytes{};
Expand All @@ -221,8 +222,7 @@ class BCN_API chaser_transaction
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;
void count(submission& set, size_t index) const NOEXCEPT;
code extend(bool& covered, submission& set, size_t index) NOEXCEPT;
code restore(submission& set, size_t index) NOEXCEPT;
code clear(size_t& at, submission& set, size_t index) NOEXCEPT;
Expand All @@ -232,7 +232,7 @@ class BCN_API chaser_transaction
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;
uint32_t time, size_t sigops) NOEXCEPT;
code validate(bool& guarded, const system::chain::transaction& tx,
const system::chain::context& ctx) NOEXCEPT;
database::tx_link to_stored(
Expand Down Expand Up @@ -313,6 +313,7 @@ class BCN_API chaser_transaction
// These are protected by strand.
pool_subscriber subscriber_{};
system::chain::context pool_{};
system::hash_digest previous_{};
database::header_link top_{};
size_t floor_{};
bool due_{ true };
Expand Down
10 changes: 8 additions & 2 deletions include/bitcoin/node/types/block_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ struct block_template
{
typedef std::shared_ptr<const block_template> cptr;

/// Height of the block.
size_t height;
/// Hash of the previous block.
system::hash_digest previous;

/// Context of the block, as the pool validates under it.
system::chain::context context;

/// Count of templates held to this one.
uint64_t sequence;
Expand All @@ -47,6 +50,9 @@ struct block_template

/// Merkle branch of the coinbase.
system::hashes branch;

/// Ids, totals and parent positions of the txs.
database::template_txs txs;
};

} // namespace node
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/node/types/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ typedef std::function<void(const code&, const map_ptr&,
const job::ptr&)> map_handler;

/// Mining types.
typedef std::function<void(const code&, size_t,
const database::tx_links&)> template_handler;
typedef std::function<void(const code&, const system::chain::context&,
const system::hash_digest&, const database::tx_links&)> template_handler;
typedef std::function<void(const code&,
const block_template::cptr&)> block_template_handler;

Expand Down
54 changes: 25 additions & 29 deletions src/chasers/chaser_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,22 @@ void chaser_template::build() NOEXCEPT

building_ = true;
request_template(
BIND(handle_build, _1, _2, _3, pool_generation(), moves_));
BIND(handle_build, _1, _2, _3, _4, pool_generation(), moves_));
}

// private
void chaser_template::handle_build(const code& ec, size_t height,
void chaser_template::handle_build(const code& ec,
const chain::context& context, const hash_digest& previous,
const database::tx_links& links, uint64_t generation,
size_t moves) NOEXCEPT
{
POST(do_build, ec, height, links, generation, moves);
POST(do_build, ec, context, previous, links, generation, moves);
}

// private
// A build that the chain moved under is not of its top, so is built again.
void chaser_template::do_build(const code& ec, size_t height,
void chaser_template::do_build(const code& ec,
const chain::context& context, const hash_digest& previous,
const database::tx_links& links, uint64_t generation,
size_t moves) NOEXCEPT
{
Expand Down Expand Up @@ -237,7 +239,7 @@ void chaser_template::do_build(const code& ec, size_t height,
}

built_ = generation;
if (const auto failed = replace(height, links))
if (const auto failed = replace(context, previous, links))
{
complete(fault(failed));
return;
Expand All @@ -250,59 +252,53 @@ void chaser_template::do_build(const code& ec, size_t height,
// private
// The template held is replaced when it is not of the chain top, or when the
// fees of the build exceed its own by more than the configured increase.
code chaser_template::replace(size_t height,
const database::tx_links& links) NOEXCEPT
code chaser_template::replace(const chain::context& context,
const hash_digest& previous, const database::tx_links& links) NOEXCEPT
{
BC_ASSERT(stranded());
const auto& query = archive();
database::template_txs txs{};
if (const auto ec = archive().get_template_txs(txs, links))
return ec;

uint64_t fees{};
for (const auto& link: links)
{
query::pool_totals totals{};
if (!query.get_pooled_totals(totals, link))
return database::error::integrity;

fees = ceilinged_add(fees, totals.fee);
}
for (const auto& tx: txs)
fees = ceilinged_add(fees, tx.fee);

const auto increase = node_settings().pool.template_fee_increase;
const auto current = held_ && (moved_ == moves_) &&
(held_->height == height);
(held_->previous == previous);

if (current && (floored_subtract(fees, held_->fees) <= increase))
return error::success;

// Leaves of the coinbase are null, as it is not of the template.
hashes txids{ null_hash };
hashes wtxids{ null_hash };
txids.reserve(add1(links.size()));
wtxids.reserve(add1(links.size()));
for (const auto& link: links)
txids.reserve(add1(txs.size()));
wtxids.reserve(add1(txs.size()));
for (const auto& tx: txs)
{
const auto row = query.to_pool_row(link);
if (row.is_terminal())
return database::error::integrity;

txids.push_back(query.get_tx_key(link));
wtxids.push_back(query.get_wtxid_key(row));
txids.push_back(tx.txid);
wtxids.push_back(tx.wtxid);
}

const auto root = merkle_root(std::move(wtxids));
const auto sequence = add1(template_generation());
held_ = to_shared<block_template>(block_template
{
height,
previous,
context,
sequence,
fees,
links,
sha256::double_hash(root, null_hash),
chain::block::merkle_branch(zero, std::move(txids))
chain::block::merkle_branch(zero, std::move(txids)),
std::move(txs)
});

generation_.store(sequence, std::memory_order_relaxed);
fire(events::template_issued, links.size());
notify(error::success, chases::template_{ height });
notify(error::success, chases::template_{ context.height });
return error::success;
}

Expand Down
10 changes: 10 additions & 0 deletions src/chasers/chaser_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@ void chaser_transaction::do_bump() NOEXCEPT
const auto opened = pooling_.load();
pooling_ = false;
if (closed() || !is_current_chain(true))
{
if (opened)
++generation_;

return;
}

// Pooling is a permanent store property, so it requires relay.
auto& query = archive();
Expand All @@ -211,6 +216,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);

// Row holds unless a flag changes or height or median time past falls.
due_ = due_ || pool_.flags != previous.flags ||
Expand Down Expand Up @@ -239,6 +245,10 @@ void chaser_transaction::do_bump() NOEXCEPT
}
}

// Opening or closing the pool changes its templates.
if (pooling_ != opened)
++generation_;

// Scan starts are mapped after the latch, as pooled rows are unordered.
if (!query.silent_enabled() || scanning_)
return;
Expand Down
6 changes: 3 additions & 3 deletions src/chasers/chaser_transaction_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ void chaser_transaction::do_build_template(

if (closed())
{
handler(network::error::service_stopped, {}, {});
handler(network::error::service_stopped, {}, {}, {});
return;
}

if (!pooling_)
{
handler(error::pooling_disabled, {}, {});
handler(error::pooling_disabled, {}, {}, {});
return;
}

Expand All @@ -150,7 +150,7 @@ void chaser_transaction::do_build_template(
settings.bytes_per_sigop, settings.maximum_outcomes);

report(pool_scan::template_, start);
handler(ec, pool_.height, links);
handler(ec, pool_, previous_, links);
}

// private
Expand Down
40 changes: 4 additions & 36 deletions src/chasers/chaser_transaction_relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,41 +225,9 @@ bool chaser_transaction::relay(const database::tx_links& links,
{
BC_ASSERT(stranded());
using algorithm = pool::algorithm;
struct relayable
{
size_t row;
database::tx_link link;
algorithm::totals_t totals;
};

const auto& query = archive();
std::vector<relayable> rows{};
std::unordered_set<database::tx_link::integer> seen{};
const auto add = [&](const database::tx_link& link) NOEXCEPT
{
if (!seen.insert(link.value).second)
return true;

algorithm::totals_t totals{};
const auto row = query.to_pool_row(link);
if (row.is_terminal() || !query.get_pooled_totals(totals, link))
return false;

rows.push_back({ row.value, link, totals });
return true;
};

for (const auto& link: links)
{
for (const auto& ancestor: query.to_confirmable_ancestors(link))
if (!add(ancestor))
return false;

if (!add(link))
return false;
}

std::ranges::sort(rows, {}, &relayable::row);
database::pooled_rows rows{};
if (archive().get_pooled_ancestry(rows, links))
return false;

const auto limit = block_limit().size;
const auto bytes_per_sigop = node_settings().pool.bytes_per_sigop;
Expand Down Expand Up @@ -302,7 +270,7 @@ bool chaser_transaction::relay(const database::tx_links& links,
continue;

mark = bit_or(mark, word);
const chases::relay event{ row.link.value, word };
const chases::relay event{ row.tx, word };
if (is_zero(key))
notify(error::success, event);
else
Expand Down
Loading
Loading