/ test / functional / rpc_dumptxoutset.py
rpc_dumptxoutset.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2019-2022 The Bitcoin Core developers
 3  # Distributed under the MIT software license, see the accompanying
 4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
 5  """Test the generation of UTXO snapshots using `dumptxoutset`.
 6  """
 7  
 8  from test_framework.blocktools import COINBASE_MATURITY
 9  from test_framework.test_framework import BitcoinTestFramework
10  from test_framework.util import (
11      assert_equal,
12      assert_raises_rpc_error,
13      sha256sum_file,
14  )
15  
16  
17  class DumptxoutsetTest(BitcoinTestFramework):
18      def set_test_params(self):
19          self.setup_clean_chain = True
20          self.num_nodes = 1
21  
22      def run_test(self):
23          """Test a trivial usage of the dumptxoutset RPC command."""
24          node = self.nodes[0]
25          mocktime = node.getblockheader(node.getblockhash(0))['time'] + 1
26          node.setmocktime(mocktime)
27          self.generate(node, COINBASE_MATURITY)
28  
29          FILENAME = 'txoutset.dat'
30          out = node.dumptxoutset(FILENAME)
31          expected_path = node.datadir_path / self.chain / FILENAME
32  
33          assert expected_path.is_file()
34  
35          assert_equal(out['coins_written'], 100)
36          assert_equal(out['base_height'], 100)
37          assert_equal(out['path'], str(expected_path))
38          # Blockhash should be deterministic based on mocked time.
39          assert_equal(
40              out['base_hash'],
41              '09abf0e7b510f61ca6cf33bab104e9ee99b3528b371d27a2d4b39abb800fba7e')
42  
43          # UTXO snapshot hash should be deterministic based on mocked time.
44          assert_equal(
45              sha256sum_file(str(expected_path)).hex(),
46              'b1bacb602eacf5fbc9a7c2ef6eeb0d229c04e98bdf0c2ea5929012cd0eae3830')
47  
48          assert_equal(
49              out['txoutset_hash'], 'a0b7baa3bf5ccbd3279728f230d7ca0c44a76e9923fca8f32dbfd08d65ea496a')
50          assert_equal(out['nchaintx'], 101)
51  
52          # Specifying a path to an existing or invalid file will fail.
53          assert_raises_rpc_error(
54              -8, '{} already exists'.format(FILENAME),  node.dumptxoutset, FILENAME)
55          invalid_path = node.datadir_path / "invalid" / "path"
56          assert_raises_rpc_error(
57              -8, "Couldn't open file {}.incomplete for writing".format(invalid_path), node.dumptxoutset, invalid_path)
58  
59  
60  if __name__ == '__main__':
61      DumptxoutsetTest().main()