Skip to content

Commit

Permalink
Drop type suffix from various types
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Jul 29, 2019
1 parent f7e3b44 commit 74e2be6
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 82 deletions.
18 changes: 9 additions & 9 deletions p2p/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from eth_keys import datatypes

from p2p.typing import CapabilityType, PayloadType, StructureType
from p2p.typing import Capability, Payload, Structure


TAddress = TypeVar('TAddress', bound='AddressAPI')
Expand Down Expand Up @@ -118,7 +118,7 @@ def __hash__(self) -> int:


class CommandAPI(ABC):
structure: StructureType
structure: Structure

cmd_id: int
cmd_id_offset: int
Expand All @@ -134,19 +134,19 @@ def is_base_protocol(self) -> bool:
...

@abstractmethod
def encode_payload(self, data: Union[PayloadType, sedes.CountableList]) -> bytes:
def encode_payload(self, data: Union[Payload, sedes.CountableList]) -> bytes:
...

@abstractmethod
def decode_payload(self, rlp_data: bytes) -> PayloadType:
def decode_payload(self, rlp_data: bytes) -> Payload:
...

@abstractmethod
def encode(self, data: PayloadType) -> Tuple[bytes, bytes]:
def encode(self, data: Payload) -> Tuple[bytes, bytes]:
...

@abstractmethod
def decode(self, data: bytes) -> PayloadType:
def decode(self, data: bytes) -> Payload:
...

@abstractmethod
Expand All @@ -159,7 +159,7 @@ def compress_payload(self, raw_payload: bytes) -> bytes:


# A payload to be delivered with a request
TRequestPayload = TypeVar('TRequestPayload', bound=PayloadType, covariant=True)
TRequestPayload = TypeVar('TRequestPayload', bound=Payload, covariant=True)


class RequestAPI(ABC, Generic[TRequestPayload]):
Expand Down Expand Up @@ -229,7 +229,7 @@ def __init__(self, transport: TransportAPI, cmd_id_offset: int, snappy_support:
...

@abstractmethod
def send_request(self, request: RequestAPI[PayloadType]) -> None:
def send_request(self, request: RequestAPI[Payload]) -> None:
...

@abstractmethod
Expand All @@ -238,5 +238,5 @@ def supports_command(self, cmd_type: Type[CommandAPI]) -> bool:

@classmethod
@abstractmethod
def as_capability(cls) -> CapabilityType:
def as_capability(cls) -> Capability:
...
8 changes: 4 additions & 4 deletions p2p/p2p_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from p2p.constants import P2P_PROTOCOL_COMMAND_LENGTH
from p2p.disconnect import DisconnectReason as _DisconnectReason
from p2p.exceptions import MalformedMessage
from p2p.typing import CapabilitiesType, PayloadType
from p2p.typing import Capabilities, Payload

from p2p.protocol import (
Command,
Expand Down Expand Up @@ -51,7 +51,7 @@ def get_reason_name(self, reason_id: int) -> str:
except ValueError:
return "unknown reason"

def decode(self, data: bytes) -> PayloadType:
def decode(self, data: bytes) -> Payload:
try:
raw_decoded = cast(Dict[str, int], super().decode(data))
except rlp.exceptions.ListDeserializationError:
Expand Down Expand Up @@ -85,7 +85,7 @@ def __init__(self,

def send_handshake(self,
client_version_string: str,
capabilities: CapabilitiesType,
capabilities: Capabilities,
listen_port: int) -> None:
self.send_hello(
version=self.version,
Expand All @@ -98,7 +98,7 @@ def send_handshake(self,
def send_hello(self,
version: int,
client_version_string: str,
capabilities: CapabilitiesType,
capabilities: Capabilities,
listen_port: int,
remote_pubkey: bytes) -> None:
data = dict(version=version,
Expand Down
20 changes: 10 additions & 10 deletions p2p/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
from p2p.protocol import (
match_protocols_with_capabilities,
Command,
PayloadType,
CapabilitiesType,
Payload,
Capabilities,
)
from p2p.transport import Transport
from p2p.tracking.connection import (
Expand Down Expand Up @@ -261,7 +261,7 @@ async def send_sub_proto_handshake(self) -> None:

@abstractmethod
async def process_sub_proto_handshake(
self, cmd: CommandAPI, msg: PayloadType) -> None:
self, cmd: CommandAPI, msg: Payload) -> None:
pass

@contextlib.contextmanager
Expand Down Expand Up @@ -340,7 +340,7 @@ async def do_p2p_handshake(self) -> None:
await self.process_p2p_handshake(cmd, msg)

@property
def capabilities(self) -> CapabilitiesType:
def capabilities(self) -> Capabilities:
return tuple(proto.as_capability() for proto in self.supported_sub_protocols)

def get_protocol_command_for(self, msg: bytes) -> CommandAPI:
Expand Down Expand Up @@ -392,7 +392,7 @@ async def _run(self) -> None:
self.logger.debug("%r disconnected: %s", self, e)
return

async def read_msg(self) -> Tuple[CommandAPI, PayloadType]:
async def read_msg(self) -> Tuple[CommandAPI, Payload]:
msg = await self.transport.recv(self.cancel_token)
cmd = self.get_protocol_command_for(msg)
# NOTE: This used to be a bottleneck but it doesn't seem to be so anymore. If we notice
Expand All @@ -416,7 +416,7 @@ async def read_msg(self) -> Tuple[CommandAPI, PayloadType]:
self.received_msgs[cmd] += 1
return cmd, decoded_msg

def handle_p2p_msg(self, cmd: CommandAPI, msg: PayloadType) -> None:
def handle_p2p_msg(self, cmd: CommandAPI, msg: Payload) -> None:
"""Handle the base protocol (P2P) messages."""
if isinstance(cmd, Disconnect):
msg = cast(Dict[str, Any], msg)
Expand All @@ -436,7 +436,7 @@ def handle_p2p_msg(self, cmd: CommandAPI, msg: PayloadType) -> None:
else:
raise UnexpectedMessage(f"Unexpected msg: {cmd} ({msg})")

def handle_sub_proto_msg(self, cmd: CommandAPI, msg: PayloadType) -> None:
def handle_sub_proto_msg(self, cmd: CommandAPI, msg: Payload) -> None:
cmd_type = type(cmd)

if self._subscribers:
Expand All @@ -454,14 +454,14 @@ def handle_sub_proto_msg(self, cmd: CommandAPI, msg: PayloadType) -> None:
else:
self.logger.warning("Peer %s has no subscribers, discarding %s msg", self, cmd)

def process_msg(self, cmd: CommandAPI, msg: PayloadType) -> None:
def process_msg(self, cmd: CommandAPI, msg: Payload) -> None:
if cmd.is_base_protocol:
self.handle_p2p_msg(cmd, msg)
else:
self.handle_sub_proto_msg(cmd, msg)

async def process_p2p_handshake(
self, cmd: CommandAPI, msg: PayloadType) -> None:
self, cmd: CommandAPI, msg: Payload) -> None:
msg = cast(Dict[str, Any], msg)
if not isinstance(cmd, Hello):
await self.disconnect(DisconnectReason.bad_protocol)
Expand Down Expand Up @@ -558,7 +558,7 @@ def disconnect_nowait(self, reason: DisconnectReason) -> None:
class PeerMessage(NamedTuple):
peer: BasePeer
command: CommandAPI
payload: PayloadType
payload: Payload


class PeerSubscriber(ABC):
Expand Down
18 changes: 9 additions & 9 deletions p2p/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
from p2p.abc import CommandAPI, ProtocolAPI, RequestAPI, TransportAPI
from p2p.constants import P2P_PROTOCOL_COMMAND_LENGTH
from p2p.exceptions import MalformedMessage
from p2p.typing import CapabilityType, CapabilitiesType, PayloadType, StructureType
from p2p.typing import Capability, Capabilities, Payload, Structure


class Command(CommandAPI):
_cmd_id: int = None
decode_strict = True
structure: StructureType
structure: Structure

_logger: logging.Logger = None

Expand All @@ -52,7 +52,7 @@ def is_base_protocol(self) -> bool:
def __str__(self) -> str:
return f"{type(self).__name__} (cmd_id={self.cmd_id})"

def encode_payload(self, data: Union[PayloadType, sedes.CountableList]) -> bytes:
def encode_payload(self, data: Union[Payload, sedes.CountableList]) -> bytes:
if isinstance(data, dict):
if not isinstance(self.structure, tuple):
raise ValueError(
Expand All @@ -73,7 +73,7 @@ def encode_payload(self, data: Union[PayloadType, sedes.CountableList]) -> bytes
encoder = sedes.List([type_ for _, type_ in self.structure])
return rlp.encode(data, sedes=encoder)

def decode_payload(self, rlp_data: bytes) -> PayloadType:
def decode_payload(self, rlp_data: bytes) -> Payload:
if isinstance(self.structure, sedes.CountableList):
decoder = self.structure
else:
Expand All @@ -92,7 +92,7 @@ def decode_payload(self, rlp_data: bytes) -> PayloadType:
in zip(self.structure, data)
}

def decode(self, data: bytes) -> PayloadType:
def decode(self, data: bytes) -> Payload:
packet_type = get_devp2p_cmd_id(data)
if packet_type != self.cmd_id:
raise MalformedMessage(f"Wrong packet type: {packet_type}, expected {self.cmd_id}")
Expand Down Expand Up @@ -121,7 +121,7 @@ def compress_payload(self, raw_payload: bytes) -> bytes:
else:
return raw_payload

def encode(self, data: PayloadType) -> Tuple[bytes, bytes]:
def encode(self, data: Payload) -> Tuple[bytes, bytes]:
encoded_payload = self.encode_payload(data)
compressed_payload = self.compress_payload(encoded_payload)

Expand Down Expand Up @@ -170,7 +170,7 @@ def logger(self) -> logging.Logger:
self._logger = logging.getLogger(f"p2p.protocol.{type(self).__name__}")
return self._logger

def send_request(self, request: RequestAPI[PayloadType]) -> None:
def send_request(self, request: RequestAPI[Payload]) -> None:
command = self.cmd_by_type[request.cmd_type]
header, body = command.encode(request.command_payload)
self.transport.send(header, body)
Expand All @@ -179,7 +179,7 @@ def supports_command(self, cmd_type: Type[CommandAPI]) -> bool:
return cmd_type in self.cmd_by_type

@classmethod
def as_capability(cls) -> CapabilityType:
def as_capability(cls) -> Capability:
return (cls.name, cls.version)

def __repr__(self) -> str:
Expand All @@ -188,7 +188,7 @@ def __repr__(self) -> str:

@to_tuple
def match_protocols_with_capabilities(protocols: Sequence[Type[ProtocolAPI]],
capabilities: CapabilitiesType,
capabilities: Capabilities,
) -> Iterable[Type[ProtocolAPI]]:
"""
Return the `Protocol` classes that match with the provided `capabilities`
Expand Down
2 changes: 1 addition & 1 deletion p2p/tools/paragon/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def finalizer() -> None:
async def process_v4_p2p_handshake(
self: BasePeer,
cmd: protocol.Command,
msg: protocol.PayloadType) -> None:
msg: protocol.Payload) -> None:
"""
This function is the replacement to the existing process_p2p_handshake
function.
Expand Down
4 changes: 2 additions & 2 deletions p2p/tools/paragon/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
BasePeerFactory,
)
from p2p.peer_pool import BasePeerPool
from p2p.typing import PayloadType
from p2p.typing import Payload

from .proto import ParagonProtocol

Expand All @@ -22,7 +22,7 @@ async def send_sub_proto_handshake(self) -> None:
pass

async def process_sub_proto_handshake(
self, cmd: CommandAPI, msg: PayloadType) -> None:
self, cmd: CommandAPI, msg: Payload) -> None:
pass

async def do_sub_proto_handshake(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions p2p/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ class TypedDictPayload(TypedDict):
pass


PayloadType = Union[
Payload = Union[
Dict[str, Any],
List[rlp.Serializable],
Tuple[rlp.Serializable, ...],
TypedDictPayload,
]


StructureType = Union[
Structure = Union[
Tuple[Tuple[str, Any], ...],
]


CapabilityType = Tuple[str, int]
CapabilitiesType = Tuple[CapabilityType, ...]
Capability = Tuple[str, int]
Capabilities = Tuple[Capability, ...]
6 changes: 3 additions & 3 deletions trinity/protocol/bcc/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
BasePeerFactory,
)
from p2p.peer_pool import BasePeerPool
from p2p.protocol import PayloadType
from p2p.protocol import Payload

from trinity.db.beacon.chain import BaseAsyncBeaconChainDB
from trinity.protocol.bcc.handlers import BCCExchangeHandler
Expand Down Expand Up @@ -98,7 +98,7 @@ async def send_sub_proto_handshake(self) -> None:
head = await self.chain_db.coro_get_canonical_head(BeaconBlock)
self.sub_proto.send_handshake(genesis_root, head.slot, self.network_id)

async def process_sub_proto_handshake(self, cmd: CommandAPI, msg: PayloadType) -> None:
async def process_sub_proto_handshake(self, cmd: CommandAPI, msg: Payload) -> None:
if not isinstance(cmd, Status):
await self.disconnect(DisconnectReason.subprotocol_error)
raise HandshakeFailure(f"Expected a BCC Status msg, got {cmd}, disconnecting")
Expand Down Expand Up @@ -172,7 +172,7 @@ async def _run(self) -> None:
async def handle_native_peer_message(self,
remote: NodeAPI,
cmd: CommandAPI,
msg: PayloadType) -> None:
msg: Payload) -> None:

if isinstance(cmd, GetBeaconBlocks):
await self.event_bus.broadcast(GetBeaconBlocksEvent(remote, cmd, msg))
Expand Down
6 changes: 3 additions & 3 deletions trinity/protocol/bcc/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

from p2p.abc import CommandAPI, NodeAPI
from p2p.peer import BasePeer
from p2p.typing import PayloadType
from p2p.typing import Payload

from eth2.beacon.typing import (
Slot,
Expand Down Expand Up @@ -115,7 +115,7 @@ def __init__(self,
async def _handle_msg(self,
remote: NodeAPI,
cmd: CommandAPI,
msg: PayloadType) -> None:
msg: Payload) -> None:

self.logger.debug("cmd %s" % cmd)
if isinstance(cmd, GetBeaconBlocks):
Expand Down Expand Up @@ -328,7 +328,7 @@ def __init__(
self.orphan_block_pool = OrphanBlockPool()

async def _handle_msg(self, base_peer: BasePeer, cmd: CommandAPI,
msg: PayloadType) -> None:
msg: Payload) -> None:
peer = cast(BCCPeer, base_peer)
self.logger.debug("cmd %s" % cmd)
if isinstance(cmd, Attestations):
Expand Down
4 changes: 2 additions & 2 deletions trinity/protocol/common/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from eth.rlp.headers import BlockHeader

from p2p.protocol import Command
from p2p.typing import PayloadType
from p2p.typing import Payload


class BaseBlockHeaders(Command):

@abstractmethod
def extract_headers(self, msg: PayloadType) -> Tuple[BlockHeader, ...]:
def extract_headers(self, msg: Payload) -> Tuple[BlockHeader, ...]:
raise NotImplementedError("Must be implemented by subclasses")
4 changes: 2 additions & 2 deletions trinity/protocol/common/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from p2p.abc import CommandAPI, NodeAPI
from p2p.disconnect import DisconnectReason
from p2p.typing import PayloadType
from p2p.typing import Payload


@dataclass
Expand Down Expand Up @@ -90,4 +90,4 @@ class PeerPoolMessageEvent(BaseEvent):
"""
remote: NodeAPI
cmd: CommandAPI
msg: PayloadType
msg: Payload
Loading

0 comments on commit 74e2be6

Please sign in to comment.