Skip to content
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

Remove code that can panic #5

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

kettlebell
Copy link
Collaborator

No description provided.

@kettlebell kettlebell changed the title Remove unwrap calls in [In|Out]boundUpgrade Remove code that can panic Nov 9, 2022
@kettlebell kettlebell force-pushed the dev-397-get-rid-of-panicking-code branch from aa061cf to 82a7374 Compare November 12, 2022 06:47
@kettlebell kettlebell changed the base branch from master to dev-309-sync-impl November 12, 2022 06:48
@kettlebell kettlebell force-pushed the dev-397-get-rid-of-panicking-code branch 2 times, most recently from 93bb2ca to 3d89acf Compare November 21, 2022 13:41
@kettlebell kettlebell changed the base branch from dev-309-sync-impl to master November 21, 2022 13:41
@kettlebell kettlebell force-pushed the dev-397-get-rid-of-panicking-code branch from 3d89acf to 45e1a15 Compare November 21, 2022 20:57
@kettlebell kettlebell marked this pull request as ready for review November 22, 2022 06:56
@kettlebell kettlebell requested a review from oskin1 November 22, 2022 06:56
Copy link
Member

@oskin1 oskin1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way to get rid of panicking code is to make illegal states unrepresentable on type-level.
P.s. here is a good article on that topic https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/

spectrum-network/src/peer_manager/peers_state.rs Outdated Show resolved Hide resolved
@@ -33,6 +33,8 @@ pub enum ProtocolHandshakeErr {
pub enum ProtocolUpgradeErr {
#[error(transparent)]
HandshakeErr(#[from] ProtocolHandshakeErr),
#[error("Unsupported {0:?}")]
UnsupportedProtocolVer(ProtocolVer),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the case when the negotiated tag is not found in Upgrade is also not possible unless some crazy state inconsistency took place. So no point in adding this exception variant, it can't be handled properly anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into it in more detail; I see what you mean. Thanks for the Parse, don't validate article, learnt a lot. Do you think it's worth wrapping up these collections in a newtype which prevents any mutation of the underlying collection, and also using a newtype-wrapped protocol id/ver?

e.g. in ProtocolUpgradeIn::supported_versions, we wrap the BTreeMap in a struct SupportedProtocolVers, and instead of handling ProtocolVer directly we have struct SupportedProtocolVer(ProtocolVer) which can only be generated by SupportedProtocolVers?

If that's too much, we can comment the use of the unwrap and explicitly allow it via #[allow(clippy::unwrap_used]. Later when we setup CI/CD, we can add clippy exclusions like e.g. https://github.com/ergoplatform/sigma-rust/blob/develop/ergotree-ir/src/lib.rs#L13-L19

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, wrappers that hold some assumption should be good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented the wrappers.

spectrum-network/src/network_controller.rs Outdated Show resolved Hide resolved
@kettlebell kettlebell force-pushed the dev-397-get-rid-of-panicking-code branch from 8c9ee32 to 4643ffb Compare November 25, 2022 03:24
@@ -117,7 +117,13 @@ impl ProtocolTag {

impl From<ProtocolTag> for ProtocolVer {
fn from(p: ProtocolTag) -> Self {
ProtocolVer::from(p.0[1])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed bug with index.

return Poll::Ready(Some(Err(
ProtocolHandlerError::Serialization(e),
)));
None
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do anything different if encoding fails here?

@kettlebell kettlebell requested a review from oskin1 November 25, 2022 07:32
@kettlebell kettlebell force-pushed the dev-397-get-rid-of-panicking-code branch from 6ad53a0 to 8f3e035 Compare April 9, 2023 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants