diff --git a/portalnet/src/overlay_service.rs b/portalnet/src/overlay_service.rs index cd5d05b50..d88024516 100644 --- a/portalnet/src/overlay_service.rs +++ b/portalnet/src/overlay_service.rs @@ -35,7 +35,7 @@ use tracing::{debug, error, info, trace, warn}; use utp_rs::{conn::ConnectionConfig, socket::UtpSocket, stream::UtpStream}; use crate::events::EventEnvelope; -use crate::storage::ShouldWeStoreContent; +use crate::storage::ContentPresence; use crate::{ discovery::Discovery, events::OverlayEvent, @@ -1204,7 +1204,7 @@ where .store .read() .is_key_within_radius_and_unavailable(key) - .map(|value| matches!(value, ShouldWeStoreContent::Store)) + .map(|value| matches!(value, ContentPresence::Missing)) .map_err(|err| { OverlayRequestError::AcceptError(format!( "Unable to check content availability {err}" @@ -1635,7 +1635,7 @@ where // Check if data should be stored, and store if it is within our radius and not already stored. let key_desired = store.read().is_key_within_radius_and_unavailable(&key); match key_desired { - Ok(ShouldWeStoreContent::Store) => { + Ok(ContentPresence::Missing) => { if let Err(err) = store.write().put(key.clone(), &content_value) { warn!( error = %err, @@ -1644,13 +1644,13 @@ where ); } } - Ok(ShouldWeStoreContent::NotWithinRadius) => { + Ok(ContentPresence::OutsideRadius) => { warn!( content.key = %key.to_hex(), "Accepted content outside radius" ); } - Ok(ShouldWeStoreContent::AlreadyStored) => { + Ok(ContentPresence::Present) => { warn!( content.key = %key.to_hex(), "Accepted content already stored" @@ -1864,7 +1864,7 @@ where .read() .is_key_within_radius_and_unavailable(&content_key) { - Ok(val) => matches!(val, ShouldWeStoreContent::Store), + Ok(val) => matches!(val, ContentPresence::Missing), Err(msg) => { error!("Unable to read store: {}", msg); false diff --git a/portalnet/src/storage.rs b/portalnet/src/storage.rs index 9518da3b7..583fe9a81 100644 --- a/portalnet/src/storage.rs +++ b/portalnet/src/storage.rs @@ -67,12 +67,16 @@ pub enum ContentStoreError { ContentKey(#[from] ContentKeyError), } -/// An enum which tells us if we should store or not store content, and if not why for better errors. +/// An enum which tells us whether a piece of content is relevant and whether it is present. +/// This detailed status information is helpful for investigation and debugging. #[derive(Debug, PartialEq)] -pub enum ShouldWeStoreContent { - Store, - NotWithinRadius, - AlreadyStored, +pub enum ContentPresence { + /// This content is within our radius, but not in our database. + Missing, + /// This content is outside our radius, so not relevant to this node. + OutsideRadius, + /// This content is within our radius, and already stored in our database. + Present, } /// A data store for Portal Network content (data). @@ -92,7 +96,7 @@ pub trait ContentStore { fn is_key_within_radius_and_unavailable( &self, key: &K, - ) -> Result; + ) -> Result; /// Returns the radius of the data store. fn radius(&self) -> Distance; @@ -162,15 +166,15 @@ impl ContentStore for MemoryContentStore { fn is_key_within_radius_and_unavailable( &self, key: &K, - ) -> Result { + ) -> Result { let distance = self.distance_to_key(key); if distance > self.radius { - return Ok(ShouldWeStoreContent::NotWithinRadius); + return Ok(ContentPresence::OutsideRadius); } if self.contains_key(key) { - return Ok(ShouldWeStoreContent::AlreadyStored); + return Ok(ContentPresence::Present); } - Ok(ShouldWeStoreContent::Store) + Ok(ContentPresence::Missing) } fn radius(&self) -> Distance { @@ -238,18 +242,18 @@ impl ContentStore for PortalStorage { fn is_key_within_radius_and_unavailable( &self, key: &K, - ) -> Result { + ) -> Result { let distance = self.distance_to_key(key); if distance > self.radius { - return Ok(ShouldWeStoreContent::NotWithinRadius); + return Ok(ContentPresence::OutsideRadius); } let key = key.content_id(); let is_key_available = self.db.get_pinned(key)?.is_some(); if is_key_available { - return Ok(ShouldWeStoreContent::AlreadyStored); + return Ok(ContentPresence::Present); } - Ok(ShouldWeStoreContent::Store) + Ok(ContentPresence::Missing) } fn radius(&self) -> Distance { @@ -1198,7 +1202,7 @@ pub mod test { store .is_key_within_radius_and_unavailable(&arb_key) .unwrap(), - ShouldWeStoreContent::Store + ContentPresence::Missing ); // Arbitrary key available. @@ -1207,7 +1211,7 @@ pub mod test { store .is_key_within_radius_and_unavailable(&arb_key) .unwrap(), - ShouldWeStoreContent::AlreadyStored + ContentPresence::Present ); }