2018-10-21 04:55:15 +08:00
|
|
|
from abc import ABC, abstractmethod
|
2019-08-01 06:00:12 +08:00
|
|
|
from typing import List, Sequence
|
2019-07-27 16:27:01 +08:00
|
|
|
|
|
|
|
from multiaddr import Multiaddr
|
|
|
|
|
|
|
|
from .id import ID
|
2018-10-21 04:55:15 +08:00
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2018-10-21 04:55:15 +08:00
|
|
|
class IAddrBook(ABC):
|
2019-07-27 16:27:01 +08:00
|
|
|
def __init__(self) -> None:
|
2018-10-27 03:51:25 +08:00
|
|
|
pass
|
2018-10-21 04:55:15 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
@abstractmethod
|
2019-07-27 16:27:01 +08:00
|
|
|
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Calls add_addrs(peer_id, [addr], ttl)
|
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
:param peer_id: the peer to add address for
|
|
|
|
:param addr: multiaddress of the peer
|
|
|
|
:param ttl: time-to-live for the address (after this time, address is no longer valid)
|
|
|
|
"""
|
2018-10-21 04:55:15 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
@abstractmethod
|
2019-07-27 16:27:01 +08:00
|
|
|
def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Adds addresses for a given peer all with the same time-to-live. If
|
|
|
|
one of the addresses already exists for the peer and has a longer TTL,
|
|
|
|
no operation should take place. If one of the addresses exists with a
|
|
|
|
shorter TTL, extend the TTL to equal param ttl.
|
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
:param peer_id: the peer to add address for
|
|
|
|
:param addr: multiaddresses of the peer
|
|
|
|
:param ttl: time-to-live for the address (after this time, address is no longer valid
|
|
|
|
"""
|
2018-10-21 04:55:15 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
@abstractmethod
|
2019-07-27 16:27:01 +08:00
|
|
|
def addrs(self, peer_id: ID) -> List[Multiaddr]:
|
2018-10-22 03:15:53 +08:00
|
|
|
"""
|
|
|
|
:param peer_id: peer to get addresses of
|
|
|
|
:return: all known (and valid) addresses for the given peer
|
|
|
|
"""
|
2018-10-21 04:55:15 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
@abstractmethod
|
2019-07-27 16:27:01 +08:00
|
|
|
def clear_addrs(self, peer_id: ID) -> None:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Removes all previously stored addresses.
|
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
:param peer_id: peer to remove addresses of
|
|
|
|
"""
|
2018-10-21 04:55:15 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
@abstractmethod
|
2019-07-27 16:27:01 +08:00
|
|
|
def peers_with_addrs(self) -> List[ID]:
|
2018-10-22 03:15:53 +08:00
|
|
|
"""
|
|
|
|
:return: all of the peer IDs stored with addresses
|
2018-10-21 04:55:15 +08:00
|
|
|
"""
|