-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,519 additions
and
285 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
crates/ethportal-api/src/types/ping_extensions/custom_payload_format.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, 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<u8, U1100>, | ||
} | ||
|
||
impl TryFrom<CustomPayload> for CustomPayloadExtensionsFormat { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: CustomPayload) -> Result<Self, Self::Error> { | ||
CustomPayloadExtensionsFormat::from_ssz_bytes(&value.payload) | ||
.map_err(|e| anyhow::anyhow!("Failed to decode CustomPayloadExtensionsFormat: {:?}", e)) | ||
} | ||
} | ||
|
||
#[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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use anyhow::bail; | ||
use ssz::Decode; | ||
|
||
use super::{ | ||
custom_payload_format::{CustomPayloadExtensionsFormat, Extensions}, | ||
extensions::{ | ||
type_0::ClientInfoRadiusCapabilities, type_1::BasicRadius, type_2::HistoryRadius, | ||
type_65535::PingError, | ||
}, | ||
}; | ||
use crate::types::portal_wire::CustomPayload; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum DecodedExtension { | ||
Capabilities(ClientInfoRadiusCapabilities), | ||
BasicRadius(BasicRadius), | ||
HistoryRadius(HistoryRadius), | ||
Error(PingError), | ||
} | ||
|
||
impl From<DecodedExtension> for Extensions { | ||
fn from(value: DecodedExtension) -> Self { | ||
match value { | ||
DecodedExtension::Capabilities(_) => Extensions::Capabilities, | ||
DecodedExtension::BasicRadius(_) => Extensions::BasicRadius, | ||
DecodedExtension::HistoryRadius(_) => Extensions::HistoryRadius, | ||
DecodedExtension::Error(_) => Extensions::Error, | ||
} | ||
} | ||
} | ||
|
||
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"); | ||
}; | ||
|
||
match extension_type { | ||
Extensions::Capabilities => { | ||
let capabilities = | ||
ClientInfoRadiusCapabilities::from_ssz_bytes(&ping_custom_payload.payload) | ||
.map_err(|err| { | ||
anyhow::anyhow!( | ||
"Failed to decode ClientInfoRadiusCapabilities: {err:?}" | ||
) | ||
})?; | ||
Ok(DecodedExtension::Capabilities(capabilities)) | ||
} | ||
Extensions::BasicRadius => { | ||
let basic_radius = BasicRadius::from_ssz_bytes(&ping_custom_payload.payload) | ||
.map_err(|err| anyhow::anyhow!("Failed to decode BasicRadius: {err:?}"))?; | ||
Ok(DecodedExtension::BasicRadius(basic_radius)) | ||
} | ||
Extensions::HistoryRadius => { | ||
let history_radius = HistoryRadius::from_ssz_bytes(&ping_custom_payload.payload) | ||
.map_err(|err| anyhow::anyhow!("Failed to decode HistoryRadius: {err:?}"))?; | ||
Ok(DecodedExtension::HistoryRadius(history_radius)) | ||
} | ||
Extensions::Error => { | ||
let error = PingError::from_ssz_bytes(&ping_custom_payload.payload) | ||
.map_err(|err| anyhow::anyhow!("Failed to decode PingError: {err:?}"))?; | ||
Ok(DecodedExtension::Error(error)) | ||
} | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/ethportal-api/src/types/ping_extensions/extensions/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod type_0; | ||
pub mod type_1; | ||
pub mod type_2; | ||
pub mod type_65535; |
Oops, something went wrong.