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