@@ -776,9 +776,22 @@ static RPCHelpMan getblocktemplate()
776
776
static unsigned int nTransactionsUpdatedLast;
777
777
const CTxMemPool& mempool = EnsureMemPool (node);
778
778
779
- if (!lpval.isNull ())
780
- {
781
- // Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
779
+ // Long Polling (BIP22)
780
+ if (!lpval.isNull ()) {
781
+ /* *
782
+ * Wait to respond until either the best block changes, OR there are more
783
+ * transactions.
784
+ *
785
+ * The check for new transactions first happens after 1 minute and
786
+ * subsequently every 10 seconds. BIP22 does not require this particular interval.
787
+ * On mainnet the mempool changes frequently enough that in practice this RPC
788
+ * returns after 60 seconds, or sooner if the best block changes.
789
+ *
790
+ * getblocktemplate is unlikely to be called by bitcoin-cli, so
791
+ * -rpcclienttimeout is not a concern. BIP22 recommends a long request timeout.
792
+ *
793
+ * The longpollid is assumed to be a tip hash if it has the right format.
794
+ */
782
795
uint256 hashWatchedChain;
783
796
unsigned int nTransactionsUpdatedLastLP;
784
797
@@ -787,6 +800,8 @@ static RPCHelpMan getblocktemplate()
787
800
// Format: <hashBestChain><nTransactionsUpdatedLast>
788
801
const std::string& lpstr = lpval.get_str ();
789
802
803
+ // Assume the longpollid is a block hash. If it's not then we return
804
+ // early below.
790
805
hashWatchedChain = ParseHashV (lpstr.substr (0 , 64 ), " longpollid" );
791
806
nTransactionsUpdatedLastLP = LocaleIndependentAtoi<int64_t >(lpstr.substr (64 ));
792
807
}
@@ -801,15 +816,20 @@ static RPCHelpMan getblocktemplate()
801
816
LEAVE_CRITICAL_SECTION (cs_main);
802
817
{
803
818
MillisecondsDouble checktxtime{std::chrono::minutes (1 )};
804
- while (tip == hashWatchedChain && IsRPCRunning ()) {
819
+ while (IsRPCRunning ()) {
820
+ // If hashWatchedChain is not a real block hash, this will
821
+ // return immediately.
805
822
std::optional<BlockRef> maybe_tip{miner.waitTipChanged (hashWatchedChain, checktxtime)};
806
823
// Node is shutting down
807
824
if (!maybe_tip) break ;
808
825
tip = maybe_tip->hash ;
809
- // Timeout: Check transactions for update
810
- // without holding the mempool lock to avoid deadlocks
811
- if (mempool.GetTransactionsUpdated () != nTransactionsUpdatedLastLP)
826
+ if (tip != hashWatchedChain) break ;
827
+
828
+ // Check transactions for update without holding the mempool
829
+ // lock to avoid deadlocks.
830
+ if (mempool.GetTransactionsUpdated () != nTransactionsUpdatedLastLP) {
812
831
break ;
832
+ }
813
833
checktxtime = std::chrono::seconds (10 );
814
834
}
815
835
}
0 commit comments