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 the invalidateblock RPC."""7from test_framework.test_framework import BitcoinTestFramework8from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR9from test_framework.blocktools import (10create_block,11)12from test_framework.util import (13assert_equal,14assert_raises_rpc_error,15)18class InvalidateTest(BitcoinTestFramework):19def set_test_params(self):20self.setup_clean_chain = True21self.num_nodes = 323def setup_network(self):24self.setup_nodes()26def run_test(self):27self.log.info("Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:")28self.log.info("Mine 4 blocks on Node 0")29self.generate(self.nodes[0], 4, sync_fun=self.no_op)30assert_equal(self.nodes[0].getblockcount(), 4)31besthash_n0 = self.nodes[0].getbestblockhash()33self.log.info("Mine competing 6 blocks on Node 1")34self.generate(self.nodes[1], 6, sync_fun=self.no_op)35assert_equal(self.nodes[1].getblockcount(), 6)37self.log.info("Connect nodes to force a reorg")38self.connect_nodes(0, 1)39self.sync_blocks(self.nodes[0:2])40assert_equal(self.nodes[0].getblockcount(), 6)42# Add a header to the tip of node 0 without submitting the block. This shouldn't43# affect results since this chain will be invalidated next.44tip = self.nodes[0].getbestblockhash()45block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] + 146block = create_block(int(tip, 16), height=self.nodes[0].getblockcount(), ntime=block_time, version=4)47block.solve()48self.nodes[0].submitheader(block.serialize().hex())49assert_equal(self.nodes[0].getblockchaininfo()["headers"], self.nodes[0].getblockchaininfo()["blocks"] + 1)51self.log.info("Invalidate block 2 on node 0 and verify we reorg to node 0's original chain")52badhash = self.nodes[1].getblockhash(2)53self.nodes[0].invalidateblock(badhash)54assert_equal(self.nodes[0].getblockcount(), 4)55assert_equal(self.nodes[0].getbestblockhash(), besthash_n0)56# Should report consistent blockchain info57assert_equal(self.nodes[0].getblockchaininfo()["headers"], self.nodes[0].getblockchaininfo()["blocks"])59self.log.info("Reconsider block 6 on node 0 again and verify that the best header is set correctly")60self.nodes[0].reconsiderblock(tip)61assert_equal(self.nodes[0].getblockchaininfo()["headers"], self.nodes[0].getblockchaininfo()["blocks"] + 1)63self.log.info("Invalidate block 2 on node 0 and verify we reorg to node 0's original chain again")64self.nodes[0].invalidateblock(badhash)65assert_equal(self.nodes[0].getblockcount(), 4)66assert_equal(self.nodes[0].getbestblockhash(), besthash_n0)67assert_equal(self.nodes[0].getblockchaininfo()["headers"], self.nodes[0].getblockchaininfo()["blocks"])69self.log.info("Make sure we won't reorg to a lower work chain:")70self.connect_nodes(1, 2)71self.log.info("Sync node 2 to node 1 so both have 6 blocks")72self.sync_blocks(self.nodes[1:3])73assert_equal(self.nodes[2].getblockcount(), 6)74self.log.info("Invalidate block 5 on node 1 so its tip is now at 4")75self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5))76assert_equal(self.nodes[1].getblockcount(), 4)77self.log.info("Invalidate block 3 on node 2, so its tip is now 2")78self.nodes[2].invalidateblock(self.nodes[2].getblockhash(3))79assert_equal(self.nodes[2].getblockcount(), 2)80self.log.info("..and then mine a block")81self.generate(self.nodes[2], 1, sync_fun=self.no_op)82self.log.info("Verify all nodes are at the right height")83self.wait_until(lambda: self.nodes[2].getblockcount() == 3, timeout=5)84self.wait_until(lambda: self.nodes[0].getblockcount() == 4, timeout=5)85self.wait_until(lambda: self.nodes[1].getblockcount() == 4, timeout=5)87self.log.info("Verify that ancestors can become chain tip candidates when we reconsider blocks")88# Invalidate node0's current chain (1' -> 2' -> 3' -> 4') so that we don't reorg back to it in this test89badhash = self.nodes[0].getblockhash(1)90self.nodes[0].invalidateblock(badhash)91# Reconsider the tip so that node0's chain becomes this chain again : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> header 792self.nodes[0].reconsiderblock(tip)93blockhash_3 = self.nodes[0].getblockhash(3)94blockhash_4 = self.nodes[0].getblockhash(4)95blockhash_6 = self.nodes[0].getblockhash(6)96assert_equal(self.nodes[0].getbestblockhash(), blockhash_6)98# Invalidate block 4 so that chain becomes : 1 -> 2 -> 399self.nodes[0].invalidateblock(blockhash_4)100assert_equal(self.nodes[0].getbestblockhash(), blockhash_3)101assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 3)102assert_equal(self.nodes[0].getblockchaininfo()['headers'], 3)104# Reconsider the header105self.nodes[0].reconsiderblock(block.hash_hex)106# Since header doesn't have block data, it can't be chain tip107# Check if it's possible for an ancestor (with block data) to be the chain tip108assert_equal(self.nodes[0].getbestblockhash(), blockhash_6)109assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 6)110assert_equal(self.nodes[0].getblockchaininfo()['headers'], 7)112self.log.info("Verify that we reconsider all ancestors as well")113blocks = self.generatetodescriptor(self.nodes[1], 10, ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR, sync_fun=self.no_op)114assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])115# Invalidate the two blocks at the tip116self.nodes[1].invalidateblock(blocks[-1])117self.nodes[1].invalidateblock(blocks[-2])118assert_equal(self.nodes[1].getbestblockhash(), blocks[-3])119# Reconsider only the previous tip120self.nodes[1].reconsiderblock(blocks[-1])121# Should be back at the tip by now122assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])124self.log.info("Verify that we reconsider all descendants")125blocks = self.generatetodescriptor(self.nodes[1], 10, ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR, sync_fun=self.no_op)126assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])127# Invalidate the two blocks at the tip128self.nodes[1].invalidateblock(blocks[-2])129self.nodes[1].invalidateblock(blocks[-4])130assert_equal(self.nodes[1].getbestblockhash(), blocks[-5])131# Reconsider only the previous tip132self.nodes[1].reconsiderblock(blocks[-4])133# Should be back at the tip by now134assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])135# Should report consistent blockchain info136assert_equal(self.nodes[1].getblockchaininfo()["headers"], self.nodes[1].getblockchaininfo()["blocks"])138self.log.info("Verify that invalidating an unknown block throws an error")139assert_raises_rpc_error(-5, "Block not found", self.nodes[1].invalidateblock, "00" * 32)140assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])143if __name__ == '__main__':144InvalidateTest(__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.