From 12fc090de995e79cdb22ea7c7bbd8fb42d2799f2 Mon Sep 17 00:00:00 2001 From: Hyon Lee Date: Wed, 31 Oct 2018 06:51:45 +0100 Subject: [PATCH] First version of code changes --- trinity/protocol/les/exchanges.py | 6 ++---- trinity/protocol/les/handlers.py | 2 +- trinity/protocol/les/normalizers.py | 23 +++++++++++++++++------ trinity/protocol/les/requests.py | 5 +++-- trinity/protocol/les/trackers.py | 9 ++++----- trinity/protocol/les/validators.py | 4 +--- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/trinity/protocol/les/exchanges.py b/trinity/protocol/les/exchanges.py index 42d9fe796b..8be9a140e1 100644 --- a/trinity/protocol/les/exchanges.py +++ b/trinity/protocol/les/exchanges.py @@ -11,9 +11,7 @@ from trinity.protocol.common.exchanges import ( BaseExchange, ) -# Q: What is the order to imports? and why sometimes new line between imports and sometimes not? from trinity.protocol.common.types import BlockBodyBundles -from trinity.rlp.block_body import BlockBody from trinity.utils.les import ( gen_request_id, ) @@ -69,8 +67,8 @@ async def __call__( # type: ignore timeout, ) -# Q: Where can I find the correct signature for this class? -class GetBlockBodiesExchange(LESExchange[Tuple[BlockBody, ...]]): + +class GetBlockBodiesExchange(LESExchange[BlockBodyBundles]): _normalizer = GetBlockBodiesNormalizer() request_class = GetBlockBodiesRequest tracker_class = GetBlockBodiesTracker diff --git a/trinity/protocol/les/handlers.py b/trinity/protocol/les/handlers.py index 36abb01a70..e0ed472e94 100644 --- a/trinity/protocol/les/handlers.py +++ b/trinity/protocol/les/handlers.py @@ -8,7 +8,7 @@ ) -class ETHExchangeHandler(BaseChainExchangeHandler): +class LESExchangeHandler(BaseChainExchangeHandler): _exchange_config = { 'get_block_bodies': GetBlockBodiesExchange, 'get_block_headers': GetBlockHeadersExchange, diff --git a/trinity/protocol/les/normalizers.py b/trinity/protocol/les/normalizers.py index 4a7920641a..4fc604eb6e 100644 --- a/trinity/protocol/les/normalizers.py +++ b/trinity/protocol/les/normalizers.py @@ -8,13 +8,13 @@ from eth.rlp.headers import BlockHeader from trinity.protocol.common.normalizers import BaseNormalizer -from trinity.rlp.block_body import BlockBody +from trinity.protocol.common.types import BlockBodyBundles + TResult = TypeVar('TResult') LESNormalizer = BaseNormalizer[Dict[str, Any], TResult] -# Q: Shouldn't this be named GetBlockHeadersNormalizer? class BlockHeadersNormalizer(LESNormalizer[Tuple[BlockHeader, ...]]): @staticmethod def normalize_result(message: Dict[str, Any]) -> Tuple[BlockHeader, ...]: @@ -22,8 +22,19 @@ def normalize_result(message: Dict[str, Any]) -> Tuple[BlockHeader, ...]: return result -class GetBlockBodiesNormalizer(LESNormalizer[Tuple[BlockBody, ...]]): +class GetBlockBodiesNormalizer(LESNormalizer[BlockBodyBundles]): @staticmethod - def normalize_result(message: Dict[str, Any]) -> Tuple[BlockBody, ...]: - result = message['bodies'] - return result + def normalize_result(message: Dict[str, Any]) -> BlockBodyBundles: + bodies = message['bodies'] + + uncles_hashes = tuple(map( + compose(keccak, rlp.encode), + tuple(body.uncles for body in bodies) + )) + transaction_roots_and_trie_data = tuple(map( + make_trie_root_and_nodes, + tuple(body.transactions for body in bodies) + )) + + body_bundles = tuple(zip(bodies, transaction_roots_and_trie_data, uncles_hashes)) + return body_bundles diff --git a/trinity/protocol/les/requests.py b/trinity/protocol/les/requests.py index 950d25dd16..6a2efa8f58 100644 --- a/trinity/protocol/les/requests.py +++ b/trinity/protocol/les/requests.py @@ -45,8 +45,9 @@ def __init__(self, self.reverse = reverse self.request_id = request_id +LESRequest = BaseRequest[Dict[str, Any]] -class GetBlockHeadersRequest(BaseRequest[Dict[str, Any]]): +class GetBlockHeadersRequest(LESRequest): cmd_type = GetBlockHeaders response_type = BlockHeaders @@ -67,7 +68,7 @@ def __init__(self, } -class GetBlockBodiesRequest(BaseRequest[Tuple[Hash32, ...]]): +class GetBlockBodiesRequest(LESRequest): cmd_type = GetBlockBodies response_type = BlockBodies diff --git a/trinity/protocol/les/trackers.py b/trinity/protocol/les/trackers.py index 31729aaf3d..3e5257fd8c 100644 --- a/trinity/protocol/les/trackers.py +++ b/trinity/protocol/les/trackers.py @@ -6,7 +6,7 @@ from eth.rlp.headers import BlockHeader from trinity.protocol.common.trackers import BasePerformanceTracker -from trinity.rlp.block_body import BlockBody +from trinity.protocol.common.types import BlockBodyBundles from trinity.utils.headers import sequence_builder from .requests import ( @@ -43,17 +43,16 @@ def _get_result_item_count(self, result: Tuple[BlockHeader, ...]) -> int: BaseGetBlockBodiesTracker = BasePerformanceTracker[ GetBlockBodiesRequest, - Tuple[BlockBody, ...], + BlockBodyBundles, ] -# Q: Where can I find the signature for this class? class GetBlockBodiesTracker(BaseGetBlockBodiesTracker): def _get_request_size(self, request: GetBlockBodiesRequest) -> Optional[int]: return len(request.command_payload['block_hashes']) - def _get_result_size(self, result: Tuple[BlockBody, ...]) -> int: + def _get_result_size(self, result: BlockBodyBundles) -> int: return len(result) - def _get_result_item_count(self, result: Tuple[BlockBody, ...]) -> int: + def _get_result_item_count(self, result: BlockBodyBundles) -> int: return len(result) diff --git a/trinity/protocol/les/validators.py b/trinity/protocol/les/validators.py index a61e3f560f..f4326b7c92 100644 --- a/trinity/protocol/les/validators.py +++ b/trinity/protocol/les/validators.py @@ -39,9 +39,7 @@ def validate_result(self, response: BlockBodyBundles) -> None: } unexpected_keys = actual_keys.difference(expected_keys) if unexpected_keys: - raise ValidationError( - "Got {0} unexpected block bodies".format(len(unexpected_keys)) - ) + raise ValidationError(f"Got {len(unexpected_keys)} unexpected block bodies") def match_payload_request_id(request: Dict[str, Any], response: Dict[str, Any]) -> None: