Skip to content

Commit

Permalink
refactor(p2p): remove HathorManager dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Oct 15, 2024
1 parent 3aa3acd commit 2af3b11
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 127 deletions.
10 changes: 8 additions & 2 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ def build(self) -> BuildArtifacts:
wallet=wallet,
rng=self._rng,
checkpoints=self._checkpoints,
capabilities=self._capabilities,
environment_info=get_environment_info(self._cmdline, str(peer.id)),
bit_signaling_service=bit_signaling_service,
verification_service=verification_service,
Expand All @@ -268,7 +267,7 @@ def build(self) -> BuildArtifacts:
**kwargs
)

p2p_manager.set_manager(manager)
p2p_manager.finalize_factories()
if poa_block_producer:
poa_block_producer.manager = manager

Expand Down Expand Up @@ -426,6 +425,7 @@ def _get_or_create_p2p_manager(self) -> ConnectionsManager:
ssl=enable_ssl,
whitelist_only=False,
rng=self._rng,
capabilities=self._get_or_create_capabilities(),
)
SyncSupportLevel.add_factories(
self._p2p_manager,
Expand Down Expand Up @@ -640,6 +640,12 @@ def _get_or_create_poa_block_producer(self) -> PoaBlockProducer | None:

return self._poa_block_producer

def _get_or_create_capabilities(self) -> list[str]:
if self._capabilities is None:
settings = self._get_or_create_settings()
self._capabilities = settings.get_default_capabilities()
return self._capabilities

def use_memory(self) -> 'Builder':
self.check_if_can_modify()
self._storage_type = StorageType.MEMORY
Expand Down
6 changes: 4 additions & 2 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,15 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
pubsub=pubsub,
)

capabilities = settings.get_default_capabilities()
p2p_manager = ConnectionsManager(
dependencies=p2p_dependencies,
my_peer=peer,
ssl=True,
whitelist_only=False,
rng=Random(),
capabilities=capabilities,
hostname=hostname,
)

SyncSupportLevel.add_factories(
Expand All @@ -371,7 +374,6 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
self.manager = HathorManager(
reactor,
settings=settings,
hostname=hostname,
pubsub=pubsub,
consensus_algorithm=consensus_algorithm,
daa=daa,
Expand All @@ -398,7 +400,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
'--x-ipython-kernel must be used with --x-asyncio-reactor')
self._start_ipykernel()

p2p_manager.set_manager(self.manager)
p2p_manager.finalize_factories()
if poa_block_producer:
poa_block_producer.manager = self.manager

Expand Down
8 changes: 8 additions & 0 deletions hathor/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ def from_yaml(cls, *, filepath: str) -> 'HathorSettings':
validators=_VALIDATORS
)

def get_default_capabilities(self) -> list[str]:
"""Return the default capabilities."""
return [
self.CAPABILITY_WHITELIST,
self.CAPABILITY_SYNC_VERSION,
self.CAPABILITY_GET_BEST_BLOCKCHAIN
]


def _parse_checkpoints(checkpoints: Union[dict[int, str], list[Checkpoint]]) -> list[Checkpoint]:
"""Parse a dictionary of raw checkpoint data into a list of checkpoints."""
Expand Down
51 changes: 0 additions & 51 deletions hathor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer import PrivatePeer
from hathor.p2p.peer_id import PeerId
from hathor.pubsub import HathorEvents, PubSubManager
from hathor.reactor import ReactorProtocol as Reactor
from hathor.reward_lock import is_spent_reward_locked
Expand Down Expand Up @@ -108,9 +107,7 @@ def __init__(
execution_manager: ExecutionManager,
vertex_handler: VertexHandler,
vertex_parser: VertexParser,
hostname: Optional[str] = None,
wallet: Optional[BaseWallet] = None,
capabilities: Optional[list[str]] = None,
checkpoints: Optional[list[Checkpoint]] = None,
rng: Optional[Random] = None,
environment_info: Optional[EnvironmentInfo] = None,
Expand Down Expand Up @@ -158,9 +155,6 @@ def __init__(
self.is_profiler_running: bool = False
self.profiler_last_start_time: float = 0

# Hostname, used to be accessed by other peers.
self.hostname = hostname

# Remote address, which can be different from local address.
self.remote_address = None

Expand Down Expand Up @@ -226,15 +220,6 @@ def __init__(
# Can be activated on the command line with --full-verification
self._full_verification = full_verification

# List of whitelisted peers
self.peers_whitelist: list[PeerId] = []

# List of capabilities of the peer
if capabilities is not None:
self.capabilities = capabilities
else:
self.capabilities = self.get_default_capabilities()

# This is included in some logs to provide more context
self.environment_info = environment_info

Expand All @@ -245,14 +230,6 @@ def __init__(
self.lc_check_sync_state.clock = self.reactor
self.lc_check_sync_state_interval = self.CHECK_SYNC_STATE_INTERVAL

def get_default_capabilities(self) -> list[str]:
"""Return the default capabilities for this manager."""
return [
self._settings.CAPABILITY_WHITELIST,
self._settings.CAPABILITY_SYNC_VERSION,
self._settings.CAPABILITY_GET_BEST_BLOCKCHAIN
]

def start(self) -> None:
""" A factory must be started only once. And it is usually automatically started.
"""
Expand Down Expand Up @@ -984,27 +961,6 @@ def on_new_tx(

return result

def has_sync_version_capability(self) -> bool:
return self._settings.CAPABILITY_SYNC_VERSION in self.capabilities

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

if peer_id in self.peers_whitelist:
self.log.info('peer already in whitelist', peer_id=peer_id)
else:
self.peers_whitelist.append(peer_id)

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

if peer_id in self.peers_whitelist:
self.peers_whitelist.remove(peer_id)
# disconnect from node
self.connections.drop_connection_by_peer_id(peer_id)

def has_recent_activity(self) -> bool:
current_timestamp = time.time()
latest_blockchain_timestamp = self.tx_storage.latest_timestamp
Expand Down Expand Up @@ -1053,13 +1009,6 @@ def get_cmd_path(self) -> Optional[str]:
"""Return the cmd path. If no cmd path is set, returns None."""
return self._cmd_path

def set_hostname_and_reset_connections(self, new_hostname: str) -> None:
"""Set the hostname and reset all connections."""
old_hostname = self.hostname
self.hostname = new_hostname
self.connections.update_hostname_entrypoints(old_hostname=old_hostname, new_hostname=self.hostname)
self.connections.disconnect_all_peers(force=True)


class ParentTxs(NamedTuple):
""" Tuple where the `must_include` hash, when present (at most 1), must be included in a pair, and a list of hashes
Expand Down
4 changes: 4 additions & 0 deletions hathor/p2p/dependencies/p2p_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,7 @@ def get_best_block_tips(self) -> list[VertexId]:
@abstractmethod
def partial_vertex_exists(self, vertex_id: VertexId) -> bool:
raise NotImplementedError

@abstractmethod
def enable_mempool_index(self) -> None:
raise NotImplementedError
4 changes: 4 additions & 0 deletions hathor/p2p/dependencies/single_process_p2p_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ def get_best_block_tips(self) -> list[VertexId]:
@override
def partial_vertex_exists(self, vertex_id: VertexId) -> bool:
return self._tx_storage.partial_vertex_exists(vertex_id)

@override
def enable_mempool_index(self) -> None:
self._indexes.enable_mempool_index()
Loading

0 comments on commit 2af3b11

Please sign in to comment.