mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
rpc: clarify longpoll behavior
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.
This commit is contained in:
parent
5315278e7c
commit
05117e6e17
1 changed files with 27 additions and 7 deletions
|
@ -776,9 +776,22 @@ static RPCHelpMan getblocktemplate()
|
||||||
static unsigned int nTransactionsUpdatedLast;
|
static unsigned int nTransactionsUpdatedLast;
|
||||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||||
|
|
||||||
if (!lpval.isNull())
|
// Long Polling (BIP22)
|
||||||
{
|
if (!lpval.isNull()) {
|
||||||
// Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
|
/**
|
||||||
|
* 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;
|
uint256 hashWatchedChain;
|
||||||
unsigned int nTransactionsUpdatedLastLP;
|
unsigned int nTransactionsUpdatedLastLP;
|
||||||
|
|
||||||
|
@ -787,6 +800,8 @@ static RPCHelpMan getblocktemplate()
|
||||||
// Format: <hashBestChain><nTransactionsUpdatedLast>
|
// Format: <hashBestChain><nTransactionsUpdatedLast>
|
||||||
const std::string& lpstr = lpval.get_str();
|
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");
|
hashWatchedChain = ParseHashV(lpstr.substr(0, 64), "longpollid");
|
||||||
nTransactionsUpdatedLastLP = LocaleIndependentAtoi<int64_t>(lpstr.substr(64));
|
nTransactionsUpdatedLastLP = LocaleIndependentAtoi<int64_t>(lpstr.substr(64));
|
||||||
}
|
}
|
||||||
|
@ -801,15 +816,20 @@ static RPCHelpMan getblocktemplate()
|
||||||
LEAVE_CRITICAL_SECTION(cs_main);
|
LEAVE_CRITICAL_SECTION(cs_main);
|
||||||
{
|
{
|
||||||
MillisecondsDouble checktxtime{std::chrono::minutes(1)};
|
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)};
|
std::optional<BlockRef> maybe_tip{miner.waitTipChanged(hashWatchedChain, checktxtime)};
|
||||||
// Node is shutting down
|
// Node is shutting down
|
||||||
if (!maybe_tip) break;
|
if (!maybe_tip) break;
|
||||||
tip = maybe_tip->hash;
|
tip = maybe_tip->hash;
|
||||||
// Timeout: Check transactions for update
|
if (tip != hashWatchedChain) break;
|
||||||
// without holding the mempool lock to avoid deadlocks
|
|
||||||
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
|
// Check transactions for update without holding the mempool
|
||||||
|
// lock to avoid deadlocks.
|
||||||
|
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
checktxtime = std::chrono::seconds(10);
|
checktxtime = std::chrono::seconds(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue