Skip to content

Commit

Permalink
refactor: change RawContentKey to alloy_primites::Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
etherhood authored Sep 11, 2024
1 parent 9e08358 commit eeef99f
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 103 deletions.
21 changes: 5 additions & 16 deletions ethportal-api/src/types/content_key/beacon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
types::content_key::{error::ContentKeyError, overlay::OverlayContentKey},
utils::bytes::hex_encode_compact,
RawContentKey,
};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};
Expand Down Expand Up @@ -81,22 +82,10 @@ pub struct HistoricalSummariesWithProofKey {
pub epoch: u64,
}

impl From<&BeaconContentKey> for Vec<u8> {
fn from(val: &BeaconContentKey) -> Self {
val.to_bytes()
}
}

impl From<BeaconContentKey> for Vec<u8> {
fn from(val: BeaconContentKey) -> Self {
val.to_bytes()
}
}

impl TryFrom<Vec<u8>> for BeaconContentKey {
impl TryFrom<RawContentKey> for BeaconContentKey {
type Error = ContentKeyError;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
fn try_from(value: RawContentKey) -> Result<Self, Self::Error> {
let Some((&selector, key)) = value.split_first() else {
return Err(ContentKeyError::InvalidLength {
received: value.len(),
Expand Down Expand Up @@ -170,7 +159,7 @@ impl OverlayContentKey for BeaconContentKey {
sha256.finalize().into()
}

fn to_bytes(&self) -> Vec<u8> {
fn to_bytes(&self) -> RawContentKey {
let mut bytes: Vec<u8> = Vec::new();

match self {
Expand All @@ -196,7 +185,7 @@ impl OverlayContentKey for BeaconContentKey {
}
}

bytes
bytes.into()
}
}

Expand Down
24 changes: 7 additions & 17 deletions ethportal-api/src/types/content_key/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{fmt, hash::Hash};
use crate::{
types::content_key::{error::ContentKeyError, overlay::OverlayContentKey},
utils::bytes::hex_encode_compact,
RawContentKey,
};

// Prefixes for the different types of history content keys:
Expand Down Expand Up @@ -44,6 +45,7 @@ impl HistoryContentKey {
.choose(&mut rand::thread_rng())
.ok_or_else(|| anyhow::Error::msg("Failed to choose random prefix"))?;
random_bytes.insert(0, *random_prefix);
let random_bytes: RawContentKey = random_bytes.into();
Self::try_from(random_bytes).map_err(anyhow::Error::msg)
}
}
Expand Down Expand Up @@ -116,22 +118,10 @@ pub struct EpochAccumulatorKey {
pub epoch_hash: B256,
}

impl From<&HistoryContentKey> for Vec<u8> {
fn from(val: &HistoryContentKey) -> Self {
val.to_bytes()
}
}

impl From<HistoryContentKey> for Vec<u8> {
fn from(val: HistoryContentKey) -> Self {
val.to_bytes()
}
}

impl TryFrom<Vec<u8>> for HistoryContentKey {
impl TryFrom<RawContentKey> for HistoryContentKey {
type Error = ContentKeyError;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
fn try_from(value: RawContentKey) -> Result<Self, Self::Error> {
let Some((&selector, key)) = value.split_first() else {
return Err(ContentKeyError::InvalidLength {
received: value.len(),
Expand Down Expand Up @@ -195,7 +185,7 @@ impl OverlayContentKey for HistoryContentKey {
sha256.finalize().into()
}

fn to_bytes(&self) -> Vec<u8> {
fn to_bytes(&self) -> RawContentKey {
let mut bytes: Vec<u8> = Vec::new();

match self {
Expand All @@ -217,7 +207,7 @@ impl OverlayContentKey for HistoryContentKey {
}
}

bytes
bytes.into()
}
}

Expand Down Expand Up @@ -326,7 +316,7 @@ mod test {
});

// round trip
let decoded = HistoryContentKey::try_from(key.to_bytes().to_vec()).unwrap();
let decoded = HistoryContentKey::try_from(key.to_bytes()).unwrap();
assert_eq!(decoded, key);

assert_eq!(key.to_bytes(), expected_content_key);
Expand Down
20 changes: 10 additions & 10 deletions ethportal-api/src/types/content_key/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use quickcheck::{Arbitrary, Gen};

use crate::{
types::content_key::error::ContentKeyError,
utils::bytes::{hex_decode, hex_encode, hex_encode_compact},
utils::bytes::{hex_encode, hex_encode_compact},
RawContentKey,
};
use std::str::FromStr;

/// Types whose values represent keys to lookup content items in an overlay network.
/// Keys are serializable.
pub trait OverlayContentKey:
Into<Vec<u8>>
+ TryFrom<Vec<u8>, Error = ContentKeyError>
TryFrom<RawContentKey, Error = ContentKeyError>
+ Clone
+ fmt::Debug
+ fmt::Display
Expand All @@ -25,7 +26,7 @@ pub trait OverlayContentKey:
fn content_id(&self) -> [u8; 32];

/// Returns the bytes of the content key.
fn to_bytes(&self) -> Vec<u8>;
fn to_bytes(&self) -> RawContentKey;

/// Returns the content key as a hex encoded "0x"-prefixed string.
fn to_hex(&self) -> String {
Expand All @@ -34,8 +35,7 @@ pub trait OverlayContentKey:

/// Returns the content key from a hex encoded "0x"-prefixed string.
fn from_hex(data: &str) -> anyhow::Result<Self> {
let bytes = hex_decode(&data.to_lowercase())?;
Ok(Self::try_from(bytes)?)
Ok(Self::try_from(RawContentKey::from_str(data)?)?)
}
}

Expand Down Expand Up @@ -67,10 +67,10 @@ impl Arbitrary for IdentityContentKey {
}
}

impl TryFrom<Vec<u8>> for IdentityContentKey {
impl TryFrom<RawContentKey> for IdentityContentKey {
type Error = ContentKeyError;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
fn try_from(value: RawContentKey) -> Result<Self, Self::Error> {
// Require that length of input is equal to 32.
if value.len() != 32 {
return Err(ContentKeyError::InvalidLength {
Expand Down Expand Up @@ -116,7 +116,7 @@ impl OverlayContentKey for IdentityContentKey {
fn content_id(&self) -> [u8; 32] {
self.value
}
fn to_bytes(&self) -> Vec<u8> {
self.value.to_vec()
fn to_bytes(&self) -> RawContentKey {
RawContentKey::from(self.value)
}
}
40 changes: 16 additions & 24 deletions ethportal-api/src/types/content_key/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{fmt, hash::Hash};
use crate::{
types::{content_key::overlay::OverlayContentKey, state_trie::nibbles::Nibbles},
utils::bytes::hex_encode_compact,
ContentKeyError,
ContentKeyError, RawContentKey,
};

// Prefixes for the different types of state content keys:
Expand Down Expand Up @@ -70,7 +70,7 @@ impl OverlayContentKey for StateContentKey {
sha256.finalize().into()
}

fn to_bytes(&self) -> Vec<u8> {
fn to_bytes(&self) -> RawContentKey {
let mut bytes: Vec<u8> = vec![];

match self {
Expand All @@ -88,25 +88,13 @@ impl OverlayContentKey for StateContentKey {
}
}

bytes
RawContentKey::from(bytes)
}
}

impl From<&StateContentKey> for Vec<u8> {
fn from(val: &StateContentKey) -> Self {
val.to_bytes()
}
}

impl From<StateContentKey> for Vec<u8> {
fn from(val: StateContentKey) -> Self {
val.to_bytes()
}
}

impl TryFrom<Vec<u8>> for StateContentKey {
impl TryFrom<RawContentKey> for StateContentKey {
type Error = ContentKeyError;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
fn try_from(value: RawContentKey) -> Result<Self, Self::Error> {
let Some((&selector, key)) = value.split_first() else {
return Err(ContentKeyError::from_decode_error(
DecodeError::InvalidLengthPrefix {
Expand Down Expand Up @@ -187,7 +175,7 @@ impl fmt::Display for StateContentKey {
mod test {
use std::{path::PathBuf, str::FromStr};

use alloy_primitives::{keccak256, Address};
use alloy_primitives::{bytes, keccak256, Address, Bytes};
use anyhow::Result;
use rstest::rstest;
use serde_yaml::Value;
Expand Down Expand Up @@ -242,16 +230,20 @@ mod test {
#[test]
fn decode_empty_key_should_fail() {
assert_eq!(
StateContentKey::try_from(vec![]).unwrap_err().to_string(),
StateContentKey::try_from(RawContentKey::new())
.unwrap_err()
.to_string(),
"Unable to decode key SSZ bytes 0x due to InvalidLengthPrefix { len: 0, expected: 1 }",
);
}

#[test]
fn decode_key_with_invalid_selector_should_fail() {
let invalid_selector_content_key = "0x0024000000c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4700005000000";
let invalid_selector_content_key = bytes!(
"0024000000c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4700005000000"
);
assert_eq!(
StateContentKey::try_from(hex_decode(invalid_selector_content_key).unwrap())
StateContentKey::try_from(invalid_selector_content_key.clone())
.unwrap_err()
.to_string(),
format!("Unable to decode key SSZ bytes {invalid_selector_content_key} due to UnionSelectorInvalid(0)"),
Expand All @@ -266,7 +258,7 @@ mod test {
let yaml = read_yaml_file(filename)?;
let yaml = yaml.as_mapping().unwrap();

let content_key_bytes = hex_decode(yaml["content_key"].as_str().unwrap())?;
let content_key_bytes = RawContentKey::from_str(yaml["content_key"].as_str().unwrap())?;
let content_key = StateContentKey::try_from(content_key_bytes.clone())?;

assert_eq!(content_key.to_bytes(), content_key_bytes);
Expand Down Expand Up @@ -300,7 +292,7 @@ mod test {
let yaml = yaml.as_mapping().unwrap();

let content_key_bytes = hex_decode(yaml["content_key"].as_str().unwrap())?;
let content_key = StateContentKey::try_from(content_key_bytes)?;
let content_key = StateContentKey::try_from(Bytes::from(content_key_bytes))?;
let expected_content_id = yaml_as_b256(&yaml["content_id"]);

assert_eq!(B256::from(content_key.content_id()), expected_content_id);
Expand Down Expand Up @@ -337,7 +329,7 @@ mod test {

fn assert_content_key(value: &Value, expected_content_key: StateContentKey) -> Result<()> {
assert_eq!(
StateContentKey::try_from(yaml_as_hex(value))?,
StateContentKey::try_from(Bytes::from(yaml_as_hex(value)))?,
expected_content_key,
"decoding from bytes {value:?} didn't match expected key {expected_content_key:?}"
);
Expand Down
2 changes: 1 addition & 1 deletion ethportal-api/src/types/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{types::enr::Enr, OverlayContentKey};
use super::query_trace::QueryTrace;

/// The SSZ encoded representation of content key.
pub type RawContentKey = Vec<u8>;
pub type RawContentKey = Bytes;
/// The SSZ encoded representation of content value.
pub type RawContentValue = Bytes;

Expand Down
3 changes: 2 additions & 1 deletion ethportal-api/src/types/portal_wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ impl From<Accept> for Value {
#[allow(clippy::unwrap_used)]
mod test {
use super::*;
use alloy_primitives::bytes;
use std::str::FromStr;
use test_log::test;

Expand Down Expand Up @@ -821,7 +822,7 @@ mod test {

#[test]
fn message_encoding_offer() {
let content_keys = vec![hex_decode("0x010203").unwrap()];
let content_keys = vec![bytes!("010203")];
let offer = Offer { content_keys };
let offer = Message::Offer(offer);

Expand Down
2 changes: 1 addition & 1 deletion portalnet/src/find/query_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<TContentKey: OverlayContentKey> QueryInfo<TContentKey> {
Request::FindNodes(FindNodes { distances })
}
QueryType::FindContent { ref target, .. } => Request::FindContent(FindContent {
content_key: target.clone().into(),
content_key: target.to_bytes().to_vec(),
}),
};

Expand Down
4 changes: 2 additions & 2 deletions portalnet/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn propagate_gossip_cross_thread<TContentKey: OverlayContentKey>(
// change content keys to raw content keys
let interested_content = interested_content
.into_iter()
.map(|(key, value)| (key.into(), value))
.map(|(key, value)| (key.to_bytes(), value))
.collect();
let offer_request = Request::PopulatedOffer(PopulatedOffer {
content_items: interested_content,
Expand Down Expand Up @@ -197,7 +197,7 @@ pub async fn trace_propagate_gossip_cross_thread<TContentKey: OverlayContentKey>
for enr in interested_enrs.into_iter() {
let (result_tx, mut result_rx) = tokio::sync::mpsc::unbounded_channel();
let offer_request = Request::PopulatedOfferWithResult(PopulatedOfferWithResult {
content_item: (content_key.clone().into(), data.clone()),
content_item: (content_key.clone().to_bytes(), data.clone()),
result_tx,
});

Expand Down
6 changes: 3 additions & 3 deletions portalnet/src/overlay/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<
TStore: 'static + ContentStore<Key = TContentKey> + Send + Sync,
> OverlayProtocol<TContentKey, TMetric, TValidator, TStore>
where
<TContentKey as TryFrom<Vec<u8>>>::Error: Debug + Display + Send,
<TContentKey as TryFrom<RawContentKey>>::Error: Debug + Display + Send,
{
pub async fn new(
config: OverlayConfig,
Expand Down Expand Up @@ -441,7 +441,7 @@ where
let direction = RequestDirection::Outgoing {
destination: enr.clone(),
};
let content_key = TContentKey::try_from(content_key).map_err(|err| {
let content_key = TContentKey::try_from(content_key.into()).map_err(|err| {
OverlayRequestError::FailedValidation(format!(
"Error decoding content key for received utp content: {err}"
))
Expand Down Expand Up @@ -527,7 +527,7 @@ where
let content_items = content_keys
.into_iter()
.map(|key| match self.store.read().get(&key) {
Ok(Some(content)) => Ok((key.into(), content.clone())),
Ok(Some(content)) => Ok((key.to_bytes(), content.clone())),
_ => Err(OverlayRequestError::ContentNotFound {
message: format!("Content key not found in local store: {key:02X?}"),
utp: false,
Expand Down
Loading

0 comments on commit eeef99f

Please sign in to comment.