From 998966b640e63942ccf581aaaa6f463df7800911 Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Mon, 20 Jan 2025 04:35:24 -0700 Subject: [PATCH] fix: update PR to match spec --- crates/ethportal-api/src/types/bytes.rs | 18 +- .../ping_extensions/custom_payload_format.rs | 77 --------- .../src/types/ping_extensions/decode.rs | 33 ++-- .../types/ping_extensions/extension_types.rs | 37 +++++ .../ping_extensions/extensions/type_0.rs | 42 +++-- .../ping_extensions/extensions/type_1.rs | 29 ++-- .../ping_extensions/extensions/type_2.rs | 29 ++-- .../ping_extensions/extensions/type_65535.rs | 23 +-- .../src/types/ping_extensions/mod.rs | 2 +- crates/ethportal-api/src/types/portal_wire.rs | 28 ++-- .../portalnet/src/overlay/ping_extensions.rs | 2 +- crates/portalnet/src/overlay/protocol.rs | 5 +- .../portalnet/src/overlay/service/manager.rs | 12 +- .../portalnet/src/overlay/service/ping/mod.rs | 156 ++++++++++-------- crates/portalnet/src/types/node.rs | 2 +- crates/subnetworks/beacon/src/jsonrpc.rs | 9 +- .../subnetworks/beacon/src/ping_extensions.rs | 2 +- crates/subnetworks/history/src/jsonrpc.rs | 9 +- .../history/src/ping_extensions.rs | 2 +- crates/subnetworks/state/src/jsonrpc.rs | 9 +- .../subnetworks/state/src/ping_extensions.rs | 2 +- 21 files changed, 247 insertions(+), 281 deletions(-) delete mode 100644 crates/ethportal-api/src/types/ping_extensions/custom_payload_format.rs create mode 100644 crates/ethportal-api/src/types/ping_extensions/extension_types.rs diff --git a/crates/ethportal-api/src/types/bytes.rs b/crates/ethportal-api/src/types/bytes.rs index fba49eb8f..e28171fba 100644 --- a/crates/ethportal-api/src/types/bytes.rs +++ b/crates/ethportal-api/src/types/bytes.rs @@ -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, B0>, B0>, B0>, B1>, B0>, B0>, B1>, + B1, + >, + B0, + >, + B0, +>; pub type ByteList32 = VariableList; pub type ByteList1024 = VariableList; +pub type ByteList1100 = VariableList; pub type ByteList2048 = VariableList; pub type ByteList32K = VariableList; pub type ByteList1G = VariableList; diff --git a/crates/ethportal-api/src/types/ping_extensions/custom_payload_format.rs b/crates/ethportal-api/src/types/ping_extensions/custom_payload_format.rs deleted file mode 100644 index 1339e22a5..000000000 --- a/crates/ethportal-api/src/types/ping_extensions/custom_payload_format.rs +++ /dev/null @@ -1,77 +0,0 @@ -use anyhow::anyhow; -use ssz::Decode; -use ssz_derive::{Decode, Encode}; -use ssz_types::{ - typenum::{ - bit::{B0, B1}, - UInt, UTerm, - }, - VariableList, -}; - -use crate::types::portal_wire::CustomPayload; - -// 1100 in binary is 10001001100 -pub type U1100 = UInt< - UInt< - UInt< - UInt, B0>, B0>, B0>, B1>, B0>, B0>, B1>, - B1, - >, - B0, - >, - B0, ->; - -#[derive(PartialEq, Debug, Encode, Decode)] -pub struct CustomPayloadExtensionsFormat { - pub r#type: u16, - pub payload: VariableList, -} - -impl TryFrom for CustomPayloadExtensionsFormat { - type Error = anyhow::Error; - - fn try_from(value: CustomPayload) -> Result { - CustomPayloadExtensionsFormat::from_ssz_bytes(&value.payload) - .map_err(|err| anyhow!("Failed to decode CustomPayloadExtensionsFormat: {err:?}")) - } -} - -#[derive(PartialEq, Debug, Clone, Copy, Eq)] -pub enum Extensions { - Capabilities, - BasicRadius, - HistoryRadius, - Error, -} - -impl TryFrom for Extensions { - type Error = ExtensionError; - - fn try_from(value: u16) -> Result { - match value { - 0 => Ok(Extensions::Capabilities), - 1 => Ok(Extensions::BasicRadius), - 2 => Ok(Extensions::HistoryRadius), - 65535 => Ok(Extensions::Error), - _ => Err(ExtensionError::NonSupportedExtension(value)), - } - } -} - -impl From 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), -} diff --git a/crates/ethportal-api/src/types/ping_extensions/decode.rs b/crates/ethportal-api/src/types/ping_extensions/decode.rs index 5864767c9..e52a2f1b8 100644 --- a/crates/ethportal-api/src/types/ping_extensions/decode.rs +++ b/crates/ethportal-api/src/types/ping_extensions/decode.rs @@ -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, @@ -29,41 +29,32 @@ impl From for Extensions { } } -impl TryFrom for DecodedExtension { - type Error = anyhow::Error; - - fn try_from(value: CustomPayload) -> Result { - let Ok(ping_custom_payload): anyhow::Result = - 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 { + 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)) } diff --git a/crates/ethportal-api/src/types/ping_extensions/extension_types.rs b/crates/ethportal-api/src/types/ping_extensions/extension_types.rs new file mode 100644 index 000000000..62cf22ad6 --- /dev/null +++ b/crates/ethportal-api/src/types/ping_extensions/extension_types.rs @@ -0,0 +1,37 @@ +#[derive(PartialEq, Debug, Clone, Copy, Eq)] +pub enum Extensions { + Capabilities, + BasicRadius, + HistoryRadius, + Error, +} + +impl TryFrom for Extensions { + type Error = ExtensionError; + + fn try_from(value: u16) -> Result { + match value { + 0 => Ok(Extensions::Capabilities), + 1 => Ok(Extensions::BasicRadius), + 2 => Ok(Extensions::HistoryRadius), + 65535 => Ok(Extensions::Error), + _ => Err(ExtensionError::NonSupportedExtension(value)), + } + } +} + +impl From 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), +} diff --git a/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs b/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs index a539aff39..6fc74478f 100644 --- a/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs +++ b/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs @@ -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::{ @@ -61,13 +59,7 @@ impl ClientInfoRadiusCapabilities { impl From 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()) } } @@ -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 @@ -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 = 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(); @@ -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 = 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(); @@ -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 = 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(); @@ -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 = 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(); diff --git a/crates/ethportal-api/src/types/ping_extensions/extensions/type_1.rs b/crates/ethportal-api/src/types/ping_extensions/extensions/type_1.rs index 46cd63871..0989a2bf8 100644 --- a/crates/ethportal-api/src/types/ping_extensions/extensions/type_1.rs +++ b/crates/ethportal-api/src/types/ping_extensions/extensions/type_1.rs @@ -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 { @@ -19,13 +16,7 @@ impl BasicRadius { impl From 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()) } } @@ -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); @@ -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 = 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(); @@ -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 = 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(); diff --git a/crates/ethportal-api/src/types/ping_extensions/extensions/type_2.rs b/crates/ethportal-api/src/types/ping_extensions/extensions/type_2.rs index 96c2832dd..77cb67505 100644 --- a/crates/ethportal-api/src/types/ping_extensions/extensions/type_2.rs +++ b/crates/ethportal-api/src/types/ping_extensions/extensions/type_2.rs @@ -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 HistoryRadius { @@ -23,13 +20,7 @@ impl HistoryRadius { impl From for CustomPayload { fn from(history_radius: HistoryRadius) -> Self { - CustomPayload::from( - CustomPayloadExtensionsFormat { - r#type: 2, - payload: history_radius.as_ssz_bytes().into(), - } - .as_ssz_bytes(), - ) + CustomPayload::from(history_radius.as_ssz_bytes()) } } @@ -54,7 +45,7 @@ mod tests { let history_radius = HistoryRadius::new(data_radius, 42); let custom_payload = CustomPayload::from(history_radius.clone()); - let decoded_extension = DecodedExtension::try_from(custom_payload).unwrap(); + let decoded_extension = DecodedExtension::decode_extension(2, custom_payload).unwrap(); if let DecodedExtension::HistoryRadius(decoded_history_radius) = decoded_extension { assert_eq!(history_radius, decoded_history_radius); @@ -78,16 +69,17 @@ mod tests { let data_radius = Distance::from(U256::MAX - U256::from(1)); let ephemeral_header_count = 4242; let history_radius = HistoryRadius::new(data_radius, ephemeral_header_count); - let custom_payload = CustomPayload::from(history_radius); + let payload = CustomPayload::from(history_radius); let ping = Ping { enr_seq: 1, - custom_payload, + payload_type: 2, + payload, }; let ping = Message::Ping(ping); let encoded: Vec = ping.clone().into(); let encoded = hex_encode(encoded); - let expected_encoded = "0x0001000000000000000c000000020006000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9210"; + let expected_encoded = "0x00010000000000000002000e000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9210"; assert_eq!(encoded, expected_encoded); let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap(); @@ -99,16 +91,17 @@ mod tests { let data_radius = Distance::from(U256::MAX - U256::from(1)); let ephemeral_header_count = 4242; let history_radius = HistoryRadius::new(data_radius, ephemeral_header_count); - let custom_payload = CustomPayload::from(history_radius); + let payload = CustomPayload::from(history_radius); let pong = Pong { enr_seq: 1, - custom_payload, + payload_type: 2, + payload, }; let pong = Message::Pong(pong); let encoded: Vec = pong.clone().into(); let encoded = hex_encode(encoded); - let expected_encoded = "0x0101000000000000000c000000020006000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9210"; + let expected_encoded = "0x01010000000000000002000e000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9210"; assert_eq!(encoded, expected_encoded); let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap(); diff --git a/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs b/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs index eb6e44856..907a9eb06 100644 --- a/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs +++ b/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs @@ -3,10 +3,7 @@ use ssz::Encode; use ssz_derive::{Decode, Encode}; use ssz_types::{typenum::U300, VariableList}; -use crate::types::{ - ping_extensions::custom_payload_format::CustomPayloadExtensionsFormat, - portal_wire::CustomPayload, -}; +use crate::types::portal_wire::CustomPayload; /// Used to respond to pings which the node can't handle #[derive(PartialEq, Debug, Clone, Encode, Decode)] @@ -35,13 +32,7 @@ impl PingError { impl From for CustomPayload { fn from(ping_error: PingError) -> Self { - CustomPayload::from( - CustomPayloadExtensionsFormat { - r#type: 65535, - payload: ping_error.as_ssz_bytes().into(), - } - .as_ssz_bytes(), - ) + CustomPayload::from(ping_error.as_ssz_bytes()) } } @@ -82,7 +73,7 @@ mod tests { let ping_error = PingError::new(error_code); let custom_payload = CustomPayload::from(ping_error.clone()); - let decoded_extension = DecodedExtension::try_from(custom_payload).unwrap(); + let decoded_extension = DecodedExtension::decode_extension(65535, custom_payload).unwrap(); if let DecodedExtension::Error(decoded_ping_error) = decoded_extension { assert_eq!(ping_error, decoded_ping_error); @@ -119,17 +110,17 @@ mod tests { let message = "hello world"; let basic_radius = PingError::new_with_message(error_code, message.as_bytes().to_vec()).unwrap(); - let custom_payload = CustomPayload::from(basic_radius); + let payload = CustomPayload::from(basic_radius); let pong = Pong { enr_seq: 1, - custom_payload, + payload_type: 65535, + payload, }; let pong = Message::Pong(pong); let encoded: Vec = pong.clone().into(); let encoded = hex_encode(encoded); - let expected_encoded = - "0x0101000000000000000c000000ffff0600000002000600000068656c6c6f20776f726c64"; + let expected_encoded = "0x010100000000000000ffff0e00000002000600000068656c6c6f20776f726c64"; assert_eq!(encoded, expected_encoded); let decoded = Message::try_from(hex_decode(&encoded).unwrap()).unwrap(); diff --git a/crates/ethportal-api/src/types/ping_extensions/mod.rs b/crates/ethportal-api/src/types/ping_extensions/mod.rs index 7f9b5a275..b07fc94a6 100644 --- a/crates/ethportal-api/src/types/ping_extensions/mod.rs +++ b/crates/ethportal-api/src/types/ping_extensions/mod.rs @@ -1,3 +1,3 @@ -pub mod custom_payload_format; pub mod decode; +pub mod extension_types; pub mod extensions; diff --git a/crates/ethportal-api/src/types/portal_wire.rs b/crates/ethportal-api/src/types/portal_wire.rs index cb29805c7..2d2c70666 100644 --- a/crates/ethportal-api/src/types/portal_wire.rs +++ b/crates/ethportal-api/src/types/portal_wire.rs @@ -17,9 +17,9 @@ use ssz_types::{typenum, BitList}; use thiserror::Error; use validator::ValidationError; +use super::bytes::ByteList1100; use crate::{ types::{ - bytes::ByteList2048, enr::{Enr, SszEnr}, network::{Network, Subnetwork}, }, @@ -72,7 +72,7 @@ pub const MAX_PORTAL_CONTENT_PAYLOAD_SIZE: usize = MAX_DISCV5_TALK_REQ_PAYLOAD_S /// Custom payload element of Ping and Pong overlay messages #[derive(Debug, PartialEq, Clone)] pub struct CustomPayload { - pub payload: ByteList2048, + pub payload: ByteList1100, } impl TryFrom<&Value> for CustomPayload { @@ -89,7 +89,7 @@ impl TryFrom<&Value> for CustomPayload { ))?, }; Ok(Self { - payload: ByteList2048::from(payload), + payload: ByteList1100::from(payload), }) } } @@ -97,7 +97,7 @@ impl TryFrom<&Value> for CustomPayload { impl From> for CustomPayload { fn from(ssz_bytes: Vec) -> Self { Self { - payload: ByteList2048::from(ssz_bytes), + payload: ByteList1100::from(ssz_bytes), } } } @@ -109,7 +109,7 @@ impl ssz::Decode for CustomPayload { fn from_ssz_bytes(bytes: &[u8]) -> Result { Ok(Self { - payload: ByteList2048::from(bytes.to_vec()), + payload: ByteList1100::from(bytes.to_vec()), }) } } @@ -338,16 +338,18 @@ impl TryFrom for Response { #[derive(Debug, PartialEq, Clone, Encode, Decode)] pub struct Ping { pub enr_seq: u64, - pub custom_payload: CustomPayload, + pub payload_type: u16, + pub payload: CustomPayload, } impl fmt::Display for Ping { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "Ping(enr_seq={}, radius={})", + "Ping(enr_seq={}, payload_type={}, payload={})", self.enr_seq, - hex_encode(self.custom_payload.as_ssz_bytes()) + self.payload_type, + hex_encode(self.payload.as_ssz_bytes()) ) } } @@ -355,16 +357,18 @@ impl fmt::Display for Ping { #[derive(Debug, PartialEq, Clone, Encode, Decode)] pub struct Pong { pub enr_seq: u64, - pub custom_payload: CustomPayload, + pub payload_type: u16, + pub payload: CustomPayload, } impl fmt::Display for Pong { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "Pong(enr_seq={}, radius={})", + "Pong(enr_seq={}, payload_type={}, payload={})", self.enr_seq, - hex_encode(self.custom_payload.as_ssz_bytes()) + self.payload_type, + hex_encode(self.payload.as_ssz_bytes()) ) } } @@ -372,7 +376,7 @@ impl fmt::Display for Pong { /// Convert to JSON Value from Pong ssz bytes impl From for Value { fn from(val: Pong) -> Self { - match U256::from_ssz_bytes(&val.custom_payload.payload.as_ssz_bytes()) { + match U256::from_ssz_bytes(&val.payload.payload.as_ssz_bytes()) { Ok(data_radius) => { let mut result = Map::new(); result.insert("enrSeq".to_owned(), Value::String(val.enr_seq.to_string())); diff --git a/crates/portalnet/src/overlay/ping_extensions.rs b/crates/portalnet/src/overlay/ping_extensions.rs index 80ef160d2..cb291148f 100644 --- a/crates/portalnet/src/overlay/ping_extensions.rs +++ b/crates/portalnet/src/overlay/ping_extensions.rs @@ -1,4 +1,4 @@ -use ethportal_api::types::ping_extensions::custom_payload_format::Extensions; +use ethportal_api::types::ping_extensions::extension_types::Extensions; pub trait PingExtension { /// Returns true if the extension is supported by the clients subnetwork. diff --git a/crates/portalnet/src/overlay/protocol.rs b/crates/portalnet/src/overlay/protocol.rs index 7df4dfb6f..cf7dbbda4 100644 --- a/crates/portalnet/src/overlay/protocol.rs +++ b/crates/portalnet/src/overlay/protocol.rs @@ -375,12 +375,13 @@ impl< // Construct the request. let enr_seq = self.discovery.local_enr().seq(); let data_radius = self.data_radius(); - let custom_payload = + let payload = ClientInfoRadiusCapabilities::new(data_radius, self.ping_extensions.raw_extensions()) .into(); let request = Ping { enr_seq, - custom_payload, + payload_type: 0, + payload, }; let direction = RequestDirection::Outgoing { destination: enr }; diff --git a/crates/portalnet/src/overlay/service/manager.rs b/crates/portalnet/src/overlay/service/manager.rs index 2d0584c21..af2f1caa8 100644 --- a/crates/portalnet/src/overlay/service/manager.rs +++ b/crates/portalnet/src/overlay/service/manager.rs @@ -2601,7 +2601,8 @@ mod tests { let ping = Ping { enr_seq: source.seq() + 1, - custom_payload: ClientInfoRadiusCapabilities::new( + payload_type: 0, + payload: ClientInfoRadiusCapabilities::new( data_radius, service.ping_extensions.raw_extensions(), ) @@ -2650,7 +2651,8 @@ mod tests { let ping = Ping { enr_seq: source.seq(), - custom_payload: ClientInfoRadiusCapabilities::new( + payload_type: 0, + payload: ClientInfoRadiusCapabilities::new( data_radius, service.ping_extensions.raw_extensions(), ) @@ -2715,7 +2717,8 @@ mod tests { let pong = Pong { enr_seq: source.seq() + 1, - custom_payload: ClientInfoRadiusCapabilities::new( + payload_type: 0, + payload: ClientInfoRadiusCapabilities::new( data_radius, service.ping_extensions.raw_extensions(), ) @@ -2763,7 +2766,8 @@ mod tests { let pong = Pong { enr_seq: source.seq(), - custom_payload: ClientInfoRadiusCapabilities::new( + payload_type: 0, + payload: ClientInfoRadiusCapabilities::new( data_radius, service.ping_extensions.raw_extensions(), ) diff --git a/crates/portalnet/src/overlay/service/ping/mod.rs b/crates/portalnet/src/overlay/service/ping/mod.rs index ba57003c1..3b19eb2c8 100644 --- a/crates/portalnet/src/overlay/service/ping/mod.rs +++ b/crates/portalnet/src/overlay/service/ping/mod.rs @@ -8,8 +8,8 @@ use ethportal_api::{ distance::Metric, enr::Enr, ping_extensions::{ - custom_payload_format::{CustomPayloadExtensionsFormat, Extensions}, decode::DecodedExtension, + extension_types::Extensions, extensions::{ type_0::ClientInfoRadiusCapabilities, type_1::BasicRadius, @@ -45,10 +45,11 @@ impl< TPingExtensions: 'static + PingExtension + Send + Sync, > OverlayService { - fn create_pong(&self, custom_payload: CustomPayload) -> Pong { + fn create_pong(&self, payload_type: u16, payload: CustomPayload) -> Pong { Pong { enr_seq: self.local_enr().seq(), - custom_payload, + payload_type, + payload, } } @@ -66,22 +67,7 @@ impl< "Handling Ping message {request}", ); - let ping_custom_payload = - match CustomPayloadExtensionsFormat::try_from(request.custom_payload) { - Ok(ping_custom_payload) => ping_custom_payload, - Err(err) => { - warn!( - protocol = %self.protocol, - request.source = %source, - request.discv5.id = %request_id, - "Failed to decode custom payload from ping: {err:?}", - ); - return self - .create_pong(PingError::new(ErrorCodes::FailedToDecodePayload).into()); - } - }; - - let extension_type = match Extensions::try_from(ping_custom_payload.r#type) { + let extension_type = match Extensions::try_from(request.payload_type) { Ok(extension) => extension, Err(err) => { warn!( @@ -90,7 +76,10 @@ impl< request.discv5.id = %request_id, "Received non-supported extension type in ping message: {err:?}", ); - return self.create_pong(PingError::new(ErrorCodes::ExtensionNotSupported).into()); + return self.create_pong( + Extensions::Error.into(), + PingError::new(ErrorCodes::ExtensionNotSupported).into(), + ); } }; @@ -101,20 +90,31 @@ impl< request.discv5.id = %request_id, "Received non-supported ping extension on this portal subnetwork: {extension_type:?}", ); - return self.create_pong(PingError::new(ErrorCodes::ExtensionNotSupported).into()); + return self.create_pong( + Extensions::Error.into(), + PingError::new(ErrorCodes::ExtensionNotSupported).into(), + ); } - let custom_payload = match extension_type { - Extensions::Capabilities => self.create_capabilities().into(), - Extensions::BasicRadius => BasicRadius { - data_radius: self.data_radius(), + match extension_type { + Extensions::Capabilities => { + self.create_pong(extension_type.into(), self.create_capabilities().into()) } - .into(), - Extensions::HistoryRadius => HistoryRadius { - data_radius: self.data_radius(), - ephemeral_header_count: 0, - } - .into(), + Extensions::BasicRadius => self.create_pong( + extension_type.into(), + BasicRadius { + data_radius: self.data_radius(), + } + .into(), + ), + Extensions::HistoryRadius => self.create_pong( + extension_type.into(), + HistoryRadius { + data_radius: self.data_radius(), + ephemeral_header_count: 0, + } + .into(), + ), Extensions::Error => { warn!( protocol = %self.protocol, @@ -122,11 +122,12 @@ impl< request.discv5.id = %request_id, "Received invalid Ping message, Errors should only be received from pong", ); - PingError::new(ErrorCodes::SystemError).into() + self.create_pong( + extension_type.into(), + PingError::new(ErrorCodes::SystemError).into(), + ) } - }; - - self.create_pong(custom_payload) + } } /// Processes a ping request from some source node. @@ -143,17 +144,18 @@ impl< self.request_node(&node.enr()); } - let extension = match DecodedExtension::try_from(ping.custom_payload) { - Ok(extension) => extension, - Err(err) => { - warn!( - protocol = %self.protocol, - request.source = %source, - "Failed to decode custom payload during process_ping: {err:?}", - ); - return; - } - }; + let extension = + match DecodedExtension::decode_extension(ping.payload_type, ping.payload) { + Ok(extension) => extension, + Err(err) => { + warn!( + protocol = %self.protocol, + request.source = %source, + "Failed to decode custom payload during process_ping: {err:?}", + ); + return; + } + }; if !self.ping_extensions.is_supported(extension.clone().into()) { warn!( @@ -210,17 +212,18 @@ impl< self.request_node(&node.enr()); } - let extension = match DecodedExtension::try_from(pong.custom_payload) { - Ok(extension) => extension, - Err(err) => { - warn!( - protocol = %self.protocol, - request.source = %source, - "Failed to decode custom payload during process_ping: {err:?}", - ); - return; - } - }; + let extension = + match DecodedExtension::decode_extension(pong.payload_type, pong.payload) { + Ok(extension) => extension, + Err(err) => { + warn!( + protocol = %self.protocol, + request.source = %source, + "Failed to decode custom payload during process_ping: {err:?}", + ); + return; + } + }; if !self.ping_extensions.is_supported(extension.clone().into()) { warn!( @@ -261,24 +264,34 @@ impl< ClientInfoRadiusCapabilities::new(self.data_radius(), self.ping_extensions.raw_extensions()) } - fn handle_base_extension(&self, extension: Extensions, node_id: NodeId) -> CustomPayload { + fn handle_base_extension( + &self, + extension: Extensions, + node_id: NodeId, + ) -> (u16, CustomPayload) { match extension { - Extensions::BasicRadius => BasicRadius { - data_radius: self.data_radius(), - } - .into(), - Extensions::HistoryRadius => HistoryRadius { - data_radius: self.data_radius(), - ephemeral_header_count: 0, - } - .into(), + Extensions::BasicRadius => ( + extension.into(), + BasicRadius { + data_radius: self.data_radius(), + } + .into(), + ), + Extensions::HistoryRadius => ( + extension.into(), + HistoryRadius { + data_radius: self.data_radius(), + ephemeral_header_count: 0, + } + .into(), + ), _ => { warn!( protocol = %self.protocol, request.dest = %node_id, "Base extension wasn't implemented: {extension:?}, sending Capabilities instead. This is a bug!", ); - self.create_capabilities().into() + (0, self.create_capabilities().into()) } } } @@ -293,17 +306,18 @@ impl< "Sending Ping message", ); - let custom_payload = match node.capabilities().map(|capabilities| { + let (payload_type, payload) = match node.capabilities().map(|capabilities| { self.ping_extensions .latest_mutually_supported_base_extension(capabilities) }) { Some(Some(extension)) => self.handle_base_extension(extension, node.enr.node_id()), - _ => self.create_capabilities().into(), + _ => (0, self.create_capabilities().into()), }; let ping = Request::Ping(Ping { enr_seq: self.local_enr().seq(), - custom_payload, + payload_type, + payload, }); let request = OverlayRequest::new( ping, diff --git a/crates/portalnet/src/types/node.rs b/crates/portalnet/src/types/node.rs index a9b690964..c11e50e74 100644 --- a/crates/portalnet/src/types/node.rs +++ b/crates/portalnet/src/types/node.rs @@ -1,7 +1,7 @@ use std::fmt; use ethportal_api::types::{ - distance::Distance, enr::Enr, ping_extensions::custom_payload_format::Extensions, + distance::Distance, enr::Enr, ping_extensions::extension_types::Extensions, }; /// A node in the overlay network routing table. diff --git a/crates/subnetworks/beacon/src/jsonrpc.rs b/crates/subnetworks/beacon/src/jsonrpc.rs index a34941325..31b3e87e9 100644 --- a/crates/subnetworks/beacon/src/jsonrpc.rs +++ b/crates/subnetworks/beacon/src/jsonrpc.rs @@ -400,10 +400,11 @@ async fn ping( ) -> Result { match network.overlay.send_ping(enr).await { Ok(pong) => { - let data_radius = match DecodedExtension::try_from(pong.custom_payload) { - Ok(DecodedExtension::Capabilities(capabilities)) => *capabilities.data_radius, - err => return Err(format!("Failed to decode capabilities: {err:?}")), - }; + let data_radius = + match DecodedExtension::decode_extension(pong.payload_type, pong.payload) { + Ok(DecodedExtension::Capabilities(capabilities)) => *capabilities.data_radius, + err => return Err(format!("Failed to decode capabilities: {err:?}")), + }; Ok(json!(PongInfo { enr_seq: pong.enr_seq, diff --git a/crates/subnetworks/beacon/src/ping_extensions.rs b/crates/subnetworks/beacon/src/ping_extensions.rs index 5f0996ef2..7ccf668dc 100644 --- a/crates/subnetworks/beacon/src/ping_extensions.rs +++ b/crates/subnetworks/beacon/src/ping_extensions.rs @@ -1,4 +1,4 @@ -use ethportal_api::types::ping_extensions::custom_payload_format::Extensions; +use ethportal_api::types::ping_extensions::extension_types::Extensions; use portalnet::overlay::ping_extensions::PingExtension; pub struct BeaconPingExtensions {} diff --git a/crates/subnetworks/history/src/jsonrpc.rs b/crates/subnetworks/history/src/jsonrpc.rs index 8bc629c87..f48b0548f 100644 --- a/crates/subnetworks/history/src/jsonrpc.rs +++ b/crates/subnetworks/history/src/jsonrpc.rs @@ -346,10 +346,11 @@ async fn ping( ) -> Result { match network.overlay.send_ping(enr).await { Ok(pong) => { - let data_radius = match DecodedExtension::try_from(pong.custom_payload) { - Ok(DecodedExtension::Capabilities(capabilities)) => *capabilities.data_radius, - err => return Err(format!("Failed to decode capabilities: {err:?}")), - }; + let data_radius = + match DecodedExtension::decode_extension(pong.payload_type, pong.payload) { + Ok(DecodedExtension::Capabilities(capabilities)) => *capabilities.data_radius, + err => return Err(format!("Failed to decode capabilities: {err:?}")), + }; Ok(json!(PongInfo { enr_seq: pong.enr_seq, diff --git a/crates/subnetworks/history/src/ping_extensions.rs b/crates/subnetworks/history/src/ping_extensions.rs index becdc32e3..315560b9a 100644 --- a/crates/subnetworks/history/src/ping_extensions.rs +++ b/crates/subnetworks/history/src/ping_extensions.rs @@ -1,4 +1,4 @@ -use ethportal_api::types::ping_extensions::custom_payload_format::Extensions; +use ethportal_api::types::ping_extensions::extension_types::Extensions; use portalnet::overlay::ping_extensions::PingExtension; pub struct HistoryPingExtensions {} diff --git a/crates/subnetworks/state/src/jsonrpc.rs b/crates/subnetworks/state/src/jsonrpc.rs index c44337bc0..0785fe33d 100644 --- a/crates/subnetworks/state/src/jsonrpc.rs +++ b/crates/subnetworks/state/src/jsonrpc.rs @@ -98,10 +98,11 @@ fn routing_table_info(network: Arc) -> Result { async fn ping(network: Arc, enr: Enr) -> Result { let pong = match network.overlay.send_ping(enr.clone()).await { Ok(pong) => { - let data_radius = match DecodedExtension::try_from(pong.custom_payload) { - Ok(DecodedExtension::Capabilities(capabilities)) => *capabilities.data_radius, - err => return Err(format!("Failed to decode capabilities: {err:?}")), - }; + let data_radius = + match DecodedExtension::decode_extension(pong.payload_type, pong.payload) { + Ok(DecodedExtension::Capabilities(capabilities)) => *capabilities.data_radius, + err => return Err(format!("Failed to decode capabilities: {err:?}")), + }; Ok(PongInfo { enr_seq: pong.enr_seq, diff --git a/crates/subnetworks/state/src/ping_extensions.rs b/crates/subnetworks/state/src/ping_extensions.rs index b6c453cc1..6c59b3528 100644 --- a/crates/subnetworks/state/src/ping_extensions.rs +++ b/crates/subnetworks/state/src/ping_extensions.rs @@ -1,4 +1,4 @@ -use ethportal_api::types::ping_extensions::custom_payload_format::Extensions; +use ethportal_api::types::ping_extensions::extension_types::Extensions; use portalnet::overlay::ping_extensions::PingExtension; pub struct StatePingExtensions {}