Skip to content

Commit

Permalink
rpc: clarify longpoll behavior
Browse files Browse the repository at this point in the history
Move the comparison to hashWatchedChain inside the while loop.

Although this early return prevents the GetTransactionsUpdated()
call in cases where the tip updates, it's only done to improve
readability. The check itself is very cheap (although a more
useful check might not be).

Also add code comments.
  • Loading branch information
Sjors committed Feb 19, 2025
1 parent 56fac7c commit 9725cd6
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,22 @@ static RPCHelpMan getblocktemplate()
static unsigned int nTransactionsUpdatedLast;
const CTxMemPool& mempool = EnsureMemPool(node);

if (!lpval.isNull())
{
// Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
// Long Polling (BIP22)
if (!lpval.isNull()) {
/**
* Wait to respond until either the best block changes, OR there are more
* transactions.
*
* The check for new transactions first happens after 1 minute and
* subsequently every 10 seconds. BIP22 does not require this particular interval.
* On mainnet the mempool changes frequently enough that in practice this RPC
* returns after 60 seconds, or sooner if the best block changes.
*
* getblocktemplate is unlikely to be called by bitcoin-cli, so
* -rpcclienttimeout is not a concern. BIP22 recommends a long request timeout.
*
* The longpollid is assumed to be a tip hash if it has the right format.
*/
uint256 hashWatchedChain;
unsigned int nTransactionsUpdatedLastLP;

Expand All @@ -787,6 +800,8 @@ static RPCHelpMan getblocktemplate()
// Format: <hashBestChain><nTransactionsUpdatedLast>
const std::string& lpstr = lpval.get_str();

// Assume the longpollid is a block hash. If it's not then we return
// early below.
hashWatchedChain = ParseHashV(lpstr.substr(0, 64), "longpollid");
nTransactionsUpdatedLastLP = LocaleIndependentAtoi<int64_t>(lpstr.substr(64));
}
Expand All @@ -801,15 +816,20 @@ static RPCHelpMan getblocktemplate()
LEAVE_CRITICAL_SECTION(cs_main);
{
MillisecondsDouble checktxtime{std::chrono::minutes(1)};
while (tip == hashWatchedChain && IsRPCRunning()) {
while (IsRPCRunning()) {
// If hashWatchedChain is not a real block hash, this will
// return immediately.
std::optional<BlockRef> maybe_tip{miner.waitTipChanged(hashWatchedChain, checktxtime)};
// Node is shutting down
if (!maybe_tip) break;
tip = maybe_tip->hash;
// Timeout: Check transactions for update
// without holding the mempool lock to avoid deadlocks
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
if (tip != hashWatchedChain) break;

// Check transactions for update without holding the mempool
// lock to avoid deadlocks.
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP) {
break;
}
checktxtime = std::chrono::seconds(10);
}
}
Expand Down

0 comments on commit 9725cd6

Please sign in to comment.