Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(p2p): rename peer_id.PeerId to peer.Peer #1113

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/legacy/ref/p2p.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The :py:mod:`hathor.p2p.states` has all states and messages of the p2p network.
to send new messages and handle the new incoming ones.


The :py:class:`hathor.p2p.peer_id.PeerId` stores the peer's identity, entrypoint, reputation and history.
The :py:class:`hathor.p2p.peer.Peer` stores the peer's identity, entrypoint, reputation and history.



Expand Down
32 changes: 16 additions & 16 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from hathor.manager import HathorManager
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer_id import PeerId
from hathor.p2p.peer import Peer
from hathor.pubsub import PubSubManager
from hathor.reactor import ReactorProtocol as Reactor
from hathor.storage import RocksDBStorage
Expand Down Expand Up @@ -96,7 +96,7 @@ class StorageType(Enum):

class BuildArtifacts(NamedTuple):
"""Artifacts created by a builder."""
peer_id: PeerId
peer: Peer
settings: HathorSettingsType
rng: Random
reactor: Reactor
Expand Down Expand Up @@ -137,7 +137,7 @@ def __init__(self) -> None:
self._checkpoints: Optional[list[Checkpoint]] = None
self._capabilities: Optional[list[str]] = None

self._peer_id: Optional[PeerId] = None
self._peer: Optional[Peer] = None
self._network: Optional[str] = None
self._cmdline: str = ''

Expand Down Expand Up @@ -212,7 +212,7 @@ def build(self) -> BuildArtifacts:
reactor = self._get_reactor()
pubsub = self._get_or_create_pubsub()

peer_id = self._get_peer_id()
peer = self._get_peer()

execution_manager = self._get_or_create_execution_manager()
consensus_algorithm = self._get_or_create_consensus()
Expand Down Expand Up @@ -256,15 +256,15 @@ def build(self) -> BuildArtifacts:
pubsub=pubsub,
consensus_algorithm=consensus_algorithm,
daa=daa,
peer_id=peer_id,
peer=peer,
tx_storage=tx_storage,
p2p_manager=p2p_manager,
event_manager=event_manager,
wallet=wallet,
rng=self._rng,
checkpoints=self._checkpoints,
capabilities=self._capabilities,
environment_info=get_environment_info(self._cmdline, peer_id.id),
environment_info=get_environment_info(self._cmdline, peer.id),
bit_signaling_service=bit_signaling_service,
verification_service=verification_service,
cpu_mining_service=cpu_mining_service,
Expand All @@ -284,7 +284,7 @@ def build(self) -> BuildArtifacts:
stratum_factory = self._create_stratum_server(manager)

self.artifacts = BuildArtifacts(
peer_id=peer_id,
peer=peer,
settings=settings,
rng=self._rng,
reactor=reactor,
Expand Down Expand Up @@ -337,9 +337,9 @@ def set_capabilities(self, capabilities: list[str]) -> 'Builder':
self._capabilities = capabilities
return self

def set_peer_id(self, peer_id: PeerId) -> 'Builder':
def set_peer(self, peer: Peer) -> 'Builder':
self.check_if_can_modify()
self._peer_id = peer_id
self._peer = peer
return self

def _get_or_create_settings(self) -> HathorSettingsType:
Expand All @@ -361,10 +361,10 @@ def _get_soft_voided_tx_ids(self) -> set[bytes]:

return set(settings.SOFT_VOIDED_TX_IDS)

def _get_peer_id(self) -> PeerId:
if self._peer_id is not None:
return self._peer_id
raise ValueError('peer_id not set')
def _get_peer(self) -> Peer:
if self._peer is not None:
return self._peer
raise ValueError('peer not set')

def _get_or_create_execution_manager(self) -> ExecutionManager:
if self._execution_manager is None:
Expand Down Expand Up @@ -416,7 +416,7 @@ def _get_or_create_p2p_manager(self) -> ConnectionsManager:

enable_ssl = True
reactor = self._get_reactor()
my_peer = self._get_peer_id()
my_peer = self._get_peer()

assert self._network is not None

Expand Down Expand Up @@ -510,12 +510,12 @@ def _get_or_create_event_storage(self) -> EventStorage:

def _get_or_create_event_manager(self) -> EventManager:
if self._event_manager is None:
peer_id = self._get_peer_id()
peer = self._get_peer()
settings = self._get_or_create_settings()
reactor = self._get_reactor()
storage = self._get_or_create_event_storage()
factory = EventWebsocketFactory(
peer_id=not_none(peer_id.id),
peer_id=not_none(peer.id),
network=settings.NETWORK_NAME,
reactor=reactor,
event_storage=storage,
Expand Down
14 changes: 7 additions & 7 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.p2p.entrypoint import Entrypoint
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer_id import PeerId
from hathor.p2p.peer import Peer
from hathor.p2p.utils import discover_hostname, get_genesis_short_hash
from hathor.pubsub import PubSubManager
from hathor.reactor import ReactorProtocol as Reactor
Expand Down Expand Up @@ -98,15 +98,15 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
self.log = logger.new()
self.reactor = reactor

peer_id = PeerId.create_from_json_path(self._args.peer) if self._args.peer else PeerId()
peer = Peer.create_from_json_path(self._args.peer) if self._args.peer else Peer()
python = f'{platform.python_version()}-{platform.python_implementation()}'

self.log.info(
'hathor-core v{hathor}',
hathor=hathor.__version__,
pid=os.getpid(),
genesis=get_genesis_short_hash(),
my_peer_id=str(peer_id.id),
my_peer_id=str(peer.id),
python=python,
platform=platform.platform(),
settings=settings_source,
Expand Down Expand Up @@ -225,7 +225,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:

if self._args.x_enable_event_queue:
self.event_ws_factory = EventWebsocketFactory(
peer_id=not_none(peer_id.id),
peer_id=not_none(peer.id),
network=network,
reactor=reactor,
event_storage=event_storage
Expand Down Expand Up @@ -307,7 +307,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
settings=settings,
reactor=reactor,
network=network,
my_peer=peer_id,
my_peer=peer,
pubsub=pubsub,
ssl=True,
whitelist_only=False,
Expand Down Expand Up @@ -348,13 +348,13 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
pubsub=pubsub,
consensus_algorithm=consensus_algorithm,
daa=daa,
peer_id=peer_id,
peer=peer,
tx_storage=tx_storage,
p2p_manager=p2p_manager,
event_manager=event_manager,
wallet=self.wallet,
checkpoints=settings.CHECKPOINTS,
environment_info=get_environment_info(args=str(self._args), peer_id=peer_id.id),
environment_info=get_environment_info(args=str(self._args), peer_id=peer.id),
full_verification=full_verification,
enable_event_queue=self._args.x_enable_event_queue,
bit_signaling_service=bit_signaling_service,
Expand Down
8 changes: 4 additions & 4 deletions hathor/cli/peer_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

""" Generates a random PeerId and print it to stdout.
""" Generates a random Peer and print it to stdout.
It may be used to testing purposes.
"""

import json


def main() -> None:
from hathor.p2p.peer_id import PeerId
from hathor.p2p.peer import Peer

peer_id = PeerId()
data = peer_id.to_json(include_private_key=True)
peer = Peer()
data = peer.to_json(include_private_key=True)
txt = json.dumps(data, indent=4)
print(txt)
2 changes: 1 addition & 1 deletion hathor/cli/run_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def prepare(self, *, register_resources: bool = True) -> None:

from hathor.builder.builder import BuildArtifacts
self.artifacts = BuildArtifacts(
peer_id=self.manager.my_peer,
peer=self.manager.my_peer,
settings=settings,
rng=self.manager.rng,
reactor=self.manager.reactor,
Expand Down
2 changes: 1 addition & 1 deletion hathor/daa.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TestMode(IntFlag):


class DifficultyAdjustmentAlgorithm:
# TODO: This singleton is temporary, and only used in PeerId. It should be removed from there, and then from here.
# TODO: This singleton is temporary, and only used in Peer. It should be removed from there, and then from here.
singleton: ClassVar[Optional['DifficultyAdjustmentAlgorithm']] = None

def __init__(self, *, settings: HathorSettings, test_mode: TestMode = TestMode.DISABLED) -> None:
Expand Down
10 changes: 5 additions & 5 deletions hathor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from hathor.mining import BlockTemplate, BlockTemplates
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer_id import PeerId
from hathor.p2p.peer import Peer
from hathor.profiler import get_cpu_profiler
from hathor.pubsub import HathorEvents, PubSubManager
from hathor.reactor import ReactorProtocol as Reactor
Expand Down Expand Up @@ -96,7 +96,7 @@ def __init__(
pubsub: PubSubManager,
consensus_algorithm: ConsensusAlgorithm,
daa: DifficultyAdjustmentAlgorithm,
peer_id: PeerId,
peer: Peer,
tx_storage: TransactionStorage,
p2p_manager: ConnectionsManager,
event_manager: EventManager,
Expand All @@ -119,7 +119,7 @@ def __init__(
) -> None:
"""
:param reactor: Twisted reactor which handles the mainloop and the events.
:param peer_id: Id of this node.
:param peer: Peer object, with peer-id of this node.
:param network: Name of the network this node participates. Usually it is either testnet or mainnet.
:type network: string

Expand Down Expand Up @@ -163,7 +163,7 @@ def __init__(
# Remote address, which can be different from local address.
self.remote_address = None

self.my_peer = peer_id
self.my_peer = peer
self.network = network

self.is_started: bool = False
Expand Down Expand Up @@ -976,7 +976,7 @@ def on_new_tx(
def has_sync_version_capability(self) -> bool:
return self._settings.CAPABILITY_SYNC_VERSION in self.capabilities

def add_peer_to_whitelist(self, peer_id):
def add_peer_to_whitelist(self, peer_id: str) -> None:
if not self._settings.ENABLE_PEER_WHITELIST:
return

Expand Down
6 changes: 3 additions & 3 deletions hathor/p2p/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from hathor.conf.settings import HathorSettings
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer_id import PeerId
from hathor.p2p.peer import Peer
from hathor.p2p.protocol import HathorLineReceiver

if TYPE_CHECKING:
Expand All @@ -39,7 +39,7 @@ class HathorServerFactory(protocol.ServerFactory):
def __init__(
self,
network: str,
my_peer: PeerId,
my_peer: Peer,
p2p_manager: ConnectionsManager,
*,
settings: HathorSettings,
Expand Down Expand Up @@ -75,7 +75,7 @@ class HathorClientFactory(protocol.ClientFactory):
def __init__(
self,
network: str,
my_peer: PeerId,
my_peer: Peer,
p2p_manager: ConnectionsManager,
*,
settings: HathorSettings,
Expand Down
20 changes: 10 additions & 10 deletions hathor/p2p/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
from hathor.conf.settings import HathorSettings
from hathor.p2p.entrypoint import Entrypoint
from hathor.p2p.netfilter.factory import NetfilterFactory
from hathor.p2p.peer import Peer
from hathor.p2p.peer_discovery import PeerDiscovery
from hathor.p2p.peer_id import PeerId
from hathor.p2p.peer_storage import PeerStorage
from hathor.p2p.protocol import HathorProtocol
from hathor.p2p.rate_limiter import RateLimiter
Expand Down Expand Up @@ -93,7 +93,7 @@ def __init__(
settings: HathorSettings,
reactor: Reactor,
network: str,
my_peer: PeerId,
my_peer: Peer,
pubsub: PubSubManager,
ssl: bool,
rng: Random,
Expand Down Expand Up @@ -159,7 +159,7 @@ def __init__(
self.received_peer_storage = PeerStorage()

# List of known peers.
self.peer_storage = PeerStorage() # dict[string (peer.id), PeerId]
self.peer_storage = PeerStorage() # dict[string (peer.id), Peer]

# Maximum unseen time before removing a peer (seconds).
self.max_peer_unseen_dt: float = 30 * 60 # 30-minutes
Expand Down Expand Up @@ -366,7 +366,7 @@ def disconnect_all_peers(self, *, force: bool = False) -> None:
for conn in self.iter_all_connections():
conn.disconnect(force=force)

def on_connection_failure(self, failure: Failure, peer: Optional[PeerId], endpoint: IStreamClientEndpoint) -> None:
def on_connection_failure(self, failure: Failure, peer: Optional[Peer], endpoint: IStreamClientEndpoint) -> None:
connecting_peer = self.connecting_peers[endpoint]
entrypoint = connecting_peer.entrypoint
self.log.warn('connection failure', entrypoint=entrypoint, failure=failure.getErrorMessage())
Expand Down Expand Up @@ -433,7 +433,7 @@ def on_peer_ready(self, protocol: HathorProtocol) -> None:
# Notify other peers about this new peer connection.
self.relay_peer_to_ready_connections(protocol.peer)

def relay_peer_to_ready_connections(self, peer: PeerId) -> None:
def relay_peer_to_ready_connections(self, peer: Peer) -> None:
"""Relay peer to all ready connections."""
for conn in self.iter_ready_connections():
if conn.peer == peer:
Expand Down Expand Up @@ -491,7 +491,7 @@ def is_peer_connected(self, peer_id: str) -> bool:
"""
return peer_id in self.connected_peers

def on_receive_peer(self, peer: PeerId, origin: Optional[ReadyState] = None) -> None:
def on_receive_peer(self, peer: Peer, origin: Optional[ReadyState] = None) -> None:
""" Update a peer information in our storage, and instantly attempt to connect
to it if it is not connected yet.
"""
Expand All @@ -503,7 +503,7 @@ def on_receive_peer(self, peer: PeerId, origin: Optional[ReadyState] = None) ->
def peers_cleanup(self) -> None:
"""Clean up aged peers."""
now = self.reactor.seconds()
to_be_removed: list[PeerId] = []
to_be_removed: list[Peer] = []
for peer in self.peer_storage.values():
assert peer.id is not None
if self.is_peer_connected(peer.id):
Expand Down Expand Up @@ -574,7 +574,7 @@ def _update_whitelist_cb(self, body: bytes) -> None:
for peer_id in peers_to_remove:
self.manager.remove_peer_from_whitelist_and_disconnect(peer_id)

def connect_to_if_not_connected(self, peer: PeerId, now: int) -> None:
def connect_to_if_not_connected(self, peer: Peer, now: int) -> None:
""" Attempts to connect if it is not connected to the peer.
"""
if not peer.entrypoints:
Expand All @@ -594,7 +594,7 @@ def connect_to_if_not_connected(self, peer: PeerId, now: int) -> None:
def _connect_to_callback(
self,
protocol: IProtocol,
peer: Optional[PeerId],
peer: Optional[Peer],
endpoint: IStreamClientEndpoint,
entrypoint: Entrypoint,
) -> None:
Expand All @@ -610,7 +610,7 @@ def _connect_to_callback(
def connect_to(
self,
entrypoint: Entrypoint,
peer: Optional[PeerId] = None,
peer: Optional[Peer] = None,
use_ssl: Optional[bool] = None,
) -> None:
""" Attempt to connect to a peer, even if a connection already exists.
Expand Down
Loading