-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement ping extensions #1616
Open
KolbyML
wants to merge
1
commit into
ethereum:master
Choose a base branch
from
KolbyML:implement-ping-extensions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with the |
||
"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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit use the var inside the string. though, this is the kind of nit I would expect clippy to pick up? seems like the rule is enabled https://github.com/ethereum/trin/blob/master/crates/ethportal-api/src/lib.rs#L5C1-L5C40 ... maybe clippy's structure isn't compatible/needs updating with the new
crates/
layout?also another nit import
anyhow::anyhow
at the top rather than importing it here