Skip to content

Commit 7682979

Browse files
authored
docs: update RawContentKey and RawContentValue docs (#1543)
1 parent 68ac212 commit 7682979

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

ethportal-api/src/types/content_key/overlay.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::{
1010
use std::str::FromStr;
1111

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

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

3136
/// Returns the content key as a hex encoded "0x"-prefixed string.

ethportal-api/src/types/content_value/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ pub trait ContentValue: Sized {
1515
type TContentKey: OverlayContentKey;
1616

1717
/// Encodes the content value into a byte vector.
18+
///
19+
/// The [RawContentValue] is better suited than `Vec<u8>` for representing content value bytes.
20+
/// For more details, see [RawContentValue] documentation. If `Vec<u8>` is still desired, one
21+
/// can obtain it with: `value.to_bytes().to_vec()`.
1822
fn encode(&self) -> RawContentValue;
23+
1924
/// Decodes `buf` into a content value.
2025
fn decode(key: &Self::TContentKey, buf: &[u8]) -> Result<Self, ContentValueError>;
2126

2227
/// Encodes the content as "0x"-prefixed hex string.
2328
fn to_hex(&self) -> String {
2429
hex_encode(self.encode())
2530
}
31+
2632
/// Decodes the "0x"-prefixed hex string as a content value.
2733
fn from_hex(key: &Self::TContentKey, data: &str) -> anyhow::Result<Self> {
2834
Ok(Self::decode(key, &hex_decode(data)?)?)

ethportal-api/src/types/portal.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,43 @@ use crate::{types::enr::Enr, OverlayContentKey};
77
use super::query_trace::QueryTrace;
88

99
/// The SSZ encoded representation of content key.
10+
///
11+
/// This is an alias for [alloy::primitives::Bytes] type, which is a wrapper around [bytes::Bytes].
12+
///
13+
/// While `Vec<u8>` is the most common way to represent the byte array, its usage as raw content
14+
/// key is discouraged in favor of this type, for following reasons:
15+
///
16+
/// - The [bytes::Bytes] is cheaply cloneable and sliceable chunk of contiguous memory
17+
/// - This makes it more suitable when cloning/coping is frequent, and modification is not
18+
/// - The [alloy::primitives::Bytes] is a wrapper around `bytes::Bytes` that implements frequently
19+
/// used traits (e.g. [ssz::Encode], [ssz::Decode], [std::fmt::Display], [std::str::FromStr]) and
20+
/// supports (de)serialization to/from hex strings (with or without "0x" prefix).
21+
///
22+
/// Lack of support in third-party libraries is the most common issue with using this type. To work
23+
/// around it, one can do any of the following:
24+
///
25+
/// - Wrap `RawContentKey` in a new type, and implement thirt-party trait that adds support
26+
/// - Use `RawContentKey::to_vec` and `RawContentKey::from` to convert to/from `Vec<u8>`
1027
pub type RawContentKey = Bytes;
28+
1129
/// The SSZ encoded representation of content value.
30+
///
31+
/// This is an alias for [alloy::primitives::Bytes] type, which is a wrapper around [bytes::Bytes].
32+
///
33+
/// While `Vec<u8>` is the most common way to represent the byte array, its usage as raw content
34+
/// value is discouraged in favor of this type, for following reasons:
35+
///
36+
/// - The [bytes::Bytes] is cheaply cloneable and sliceable chunk of contiguous memory
37+
/// - This makes it more suitable when cloning/coping is frequent, and modification is not
38+
/// - The [alloy::primitives::Bytes] is a wrapper around `bytes::Bytes` that implements frequently
39+
/// used traits (e.g. [ssz::Encode], [ssz::Decode], [std::fmt::Display], [std::str::FromStr]) and
40+
/// supports (de)serialization to/from hex strings (with or without "0x" prefix).
41+
///
42+
/// Lack of support in third-party libraries is the most common issue with using this type. To work
43+
/// around it, one can do any of the following:
44+
///
45+
/// - Wrap `RawContentValue` in a new type, and implement thirt-party trait that adds support
46+
/// - Use `RawContentValue::to_vec` and `RawContentValue::from` to convert to/from `Vec<u8>`
1247
pub type RawContentValue = Bytes;
1348

1449
pub type DataRadius = U256;

0 commit comments

Comments
 (0)