mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
linter respacing
This commit is contained in:
@ -1,20 +1,20 @@
|
||||
import time
|
||||
import threading
|
||||
from typing import Optional
|
||||
import time
|
||||
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
from libp2p.abc import IPeerRecord
|
||||
import libp2p.peer.pb.peer_record_pb2 as pb
|
||||
|
||||
from multiaddr import Multiaddr
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
from libp2p.peer.id import ID
|
||||
|
||||
import libp2p.peer.pb.peer_record_pb2 as pb
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
|
||||
PEER_RECORD_ENVELOPE_DOMAIN = "libp2p-peer-record"
|
||||
PEER_RECORD_ENVELOPE_PAYLOAD_TYPE = b'\x03\x01'
|
||||
PEER_RECORD_ENVELOPE_PAYLOAD_TYPE = b"\x03\x01"
|
||||
|
||||
_last_timestamp_lock = threading.Lock()
|
||||
_last_timestamp: int = 0
|
||||
|
||||
|
||||
class PeerRecord(IPeerRecord):
|
||||
"""
|
||||
A record that contains metatdata about a peer in the libp2p network.
|
||||
@ -27,15 +27,16 @@ class PeerRecord(IPeerRecord):
|
||||
|
||||
PeerRecords are designed to be signed and transmitted in libp2p routing Envelopes.
|
||||
"""
|
||||
|
||||
peer_id: ID
|
||||
addrs: list[Multiaddr]
|
||||
seq: int
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
peer_id: Optional[ID] = None,
|
||||
addrs: Optional[list[Multiaddr]] = None,
|
||||
seq: Optional[int] = None,
|
||||
peer_id: ID | None = None,
|
||||
addrs: list[Multiaddr] | None = None,
|
||||
seq: int | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Initialize a new PeerRecord.
|
||||
@ -127,6 +128,7 @@ class PeerRecord(IPeerRecord):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def unmarshal_record(data: bytes) -> PeerRecord:
|
||||
"""
|
||||
Deserialize a PeerRecord from its serialized byte representation.
|
||||
@ -153,6 +155,7 @@ def unmarshal_record(data: bytes) -> PeerRecord:
|
||||
|
||||
return record
|
||||
|
||||
|
||||
def timestamp_seq() -> int:
|
||||
"""
|
||||
Generate a strictly increasing timestamp-based sequence number.
|
||||
@ -185,6 +188,7 @@ def peer_record_from_peer_info(info: PeerInfo) -> PeerRecord:
|
||||
record.addrs = info.addrs
|
||||
return record
|
||||
|
||||
|
||||
def peer_record_from_protobuf(msg: pb.PeerRecord) -> PeerRecord:
|
||||
"""
|
||||
Convert a protobuf PeerRecord message into a PeerRecord object.
|
||||
@ -220,6 +224,7 @@ def addrs_from_protobuf(addrs: list[pb.PeerRecord.AddressInfo]) -> list[Multiadd
|
||||
continue
|
||||
return out
|
||||
|
||||
|
||||
def addrs_to_protobuf(addrs: list[Multiaddr]) -> list[pb.PeerRecord.AddressInfo]:
|
||||
"""
|
||||
Convert a list of Multiaddr objects into their protobuf representation.
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
import time
|
||||
from libp2p.peer.id import ID
|
||||
import libp2p.peer.pb.peer_record_pb2 as pb
|
||||
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
from libp2p.peer.id import ID
|
||||
import libp2p.peer.pb.peer_record_pb2 as pb
|
||||
from libp2p.peer.peer_record import (
|
||||
PeerRecord,
|
||||
addrs_from_protobuf,
|
||||
@ -12,6 +13,7 @@ from libp2p.peer.peer_record import (
|
||||
|
||||
# Testing methods from PeerRecord base class and PeerRecord protobuf:
|
||||
|
||||
|
||||
def test_basic_protobuf_serializatrion_deserialization():
|
||||
record = pb.PeerRecord()
|
||||
record.seq = 1
|
||||
@ -22,6 +24,7 @@ def test_basic_protobuf_serializatrion_deserialization():
|
||||
|
||||
assert new_record.seq == 1
|
||||
|
||||
|
||||
def test_timestamp_seq_monotonicity():
|
||||
rec1 = PeerRecord()
|
||||
time.sleep(1)
|
||||
@ -31,6 +34,7 @@ def test_timestamp_seq_monotonicity():
|
||||
assert isinstance(rec2.seq, int)
|
||||
assert rec2.seq > rec1.seq, f"Expected seq2 ({rec2.seq}) > seq1 ({rec1.seq})"
|
||||
|
||||
|
||||
def test_addrs_from_protobuf_multiple_addresses():
|
||||
ma1 = Multiaddr("/ip4/127.0.0.1/tcp/4001")
|
||||
ma2 = Multiaddr("/ip4/127.0.0.1/tcp/4002")
|
||||
@ -44,6 +48,7 @@ def test_addrs_from_protobuf_multiple_addresses():
|
||||
result = addrs_from_protobuf([addr_info1, addr_info2])
|
||||
assert result == [ma1, ma2]
|
||||
|
||||
|
||||
def test_peer_record_from_protobuf():
|
||||
peer_id = ID.from_base58("QmNM23MiU1Kd7yfiKVdUnaDo8RYca8By4zDmr7uSaVV8Px")
|
||||
record = pb.PeerRecord()
|
||||
@ -64,6 +69,7 @@ def test_peer_record_from_protobuf():
|
||||
assert str(result.addrs[0]) == "/ip4/127.0.0.1/tcp/4001"
|
||||
assert str(result.addrs[1]) == "/ip4/127.0.0.1/tcp/4002"
|
||||
|
||||
|
||||
def test_to_protobuf_generates_correct_message():
|
||||
peer_id = ID.from_base58("QmNM23MiU1Kd7yfiKVdUnaDo8RYca8By4zDmr7uSaVV8Px")
|
||||
addrs = [Multiaddr("/ip4/127.0.0.1/tcp/4001")]
|
||||
@ -78,11 +84,12 @@ def test_to_protobuf_generates_correct_message():
|
||||
assert len(proto.addresses) == 1
|
||||
assert proto.addresses[0].multiaddr == addrs[0].to_bytes()
|
||||
|
||||
|
||||
def test_unmarshal_record_roundtrip():
|
||||
record = PeerRecord(
|
||||
peer_id = ID.from_base58("QmNM23MiU1Kd7yfiKVdUnaDo8RYca8By4zDmr7uSaVV8Px"),
|
||||
addrs = [Multiaddr("/ip4/127.0.0.1/tcp/4001")],
|
||||
seq = 999,
|
||||
peer_id=ID.from_base58("QmNM23MiU1Kd7yfiKVdUnaDo8RYca8By4zDmr7uSaVV8Px"),
|
||||
addrs=[Multiaddr("/ip4/127.0.0.1/tcp/4001")],
|
||||
seq=999,
|
||||
)
|
||||
|
||||
serialized = record.to_protobuf().SerializeToString()
|
||||
@ -93,6 +100,7 @@ def test_unmarshal_record_roundtrip():
|
||||
assert len(deserialized.addrs) == 1
|
||||
assert deserialized.addrs[0] == record.addrs[0]
|
||||
|
||||
|
||||
def test_marshal_record_and_equal():
|
||||
peer_id = ID.from_base58("QmNM23MiU1Kd7yfiKVdUnaDo8RYca8By4zDmr7uSaVV8Px")
|
||||
addrs = [Multiaddr("/ip4/127.0.0.1/tcp/4001")]
|
||||
|
||||
Reference in New Issue
Block a user