Skip to content

Commit 3d369bc

Browse files
committed
Proto-Book: added tests
1 parent 5de4584 commit 3d369bc

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

libp2p/peer/peerdata.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def __init__(self) -> None:
4646
self.ttl = 0
4747
self.latmap = 0
4848

49+
# --------PROTO-BOOK--------
50+
4951
def get_protocols(self) -> list[str]:
5052
"""
5153
:return: all protocols associated with given peer
@@ -94,6 +96,7 @@ def clear_protocol_data(self) -> None:
9496
"""Clear all protocols"""
9597
self.protocols = []
9698

99+
# -------ADDR-BOOK---------
97100
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
98101
"""
99102
:param addrs: multiaddresses to add
@@ -112,6 +115,9 @@ def clear_addrs(self) -> None:
112115
"""Clear all addresses."""
113116
self.addrs = []
114117

118+
# TODO! ADDRS_STREAM
119+
120+
# -------METADATA-----------
115121
def put_metadata(self, key: str, val: Any) -> None:
116122
"""
117123
:param key: key in KV pair
@@ -133,6 +139,7 @@ def clear_metadata(self) -> None:
133139
"""Clears metadata."""
134140
self.metadata = {}
135141

142+
# -------KEY-BOOK---------------
136143
def add_pubkey(self, pubkey: PublicKey) -> None:
137144
"""
138145
:param pubkey:
@@ -168,6 +175,7 @@ def clear_keydata(self) -> None:
168175
self.pubkey = None
169176
self.privkey = None
170177

178+
# ----------METRICS--------------
171179
def record_latency(self, new_latency: float) -> None:
172180
"""
173181
Records a new latency measurement for the given peer
@@ -196,6 +204,7 @@ def clear_metrics(self) -> None:
196204
def update_last_identified(self) -> None:
197205
self.last_identified = int(time.time())
198206

207+
# ----------TTL------------------
199208
def get_last_identified(self) -> int:
200209
"""
201210
:return: last identified timestamp

libp2p/peer/peerstore.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ def peer_ids(self) -> list[ID]:
6262
def clear_peerdata(self, peer_id: ID) -> None:
6363
"""Clears the peer data of the peer"""
6464

65+
def valid_peer_ids(self) -> list[ID]:
66+
"""
67+
:return: all of the valid peer IDs stored in peer store
68+
"""
69+
valid_peer_ids: list[ID] = []
70+
for peer_id, peer_data in self.peer_data_map.items():
71+
if not peer_data.is_expired():
72+
valid_peer_ids.append(peer_id)
73+
else:
74+
peer_data.clear_addrs()
75+
return valid_peer_ids
76+
77+
# --------PROTO-BOOK--------
78+
6579
def get_protocols(self, peer_id: ID) -> list[str]:
6680
"""
6781
:param peer_id: peer ID to get protocols for
@@ -112,17 +126,7 @@ def clear_protocol_data(self, peer_id: ID) -> None:
112126
peer_data = self.peer_data_map[peer_id]
113127
peer_data.clear_protocol_data()
114128

115-
def valid_peer_ids(self) -> list[ID]:
116-
"""
117-
:return: all of the valid peer IDs stored in peer store
118-
"""
119-
valid_peer_ids: list[ID] = []
120-
for peer_id, peer_data in self.peer_data_map.items():
121-
if not peer_data.is_expired():
122-
valid_peer_ids.append(peer_id)
123-
else:
124-
peer_data.clear_addrs()
125-
return valid_peer_ids
129+
# ------METADATA---------
126130

127131
def get(self, peer_id: ID, key: str) -> Any:
128132
"""
@@ -153,6 +157,8 @@ def clear_metadata(self, peer_id: ID) -> None:
153157
peer_data = self.peer_data_map[peer_id]
154158
peer_data.clear_metadata()
155159

160+
# -------ADDR-BOOK--------
161+
156162
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int = 0) -> None:
157163
"""
158164
:param peer_id: peer ID to add address for
@@ -215,6 +221,8 @@ def addr_stream(self, peer_id: ID) -> None:
215221
"""addr_stream"""
216222
# TODO!
217223

224+
# -------KEY-BOOK---------
225+
218226
def add_pubkey(self, peer_id: ID, pubkey: PublicKey) -> None:
219227
"""
220228
:param peer_id: peer ID to add public key for
@@ -288,6 +296,8 @@ def clear_keydata(self, peer_id: ID) -> None:
288296
peer_data = self.peer_data_map[peer_id]
289297
peer_data.clear_keydata()
290298

299+
# --------METRICS--------
300+
291301
def record_latency(self, peer_id: ID, RTT: float) -> None:
292302
"""
293303
Records a new latency measurement for the given peer

tests/core/peer/test_peerdata.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,56 @@ def test_set_protocols():
3939
assert peer_data.get_protocols() == protocols
4040

4141

42+
# Test case when removing protocols:
43+
def test_remove_protocols():
44+
peer_data = PeerData()
45+
protocols: Sequence[str] = ["protocol1", "protocol2"]
46+
peer_data.set_protocols(protocols)
47+
48+
peer_data.remove_protocols(["protocol1"])
49+
assert peer_data.get_protocols() == ["protocol2"]
50+
51+
52+
# Test case when supports protocols:
53+
def test_supports_protocols():
54+
peer_data = PeerData()
55+
peer_data.set_protocols(["protocol1", "protocol2", "protocol3"])
56+
57+
input_protocols = ["protocol1", "protocol4", "protocol2"]
58+
supported = peer_data.supports_protocols(input_protocols)
59+
60+
assert supported == ["protocol1", "protocol2"]
61+
62+
63+
def test_first_supported_protocol_found():
64+
peer_data = PeerData()
65+
peer_data.set_protocols(["protocolA", "protocolB"])
66+
67+
input_protocols = ["protocolC", "protocolB", "protocolA"]
68+
first = peer_data.first_supported_protocol(input_protocols)
69+
70+
assert first == "protocolB"
71+
72+
73+
def test_first_supported_protocol_none():
74+
peer_data = PeerData()
75+
peer_data.set_protocols(["protocolX", "protocolY"])
76+
77+
input_protocols = ["protocolA", "protocolB"]
78+
first = peer_data.first_supported_protocol(input_protocols)
79+
80+
assert first == "None supported"
81+
82+
83+
def test_clear_protocol_data():
84+
peer_data = PeerData()
85+
peer_data.set_protocols(["proto1", "proto2"])
86+
87+
peer_data.clear_protocol_data()
88+
89+
assert peer_data.get_protocols() == []
90+
91+
4292
# Test case when adding addresses
4393
def test_add_addrs():
4494
peer_data = PeerData()

0 commit comments

Comments
 (0)