# 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
    #     self.assertGreaterEqual(srv.getbalance(address), 50000000000)
    #     self.assertEqual(srv.getutxos(address)[0]['txid'], txid)
    #     self.assertEqual(srv.gettransactions(address)[0].txid, txid)

    def test_service_getblock_id(self):
        srv = ServiceTest(min_providers=3, timeout=TIMEOUT_TEST, cache_uri='')
        srv.getblock('0000000000000a3290f20e75860d505ce0e948a1d1d846bec7e39015d242884b', parse_transactions=False)
        for provider in srv.results:
            blk = srv.results[provider]
            if blk['bits']:
                self.assertEqual(blk['bits'], 436956491)
            self.assertEqual(blk['height'], 150000)
            self.assertEqual(blk['merkle_root'], 'be0b136f2f3db38d4f55f1963f0acac506d637b3c27a4c42f3504836a4ec52b1')
            if blk['nonce']:
                self.assertEqual(blk['nonce'], 1796110725)
            if blk['prev_block']:
                self.assertEqual(blk['prev_block'], '00000000000008df4269884f1d3bfc2aed3ea747292abb89be3dc3faa8c5d26f')
            self.assertEqual(blk['time'], 1319118291)
            self.assertEqual(blk['tx_count'], 10)
            self.assertEqual(len(blk['txs']), 10)
            if blk['version']:
                self.assertEqual(blk['version'], 1)
            self.assertEqual(blk['txs'][9],
                             '13e3167d46334600b59a5aa286dd02147ac33e64bfc2e188e1f0c0a442182584')

    def test_service_getblock_height(self):
        srv = ServiceTest(timeout=TIMEOUT_TEST, exclude_providers=['chainso'], cache_uri='')
        b = srv.getblock(599999, parse_transactions=True, limit=3)
        print("Test getblock using provider %s" % list(srv.results.keys())[0])
        self.assertEqual(b.height, 599999)

# 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

    #     self.assertGreaterEqual(srv.getbalance(address), 50000000000)
    #     self.assertEqual(srv.getutxos(address)[0]['txid'], txid)
    #     self.assertEqual(srv.gettransactions(address)[0].txid, txid)

    def test_service_getblock_id(self):
        srv = ServiceTest(min_providers=3, timeout=TIMEOUT_TEST, cache_uri='')
        srv.getblock('0000000000000a3290f20e75860d505ce0e948a1d1d846bec7e39015d242884b', parse_transactions=False)
        for provider in srv.results:
            blk = srv.results[provider]
            if blk['bits']:
                self.assertEqual(blk['bits'], 436956491)
            self.assertEqual(blk['height'], 150000)
            self.assertEqual(blk['merkle_root'], 'be0b136f2f3db38d4f55f1963f0acac506d637b3c27a4c42f3504836a4ec52b1')
            if blk['nonce']:
                self.assertEqual(blk['nonce'], 1796110725)
            if blk['prev_block']:
                self.assertEqual(blk['prev_block'], '00000000000008df4269884f1d3bfc2aed3ea747292abb89be3dc3faa8c5d26f')
            self.assertEqual(blk['time'], 1319118291)
            self.assertEqual(blk['tx_count'], 10)
            self.assertEqual(len(blk['txs']), 10)
            if blk['version']:
                self.assertEqual(blk['version'], 1)
            self.assertEqual(blk['txs'][9],
                             '13e3167d46334600b59a5aa286dd02147ac33e64bfc2e188e1f0c0a442182584')

    def test_service_getblock_height(self):
        srv = ServiceTest(timeout=TIMEOUT_TEST, exclude_providers=['chainso'], cache_uri='')
        b = srv.getblock(599999, parse_transactions=True, limit=3)
        print("Test getblock using provider %s" % list(srv.results.keys())[0])
        self.assertEqual(b.height, 599999)

        self.assertEqual(to_hexstring(b.block_hash), '00000000000000000003ecd827f336c6971f6f77a0b9fba362398dd867975645')