Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2014-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test longpolling with getblocktemplate."""7import random8import threading10from test_framework.blocktools import NORMAL_GBT_REQUEST_PARAMS11from test_framework.test_framework import BitcoinTestFramework12from test_framework.util import assert_equal13from test_framework.wallet import MiniWallet16class LongpollThread(threading.Thread):17def __init__(self, node):18threading.Thread.__init__(self)19# query current longpollid20template = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)21self.longpollid = template['longpollid']22# create a new connection to the node for this thread23self.node = node.create_new_rpc_connection(client_timeout=600)25def run(self):26self.node.getblocktemplate({'longpollid': self.longpollid, **NORMAL_GBT_REQUEST_PARAMS})28class GetBlockTemplateLPTest(BitcoinTestFramework):29def set_test_params(self):30self.num_nodes = 232def run_test(self):33self.log.info("Warning: this test will take about 70 seconds in the best case. Be patient.")34self.log.info("Test that longpollid doesn't change between successive getblocktemplate() invocations if nothing else happens")35self.generate(self.nodes[0], 10)36template = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)37longpollid = template['longpollid']38template2 = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)39assert_equal(template2['longpollid'], longpollid)41self.log.info("Test that longpoll waits if we do nothing")42thr = LongpollThread(self.nodes[0])43with self.nodes[0].assert_debug_log(["ThreadRPCServer method=getblocktemplate"], timeout=3):44thr.start()45# check that thread still lives46thr.join(5) # wait 5 seconds or until thread exits47assert thr.is_alive()49self.miniwallet = MiniWallet(self.nodes[0])50self.log.info("Test that longpoll will terminate if another node generates a block")51self.generate(self.nodes[1], 1) # generate a block on another node52# check that thread will exit now that new transaction entered mempool53thr.join(5) # wait 5 seconds or until thread exits54assert not thr.is_alive()56self.log.info("Test that longpoll will terminate if we generate a block ourselves")57thr = LongpollThread(self.nodes[0])58with self.nodes[0].assert_debug_log(["ThreadRPCServer method=getblocktemplate"], timeout=3):59thr.start()60self.generate(self.nodes[0], 1) # generate a block on own node61thr.join(5) # wait 5 seconds or until thread exits62assert not thr.is_alive()64self.log.info("Test that introducing a new transaction into the mempool will terminate the longpoll")65thr = LongpollThread(self.nodes[0])66with self.nodes[0].assert_debug_log(["ThreadRPCServer method=getblocktemplate"], timeout=3):67thr.start()68# generate a transaction and submit it69self.miniwallet.send_self_transfer(from_node=random.choice(self.nodes))70# after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned71thr.join(60 + 20)72assert not thr.is_alive()74if __name__ == '__main__':75GetBlockTemplateLPTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.