/ test / functional / wallet_coinbase_category.py
wallet_coinbase_category.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2014-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 coinbase transactions return the correct categories.
 6  
 7  Tests listtransactions, listsinceblock, and gettransaction.
 8  """
 9  
10  from test_framework.test_framework import BitcoinTestFramework
11  from test_framework.util import (
12      assert_array_result
13  )
14  
15  class CoinbaseCategoryTest(BitcoinTestFramework):
16      def add_options(self, parser):
17          self.add_wallet_options(parser)
18  
19      def set_test_params(self):
20          self.num_nodes = 1
21          self.setup_clean_chain = True
22  
23      def skip_test_if_missing_module(self):
24          self.skip_if_no_wallet()
25  
26      def assert_category(self, category, address, txid, skip):
27          assert_array_result(self.nodes[0].listtransactions(skip=skip),
28                              {"address": address},
29                              {"category": category})
30          assert_array_result(self.nodes[0].listsinceblock()["transactions"],
31                              {"address": address},
32                              {"category": category})
33          assert_array_result(self.nodes[0].gettransaction(txid)["details"],
34                              {"address": address},
35                              {"category": category})
36  
37      def run_test(self):
38          # Generate one block to an address
39          address = self.nodes[0].getnewaddress()
40          self.generatetoaddress(self.nodes[0], 1, address)
41          hash = self.nodes[0].getbestblockhash()
42          txid = self.nodes[0].getblock(hash)["tx"][0]
43  
44          # Coinbase transaction is immature after 1 confirmation
45          self.assert_category("immature", address, txid, 0)
46  
47          # Mine another 99 blocks on top
48          self.generate(self.nodes[0], 99)
49          # Coinbase transaction is still immature after 100 confirmations
50          self.assert_category("immature", address, txid, 99)
51  
52          # Mine one more block
53          self.generate(self.nodes[0], 1)
54          # Coinbase transaction is now matured, so category is "generate"
55          self.assert_category("generate", address, txid, 100)
56  
57          # Orphan block that paid to address
58          self.nodes[0].invalidateblock(hash)
59          # Coinbase transaction is now orphaned
60          self.assert_category("orphan", address, txid, 100)
61  
62  if __name__ == '__main__':
63      CoinbaseCategoryTest().main()