# Path: electrumx/lib/util.py
# class cachedproperty(object):
# 
#     def __init__(self, f):
#         self.f = f
# 
#     def __get__(self, obj, type_):
#         obj = obj or type_
#         value = self.f(obj)
#         setattr(obj, self.f.__name__, value)
#         return value
# 
# def protocol_tuple(s):
#     '''Converts a protocol version number, such as "1.0" to a tuple (1, 0).
# 
#     If the version number is bad, (0, ) indicating version 0 is returned.'''
#     try:
#         return tuple(int(part) for part in s.split('.'))
#     except (TypeError, ValueError, AttributeError):
#         return (0, )
# 
# def version_string(ptuple):
#     '''Convert a version tuple such as (1, 2) to "1.2".
#     There is always at least one dot, so (1, ) becomes "1.0".'''
#     while len(ptuple) < 2:
#         ptuple += (0, )
#     return '.'.join(str(p) for p in ptuple)


from ipaddress import ip_address, IPv4Address, IPv6Address, IPv4Network, IPv6Network
from socket import AF_INET, AF_INET6
from aiorpcx import is_valid_hostname
from electrumx.lib.util import cachedproperty, protocol_tuple, version_string
# Copyright (c) 2017-2021, Neil Booth
#
# All rights reserved.
#
# This file is licensed under the Open BSV License version 3, see LICENCE for details.

'''Representation of a peer server.'''




class Peer(object):

    # Protocol version
    ATTRS = ('host', 'features',
             # metadata
             'source', 'ip_addr',
             'last_good', 'last_try', 'try_count')
    FEATURES = ('pruning', 'server_version', 'protocol_min', 'protocol_max',
                'ssl_port', 'tcp_port')
    # This should be set by the application
    DEFAULT_PORTS = {}

    def __init__(self, host, features, source='unknown', ip_addr=None,
                 last_good=0, last_try=0, try_count=0):
        '''Create a peer given a host name (or IP address as a string),
        a dictionary of features, and a record of the source.'''
        assert isinstance(host, str)
        assert isinstance(features, dict)
        assert host in features.get('hosts', {})

# Path: electrumx/lib/util.py
# class cachedproperty(object):
# 
#     def __init__(self, f):
#         self.f = f
# 
#     def __get__(self, obj, type_):
#         obj = obj or type_
#         value = self.f(obj)
#         setattr(obj, self.f.__name__, value)
#         return value
# 
# def protocol_tuple(s):
#     '''Converts a protocol version number, such as "1.0" to a tuple (1, 0).
# 
#     If the version number is bad, (0, ) indicating version 0 is returned.'''
#     try:
#         return tuple(int(part) for part in s.split('.'))
#     except (TypeError, ValueError, AttributeError):
#         return (0, )
# 
# def version_string(ptuple):
#     '''Convert a version tuple such as (1, 2) to "1.2".
#     There is always at least one dot, so (1, ) becomes "1.0".'''
#     while len(ptuple) < 2:
#         ptuple += (0, )
#     return '.'.join(str(p) for p in ptuple)


# Path: electrumx/lib/peer.py
from ipaddress import ip_address, IPv4Address, IPv6Address, IPv4Network, IPv6Network
from socket import AF_INET, AF_INET6
from aiorpcx import is_valid_hostname
from electrumx.lib.util import cachedproperty, protocol_tuple, version_string

# Copyright (c) 2017-2021, Neil Booth
#
# All rights reserved.
#
# This file is licensed under the Open BSV License version 3, see LICENCE for details.

'''Representation of a peer server.'''




class Peer(object):

    # Protocol version
    ATTRS = ('host', 'features',
             # metadata
             'source', 'ip_addr',
             'last_good', 'last_try', 'try_count')
    FEATURES = ('pruning', 'server_version', 'protocol_min', 'protocol_max',
                'ssl_port', 'tcp_port')
    # This should be set by the application
    DEFAULT_PORTS = {}

    def __init__(self, host, features, source='unknown', ip_addr=None,
                 last_good=0, last_try=0, try_count=0):
        '''Create a peer given a host name (or IP address as a string),
        a dictionary of features, and a record of the source.'''
        assert isinstance(host, str)
        assert isinstance(features, dict)
        assert host in features.get('hosts', {})

        self.host = host