Skip to content

Commit

Permalink
First version of code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hoi committed Oct 31, 2018
1 parent 5dabbbc commit 133b687
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
6 changes: 2 additions & 4 deletions trinity/protocol/les/exchanges.py
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions trinity/protocol/les/normalizers.py
Expand Up @@ -8,22 +8,33 @@
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, ...]:
result = message['headers']
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
5 changes: 3 additions & 2 deletions trinity/protocol/les/requests.py
Expand Up @@ -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

Expand All @@ -67,7 +68,7 @@ def __init__(self,
}


class GetBlockBodiesRequest(BaseRequest[Tuple[Hash32, ...]]):
class GetBlockBodiesRequest(LESRequest):
cmd_type = GetBlockBodies
response_type = BlockBodies

Expand Down
9 changes: 4 additions & 5 deletions trinity/protocol/les/trackers.py
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
4 changes: 1 addition & 3 deletions trinity/protocol/les/validators.py
Expand Up @@ -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:
Expand Down

0 comments on commit 133b687

Please sign in to comment.