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) 2019-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 generation of UTXO snapshots using `dumptxoutset`.6"""8from test_framework.blocktools import COINBASE_MATURITY9from test_framework.test_framework import BitcoinTestFramework10from test_framework.util import (11assert_equal,12assert_raises_rpc_error,13sha256sum_file,14)17class DumptxoutsetTest(BitcoinTestFramework):18def set_test_params(self):19self.setup_clean_chain = True20self.num_nodes = 122def test_dumptxoutset_with_fork(self):23node = self.nodes[0]24tip = node.getbestblockhash()25target_height = node.getblockcount() - 1026target_hash = node.getblockhash(target_height)28# Create a fork of two blocks at the target height29invalid_block = node.getblockhash(target_height + 1)30node.invalidateblock(invalid_block)31# Reset mocktime to not regenerate the same blockhash32node.setmocktime(0)33self.generate(node, 2)35# Move back on to actual main chain36node.reconsiderblock(invalid_block)37self.wait_until(lambda: node.getbestblockhash() == tip)39# Use dumptxoutset at the forked height40out = node.dumptxoutset("txoutset_fork.dat", "rollback", {"rollback": target_height})42# Verify the snapshot was created at the target height and not the fork tip43assert_equal(out['base_height'], target_height)44assert_equal(out['base_hash'], target_hash)46# Cover the same case as above with an in-memory database47out_mem = node.dumptxoutset("txoutset_fork_mem.dat", "rollback", {"rollback": target_height, "in_memory": True})48assert_equal(out_mem['base_height'], target_height)49assert_equal(out_mem['base_hash'], target_hash)52def run_test(self):53"""Test a trivial usage of the dumptxoutset RPC command."""54node = self.nodes[0]55mocktime = node.getblockheader(node.getblockhash(0))['time'] + 156node.setmocktime(mocktime)57self.generate(node, COINBASE_MATURITY)59FILENAME = 'txoutset.dat'60out = node.dumptxoutset(FILENAME, "latest")61expected_path = node.chain_path / FILENAME63assert expected_path.is_file()65assert_equal(out['coins_written'], 100)66assert_equal(out['base_height'], 100)67assert_equal(out['path'], str(expected_path))68# Blockhash should be deterministic based on mocked time.69assert_equal(70out['base_hash'],71'220aee93f0f5409631f35488898258f0930952bd620063cb4d7d87f7c28a8f50')73# UTXO snapshot hash should be deterministic based on mocked time.74assert_equal(75sha256sum_file(str(expected_path)).hex(),76'e8c59b1bc1f19061c67eb7a392f4ea17eea83af58646ea2909e270546699c36c')78assert_equal(79out['txoutset_hash'], '771d773b5c27b6f35f598ce764652a2cf28fbc268341eb1827844e416c629c7d')80assert_equal(out['nchaintx'], 101)82# Specifying a path to an existing or invalid file will fail.83assert_raises_rpc_error(84-8, '{} already exists'.format(FILENAME), node.dumptxoutset, FILENAME, "latest")85invalid_path = node.datadir_path / "invalid" / "path"86assert_raises_rpc_error(87-8, "Couldn't open file {}.incomplete for writing".format(invalid_path), node.dumptxoutset, invalid_path, "latest")89self.log.info("Test that dumptxoutset with unknown dump type fails")90assert_raises_rpc_error(91-8, 'Invalid snapshot type "bogus" specified. Please specify "rollback" or "latest"', node.dumptxoutset, 'utxos.dat', "bogus")93self.log.info("Testing dumptxoutset with chain fork at target height")94self.test_dumptxoutset_with_fork()97if __name__ == '__main__':98DumptxoutsetTest(__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.