Skip to content

Commit

Permalink
fix: removing clone() and unused impls
Browse files Browse the repository at this point in the history
  • Loading branch information
etherhood committed Sep 10, 2024
1 parent 046a4e1 commit 585bbc8
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 52 deletions.
13 changes: 0 additions & 13 deletions ethportal-api/src/types/content_key/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
utils::bytes::hex_encode_compact,
RawContentKey,
};
use alloy_primitives::Bytes;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};
use ssz::{Decode, DecodeError, Encode};
Expand Down Expand Up @@ -83,18 +82,6 @@ pub struct HistoricalSummariesWithProofKey {
pub epoch: u64,
}

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

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

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

Expand Down
12 changes: 0 additions & 12 deletions ethportal-api/src/types/content_key/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,6 @@ pub struct EpochAccumulatorKey {
pub epoch_hash: B256,
}

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

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

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

Expand Down
8 changes: 4 additions & 4 deletions ethportal-api/src/types/content_key/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ 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.
Expand Down Expand Up @@ -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)?;
Ok(Self::try_from(bytes.into())?)
Ok(Self::try_from(RawContentKey::from_str(data)?)?)
}
}

Expand Down Expand Up @@ -117,6 +117,6 @@ impl OverlayContentKey for IdentityContentKey {
self.value
}
fn to_bytes(&self) -> RawContentKey {
self.value.to_vec().into()
RawContentKey::from(self.value)
}
}
18 changes: 9 additions & 9 deletions ethportal-api/src/types/content_key/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ impl OverlayContentKey for StateContentKey {
}
}

bytes.into()
RawContentKey::from(bytes)
}
}

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

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

Expand Down Expand Up @@ -242,7 +242,7 @@ mod test {
#[test]
fn decode_empty_key_should_fail() {
assert_eq!(
StateContentKey::try_from(Bytes::new())
StateContentKey::try_from(RawContentKey::new())
.unwrap_err()
.to_string(),
"Unable to decode key SSZ bytes 0x due to InvalidLengthPrefix { len: 0, expected: 1 }",
Expand Down Expand Up @@ -270,10 +270,10 @@ 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 = StateContentKey::try_from(Bytes::from(content_key_bytes.clone()))?;
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(), Bytes::from(content_key_bytes));
assert_eq!(content_key.to_bytes(), content_key_bytes);
Ok(())
}

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().to_bytes().to_vec(),
content_key: target.to_bytes().to_vec(),
}),
};

Expand Down
8 changes: 4 additions & 4 deletions portalnet/src/overlay/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ where
TMetric::distance(&node_id.raw(), &content_id) <= node.data_radius;
if is_within_radius {
let content_items: Vec<(RawContentKey, Vec<u8>)> =
vec![(content_key.clone().to_bytes(), content.clone())];
vec![(content_key.to_bytes(), content.clone())];
let offer_request = Request::PopulatedOffer(PopulatedOffer { content_items });

// if we have met the max outbound utp transfer limit continue the loop as we aren't
Expand Down Expand Up @@ -1030,7 +1030,7 @@ where
let content_keys: Vec<TContentKey> = request
.content_keys
.into_iter()
.map(|k| (TContentKey::try_from)(k.as_ssz_bytes().into()))
.map(TContentKey::try_from)
.collect::<Result<Vec<TContentKey>, _>>()
.map_err(|_| {
OverlayRequestError::AcceptError(
Expand Down Expand Up @@ -1654,7 +1654,7 @@ where
}
};
let request = Request::FindContent(FindContent {
content_key: content_key.clone().to_bytes().to_vec(),
content_key: content_key.to_bytes().to_vec(),
});
let direction = RequestDirection::Outgoing {
destination: fallback_peer.clone(),
Expand Down Expand Up @@ -2022,7 +2022,7 @@ where
let content_keys_offered: Result<Vec<TContentKey>, TContentKey::Error> =
content_keys_offered
.into_iter()
.map(|key| TContentKey::try_from(key.as_ssz_bytes().into()))
.map(TContentKey::try_from)
.collect();

let content_keys_offered: Vec<TContentKey> = content_keys_offered
Expand Down
6 changes: 3 additions & 3 deletions trin-beacon/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl ContentStore for BeaconStorage {
type Key = BeaconContentKey;

fn get(&self, key: &Self::Key) -> Result<Option<Vec<u8>>, ContentStoreError> {
let content_key: RawContentKey = key.clone().to_bytes();
let content_key: RawContentKey = key.to_bytes();
let beacon_content_key = BeaconContentKey::try_from(content_key).map_err(|err| {
ContentStoreError::InvalidData {
message: format!("Error deserializing BeaconContentKey value: {err:?}"),
Expand Down Expand Up @@ -217,7 +217,7 @@ impl ContentStore for BeaconStorage {
&self,
key: &Self::Key,
) -> Result<ShouldWeStoreContent, ContentStoreError> {
let content_key: RawContentKey = key.clone().to_bytes();
let content_key: RawContentKey = key.to_bytes();
let beacon_content_key = BeaconContentKey::try_from(content_key).map_err(|err| {
ContentStoreError::InvalidData {
message: format!("Error deserializing BeaconContentKey value: {err:?}"),
Expand Down Expand Up @@ -379,7 +379,7 @@ impl BeaconStorage {
value: &Vec<u8>,
) -> Result<(), ContentStoreError> {
let content_id = key.content_id();
let content_key: RawContentKey = key.clone().to_bytes();
let content_key: RawContentKey = key.to_bytes();

match content_key.first() {
Some(&LIGHT_CLIENT_BOOTSTRAP_KEY_PREFIX) => {
Expand Down
2 changes: 1 addition & 1 deletion trin-history/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ async fn find_content(
enr: discv5::enr::Enr<discv5::enr::CombinedKey>,
content_key: HistoryContentKey,
) -> Result<Value, String> {
match network.overlay.send_find_content(enr, content_key.into()).await {
match network.overlay.send_find_content(enr, content_key.to_bytes().to_vec()).await {
Ok((content, utp_transfer)) => match content {
Content::ConnectionId(id) => Err(format!(
"FindContent request returned a connection id ({id:?}) instead of conducting utp transfer."
Expand Down
2 changes: 1 addition & 1 deletion trin-state/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ async fn find_content(
) -> Result<Value, String> {
let result = network
.overlay
.send_find_content(enr, content_key.into())
.send_find_content(enr, content_key.to_bytes().to_vec())
.await
.and_then(|(content, utp_transfer)| match content {
Content::ConnectionId(id) => Err(OverlayRequestError::Failure(format!(
Expand Down
8 changes: 4 additions & 4 deletions trin-validation/src/header_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ mod test {
use super::*;
use std::{fs, str::FromStr};

use alloy_primitives::{Address, Bloom, Bytes, B256, U256};
use alloy_primitives::{Address, Bloom, B256, U256};
use alloy_rlp::Decodable;
use rstest::*;
use serde_json::{json, Value};
Expand All @@ -243,7 +243,7 @@ mod test {
jsonrpc::{endpoints::HistoryEndpoint, request::HistoryJsonRpcRequest},
},
utils::bytes::{hex_decode, hex_encode},
HistoryContentKey,
HistoryContentKey, RawContentKey,
};

#[rstest]
Expand Down Expand Up @@ -273,8 +273,8 @@ mod test {
let obj = hwps.get(&block_number.to_string()).unwrap();
// Validate content_key decodes
let raw_ck = obj.get("content_key").unwrap().as_str().unwrap();
let raw_ck = hex_decode(raw_ck).unwrap();
let ck = HistoryContentKey::try_from(Bytes::from(raw_ck)).unwrap();
let raw_ck = RawContentKey::from_str(raw_ck).unwrap();
let ck = HistoryContentKey::try_from(raw_ck).unwrap();
match ck {
HistoryContentKey::BlockHeaderWithProof(_) => (),
_ => panic!("Invalid test, content key decoded improperly"),
Expand Down

0 comments on commit 585bbc8

Please sign in to comment.