Skip to content

Commit 2ed3618

Browse files
authored
feat: remove DistanceFunction (#1838)
1 parent 25585ef commit 2ed3618

File tree

10 files changed

+121
-94
lines changed

10 files changed

+121
-94
lines changed

crates/ethportal-api/src/types/distance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub trait Metric {
8282
}
8383

8484
/// The XOR metric defined in the Kademlia paper.
85+
#[derive(Debug)]
8586
pub struct XorMetric;
8687

8788
impl Metric for XorMetric {

crates/portalnet/src/overlay/service/manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ mod tests {
977977
use tokio::{sync::mpsc::unbounded_channel, time::timeout};
978978
use tokio_test::{assert_pending, assert_ready, task};
979979
use trin_metrics::portalnet::PORTALNET_METRICS;
980-
use trin_storage::{DistanceFunction, MemoryContentStore};
980+
use trin_storage::MemoryContentStore;
981981
use trin_validation::validator::MockValidator;
982982

983983
use super::*;
@@ -1028,7 +1028,7 @@ mod tests {
10281028
let utp_controller = Arc::new(utp_controller);
10291029

10301030
let node_id = discovery.local_enr().node_id();
1031-
let store = MemoryContentStore::new(node_id, DistanceFunction::Xor);
1031+
let store = MemoryContentStore::new(node_id);
10321032
let store = Arc::new(Mutex::new(store));
10331033

10341034
let overlay_config = OverlayConfig::default();

crates/portalnet/tests/overlay.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tokio::{
3030
sync::{mpsc, mpsc::unbounded_channel},
3131
time::{self, Duration},
3232
};
33-
use trin_storage::{ContentStore, DistanceFunction, MemoryContentStore};
33+
use trin_storage::{ContentStore, MemoryContentStore};
3434
use trin_validation::validator::MockValidator;
3535
use utp_rs::socket::UtpSocket;
3636

@@ -47,7 +47,7 @@ async fn init_overlay(
4747
let overlay_config = OverlayConfig::default();
4848

4949
let node_id = discovery.local_enr().node_id();
50-
let store = MemoryContentStore::new(node_id, DistanceFunction::Xor);
50+
let store = MemoryContentStore::new(node_id);
5151
let store = Arc::new(Mutex::new(store));
5252

5353
let (_utp_talk_req_tx, utp_talk_req_rx) = unbounded_channel();

crates/storage/src/config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ethportal_api::types::{distance::Distance, network::Subnetwork};
55
use r2d2::Pool;
66
use r2d2_sqlite::SqliteConnectionManager;
77

8-
use crate::{error::ContentStoreError, utils::setup_sql, DistanceFunction};
8+
use crate::{error::ContentStoreError, utils::setup_sql};
99

1010
const BYTES_IN_MB_U64: u64 = 1000 * 1000;
1111

@@ -103,7 +103,6 @@ impl PortalStorageConfigFactory {
103103
storage_capacity_bytes: capacity_bytes,
104104
node_id: self.node_id,
105105
node_data_dir: self.node_data_dir.clone(),
106-
distance_fn: DistanceFunction::Xor,
107106
sql_connection_pool: self.sql_connection_pool.clone(),
108107
max_radius,
109108
})
@@ -124,7 +123,6 @@ pub struct PortalStorageConfig {
124123
pub storage_capacity_bytes: u64,
125124
pub node_id: NodeId,
126125
pub node_data_dir: PathBuf,
127-
pub distance_fn: DistanceFunction,
128126
pub sql_connection_pool: Pool<SqliteConnectionManager>,
129127
pub max_radius: Distance,
130128
}

crates/storage/src/lib.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod test_utils;
55
pub mod utils;
66
pub mod versioned;
77

8-
use std::{ops::Deref, str::FromStr};
8+
use std::{marker::PhantomData, ops::Deref, str::FromStr};
99

1010
use alloy::primitives::{Bytes, B256};
1111
pub use config::{PortalStorageConfig, PortalStorageConfigFactory};
@@ -22,21 +22,6 @@ use rusqlite::types::{FromSql, FromSqlError, ValueRef};
2222

2323
pub const DATABASE_NAME: &str = "trin.sqlite";
2424

25-
// TODO: Replace enum with generic type parameter. This will require that we have a way to
26-
// associate a "find farthest" query with the generic Metric.
27-
#[derive(Copy, Clone, Debug)]
28-
pub enum DistanceFunction {
29-
Xor,
30-
}
31-
32-
impl DistanceFunction {
33-
pub fn distance(&self, node_id: &NodeId, other: &[u8; 32]) -> Distance {
34-
match self {
35-
DistanceFunction::Xor => XorMetric::distance(&node_id.raw(), other),
36-
}
37-
}
38-
}
39-
4025
/// An enum which tells us if we should store or not store content, and if not why for better
4126
/// errors.
4227
#[derive(Debug, PartialEq)]
@@ -76,25 +61,25 @@ pub trait ContentStore {
7661
}
7762

7863
/// An in-memory `ContentStore`.
79-
pub struct MemoryContentStore {
64+
pub struct MemoryContentStore<TMetric: Metric = XorMetric> {
8065
/// The content store.
8166
store: std::collections::HashMap<Vec<u8>, RawContentValue>,
8267
/// The `NodeId` of the local node.
8368
node_id: NodeId,
84-
/// The distance function used by the store to compute distances.
85-
distance_fn: DistanceFunction,
8669
/// The radius of the store.
8770
radius: Distance,
71+
/// Phantom metric
72+
_metric: PhantomData<TMetric>,
8873
}
8974

90-
impl MemoryContentStore {
75+
impl<TMetric: Metric> MemoryContentStore<TMetric> {
9176
/// Constructs a new `MemoryPortalContentStore`.
92-
pub fn new(node_id: NodeId, distance_fn: DistanceFunction) -> Self {
77+
pub fn new(node_id: NodeId) -> Self {
9378
Self {
9479
store: std::collections::HashMap::new(),
9580
node_id,
96-
distance_fn,
9781
radius: Distance::MAX,
82+
_metric: PhantomData,
9883
}
9984
}
10085

@@ -105,7 +90,7 @@ impl MemoryContentStore {
10590

10691
/// Returns the distance to `key` from the local `NodeId` according to the distance function.
10792
fn distance_to_key<K: OverlayContentKey>(&self, key: &K) -> Distance {
108-
self.distance_fn.distance(&self.node_id, &key.content_id())
93+
TMetric::distance(&self.node_id.raw(), &key.content_id())
10994
}
11095

11196
/// Returns `true` if the content store contains data for `key`.
@@ -115,7 +100,7 @@ impl MemoryContentStore {
115100
}
116101
}
117102

118-
impl ContentStore for MemoryContentStore {
103+
impl<TMetric: Metric> ContentStore for MemoryContentStore<TMetric> {
119104
type Key = IdentityContentKey;
120105

121106
fn get(&self, key: &Self::Key) -> Result<Option<RawContentValue>, ContentStoreError> {
@@ -208,14 +193,14 @@ pub struct DataSize {
208193
#[allow(clippy::unwrap_used)]
209194
pub mod test {
210195
use alloy::primitives::{bytes, B512};
211-
use ethportal_api::IdentityContentKey;
196+
use ethportal_api::{types::distance::XorMetric, IdentityContentKey};
212197

213198
use super::*;
214199

215200
#[test]
216201
fn memory_store_contains_key() {
217202
let node_id = NodeId::random();
218-
let mut store = MemoryContentStore::new(node_id, DistanceFunction::Xor);
203+
let mut store = MemoryContentStore::<XorMetric>::new(node_id);
219204

220205
let val = vec![0xef];
221206

@@ -231,7 +216,7 @@ pub mod test {
231216
#[test]
232217
fn memory_store_get() {
233218
let node_id = NodeId::random();
234-
let mut store = MemoryContentStore::new(node_id, DistanceFunction::Xor);
219+
let mut store = MemoryContentStore::<XorMetric>::new(node_id);
235220

236221
let val = bytes!("ef");
237222

@@ -247,7 +232,7 @@ pub mod test {
247232
#[test]
248233
fn memory_store_put() {
249234
let node_id = NodeId::random();
250-
let mut store = MemoryContentStore::new(node_id, DistanceFunction::Xor);
235+
let mut store = MemoryContentStore::<XorMetric>::new(node_id);
251236

252237
let val = bytes!("ef");
253238

@@ -260,7 +245,7 @@ pub mod test {
260245
#[test]
261246
fn memory_store_is_within_radius_and_unavailable() {
262247
let node_id = NodeId::random();
263-
let mut store = MemoryContentStore::new(node_id, DistanceFunction::Xor);
248+
let mut store = MemoryContentStore::<XorMetric>::new(node_id);
264249

265250
let val = bytes!("ef");
266251

crates/storage/src/versioned/id_indexed_v1/config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use r2d2::Pool;
66
use r2d2_sqlite::SqliteConnectionManager;
77

88
use super::pruning_strategy::PruningConfig;
9-
use crate::{versioned::ContentType, DistanceFunction, PortalStorageConfig};
9+
use crate::{versioned::ContentType, PortalStorageConfig};
1010

1111
/// The config for the IdIndexedV1Store
1212
#[derive(Clone, Debug)]
@@ -17,7 +17,6 @@ pub struct IdIndexedV1StoreConfig {
1717
pub node_data_dir: PathBuf,
1818
pub storage_capacity_bytes: u64,
1919
pub sql_connection_pool: Pool<SqliteConnectionManager>,
20-
pub distance_fn: DistanceFunction,
2120
pub pruning_config: PruningConfig,
2221
pub max_radius: Distance,
2322
}
@@ -35,7 +34,6 @@ impl IdIndexedV1StoreConfig {
3534
node_data_dir: config.node_data_dir,
3635
storage_capacity_bytes: config.storage_capacity_bytes,
3736
sql_connection_pool: config.sql_connection_pool,
38-
distance_fn: config.distance_fn,
3937
// consider making this a parameter if we start using non-default value
4038
pruning_config: PruningConfig::default(),
4139
max_radius: config.max_radius,

crates/storage/src/versioned/id_indexed_v1/pruning_strategy.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ mod tests {
203203
use rstest::rstest;
204204

205205
use super::*;
206-
use crate::{versioned::ContentType, DistanceFunction};
206+
use crate::versioned::ContentType;
207207

208208
const DEFAULT_STORAGE_CAPACITY_BYTES: u64 = 1_000_000;
209209
const EXTRA_DISK_USAGE_PER_ENTRY: u64 = 100;
@@ -220,7 +220,6 @@ mod tests {
220220
node_data_dir: PathBuf::default(),
221221
storage_capacity_bytes,
222222
sql_connection_pool: Pool::new(SqliteConnectionManager::memory()).unwrap(),
223-
distance_fn: DistanceFunction::Xor,
224223
pruning_config: PruningConfig::default(),
225224
max_radius: Distance::MAX,
226225
};

0 commit comments

Comments
 (0)