Repository: bitcoin/bitcoin
File: test/functional/rpc_blockchain.py (Python)

Code before:

        # hash_type none should not return a UTXO set hash.
        res5 = node.gettxoutsetinfo(hash_type='none')
        assert 'hash_serialized_3' not in res5

        # hash_type muhash should return a different UTXO set hash.
        res6 = node.gettxoutsetinfo(hash_type='muhash')
        assert 'muhash' in res6
        assert res['hash_serialized_3'] != res6['muhash']

        # muhash should not be returned unless requested.
        for r in [res, res2, res3, res4, res5]:
            assert 'muhash' not in r

        # Unknown hash_type raises an error
        assert_raises_rpc_error(-8, "'foo hash' is not a valid hash_type", node.gettxoutsetinfo, "foo hash")

    def _test_getblockheader(self):
        self.log.info("Test getblockheader")
        node = self.nodes[0]

        assert_raises_rpc_error(-8, "hash must be of length 64 (not 8, for 'nonsense')", node.getblockheader, "nonsense")
        assert_raises_rpc_error(-8, "hash must be hexadecimal string (not 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844')", node.getblockheader, "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
        assert_raises_rpc_error(-5, "Block not found", node.getblockheader, "0cf7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")

        besthash = node.getbestblockhash()
        secondbesthash = node.getblockhash(HEIGHT - 1)
        header = node.getblockheader(blockhash=besthash)

        assert_equal(header['hash'], besthash)
        assert_equal(header['height'], HEIGHT)
        assert_equal(header['confirmations'], 1)
        assert_equal(header['previousblockhash'], secondbesthash)
        assert_is_hex_string(header['chainwork'])
        assert_equal(header['nTx'], 1)
        assert_is_hash_string(header['hash'])
        assert_is_hash_string(header['previousblockhash'])
        assert_is_hash_string(header['merkleroot'])
        assert_equal(header['bits'], nbits_str(REGTEST_N_BITS))
        assert_equal(header['target'], target_str(REGTEST_TARGET))
        assert isinstance(header['time'], int)
        assert_equal(header['mediantime'], TIME_RANGE_MTP)
        assert isinstance(header['nonce'], int)
        assert isinstance(header['version'], int)
        assert isinstance(int(header['versionHex'], 16), int)
        assert isinstance(header['difficulty'], Decimal)

        # Test with verbose=False, which should return the header as hex.
        header_hex = node.getblockheader(blockhash=besthash, verbose=False)
        assert_is_hex_string(header_hex)


Review comment (suggestion):
Should we use `COIN` here instead of `100000000`

found in `bitcoin/test/functional/test_framework/messages.py`

Code after:
        del res4['disk_size']
        assert_equal(res, res4)

        # hash_type none should not return a UTXO set hash.
        res5 = node.gettxoutsetinfo(hash_type='none')
        assert 'hash_serialized_3' not in res5

        # hash_type muhash should return a different UTXO set hash.
        res6 = node.gettxoutsetinfo(hash_type='muhash')
        assert 'muhash' in res6
        assert res['hash_serialized_3'] != res6['muhash']

        # muhash should not be returned unless requested.
        for r in [res, res2, res3, res4, res5]:
            assert 'muhash' not in r

        # Unknown hash_type raises an error
        assert_raises_rpc_error(-8, "'foo hash' is not a valid hash_type", node.gettxoutsetinfo, "foo hash")

    def _test_gettxout(self):
        self.log.info("Validating gettxout RPC response")
        node = self.nodes[0]

        # Get the best block hash and the block, which
        # should only include the coinbase transaction.
        best_block_hash = node.getbestblockhash()
        block = node.getblock(best_block_hash)
        assert_equal(block['nTx'], 1)

        # Get the transaction ID of the coinbase tx and
        # the transaction output.
        txid = block['tx'][0]
        txout = node.gettxout(txid, 0)

        # Validate the gettxout response
        assert_equal(txout['bestblock'], best_block_hash)
        assert_equal(txout['confirmations'], 1)
        assert_equal(txout['value'], 25)
        assert_equal(txout['scriptPubKey']['address'], self.wallet.get_address())
        assert_equal(txout['scriptPubKey']['hex'], self.wallet.get_output_script().hex())
        decoded_script = node.decodescript(self.wallet.get_output_script().hex())
        assert_equal(txout['scriptPubKey']['asm'], decoded_script['asm'])
        assert_equal(txout['scriptPubKey']['desc'], decoded_script['desc'])
        assert_equal(txout['scriptPubKey']['type'], decoded_script['type'])
        assert_equal(txout['coinbase'], True)

    def _test_getblockheader(self):
        self.log.info("Test getblockheader")
        node = self.nodes[0]

        assert_raises_rpc_error(-8, "hash must be of length 64 (not 8, for 'nonsense')", node.getblockheader, "nonsense")