Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(portalnet): name for content presence #983

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions portalnet/src/overlay_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
36 changes: 20 additions & 16 deletions portalnet/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -92,7 +96,7 @@ pub trait ContentStore {
fn is_key_within_radius_and_unavailable<K: OverlayContentKey>(
&self,
key: &K,
) -> Result<ShouldWeStoreContent, ContentStoreError>;
) -> Result<ContentPresence, ContentStoreError>;

/// Returns the radius of the data store.
fn radius(&self) -> Distance;
Expand Down Expand Up @@ -162,15 +166,15 @@ impl ContentStore for MemoryContentStore {
fn is_key_within_radius_and_unavailable<K: OverlayContentKey>(
&self,
key: &K,
) -> Result<ShouldWeStoreContent, ContentStoreError> {
) -> Result<ContentPresence, ContentStoreError> {
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 {
Expand Down Expand Up @@ -238,18 +242,18 @@ impl ContentStore for PortalStorage {
fn is_key_within_radius_and_unavailable<K: OverlayContentKey>(
&self,
key: &K,
) -> Result<ShouldWeStoreContent, ContentStoreError> {
) -> Result<ContentPresence, ContentStoreError> {
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 {
Expand Down Expand Up @@ -1198,7 +1202,7 @@ pub mod test {
store
.is_key_within_radius_and_unavailable(&arb_key)
.unwrap(),
ShouldWeStoreContent::Store
ContentPresence::Missing
);

// Arbitrary key available.
Expand All @@ -1207,7 +1211,7 @@ pub mod test {
store
.is_key_within_radius_and_unavailable(&arb_key)
.unwrap(),
ShouldWeStoreContent::AlreadyStored
ContentPresence::Present
);
}

Expand Down