Skip to content

Commit

Permalink
Add waitNext() to BlockTemplate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Jan 31, 2025
1 parent 8fa10ed commit 3925d85
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <consensus/amount.h> // for CAmount
#include <interfaces/types.h> // for BlockRef
#include <node/types.h> // for BlockCreateOptions
#include <node/types.h> // for BlockCreateOptions, BlockWaitOptions
#include <primitives/block.h> // for CBlock, CBlockHeader
#include <primitives/transaction.h> // for CTransactionRef
#include <stdint.h> // for int64_t
Expand Down Expand Up @@ -56,6 +56,16 @@ class BlockTemplate
* @returns if the block was processed, independent of block validity
*/
virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase) = 0;

/**
* Waits for fees in the next block to rise, a new tip or the timeout.
*
* @param[in] options Control the timeout (default forever) and by how much total fees
* for the next block should rise (default infinite).
*
* @returns a new BlockTemplate or nothing if the timeout occurs.
*/
virtual std::unique_ptr<BlockTemplate> waitNext(const node::BlockWaitOptions options = {}) = 0;
};

//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)
Expand Down
6 changes: 6 additions & 0 deletions src/ipc/capnp/mining.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
getWitnessCommitmentIndex @7 (context: Proxy.Context) -> (result: Int32);
getCoinbaseMerklePath @8 (context: Proxy.Context) -> (result: List(Data));
submitSolution @9 (context: Proxy.Context, version: UInt32, timestamp: UInt32, nonce: UInt32, coinbase :Data) -> (result: Bool);
waitNext @10 (context: Proxy.Context, options: BlockWaitOptions) -> (result: BlockTemplate);
}

struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {
Expand All @@ -39,6 +40,11 @@ struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {
coinbaseOutputMaxAdditionalSigops @2 :UInt64 $Proxy.name("coinbase_output_max_additional_sigops");
}

struct BlockWaitOptions $Proxy.wrap("node::BlockWaitOptions") {
timeout @0 : Float64 $Proxy.name("timeout");
feeThreshold @1 : Int64 $Proxy.name("fee_threshold");
}

# Note: serialization of the BlockValidationState C++ type is somewhat fragile
# and using the struct can be awkward. It would be good if testBlockValidity
# method were changed to return validity information in a simpler format.
Expand Down
98 changes: 96 additions & 2 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ using interfaces::Mining;
using interfaces::Node;
using interfaces::WalletLoader;
using node::BlockAssembler;
using node::BlockWaitOptions;
using util::Join;

namespace node {
Expand Down Expand Up @@ -871,7 +872,7 @@ class ChainImpl : public Chain
class BlockTemplateImpl : public BlockTemplate
{
public:
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template, NodeContext& node) : m_block_template(std::move(block_template)), m_node(node)
explicit BlockTemplateImpl(BlockAssembler::Options assemble_options, std::unique_ptr<CBlockTemplate> block_template, NodeContext& node) : m_assemble_options(std::move(assemble_options)), m_block_template(std::move(block_template)), m_node(node)
{
assert(m_block_template);
}
Expand Down Expand Up @@ -936,9 +937,102 @@ class BlockTemplateImpl : public BlockTemplate
return chainman().ProcessNewBlock(block_ptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr);
}

std::unique_ptr<BlockTemplate> waitNext(BlockWaitOptions options) override
{
// Delay calculating the current template fees, just in case a new block
// comes in before the next tick.
CAmount current_fees = -1;

// Alternate waiting for a new tip and checking if fees have risen.
// The latter check is expensive so we only run it once per second.
auto now{NodeClock::now()};
const auto deadline = now + options.timeout;
const MillisecondsDouble tick{1000};

bool tip_changed{false};
// Helper to check if the tip has has changed, and also update tip_changed.
auto check_tip_changed = [this, &tip_changed]() EXCLUSIVE_LOCKS_REQUIRED(notifications().m_tip_block_mutex) {
if (tip_changed) return true;
AssertLockHeld(notifications().m_tip_block_mutex);
const auto tip_block{notifications().TipBlock()};
// This is an instance method on BlockTemplate and no template
// could have been generated before a tip exists.
Assume(tip_block);
tip_changed = notifications().TipBlock() != m_block_template->block.hashPrevBlock;
return tip_changed;
};

while (now <= deadline) {
{
WAIT_LOCK(notifications().m_tip_block_mutex, lock);
// First make sure the tip hasn't already changed:
check_tip_changed() || notifications().m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(notifications().m_tip_block_mutex) {
return check_tip_changed() || chainman().m_interrupt;
});
}

if (chainman().m_interrupt) return nullptr;

// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
LOCK(::cs_main);

/**
* We determine if fees increased compared to the previous template by generating
* a fresh template. There may be more efficient ways to determine how much
* (approximate) fees for the next block increased, perhaps more so after
* Cluster Mempool.
*
* We'll also create a new template if the tip changed during the last tick.
*/
if (options.fee_threshold != MAX_MONEY || tip_changed) {
auto block_template{std::make_unique<BlockTemplateImpl>(m_assemble_options, BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), m_assemble_options}.CreateNewBlock(), m_node)};

// If the tip changed, return the new template regardless of its fees.
if (tip_changed) {
return block_template;
}

// Calculate the original template total fees if we haven't already
if (current_fees == -1) {
current_fees = 0;
for (CAmount fee : m_block_template->vTxFees) {
// Skip coinbase
if (fee < 0) continue;
current_fees += fee;
}
}

CAmount new_fees = 0;
for (CAmount fee : block_template->m_block_template->vTxFees) {
// Skip coinbase
if (fee < 0) continue;
new_fees += fee;
Assume(options.fee_threshold != MAX_MONEY);
if (new_fees >= current_fees + options.fee_threshold) return block_template;
}
}

/**
* Break out of while when using mock time. While functional tests
* can call setmocktime to move the clock and exit this loop, a
* unit tests would need to spawn a thread to achieve this.
* Making the while loop use now < deadline won't work either, because
* then there's no wait to test its internals.
*/
if (now == deadline) break;
now = NodeClock::now();
}

return nullptr;
}

const BlockAssembler::Options m_assemble_options;

const std::unique_ptr<CBlockTemplate> m_block_template;

NodeContext* context() { return &m_node; }
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
KernelNotifications& notifications() { return *Assert(m_node.notifications); }
NodeContext& m_node;
};

Expand Down Expand Up @@ -985,7 +1079,7 @@ class MinerImpl : public Mining
{
BlockAssembler::Options assemble_options{options};
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(), m_node);
return std::make_unique<BlockTemplateImpl>(assemble_options, BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(), m_node);
}

NodeContext* context() override { return &m_node; }
Expand Down
24 changes: 24 additions & 0 deletions src/node/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#define BITCOIN_NODE_TYPES_H

#include <cstddef>
#include <consensus/amount.h>
#include <script/script.h>
#include <util/time.h>

namespace node {
enum class TransactionError {
Expand Down Expand Up @@ -61,6 +63,28 @@ struct BlockCreateOptions {
*/
CScript coinbase_output_script{CScript() << OP_TRUE};
};

struct BlockWaitOptions {
/**
* How long to wait before returning nullptr instead of a new template.
* Default is to wait forever.
*/
MillisecondsDouble timeout{MillisecondsDouble::max()};

/**
* Wait until total fees in the new template exceed fees in the origal
* template by at least this amount (in sats). The default is to ignore
* fee increases and only wait for a tip change.
*
* This is not an std::optional<CAmount> because (the current version of)
* libmultiprocess maps missing values to 0.
* https://github.com/bitcoin/bitcoin/pull/31283#discussion_r1937622086
*
* Use a magic value instead.
*/
CAmount fee_threshold{MAX_MONEY};
};

} // namespace node

#endif // BITCOIN_NODE_TYPES_H
24 changes: 18 additions & 6 deletions src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const
tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
Txid hashLowFeeTx = tx.GetHash();
AddToMempool(tx_mempool, entry.Fee(feeToUse).FromTx(tx));
block_template = mining->createNewBlock(options);
BOOST_REQUIRE(block_template);

// waitNext() should return nullptr because there is no better template
auto should_be_nullptr = block_template->waitNext({.timeout = MillisecondsDouble{0}, .fee_threshold = 1});
BOOST_REQUIRE(should_be_nullptr == nullptr);

block = block_template->getBlock();
// Verify that the free tx and the low fee tx didn't get selected
for (size_t i=0; i<block.vtx.size(); ++i) {
Expand All @@ -196,7 +199,9 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const
tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
hashLowFeeTx = tx.GetHash();
AddToMempool(tx_mempool, entry.Fee(feeToUse + 2).FromTx(tx));
block_template = mining->createNewBlock(options);

// waitNext() should return if fees for the new template are at least 1 sat up
block_template = block_template->waitNext({.fee_threshold = 1});
BOOST_REQUIRE(block_template);
block = block_template->getBlock();
BOOST_REQUIRE_EQUAL(block.vtx.size(), 6U);
Expand Down Expand Up @@ -672,8 +677,10 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
const int current_height{mining->getTip()->height};

// Simple block creation, nothing special yet:
block_template = mining->createNewBlock(options);
BOOST_REQUIRE(block_template);
if (current_height % 2 == 0) {
block_template = mining->createNewBlock(options);
BOOST_REQUIRE(block_template);
} // for odd heights the next template is created at the end of this loop

CBlock block{block_template->getBlock()};
CMutableTransaction txCoinbase(*block.vtx[0]);
Expand Down Expand Up @@ -710,7 +717,12 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
BOOST_REQUIRE_EQUAL(maybe_new_tip->GetBlockHash(), block.GetHash());
}
// This just adds coverage
mining->waitTipChanged(block.hashPrevBlock);
if (current_height % 2 == 0) {
block_template = block_template->waitNext();
BOOST_REQUIRE(block_template);
} else {
mining->waitTipChanged(block.hashPrevBlock);
}
}

LOCK(cs_main);
Expand Down

0 comments on commit 3925d85

Please sign in to comment.