Skip to content

Commit

Permalink
fix: update PR to match spec
Browse files Browse the repository at this point in the history
  • Loading branch information
KolbyML committed Jan 20, 2025
1 parent f8a583c commit 998966b
Show file tree
Hide file tree
Showing 21 changed files with 247 additions and 281 deletions.
18 changes: 17 additions & 1 deletion crates/ethportal-api/src/types/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
use ssz_types::{typenum, VariableList};
use ssz_types::{
typenum::{self, UInt, UTerm, B0, B1},
VariableList,
};

// 1100 in binary is 10001001100
pub type U1100 = UInt<
UInt<
UInt<
UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B1>, B0>, B0>, B1>,
B1,
>,
B0,
>,
B0,
>;

pub type ByteList32 = VariableList<u8, typenum::U32>;
pub type ByteList1024 = VariableList<u8, typenum::U1024>;
pub type ByteList1100 = VariableList<u8, U1100>;
pub type ByteList2048 = VariableList<u8, typenum::U2048>;
pub type ByteList32K = VariableList<u8, typenum::U32768>;
pub type ByteList1G = VariableList<u8, typenum::U1073741824>;

This file was deleted.

33 changes: 12 additions & 21 deletions crates/ethportal-api/src/types/ping_extensions/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, bail};
use ssz::Decode;

use super::{
custom_payload_format::{CustomPayloadExtensionsFormat, Extensions},
extension_types::Extensions,
extensions::{
type_0::ClientInfoRadiusCapabilities, type_1::BasicRadius, type_2::HistoryRadius,
type_65535::PingError,
Expand All @@ -29,41 +29,32 @@ impl From<DecodedExtension> for Extensions {
}
}

impl TryFrom<CustomPayload> for DecodedExtension {
type Error = anyhow::Error;

fn try_from(value: CustomPayload) -> Result<Self, anyhow::Error> {
let Ok(ping_custom_payload): anyhow::Result<CustomPayloadExtensionsFormat> =
value.try_into()
else {
bail!("Failed to decode CustomPayloadExtensionsFormat");
};

let Ok(extension_type) = Extensions::try_from(ping_custom_payload.r#type) else {
bail!("Failed to decode extension type");
impl DecodedExtension {
pub fn decode_extension(payload_type: u16, payload: CustomPayload) -> anyhow::Result<Self> {
let Ok(extension_type) = Extensions::try_from(payload_type) else {
bail!("Failed to decode extension type {payload_type}");
};

match extension_type {
Extensions::Capabilities => {
let capabilities =
ClientInfoRadiusCapabilities::from_ssz_bytes(&ping_custom_payload.payload)
.map_err(|err| {
anyhow!("Failed to decode ClientInfoRadiusCapabilities: {err:?}")
})?;
let capabilities = ClientInfoRadiusCapabilities::from_ssz_bytes(&payload.payload)
.map_err(|err| {
anyhow!("Failed to decode ClientInfoRadiusCapabilities: {err:?}")
})?;
Ok(DecodedExtension::Capabilities(capabilities))
}
Extensions::BasicRadius => {
let basic_radius = BasicRadius::from_ssz_bytes(&ping_custom_payload.payload)
let basic_radius = BasicRadius::from_ssz_bytes(&payload.payload)
.map_err(|err| anyhow!("Failed to decode BasicRadius: {err:?}"))?;
Ok(DecodedExtension::BasicRadius(basic_radius))
}
Extensions::HistoryRadius => {
let history_radius = HistoryRadius::from_ssz_bytes(&ping_custom_payload.payload)
let history_radius = HistoryRadius::from_ssz_bytes(&payload.payload)
.map_err(|err| anyhow!("Failed to decode HistoryRadius: {err:?}"))?;
Ok(DecodedExtension::HistoryRadius(history_radius))
}
Extensions::Error => {
let error = PingError::from_ssz_bytes(&ping_custom_payload.payload)
let error = PingError::from_ssz_bytes(&payload.payload)
.map_err(|err| anyhow!("Failed to decode PingError: {err:?}"))?;
Ok(DecodedExtension::Error(error))
}
Expand Down
37 changes: 37 additions & 0 deletions crates/ethportal-api/src/types/ping_extensions/extension_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[derive(PartialEq, Debug, Clone, Copy, Eq)]
pub enum Extensions {
Capabilities,
BasicRadius,
HistoryRadius,
Error,
}

impl TryFrom<u16> for Extensions {
type Error = ExtensionError;

fn try_from(value: u16) -> Result<Self, ExtensionError> {
match value {
0 => Ok(Extensions::Capabilities),
1 => Ok(Extensions::BasicRadius),
2 => Ok(Extensions::HistoryRadius),
65535 => Ok(Extensions::Error),
_ => Err(ExtensionError::NonSupportedExtension(value)),
}
}
}

impl From<Extensions> for u16 {
fn from(value: Extensions) -> u16 {
match value {
Extensions::Capabilities => 0,
Extensions::BasicRadius => 1,
Extensions::HistoryRadius => 2,
Extensions::Error => 65535,
}
}
}

#[derive(Debug)]
pub enum ExtensionError {
NonSupportedExtension(u16),
}
42 changes: 19 additions & 23 deletions crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use ssz_types::{
use crate::{
types::{
distance::Distance,
ping_extensions::custom_payload_format::{
CustomPayloadExtensionsFormat, ExtensionError, Extensions,
},
ping_extensions::extension_types::{ExtensionError, Extensions},
portal_wire::CustomPayload,
},
version::{
Expand Down Expand Up @@ -61,13 +59,7 @@ impl ClientInfoRadiusCapabilities {

impl From<ClientInfoRadiusCapabilities> for CustomPayload {
fn from(client_info_radius_capacities: ClientInfoRadiusCapabilities) -> Self {
CustomPayload::from(
CustomPayloadExtensionsFormat {
r#type: 0,
payload: client_info_radius_capacities.as_ssz_bytes().into(),
}
.as_ssz_bytes(),
)
CustomPayload::from(client_info_radius_capacities.as_ssz_bytes())
}
}

Expand Down Expand Up @@ -236,7 +228,7 @@ mod tests {
ClientInfoRadiusCapabilities::new(radius, capabilities);
let custom_payload = CustomPayload::from(client_info_radius_capabilities.clone());

let decoded_extension = DecodedExtension::try_from(custom_payload).unwrap();
let decoded_extension = DecodedExtension::decode_extension(0, custom_payload).unwrap();

if let DecodedExtension::Capabilities(decoded_client_info_radius_capabilities) =
decoded_extension
Expand Down Expand Up @@ -285,16 +277,17 @@ mod tests {
data_radius,
capabilities,
);
let custom_payload = CustomPayload::from(capabilities_payload);
let payload = CustomPayload::from(capabilities_payload);
let ping = Ping {
enr_seq: 1,
custom_payload,
payload_type: 0,
payload,
};
let ping = Message::Ping(ping);

let encoded: Vec<u8> = ping.clone().into();
let encoded = hex_encode(encoded);
let expected_encoded = "0x0001000000000000000c00000000000600000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff550000007472696e2f76302e312e312d62363166646335632f6c696e75782d7838365f36342f7275737463312e38312e3000000100ffff";
let expected_encoded = "0x00010000000000000000000e00000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff550000007472696e2f76302e312e312d62363166646335632f6c696e75782d7838365f36342f7275737463312e38312e3000000100ffff";
assert_eq!(encoded, expected_encoded);

let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap();
Expand All @@ -307,16 +300,17 @@ mod tests {
let capabilities = vec![0, 1, 65535];
let capabilities_payload =
ClientInfoRadiusCapabilities::new_with_client_info(None, data_radius, capabilities);
let custom_payload = CustomPayload::from(capabilities_payload);
let payload = CustomPayload::from(capabilities_payload);
let ping = Ping {
enr_seq: 1,
custom_payload,
payload_type: 0,
payload,
};
let ping = Message::Ping(ping);

let encoded: Vec<u8> = ping.clone().into();
let encoded = hex_encode(encoded);
let expected_encoded = "0x0001000000000000000c00000000000600000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2800000000000100ffff";
let expected_encoded = "0x00010000000000000000000e00000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2800000000000100ffff";
assert_eq!(encoded, expected_encoded);

let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap();
Expand All @@ -334,16 +328,17 @@ mod tests {
data_radius,
capabilities,
);
let custom_payload = CustomPayload::from(capabilities_payload);
let payload = CustomPayload::from(capabilities_payload);
let pong = Pong {
enr_seq: 1,
custom_payload,
payload_type: 0,
payload,
};
let pong = Message::Pong(pong);

let encoded: Vec<u8> = pong.clone().into();
let encoded = hex_encode(encoded);
let expected_encoded = "0x0101000000000000000c00000000000600000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff550000007472696e2f76302e312e312d62363166646335632f6c696e75782d7838365f36342f7275737463312e38312e3000000100ffff";
let expected_encoded = "0x01010000000000000000000e00000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff550000007472696e2f76302e312e312d62363166646335632f6c696e75782d7838365f36342f7275737463312e38312e3000000100ffff";
assert_eq!(encoded, expected_encoded);

let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap();
Expand All @@ -356,16 +351,17 @@ mod tests {
let capabilities = vec![0, 1, 65535];
let capabilities_payload =
ClientInfoRadiusCapabilities::new_with_client_info(None, data_radius, capabilities);
let custom_payload = CustomPayload::from(capabilities_payload);
let payload = CustomPayload::from(capabilities_payload);
let pong = Pong {
enr_seq: 1,
custom_payload,
payload_type: 0,
payload,
};
let pong = Message::Pong(pong);

let encoded: Vec<u8> = pong.clone().into();
let encoded = hex_encode(encoded);
let expected_encoded = "0x0101000000000000000c00000000000600000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2800000000000100ffff";
let expected_encoded = "0x01010000000000000000000e00000028000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2800000000000100ffff";
assert_eq!(encoded, expected_encoded);

let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap();
Expand Down
29 changes: 11 additions & 18 deletions crates/ethportal-api/src/types/ping_extensions/extensions/type_1.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use ssz::Encode;
use ssz_derive::{Decode, Encode};

use crate::types::{
distance::Distance, ping_extensions::custom_payload_format::CustomPayloadExtensionsFormat,
portal_wire::CustomPayload,
};
use crate::types::{distance::Distance, portal_wire::CustomPayload};

#[derive(PartialEq, Debug, Clone, Encode, Decode)]
pub struct BasicRadius {
Expand All @@ -19,13 +16,7 @@ impl BasicRadius {

impl From<BasicRadius> for CustomPayload {
fn from(basic_radius: BasicRadius) -> Self {
CustomPayload::from(
CustomPayloadExtensionsFormat {
r#type: 1,
payload: basic_radius.as_ssz_bytes().into(),
}
.as_ssz_bytes(),
)
CustomPayload::from(basic_radius.as_ssz_bytes())
}
}

Expand All @@ -50,7 +41,7 @@ mod tests {
let basic_radius = BasicRadius::new(data_radius);
let custom_payload = CustomPayload::from(basic_radius.clone());

let decoded_extension = DecodedExtension::try_from(custom_payload).unwrap();
let decoded_extension = DecodedExtension::decode_extension(1, custom_payload).unwrap();

if let DecodedExtension::BasicRadius(decoded_basic_radius) = decoded_extension {
assert_eq!(basic_radius, decoded_basic_radius);
Expand All @@ -73,16 +64,17 @@ mod tests {
fn message_encoding_ping_basic_radius() {
let data_radius = Distance::from(U256::MAX - U256::from(1));
let basic_radius = BasicRadius::new(data_radius);
let custom_payload = CustomPayload::from(basic_radius);
let payload = CustomPayload::from(basic_radius);
let ping = Ping {
enr_seq: 1,
custom_payload,
payload_type: 1,
payload,
};
let ping = Message::Ping(ping);

let encoded: Vec<u8> = ping.clone().into();
let encoded = hex_encode(encoded);
let expected_encoded = "0x0001000000000000000c000000010006000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
let expected_encoded = "0x00010000000000000001000e000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
assert_eq!(encoded, expected_encoded);

let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap();
Expand All @@ -93,16 +85,17 @@ mod tests {
fn message_encoding_pong_basic_radius() {
let data_radius = Distance::from(U256::MAX - U256::from(1));
let basic_radius = BasicRadius::new(data_radius);
let custom_payload = CustomPayload::from(basic_radius);
let payload = CustomPayload::from(basic_radius);
let pong = Pong {
enr_seq: 1,
custom_payload,
payload_type: 1,
payload,
};
let pong = Message::Pong(pong);

let encoded: Vec<u8> = pong.clone().into();
let encoded = hex_encode(encoded);
let expected_encoded = "0x0101000000000000000c000000010006000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
let expected_encoded = "0x01010000000000000001000e000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
assert_eq!(encoded, expected_encoded);

let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap();
Expand Down
Loading

0 comments on commit 998966b

Please sign in to comment.