Skip to content

Commit ca05454

Browse files
authored
Remove chain info property from LESPeer (#1264)
1 parent b08a754 commit ca05454

File tree

8 files changed

+43
-63
lines changed

8 files changed

+43
-63
lines changed

p2p/peer.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103

104104
if TYPE_CHECKING:
105105
from trinity.db.header import BaseAsyncHeaderDB # noqa: F401
106+
from trinity.protocol.common.proto import ChainInfo # noqa: F401
106107
from trinity.protocol.eth.requests import HeaderRequest # noqa: F401
107108
from trinity.protocol.base_request import BaseRequest # noqa: F401
108109

@@ -291,6 +292,7 @@ async def genesis(self) -> BlockHeader:
291292

292293
@property
293294
async def _local_chain_info(self) -> 'ChainInfo':
295+
from trinity.protocol.common.proto import ChainInfo # noqa: F811
294296
genesis = await self.genesis
295297
head = await self.wait(self.headerdb.coro_get_canonical_head())
296298
total_difficulty = await self.headerdb.coro_get_score(head.hash)
@@ -1017,18 +1019,6 @@ async def __anext__(self) -> BasePeer:
10171019
}
10181020

10191021

1020-
class ChainInfo:
1021-
def __init__(self,
1022-
block_number: int,
1023-
block_hash: Hash32,
1024-
total_difficulty: int,
1025-
genesis_hash: Hash32) -> None:
1026-
self.block_number = block_number
1027-
self.block_hash = block_hash
1028-
self.total_difficulty = total_difficulty
1029-
self.genesis_hash = genesis_hash
1030-
1031-
10321022
def _test() -> None:
10331023
"""
10341024
Create a Peer instance connected to a local geth instance and log messages exchanged with it.

tests/trinity/integration/test_lightchain_integration.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,8 @@ async def wait_for_header_sync(block_number):
207207
'0xf709ed2c57efc18a1675e8c740f3294c9e2cb36ba7bb3b89d3ab4c8fef9d8860')
208208

209209
assert len(peer_pool) == 1
210-
head_info = peer_pool.highest_td_peer.head_info
211-
head = await peer_chain.get_block_header_by_hash(head_info.block_hash)
212-
assert head.block_number == head_info.block_number
210+
peer = peer_pool.highest_td_peer
211+
head = await peer_chain.get_block_header_by_hash(peer.head_hash)
213212

214213
# In order to answer queries for contract code, geth needs the state trie entry for the block
215214
# we specify in the query, but because of fast sync we can only assume it has that for recent

trinity/protocol/common/proto.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import NamedTuple
2+
3+
from eth_typing import (
4+
BlockNumber,
5+
Hash32,
6+
)
7+
8+
9+
class ChainInfo(NamedTuple):
10+
block_number: BlockNumber
11+
block_hash: Hash32
12+
total_difficulty: int
13+
genesis_hash: Hash32

trinity/protocol/eth/proto.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import (
33
List,
44
Tuple,
5-
TYPE_CHECKING,
65
Union,
76
)
87

@@ -19,6 +18,7 @@
1918
Protocol,
2019
)
2120

21+
from trinity.protocol.common.proto import ChainInfo
2222
from trinity.rlp.block_body import BlockBody
2323

2424
from .commands import (
@@ -36,11 +36,6 @@
3636
Transactions,
3737
)
3838

39-
if TYPE_CHECKING:
40-
from p2p.peer import ( # noqa: F401
41-
ChainInfo
42-
)
43-
4439

4540
class ETHProtocol(Protocol):
4641
name = 'eth'
@@ -52,13 +47,13 @@ class ETHProtocol(Protocol):
5247
cmd_length = 17
5348
logger = logging.getLogger("p2p.eth.ETHProtocol")
5449

55-
def send_handshake(self, head_info: 'ChainInfo') -> None:
50+
def send_handshake(self, chain_info: ChainInfo) -> None:
5651
resp = {
5752
'protocol_version': self.version,
5853
'network_id': self.peer.network_id,
59-
'td': head_info.total_difficulty,
60-
'best_hash': head_info.block_hash,
61-
'genesis_hash': head_info.genesis_hash,
54+
'td': chain_info.total_difficulty,
55+
'best_hash': chain_info.block_hash,
56+
'genesis_hash': chain_info.genesis_hash,
6257
}
6358
cmd = Status(self.cmd_id_offset)
6459
self.logger.debug("Sending ETH/Status msg: %s", resp)

trinity/protocol/les/commands.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,6 @@ def _serialize_item(self, key: str, value: bytes) -> bytes:
111111
# See comment in the definition of item_sedes as to why we do this.
112112
return b''
113113

114-
def as_head_info(self, decoded: _DecodedMsgType) -> HeadInfo:
115-
decoded = cast(Dict[str, Any], decoded)
116-
return HeadInfo(
117-
block_number=decoded['headNum'],
118-
block_hash=decoded['headHash'],
119-
total_difficulty=decoded['headTd'],
120-
reorg_depth=0,
121-
)
122-
123114

124115
class Announce(Command):
125116
_cmd_id = 1
@@ -128,19 +119,10 @@ class Announce(Command):
128119
('head_number', sedes.big_endian_int),
129120
('head_td', sedes.big_endian_int),
130121
('reorg_depth', sedes.big_endian_int),
122+
# TODO: The params CountableList may contain any of the values from the
123+
# Status msg. Need to extend this command to process that too.
131124
('params', sedes.CountableList(sedes.List([sedes.text, sedes.raw]))),
132125
]
133-
# TODO: The params CountableList above may contain any of the values from the Status msg.
134-
# Need to extend this command to process that too.
135-
136-
def as_head_info(self, decoded: _DecodedMsgType) -> HeadInfo:
137-
decoded = cast(Dict[str, Any], decoded)
138-
return HeadInfo(
139-
block_number=decoded['head_number'],
140-
block_hash=decoded['head_hash'],
141-
total_difficulty=decoded['head_td'],
142-
reorg_depth=decoded['reorg_depth'],
143-
)
144126

145127

146128
class GetBlockHeadersQuery(rlp.Serializable):

trinity/protocol/les/peer.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
cast,
44
Dict,
55
List,
6+
Union,
7+
)
8+
9+
from eth_typing import (
10+
BlockNumber,
11+
Hash32,
612
)
713

814
from eth_utils import encode_hex
@@ -19,7 +25,6 @@
1925

2026
from .commands import (
2127
Announce,
22-
HeadInfo,
2328
Status,
2429
StatusV2,
2530
)
@@ -38,10 +43,9 @@ class LESPeer(BasePeer):
3843

3944
_supported_sub_protocols = [LESProtocol, LESProtocolV2]
4045
sub_proto: LESProtocol = None
41-
# TODO: This will no longer be needed once we've fixed #891, and then it should be removed.
42-
head_info: HeadInfo = None
4346

4447
_requests: LESExchangeHandler = None
48+
head_number: BlockNumber = None
4549

4650
def get_extra_stats(self) -> List[str]:
4751
stats_pairs = self.requests.get_stats().items()
@@ -54,10 +58,11 @@ def requests(self) -> LESExchangeHandler:
5458
return self._requests
5559

5660
def handle_sub_proto_msg(self, cmd: Command, msg: _DecodedMsgType) -> None:
61+
head_info = cast(Dict[str, Union[int, Hash32, BlockNumber]], msg)
5762
if isinstance(cmd, Announce):
58-
self.head_info = cmd.as_head_info(msg)
59-
self.head_td = self.head_info.total_difficulty
60-
self.head_hash = self.head_info.block_hash
63+
self.head_td = head_info['head_td']
64+
self.head_hash = head_info['head_hash']
65+
self.head_number = head_info['head_number']
6166

6267
super().handle_sub_proto_msg(cmd, msg)
6368

@@ -83,6 +88,6 @@ async def process_sub_proto_handshake(
8388
"{} genesis ({}) does not match ours ({}), disconnecting".format(
8489
self, encode_hex(msg['genesisHash']), genesis.hex_hash))
8590
# TODO: Disconnect if the remote doesn't serve headers.
86-
self.head_info = cmd.as_head_info(msg)
87-
self.head_td = self.head_info.total_difficulty
88-
self.head_hash = self.head_info.block_hash
91+
self.head_td = msg['headTd']
92+
self.head_hash = msg['headHash']
93+
self.head_number = msg['headNum']

trinity/protocol/les/proto.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import (
22
List,
33
Tuple,
4-
TYPE_CHECKING,
54
Union,
65
)
76

@@ -16,6 +15,8 @@
1615
Protocol,
1716
)
1817

18+
from trinity.protocol.common.proto import ChainInfo
19+
1920
from .commands import (
2021
Status,
2122
StatusV2,
@@ -38,11 +39,6 @@
3839
)
3940
from . import constants
4041

41-
if TYPE_CHECKING:
42-
from p2p.peer import ( # noqa: F401
43-
ChainInfo
44-
)
45-
4642

4743
class LESProtocol(Protocol):
4844
name = 'les'
@@ -51,7 +47,7 @@ class LESProtocol(Protocol):
5147
ContractCodes]
5248
cmd_length = 15
5349

54-
def send_handshake(self, chain_info: 'ChainInfo') -> None:
50+
def send_handshake(self, chain_info: ChainInfo) -> None:
5551
resp = {
5652
'protocolVersion': self.version,
5753
'networkId': self.peer.network_id,
@@ -148,7 +144,7 @@ class LESProtocolV2(LESProtocol):
148144
ProofsV2, ContractCodes]
149145
cmd_length = 21
150146

151-
def send_handshake(self, chain_info: 'ChainInfo') -> None:
147+
def send_handshake(self, chain_info: ChainInfo) -> None:
152148
resp = {
153149
'announceType': constants.LES_ANNOUNCE_SIMPLE,
154150
'protocolVersion': self.version,

trinity/sync/light/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ async def _raise_for_empty_code(
278278
# our best peer doesn't have the header we want, there are no eligible peers.
279279
raise NoEligiblePeers("Our best peer does not have the header %s" % block_hash)
280280

281-
head_number = peer.head_info.block_number
281+
head_number = peer.head_number
282282
if head_number - header.block_number > MAX_REORG_DEPTH:
283283
# The peer claims to be far ahead of the header we requested
284284
if self.headerdb.get_canonical_block_hash(header.block_number) == block_hash:

0 commit comments

Comments
 (0)