# Path: bitcoinlib/encoding.py
# def to_hexstring(string):
#     """
#     Convert bytes, string to a hexadecimal string. Use instead of built-in hex() method if format
#     of input string is not known.
# 
#     >>> to_hexstring(b'\\x12\\xaa\\xdd')
#     '12aadd'
# 
#     :param string: Variable to convert to hex string
#     :type string: bytes, str
# 
#     :return: hexstring
#     """
#     if not string:
#         return ''
#     try:
#         bytes.fromhex(string)
#         return string
#     except (ValueError, TypeError):
#         pass
# 
#     if not isinstance(string, bytes):
#         string = bytes(string, 'utf8')
#     return string.hex()
# 
# Path: tests/test_custom.py
# class CustomAssertions:
# 
#     def assertDictEqualExt(self, result_dict, expected_dict, none_allowed=None):
#         """
#         Compare dictionaries, skip items not found in expected dictionary.
# 
#         Lists and recursion's in dictionaries are allowed.
# 
#         :param result_dict: First dictionary with results
#         :type result_dict: dict
#         :param expected_dict: Second dictionary with expected values
#         :type expected_dict: dict
#         :param none_allowed: List of fields for which None value in result_dict is allowed
#         :type none_allowed: list
# 
#         :return bool: Dictionaries are identical?
#         """
#         if none_allowed is None:
#             none_allowed = []
#         if not isinstance(expected_dict, dict):
#             if expected_dict == result_dict:
#                 return True
#             else:
#                 raise AssertionError("Different value for %s != %s" % (result_dict, expected_dict))
#         expected_keys = expected_dict.keys()
#         for k in result_dict:
#             if k not in expected_keys:
#                 continue
#             if isinstance(result_dict[k], dict):
#                 self.assertDictEqualExt(result_dict[k], expected_dict[k], none_allowed)
#             elif isinstance(result_dict[k], list):
#                 for i in range(len(result_dict[k])):
#                     if not isinstance(expected_dict[k], list):
#                         raise AssertionError("No list expected for %s attribute, expected '%s' but received: '%s'" %
#                                              (k, expected_dict[k], result_dict[k]))
#                     self.assertDictEqualExt(result_dict[k][i], expected_dict[k][i], none_allowed)
#             elif result_dict[k] != expected_dict[k]:
#                 if isinstance(result_dict[k], datetime):
#                     if result_dict[k].date() == expected_dict[k].date():
#                         continue
#                 if result_dict[k] is not None or k not in none_allowed:
#                     raise AssertionError("Different value for '%s': %s != %s" % (k, result_dict[k], expected_dict[k]))


import unittest
import logging
    import mysql.connector
    import psycopg2
    from parameterized import parameterized_class
    from psycopg2 import sql
    from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from bitcoinlib.services.services import *
from bitcoinlib.encoding import to_hexstring
from tests.test_custom import CustomAssertions
            # Get transaction from cache
            t = srv.gettransaction('656333aa98d6207de1eb59e029850b649935e6ef2e5f90bc8834a13333b90963')
            self.assertGreaterEqual(srv.results_cache_n, 1)
            self.assertEqual(t.raw_hex(), rawtx)

    def test_service_cache_with_latest_tx_query(self):
        srv = ServiceTest(cache_uri=DATABASEFILE_CACHE_UNITTESTS2)
        address = 'bc1qxfrgfhs49d7dtcfzlhp7f7cwsp8zpp60hywp0f'
        after_txid = '13401ad121c8ae91e18b4bb0db5d8f350a2b0b5ddd5ca26165137bf07fefad90'
        srv.gettransaction('4156e78f347e47d2ccdd4a19614d958c6e4502d09a68f63ed0c72691f63a5028')
        txs = srv.gettransactions(address, limit=5)
        self.assertGreaterEqual(len(txs), 5)
        txs = srv.gettransactions(address, after_txid=after_txid, limit=5)
        self.assertGreaterEqual(len(txs), 5)

    def test_service_cache_correctly_update_spent_info(self):
        srv = ServiceTest(cache_uri=DATABASEFILE_CACHE_UNITTESTS2, exclude_providers=['chainso'])
        srv.gettransactions('1KoAvaL3wfpcNvGCQYkqFJG9Ccqm52sZHa', limit=1)
        txs = srv.gettransactions('1KoAvaL3wfpcNvGCQYkqFJG9Ccqm52sZHa')
        self.assertTrue(txs[0].outputs[0].spent)

    def test_service_cache_getblock_hash(self):

        def check_block_128594(b):
            self.assertEqual(b.height, 128594)
            self.assertEqual(to_hexstring(b.block_hash),
                             '0000000000001a7dcac3c01bf10c5d5fe53dc8cc4b9c94001662e9d7bd36f6cc')
            self.assertEqual(to_hexstring(b.merkle_root),
                             '36cbe8252102410779271e8e325183f63ed9c18534ebc13ef4220f57ae2a9c17')
            self.assertEqual(b.nonce_int, 423727070)

# Path: bitcoinlib/encoding.py
# def to_hexstring(string):
#     """
#     Convert bytes, string to a hexadecimal string. Use instead of built-in hex() method if format
#     of input string is not known.
# 
#     >>> to_hexstring(b'\\x12\\xaa\\xdd')
#     '12aadd'
# 
#     :param string: Variable to convert to hex string
#     :type string: bytes, str
# 
#     :return: hexstring
#     """
#     if not string:
#         return ''
#     try:
#         bytes.fromhex(string)
#         return string
#     except (ValueError, TypeError):
#         pass
# 
#     if not isinstance(string, bytes):
#         string = bytes(string, 'utf8')
#     return string.hex()
# 
# Path: tests/test_custom.py
# class CustomAssertions:
# 
#     def assertDictEqualExt(self, result_dict, expected_dict, none_allowed=None):
#         """
#         Compare dictionaries, skip items not found in expected dictionary.
# 
#         Lists and recursion's in dictionaries are allowed.
# 
#         :param result_dict: First dictionary with results
#         :type result_dict: dict
#         :param expected_dict: Second dictionary with expected values
#         :type expected_dict: dict
#         :param none_allowed: List of fields for which None value in result_dict is allowed
#         :type none_allowed: list
# 
#         :return bool: Dictionaries are identical?
#         """
#         if none_allowed is None:
#             none_allowed = []
#         if not isinstance(expected_dict, dict):
#             if expected_dict == result_dict:
#                 return True
#             else:
#                 raise AssertionError("Different value for %s != %s" % (result_dict, expected_dict))
#         expected_keys = expected_dict.keys()
#         for k in result_dict:
#             if k not in expected_keys:
#                 continue
#             if isinstance(result_dict[k], dict):
#                 self.assertDictEqualExt(result_dict[k], expected_dict[k], none_allowed)
#             elif isinstance(result_dict[k], list):
#                 for i in range(len(result_dict[k])):
#                     if not isinstance(expected_dict[k], list):
#                         raise AssertionError("No list expected for %s attribute, expected '%s' but received: '%s'" %
#                                              (k, expected_dict[k], result_dict[k]))
#                     self.assertDictEqualExt(result_dict[k][i], expected_dict[k][i], none_allowed)
#             elif result_dict[k] != expected_dict[k]:
#                 if isinstance(result_dict[k], datetime):
#                     if result_dict[k].date() == expected_dict[k].date():
#                         continue
#                 if result_dict[k] is not None or k not in none_allowed:
#                     raise AssertionError("Different value for '%s': %s != %s" % (k, result_dict[k], expected_dict[k]))


# Path: tests/test_services.py
import unittest
import logging
    import mysql.connector
    import psycopg2
    from parameterized import parameterized_class
    from psycopg2 import sql
    from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from bitcoinlib.services.services import *
from bitcoinlib.encoding import to_hexstring
from tests.test_custom import CustomAssertions

            # Get transaction from cache
            t = srv.gettransaction('656333aa98d6207de1eb59e029850b649935e6ef2e5f90bc8834a13333b90963')
            self.assertGreaterEqual(srv.results_cache_n, 1)
            self.assertEqual(t.raw_hex(), rawtx)

    def test_service_cache_with_latest_tx_query(self):
        srv = ServiceTest(cache_uri=DATABASEFILE_CACHE_UNITTESTS2)
        address = 'bc1qxfrgfhs49d7dtcfzlhp7f7cwsp8zpp60hywp0f'
        after_txid = '13401ad121c8ae91e18b4bb0db5d8f350a2b0b5ddd5ca26165137bf07fefad90'
        srv.gettransaction('4156e78f347e47d2ccdd4a19614d958c6e4502d09a68f63ed0c72691f63a5028')
        txs = srv.gettransactions(address, limit=5)
        self.assertGreaterEqual(len(txs), 5)
        txs = srv.gettransactions(address, after_txid=after_txid, limit=5)
        self.assertGreaterEqual(len(txs), 5)

    def test_service_cache_correctly_update_spent_info(self):
        srv = ServiceTest(cache_uri=DATABASEFILE_CACHE_UNITTESTS2, exclude_providers=['chainso'])
        srv.gettransactions('1KoAvaL3wfpcNvGCQYkqFJG9Ccqm52sZHa', limit=1)
        txs = srv.gettransactions('1KoAvaL3wfpcNvGCQYkqFJG9Ccqm52sZHa')
        self.assertTrue(txs[0].outputs[0].spent)

    def test_service_cache_getblock_hash(self):

        def check_block_128594(b):
            self.assertEqual(b.height, 128594)
            self.assertEqual(to_hexstring(b.block_hash),
                             '0000000000001a7dcac3c01bf10c5d5fe53dc8cc4b9c94001662e9d7bd36f6cc')
            self.assertEqual(to_hexstring(b.merkle_root),
                             '36cbe8252102410779271e8e325183f63ed9c18534ebc13ef4220f57ae2a9c17')
            self.assertEqual(b.nonce_int, 423727070)

            self.assertEqual(to_hexstring(b.prev_block),