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