Skip to content

Commit

Permalink
remove serde related code
Browse files Browse the repository at this point in the history
  • Loading branch information
drHuangMHT committed Feb 28, 2025
1 parent 00d8d33 commit e6f3d46
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 152 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions misc/peer-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ rust-version.workspace = true
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
lru = "0.12.3"
serde = { version = "1.0.217", features = ["derive"], optional = true }
libp2p-identity = { workspace = true, optional = true}

[dev-dependencies]
Expand All @@ -23,6 +22,3 @@ serde_json = { version = "1.0.134" }

[lints]
workspace = true

[features]
serde = ["dep:serde", "libp2p-identity/serde"]
147 changes: 0 additions & 147 deletions misc/peer-store/src/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@
//! let store: MemoryStore<()> = MemoryStore::new(Default::default());
//! let behaviour = Behaviour::new(store);
//! ```
//!
//! ## Persistent storage
//! [`MemoryStore`] keeps all records in memory and will lose all the data
//! once de-allocated. In order to persist the data, enable `serde` feature
//! of `lib2p-peer-store` to read from and write to disk.
use std::{
collections::{HashMap, VecDeque},
num::NonZeroUsize,
task::Waker,
};

#[cfg(feature = "serde")]
use ::serde::{Deserialize, Serialize};
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::FromSwarm;
use lru::LruCache;
Expand All @@ -37,24 +30,14 @@ pub enum Event {
/// A in-memory store that uses LRU cache for bounded storage of addresses
/// and a frequency-based ordering of addresses.
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(
serialize = "T: Clone + Serialize ",
deserialize = "T: Clone + Deserialize<'de>"
))
)]
pub struct MemoryStore<T = ()> {
/// The internal store.
records: HashMap<PeerId, PeerRecord<T>>,
/// Events to emit to [`Behaviour`](crate::Behaviour) and [`Swarm`](libp2p_swarm::Swarm)
#[cfg_attr(feature = "serde", serde(skip))]
pending_events: VecDeque<crate::store::Event<Event>>,
/// Config of the store.
config: Config,
/// Waker for store events.
#[cfg_attr(feature = "serde", serde(skip))]
waker: Option<Waker>,
}

Expand Down Expand Up @@ -190,7 +173,6 @@ impl<T> Store for MemoryStore<T> {

/// Config for [`MemoryStore`].
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Config {
/// The capacaity of an address store.
/// The least active address will be discarded to make room for new address.
Expand Down Expand Up @@ -220,16 +202,6 @@ impl Config {

/// Internal record of [`MemoryStore`].
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(
serialize = "T: Clone + Serialize ",
deserialize = "T: Clone + Deserialize<'de>"
))
)]
#[cfg_attr(feature = "serde", serde(from = "serde::PeerRecord<T>"))]
#[cfg_attr(feature = "serde", serde(into = "serde::PeerRecord<T>"))]
pub struct PeerRecord<T> {
/// A LRU(Least Recently Used) cache for addresses.
/// Will delete the least-recently-used record when full.
Expand Down Expand Up @@ -281,73 +253,6 @@ impl<T> PeerRecord<T> {
}
}

#[cfg(feature = "serde")]
pub(crate) mod serde {
use std::num::NonZeroUsize;

use libp2p_core::Multiaddr;
use serde::{Deserialize, Serialize};

impl<T> super::PeerRecord<T> {
/// Build from an iterator. The order is reversed(FILO-ish).
pub(crate) fn from_iter(
cap: NonZeroUsize,
addr_iter: impl Iterator<Item = Multiaddr>,
custom_data: Option<T>,
) -> Self {
let mut lru = lru::LruCache::new(cap);
for addr in addr_iter {
lru.get_or_insert(addr, || ());
}
Self {
addresses: lru,
custom_data,
}
}

pub(crate) fn destruct(self) -> (NonZeroUsize, Vec<Multiaddr>, Option<T>) {
let cap = self.addresses.cap();
let mut addresses = self
.addresses
.into_iter()
.map(|(addr, _)| addr)
.collect::<Vec<_>>();
// This is somewhat unusual: `LruCache::iter()` retains LRU order
// while `LruCache::into_iter()` reverses the order.
addresses.reverse();
(cap, addresses, self.custom_data)
}
}

/// Helper struct for serializing and deserializing [`PeerRecord`](super::PeerRecord)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerRecord<T> {
pub addresses: Vec<Multiaddr>,
pub cap: NonZeroUsize,
pub custom_data: Option<T>,
}
impl<T: Clone> From<PeerRecord<T>> for super::PeerRecord<T> {
fn from(value: PeerRecord<T>) -> Self {
// Need to reverse the iterator because `LruCache` is FILO.
super::PeerRecord::from_iter(
value.cap,
value.addresses.into_iter().rev(),
value.custom_data,
)
}
}
impl<T: Clone> From<super::PeerRecord<T>> for PeerRecord<T> {
fn from(value: super::PeerRecord<T>) -> Self {
let (cap, addresses, custom_data) = value.destruct();
PeerRecord {
addresses,
cap,
custom_data,
}
}
}
}

#[cfg(test)]
mod test {
use std::{num::NonZero, str::FromStr};
Expand Down Expand Up @@ -428,58 +333,6 @@ mod test {
);
}

#[test]
fn serde_roundtrip() {
let store_json = r#"
{
"records": {
"1Aea5mXJrZNUwKxNU2y9xFE2qTFMjvFYSBf4T8cWEEP5Zd":
{
"addresses": [ "/ip4/127.0.0.2", "/ip4/127.0.0.3", "/ip4/127.0.0.4",
"/ip4/127.0.0.5", "/ip4/127.0.0.6", "/ip4/127.0.0.7",
"/ip4/127.0.0.8", "/ip4/127.0.0.9" ],
"cap": 8,
"custom_data": 7
}
},
"config": {
"record_capacity": 8
}
}
"#;
let store: MemoryStore<u32> = serde_json::from_str(store_json).unwrap();
let peer = PeerId::from_str("1Aea5mXJrZNUwKxNU2y9xFE2qTFMjvFYSBf4T8cWEEP5Zd")
.expect("Parsing to succeed.");
let mut addresses = Vec::new();
for i in 2..10 {
let addr_string = format!("/ip4/127.0.0.{}", i);
addresses.push(Multiaddr::from_str(&addr_string).expect("parsing to succeed"));
}
// should retain order when deserializing from bytes.
assert_eq!(
store
.addresses_of_peer(&peer)
.expect("Peer to exist")
.cloned()
.collect::<Vec<_>>(),
addresses
);
assert_eq!(*store.get_custom_data(&peer).expect("Peer to exist"), 7);
let ser = serde_json::to_string(&store).expect("Serialize to succeed.");
let store_de: MemoryStore<u32> =
serde_json::from_str(&ser).expect("Deserialize to succeed");
// should retain order when serializing
assert_eq!(
store_de
.addresses_of_peer(&peer)
.expect("Peer to exist")
.cloned()
.collect::<Vec<_>>(),
addresses
);
assert_eq!(*store_de.get_custom_data(&peer).expect("Peer to exist"), 7);
}

#[test]
fn update_address_on_connect() {
async fn expect_record_update(
Expand Down

0 comments on commit e6f3d46

Please sign in to comment.