Add ttl for peer data expiration (#655)

* Add ttl and last_identified to peerdata

* Add test for ttl

Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com>

* Fix lint and add newsfragments

Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com>

* Fix failing ci

Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com>

* fix ttl time from 600 to 120

Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com>

* fix test ttl timeout and lint errors

Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com>

* Fix docstrings

Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com>

* rebase main

* remove print statement

---------

Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com>
Co-authored-by: pacrob <5199899+pacrob@users.noreply.github.com>
This commit is contained in:
Sukhman Singh
2025-06-10 00:12:59 +05:30
committed by GitHub
parent bdadec7519
commit 22d93b39ae
8 changed files with 158 additions and 16 deletions

View File

@ -1,3 +1,5 @@
import time
import pytest
from multiaddr import Multiaddr
@ -18,9 +20,34 @@ def test_peer_info_empty():
def test_peer_info_basic():
store = PeerStore()
store.add_addr(ID(b"peer"), Multiaddr("/ip4/127.0.0.1/tcp/4001"), 10)
info = store.peer_info(ID(b"peer"))
store.add_addr(ID(b"peer"), Multiaddr("/ip4/127.0.0.1/tcp/4001"), 1)
# update ttl to new value
store.add_addr(ID(b"peer"), Multiaddr("/ip4/127.0.0.1/tcp/4002"), 2)
time.sleep(1)
info = store.peer_info(ID(b"peer"))
assert info.peer_id == ID(b"peer")
assert info.addrs == [
Multiaddr("/ip4/127.0.0.1/tcp/4001"),
Multiaddr("/ip4/127.0.0.1/tcp/4002"),
]
# Check that addresses are cleared after ttl
time.sleep(2)
info = store.peer_info(ID(b"peer"))
assert info.peer_id == ID(b"peer")
assert info.addrs == []
assert store.peer_ids() == [ID(b"peer")]
assert store.valid_peer_ids() == []
# Check if all the data remains valid if ttl is set to default(0)
def test_peer_permanent_ttl():
store = PeerStore()
store.add_addr(ID(b"peer"), Multiaddr("/ip4/127.0.0.1/tcp/4001"))
time.sleep(1)
info = store.peer_info(ID(b"peer"))
assert info.peer_id == ID(b"peer")
assert info.addrs == [Multiaddr("/ip4/127.0.0.1/tcp/4001")]

View File

@ -44,12 +44,12 @@ async def test_simple_two_nodes():
@pytest.mark.trio
async def test_timed_cache_two_nodes():
# Two nodes using LastSeenCache with a TTL of 120 seconds
# Two nodes using LastSeenCache with a TTL of 10 seconds
def get_msg_id(msg):
return msg.data + msg.from_id
async with PubsubFactory.create_batch_with_floodsub(
2, seen_ttl=120, msg_id_constructor=get_msg_id
2, seen_ttl=10, msg_id_constructor=get_msg_id
) as pubsubs_fsub:
message_indices = [1, 1, 2, 1, 3, 1, 4, 1, 5, 1]
expected_received_indices = [1, 2, 3, 4, 5]