Skip to content

Commit

Permalink
docs: update RawContentKey and RawContentValue docs (ethereum#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev authored Oct 21, 2024
1 parent 68ac212 commit 7682979
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ethportal-api/src/types/content_key/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::{
use std::str::FromStr;

/// Types whose values represent keys to lookup content items in an overlay network.
/// Keys are serializable.
///
/// Keys are serializable as "0x" prefixed hex strings.
pub trait OverlayContentKey:
TryFrom<RawContentKey, Error = ContentKeyError>
+ Clone
Expand All @@ -26,6 +27,10 @@ pub trait OverlayContentKey:
fn content_id(&self) -> [u8; 32];

/// Returns the bytes of the content key.
///
/// The [RawContentKey] is better suited than `Vec<u8>` for representing content key bytes.
/// For more details, see [RawContentKey] documentation. If `Vec<u8>` is still desired, one can
/// obtain it with: `key.to_bytes().to_vec()`.
fn to_bytes(&self) -> RawContentKey;

/// Returns the content key as a hex encoded "0x"-prefixed string.
Expand Down
6 changes: 6 additions & 0 deletions ethportal-api/src/types/content_value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ pub trait ContentValue: Sized {
type TContentKey: OverlayContentKey;

/// Encodes the content value into a byte vector.
///
/// The [RawContentValue] is better suited than `Vec<u8>` for representing content value bytes.
/// For more details, see [RawContentValue] documentation. If `Vec<u8>` is still desired, one
/// can obtain it with: `value.to_bytes().to_vec()`.
fn encode(&self) -> RawContentValue;

/// Decodes `buf` into a content value.
fn decode(key: &Self::TContentKey, buf: &[u8]) -> Result<Self, ContentValueError>;

/// Encodes the content as "0x"-prefixed hex string.
fn to_hex(&self) -> String {
hex_encode(self.encode())
}

/// Decodes the "0x"-prefixed hex string as a content value.
fn from_hex(key: &Self::TContentKey, data: &str) -> anyhow::Result<Self> {
Ok(Self::decode(key, &hex_decode(data)?)?)
Expand Down
35 changes: 35 additions & 0 deletions ethportal-api/src/types/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,43 @@ use crate::{types::enr::Enr, OverlayContentKey};
use super::query_trace::QueryTrace;

/// The SSZ encoded representation of content key.
///
/// This is an alias for [alloy::primitives::Bytes] type, which is a wrapper around [bytes::Bytes].
///
/// While `Vec<u8>` is the most common way to represent the byte array, its usage as raw content
/// key is discouraged in favor of this type, for following reasons:
///
/// - The [bytes::Bytes] is cheaply cloneable and sliceable chunk of contiguous memory
/// - This makes it more suitable when cloning/coping is frequent, and modification is not
/// - The [alloy::primitives::Bytes] is a wrapper around `bytes::Bytes` that implements frequently
/// used traits (e.g. [ssz::Encode], [ssz::Decode], [std::fmt::Display], [std::str::FromStr]) and
/// supports (de)serialization to/from hex strings (with or without "0x" prefix).
///
/// Lack of support in third-party libraries is the most common issue with using this type. To work
/// around it, one can do any of the following:
///
/// - Wrap `RawContentKey` in a new type, and implement thirt-party trait that adds support
/// - Use `RawContentKey::to_vec` and `RawContentKey::from` to convert to/from `Vec<u8>`
pub type RawContentKey = Bytes;

/// The SSZ encoded representation of content value.
///
/// This is an alias for [alloy::primitives::Bytes] type, which is a wrapper around [bytes::Bytes].
///
/// While `Vec<u8>` is the most common way to represent the byte array, its usage as raw content
/// value is discouraged in favor of this type, for following reasons:
///
/// - The [bytes::Bytes] is cheaply cloneable and sliceable chunk of contiguous memory
/// - This makes it more suitable when cloning/coping is frequent, and modification is not
/// - The [alloy::primitives::Bytes] is a wrapper around `bytes::Bytes` that implements frequently
/// used traits (e.g. [ssz::Encode], [ssz::Decode], [std::fmt::Display], [std::str::FromStr]) and
/// supports (de)serialization to/from hex strings (with or without "0x" prefix).
///
/// Lack of support in third-party libraries is the most common issue with using this type. To work
/// around it, one can do any of the following:
///
/// - Wrap `RawContentValue` in a new type, and implement thirt-party trait that adds support
/// - Use `RawContentValue::to_vec` and `RawContentValue::from` to convert to/from `Vec<u8>`
pub type RawContentValue = Bytes;

pub type DataRadius = U256;
Expand Down

0 comments on commit 7682979

Please sign in to comment.