# Path: electrumx/lib/util.py
# class ConnectionLogger(logging.LoggerAdapter):
# class CompactFormatter(logging.Formatter):
# class cachedproperty(object):
# class LogicalFile(object):
#     def process(self, msg, kwargs):
#     def format(self, record):
# def make_logger(name, *, handler, level):
# def class_logger(path, classname):
#     def __init__(self, f):
#     def __get__(self, obj, type_):
# def formatted_time(t, sep=' '):
# def deep_getsizeof(obj):
#     def size(o):
# def subclasses(base_class, strict=True):
#     def select(obj):
# def chunks(items, size):
# def resolve_limit(limit):
# def bytes_to_int(be_bytes):
# def int_to_bytes(value):
# def increment_byte_string(bs):
#     def __init__(self, prefix, digits, file_size):
#     def read(self, start, size=-1):
#     def write(self, start, b):
#     def open_file(self, start, create):
# def open_file(filename, create=False):
# def open_truncate(filename):
# def address_string(address):
# def protocol_tuple(s):
# def version_string(ptuple):
# def protocol_version(client_req, min_tuple, max_tuple):
# def pack_varint(n):
# def pack_varbytes(data):
# 
# Path: electrumx/lib/util.py
# class ConnectionLogger(logging.LoggerAdapter):
# class CompactFormatter(logging.Formatter):
# class cachedproperty(object):
# class LogicalFile(object):
#     def process(self, msg, kwargs):
#     def format(self, record):
# def make_logger(name, *, handler, level):
# def class_logger(path, classname):
#     def __init__(self, f):
#     def __get__(self, obj, type_):
# def formatted_time(t, sep=' '):
# def deep_getsizeof(obj):
#     def size(o):
# def subclasses(base_class, strict=True):
#     def select(obj):
# def chunks(items, size):
# def resolve_limit(limit):
# def bytes_to_int(be_bytes):
# def int_to_bytes(value):
# def increment_byte_string(bs):
#     def __init__(self, prefix, digits, file_size):
#     def read(self, start, size=-1):
#     def write(self, start, b):
#     def open_file(self, start, create):
# def open_file(filename, create=False):
# def open_truncate(filename):
# def address_string(address):
# def protocol_tuple(s):
# def version_string(ptuple):
# def protocol_version(client_req, min_tuple, max_tuple):
# def pack_varint(n):
# def pack_varbytes(data):
# 
# Path: electrumx/lib/hash.py
# def hash_to_hex_str(x):
#     '''Convert a big-endian binary hash to displayed hex string.
# 
#     Display form of a binary hash is reversed and converted to hex.
#     '''
#     return bytes(reversed(x)).hex()
# 
# HASHX_LEN = 11


import array
import ast
import bisect
import time
from collections import defaultdict
from electrumx.lib import util
from electrumx.lib.util import (
    pack_be_uint16, pack_le_uint64, unpack_be_uint16_from, unpack_le_uint64,
)
from electrumx.lib.hash import hash_to_hex_str, HASHX_LEN

    def assert_flushed(self):
        assert not self.unflushed

    def flush(self):
        start_time = time.monotonic()
        self.flush_count += 1
        flush_id = pack_be_uint16(self.flush_count)
        unflushed = self.unflushed

        with self.db.write_batch() as batch:
            for hashX in sorted(unflushed):
                key = hashX + flush_id
                batch.put(key, bytes(unflushed[hashX]))
            self.write_state(batch)

        count = len(unflushed)
        unflushed.clear()
        self.unflushed_count = 0

        if self.db.for_sync:
            elapsed = time.monotonic() - start_time
            self.logger.info(f'flushed history in {elapsed:.1f}s '
                             f'for {count:,d} addrs')

    def backup(self, hashXs, tx_count):
        # Not certain this is needed, but it doesn't hurt
        self.flush_count += 1
        nremoves = 0
        bisect_left = bisect.bisect_left

# Path: electrumx/lib/util.py
# class ConnectionLogger(logging.LoggerAdapter):
# class CompactFormatter(logging.Formatter):
# class cachedproperty(object):
# class LogicalFile(object):
#     def process(self, msg, kwargs):
#     def format(self, record):
# def make_logger(name, *, handler, level):
# def class_logger(path, classname):
#     def __init__(self, f):
#     def __get__(self, obj, type_):
# def formatted_time(t, sep=' '):
# def deep_getsizeof(obj):
#     def size(o):
# def subclasses(base_class, strict=True):
#     def select(obj):
# def chunks(items, size):
# def resolve_limit(limit):
# def bytes_to_int(be_bytes):
# def int_to_bytes(value):
# def increment_byte_string(bs):
#     def __init__(self, prefix, digits, file_size):
#     def read(self, start, size=-1):
#     def write(self, start, b):
#     def open_file(self, start, create):
# def open_file(filename, create=False):
# def open_truncate(filename):
# def address_string(address):
# def protocol_tuple(s):
# def version_string(ptuple):
# def protocol_version(client_req, min_tuple, max_tuple):
# def pack_varint(n):
# def pack_varbytes(data):
# 
# Path: electrumx/lib/util.py
# class ConnectionLogger(logging.LoggerAdapter):
# class CompactFormatter(logging.Formatter):
# class cachedproperty(object):
# class LogicalFile(object):
#     def process(self, msg, kwargs):
#     def format(self, record):
# def make_logger(name, *, handler, level):
# def class_logger(path, classname):
#     def __init__(self, f):
#     def __get__(self, obj, type_):
# def formatted_time(t, sep=' '):
# def deep_getsizeof(obj):
#     def size(o):
# def subclasses(base_class, strict=True):
#     def select(obj):
# def chunks(items, size):
# def resolve_limit(limit):
# def bytes_to_int(be_bytes):
# def int_to_bytes(value):
# def increment_byte_string(bs):
#     def __init__(self, prefix, digits, file_size):
#     def read(self, start, size=-1):
#     def write(self, start, b):
#     def open_file(self, start, create):
# def open_file(filename, create=False):
# def open_truncate(filename):
# def address_string(address):
# def protocol_tuple(s):
# def version_string(ptuple):
# def protocol_version(client_req, min_tuple, max_tuple):
# def pack_varint(n):
# def pack_varbytes(data):
# 
# Path: electrumx/lib/hash.py
# def hash_to_hex_str(x):
#     '''Convert a big-endian binary hash to displayed hex string.
# 
#     Display form of a binary hash is reversed and converted to hex.
#     '''
#     return bytes(reversed(x)).hex()
# 
# HASHX_LEN = 11


# Path: electrumx/server/history.py
import array
import ast
import bisect
import time
from collections import defaultdict
from electrumx.lib import util
from electrumx.lib.util import (
    pack_be_uint16, pack_le_uint64, unpack_be_uint16_from, unpack_le_uint64,
)
from electrumx.lib.hash import hash_to_hex_str, HASHX_LEN


    def assert_flushed(self):
        assert not self.unflushed

    def flush(self):
        start_time = time.monotonic()
        self.flush_count += 1
        flush_id = pack_be_uint16(self.flush_count)
        unflushed = self.unflushed

        with self.db.write_batch() as batch:
            for hashX in sorted(unflushed):
                key = hashX + flush_id
                batch.put(key, bytes(unflushed[hashX]))
            self.write_state(batch)

        count = len(unflushed)
        unflushed.clear()
        self.unflushed_count = 0

        if self.db.for_sync:
            elapsed = time.monotonic() - start_time
            self.logger.info(f'flushed history in {elapsed:.1f}s '
                             f'for {count:,d} addrs')

    def backup(self, hashXs, tx_count):
        # Not certain this is needed, but it doesn't hurt
        self.flush_count += 1
        nremoves = 0
        bisect_left = bisect.bisect_left

        chunks = util.chunks