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): simplify factory [part 1/5] #1151

Merged
merged 1 commit into from
Oct 14, 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
15 changes: 1 addition & 14 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def __init__(self) -> None:
self._capabilities: Optional[list[str]] = None

self._peer: Optional[PrivatePeer] = None
self._network: Optional[str] = None
self._cmdline: str = ''

self._storage_type: StorageType = StorageType.MEMORY
Expand Down Expand Up @@ -207,9 +206,6 @@ def build(self) -> BuildArtifacts:
if self.artifacts is not None:
raise ValueError('cannot call build twice')

if self._network is None:
raise TypeError('you must set a network')

if SyncSupportLevel.ENABLED not in {self._sync_v1_support, self._sync_v2_support}:
raise TypeError('you must enable at least one sync version')

Expand Down Expand Up @@ -257,7 +253,6 @@ def build(self) -> BuildArtifacts:
manager = HathorManager(
reactor,
settings=settings,
network=self._network,
pubsub=pubsub,
consensus_algorithm=consensus_algorithm,
daa=daa,
Expand Down Expand Up @@ -423,12 +418,9 @@ def _get_or_create_p2p_manager(self) -> ConnectionsManager:
reactor = self._get_reactor()
my_peer = self._get_peer()

assert self._network is not None

self._p2p_manager = ConnectionsManager(
settings=self._get_or_create_settings(),
reactor=reactor,
network=self._network,
my_peer=my_peer,
pubsub=self._get_or_create_pubsub(),
ssl=enable_ssl,
Expand Down Expand Up @@ -522,7 +514,7 @@ def _get_or_create_event_manager(self) -> EventManager:
storage = self._get_or_create_event_storage()
factory = EventWebsocketFactory(
peer_id=str(peer.id),
network=settings.NETWORK_NAME,
settings=settings,
reactor=reactor,
event_storage=storage,
)
Expand Down Expand Up @@ -776,11 +768,6 @@ def set_pubsub(self, pubsub: PubSubManager) -> 'Builder':
self._pubsub = pubsub
return self

def set_network(self, network: str) -> 'Builder':
self.check_if_can_modify()
self._network = network
return self

def set_sync_v1_support(self, support_level: SyncSupportLevel) -> 'Builder':
self.check_if_can_modify()
self._sync_v1_support = support_level
Expand Down
5 changes: 1 addition & 4 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
self.log.info('with wallet', wallet=self.wallet, path=self._args.data)

hostname = self.get_hostname()
network = settings.NETWORK_NAME

sync_choice: SyncChoice
if self._args.sync_bridge:
Expand Down Expand Up @@ -245,7 +244,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
if self._args.x_enable_event_queue:
self.event_ws_factory = EventWebsocketFactory(
peer_id=str(peer.id),
network=network,
settings=settings,
reactor=reactor,
event_storage=event_storage
)
Expand Down Expand Up @@ -322,7 +321,6 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
p2p_manager = ConnectionsManager(
settings=settings,
reactor=reactor,
network=network,
my_peer=peer,
pubsub=pubsub,
ssl=True,
Expand Down Expand Up @@ -367,7 +365,6 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
self.manager = HathorManager(
reactor,
settings=settings,
network=network,
hostname=hostname,
pubsub=pubsub,
consensus_algorithm=consensus_algorithm,
Expand Down
3 changes: 2 additions & 1 deletion hathor/cli/events_simulator/events_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def execute(args: Namespace, reactor: 'ReactorProtocol') -> None:
os.environ['HATHOR_CONFIG_YAML'] = UNITTESTS_SETTINGS_FILEPATH
from hathor.cli.events_simulator.event_forwarding_websocket_factory import EventForwardingWebsocketFactory
from hathor.cli.events_simulator.scenario import Scenario
from hathor.conf.get_settings import get_global_settings
from hathor.simulator import Simulator

try:
Expand All @@ -70,7 +71,7 @@ def execute(args: Namespace, reactor: 'ReactorProtocol') -> None:
forwarding_ws_factory = EventForwardingWebsocketFactory(
simulator=simulator,
peer_id='simulator_peer_id',
network='simulator_network',
settings=get_global_settings(),
reactor=reactor,
event_storage=event_ws_factory._event_storage
)
Expand Down
5 changes: 3 additions & 2 deletions hathor/event/websocket/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from autobahn.twisted.websocket import WebSocketServerFactory
from structlog import get_logger

from hathor.conf.settings import HathorSettings
from hathor.event.model.base_event import BaseEvent
from hathor.event.storage import EventStorage
from hathor.event.websocket.protocol import EventWebsocketProtocol
Expand Down Expand Up @@ -45,14 +46,14 @@ def __init__(
self,
*,
peer_id: str,
network: str,
settings: HathorSettings,
reactor: Reactor,
event_storage: EventStorage
) -> None:
super().__init__()
self.log = logger.new()
self._peer_id = peer_id
self._network = network
self._network = settings.NETWORK_NAME
self._reactor = reactor
self._event_storage = event_storage
self._connections: set[EventWebsocketProtocol] = set()
Expand Down
5 changes: 1 addition & 4 deletions hathor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def __init__(
bit_signaling_service: BitSignalingService,
verification_service: VerificationService,
cpu_mining_service: CpuMiningService,
network: str,
execution_manager: ExecutionManager,
vertex_handler: VertexHandler,
vertex_parser: VertexParser,
Expand All @@ -126,8 +125,6 @@ def __init__(
"""
:param reactor: Twisted reactor which handles the mainloop and the events.
: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

:param tx_storage: Required storage backend.
:type tx_storage: :py:class:`hathor.transaction.storage.transaction_storage.TransactionStorage`
Expand Down Expand Up @@ -170,7 +167,7 @@ def __init__(
self.remote_address = None

self.my_peer = peer
self.network = network
self.network = settings.NETWORK_NAME

self.is_started: bool = False

Expand Down
2 changes: 1 addition & 1 deletion hathor/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def collect_peer_connection_metrics(self) -> None:
metric = PeerConnectionMetrics(
connection_string=str(connection.entrypoint) if connection.entrypoint else "",
peer_id=str(connection.peer.id),
network=connection.network,
network=settings.NETWORK_NAME,
received_messages=connection.metrics.received_messages,
sent_messages=connection.metrics.sent_messages,
received_bytes=connection.metrics.received_bytes,
Expand Down
64 changes: 13 additions & 51 deletions hathor/p2p/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING, Optional
from abc import ABC

from twisted.internet import protocol
from twisted.internet.interfaces import IAddress
Expand All @@ -22,23 +22,12 @@
from hathor.p2p.peer import PrivatePeer
from hathor.p2p.protocol import HathorLineReceiver

if TYPE_CHECKING:
from hathor.manager import HathorManager # noqa: F401

MyServerProtocol = HathorLineReceiver
MyClientProtocol = HathorLineReceiver


class HathorServerFactory(protocol.ServerFactory):
""" HathorServerFactory is used to generate HathorProtocol objects when a new connection arrives.
"""

manager: Optional[ConnectionsManager]
protocol: type[MyServerProtocol] = MyServerProtocol
class _HathorLineReceiverFactory(ABC, protocol.Factory):
inbound: bool

def __init__(
self,
network: str,
my_peer: PrivatePeer,
p2p_manager: ConnectionsManager,
*,
Expand All @@ -47,56 +36,29 @@ def __init__(
):
super().__init__()
self._settings = settings
self.network = network
self.my_peer = my_peer
self.p2p_manager = p2p_manager
self.use_ssl = use_ssl

def buildProtocol(self, addr: IAddress) -> MyServerProtocol:
assert self.protocol is not None
p = self.protocol(
network=self.network,
def buildProtocol(self, addr: IAddress) -> HathorLineReceiver:
p = HathorLineReceiver(
my_peer=self.my_peer,
p2p_manager=self.p2p_manager,
use_ssl=self.use_ssl,
inbound=True,
inbound=self.inbound,
settings=self._settings
)
p.factory = self
return p


class HathorClientFactory(protocol.ClientFactory):
""" HathorClientFactory is used to generate HathorProtocol objects when we connected to another peer.
class HathorServerFactory(_HathorLineReceiverFactory, protocol.ServerFactory):
""" HathorServerFactory is used to generate HathorProtocol objects when a new connection arrives.
"""
inbound = True

protocol: type[MyClientProtocol] = MyClientProtocol

def __init__(
self,
network: str,
my_peer: PrivatePeer,
p2p_manager: ConnectionsManager,
*,
settings: HathorSettings,
use_ssl: bool,
):
super().__init__()
self._settings = settings
self.network = network
self.my_peer = my_peer
self.p2p_manager = p2p_manager
self.use_ssl = use_ssl

def buildProtocol(self, addr: IAddress) -> MyClientProtocol:
assert self.protocol is not None
p = self.protocol(
network=self.network,
my_peer=self.my_peer,
p2p_manager=self.p2p_manager,
use_ssl=self.use_ssl,
inbound=False,
settings=self._settings
)
p.factory = self
return p
class HathorClientFactory(_HathorLineReceiverFactory, protocol.ClientFactory):
""" HathorClientFactory is used to generate HathorProtocol objects when we connected to another peer.
"""
inbound = False
9 changes: 3 additions & 6 deletions hathor/p2p/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def __init__(
self,
settings: HathorSettings,
reactor: Reactor,
network: str,
my_peer: PrivatePeer,
pubsub: PubSubManager,
ssl: bool,
Expand All @@ -114,8 +113,6 @@ def __init__(
self.reactor = reactor
self.my_peer = my_peer

self.network = network

# List of address descriptions to listen for new connections (eg: [tcp:8000])
self.listen_address_descriptions: list[str] = []

Expand All @@ -132,10 +129,10 @@ def __init__(
from hathor.p2p.factory import HathorClientFactory, HathorServerFactory
self.use_ssl = ssl
self.server_factory = HathorServerFactory(
self.network, self.my_peer, p2p_manager=self, use_ssl=self.use_ssl, settings=self._settings
self.my_peer, p2p_manager=self, use_ssl=self.use_ssl, settings=self._settings
)
self.client_factory = HathorClientFactory(
self.network, self.my_peer, p2p_manager=self, use_ssl=self.use_ssl, settings=self._settings
self.my_peer, p2p_manager=self, use_ssl=self.use_ssl, settings=self._settings
)

# Global maximum number of connections.
Expand Down Expand Up @@ -407,7 +404,7 @@ def on_peer_ready(self, protocol: HathorProtocol) -> None:
self.unverified_peer_storage.pop(protocol.peer.id, None)

# we emit the event even if it's a duplicate peer as a matching
# NETWORK_PEER_DISCONNECTED will be emmited regardless
# NETWORK_PEER_DISCONNECTED will be emitted regardless
self.pubsub.publish(
HathorEvents.NETWORK_PEER_READY,
protocol=protocol,
Expand Down
3 changes: 0 additions & 3 deletions hathor/p2p/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class WarningFlags(str, Enum):
NO_PEER_ID_URL = 'no_peer_id_url'
NO_ENTRYPOINTS = 'no_entrypoints'

network: str
my_peer: PrivatePeer
connections: 'ConnectionsManager'
node: 'HathorManager'
Expand All @@ -99,7 +98,6 @@ def peer(self) -> PublicPeer:

def __init__(
self,
network: str,
my_peer: PrivatePeer,
p2p_manager: 'ConnectionsManager',
*,
Expand All @@ -108,7 +106,6 @@ def __init__(
inbound: bool,
) -> None:
self._settings = settings
self.network = network
self.my_peer = my_peer
self.connections = p2p_manager

Expand Down
4 changes: 2 additions & 2 deletions hathor/p2p/states/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _get_hello_data(self) -> dict[str, Any]:
remote = protocol.transport.getPeer()
data = {
'app': self._app(),
'network': protocol.network,
'network': self._settings.NETWORK_NAME,
'remote_address': format_address(remote),
'genesis_short_hash': get_genesis_short_hash(),
'timestamp': protocol.node.reactor.seconds(),
Expand Down Expand Up @@ -135,7 +135,7 @@ def handle_hello(self, payload: str) -> None:
# XXX: this used to be a warning, but it shouldn't be since it's perfectly normal
self.log.debug('different versions', theirs=remote_app, ours=our_app)

if data['network'] != protocol.network:
if data['network'] != self._settings.NETWORK_NAME:
protocol.send_error_and_close_connection('Wrong network.')
return

Expand Down
2 changes: 0 additions & 2 deletions hathor/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def __init__(self, seed: Optional[int] = None):
self.seed = seed
self.rng = Random(self.seed)
self.settings = get_global_settings()._replace(AVG_TIME_BETWEEN_BLOCKS=SIMULATOR_AVG_TIME_BETWEEN_BLOCKS)
self._network = 'testnet'
self._clock = MemoryReactorHeapClock()
self._peers: OrderedDict[str, HathorManager] = OrderedDict()
self._connections: list['FakeConnection'] = []
Expand All @@ -80,7 +79,6 @@ def get_default_builder(self) -> Builder:
Returns a builder with default configuration, for convenience when using create_peer() or create_artifacts()
"""
return Builder() \
.set_network(self._network) \
.set_peer(PrivatePeer.auto_generated()) \
.set_soft_voided_tx_ids(set()) \
.enable_full_verification() \
Expand Down
1 change: 1 addition & 0 deletions hathor/transaction/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ def get_feature_activation_bit_value(self, bit: int) -> int:
def iter_transactions_in_this_block(self) -> Iterator[BaseTransaction]:
"""Return an iterator of the transactions that have this block as meta.first_block."""
from hathor.transaction.storage.traversal import BFSOrderWalk
assert self.storage is not None
bfs = BFSOrderWalk(self.storage, is_dag_verifications=True, is_dag_funds=True, is_left_to_right=False)
for tx in bfs.run(self, skip_root=True):
tx_meta = tx.get_metadata()
Expand Down
2 changes: 2 additions & 0 deletions hathor/transaction/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from hathor.transaction.storage.cache_storage import TransactionCacheStorage
from hathor.transaction.storage.memory_storage import TransactionMemoryStorage
from hathor.transaction.storage.transaction_storage import TransactionStorage
from hathor.transaction.storage.vertex_storage_protocol import VertexStorageProtocol

try:
from hathor.transaction.storage.rocksdb_storage import TransactionRocksDBStorage
Expand All @@ -26,4 +27,5 @@
'TransactionMemoryStorage',
'TransactionCacheStorage',
'TransactionRocksDBStorage',
'VertexStorageProtocol'
]
Loading
Loading