-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(p2p): implement P2P dependencies
- Loading branch information
Showing
31 changed files
with
426 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2024 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from hathor.p2p.dependencies.p2p_dependencies import P2PDependencies | ||
|
||
__all__ = [ | ||
'P2PDependencies', | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2024 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from hathor.conf.settings import HathorSettings | ||
from hathor.p2p.dependencies.protocols import ( | ||
P2PTransactionStorageProtocol, | ||
P2PVerificationServiceProtocol, | ||
P2PVertexHandlerProtocol, | ||
) | ||
from hathor.reactor import ReactorProtocol | ||
from hathor.transaction.vertex_parser import VertexParser | ||
|
||
|
||
class P2PDependencies: | ||
"""A simple class to unify all node dependencies that are required by P2P.""" | ||
|
||
__slots__ = ( | ||
'reactor', | ||
'settings', | ||
'vertex_parser', | ||
'vertex_handler', | ||
'verification_service', | ||
'tx_storage', | ||
'capabilities', | ||
'whitelist_only', | ||
'_has_sync_version_capability', | ||
) | ||
|
||
def __init__( | ||
self, | ||
*, | ||
reactor: ReactorProtocol, | ||
settings: HathorSettings, | ||
vertex_parser: VertexParser, | ||
vertex_handler: P2PVertexHandlerProtocol, | ||
verification_service: P2PVerificationServiceProtocol, | ||
tx_storage: P2PTransactionStorageProtocol, | ||
capabilities: list[str], | ||
whitelist_only: bool, | ||
) -> None: | ||
self.reactor = reactor | ||
self.settings = settings | ||
self.vertex_parser = vertex_parser | ||
self.vertex_handler = vertex_handler | ||
self.verification_service = verification_service | ||
self.tx_storage = tx_storage | ||
|
||
# List of capabilities of the peer | ||
self.capabilities = capabilities | ||
|
||
# Parameter to explicitly enable whitelist-only mode, when False it will still check the whitelist for sync-v1 | ||
self.whitelist_only = whitelist_only | ||
|
||
self._has_sync_version_capability = settings.CAPABILITY_SYNC_VERSION in capabilities | ||
|
||
def has_sync_version_capability(self) -> bool: | ||
return self._has_sync_version_capability |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2024 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Protocol | ||
|
||
from hathor.indexes.height_index import HeightInfo | ||
from hathor.transaction import Block, Vertex | ||
from hathor.types import VertexId | ||
|
||
|
||
class P2PVertexHandlerProtocol(Protocol): | ||
"""Abstract the VertexHandler as a Python protocol to be used in P2P classes.""" | ||
|
||
def on_new_vertex(self, vertex: Vertex, *, fails_silently: bool = True) -> bool: ... | ||
|
||
|
||
class P2PVerificationServiceProtocol(Protocol): | ||
"""Abstract the VerificationService as a Python protocol to be used in P2P classes.""" | ||
|
||
def verify_basic(self, vertex: Vertex) -> None: ... | ||
|
||
|
||
class P2PTransactionStorageProtocol(Protocol): | ||
"""Abstract the TransactionStorage as a Python protocol to be used in P2P classes.""" | ||
|
||
def get_vertex(self, vertex_id: VertexId) -> Vertex: ... | ||
def get_block(self, block_id: VertexId) -> Block: ... | ||
def transaction_exists(self, vertex_id: VertexId) -> bool: ... | ||
def can_validate_full(self, vertex: Vertex) -> bool: ... | ||
def compare_bytes_with_local_tx(self, vertex: Vertex) -> bool: ... | ||
def get_best_block(self) -> Block: ... | ||
def get_n_height_tips(self, n_blocks: int) -> list[HeightInfo]: ... | ||
def get_mempool_tips(self) -> set[VertexId]: ... | ||
def get_block_id_by_height(self, height: int) -> VertexId | None: ... | ||
def partial_vertex_exists(self, vertex_id: VertexId) -> bool: ... |
Oops, something went wrong.