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

fix (reth-consensus): use derive_more::Error replace std::error::Error #10890

Closed
wants to merge 2 commits into from

Conversation

nkysg
Copy link
Contributor

@nkysg nkysg commented Sep 13, 2024

ref #10828, fix #10831.

after #10831, use cargo expand
the code is

#[automatically_derived]
impl derive_more::Error for ConsensusError {
    fn source(&self) -> Option<&(dyn derive_more::Error + 'static)> {
        use derive_more::__private::AsDynError;
        match self {
            ConsensusError::BodyOmmersHashDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::BodyStateRootDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::BodyTransactionRootDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::BodyReceiptRootDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::BodyBloomLogDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::BodyWithdrawalsRootDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::BodyRequestsRootDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::ParentHashMismatch(source) => Some(source.as_dyn_error()),
            ConsensusError::BlobGasUsedDiff(source) => Some(source.as_dyn_error()),
            ConsensusError::InvalidTransaction(source) => Some(source.as_dyn_error()),
            ConsensusError::BaseFeeDiff(source) => Some(source.as_dyn_error()),
            _ => None,
        }
    }
}

but the original code is

impl std::error::Error for ConsensusError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidTransaction(err) => std::error::Error::source(err),
_ => Option::None,
}
}
}

BodyStateRootDiff(GotExpectedBoxed<B256>),

/// Error when the transaction root in the block is different from the expected transaction
/// root.
#[display("mismatched block transaction root: {_0}")]
#[error(ignore)]
BodyTransactionRootDiff(GotExpectedBoxed<B256>),
Copy link
Contributor Author

@nkysg nkysg Sep 13, 2024

Choose a reason for hiding this comment

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

the rule is complexd, For me personally it feels harder to understand.

https://docs.rs/derive_more/latest/derive_more/derive.Error.html

When and how does it derive source()?
It’s a struct/variant with named fields and one is the fields is called source. Then it would return that field as the source.
It’s a tuple struct/variant and there’s exactly one field that is not used as the backtrace. So either a tuple struct with one field, or one with two where one is the backtrace. Then it returns this field as the source.
One of the fields is annotated with #[error(source)]. Then it would return that field as the source.

Ignoring fields for derives
It’s possible to ignore a field or a whole enum variant completely for this derive using the #[error(ignore)] attribute. This will ignore it both for detecting backtrace and source. It’s also possible to mark a field only ignored for one of these methods by using #[error(not(backtrace))] or #[error(not(source))].

I have tried it only support #[error(ignore)].

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

yeah, I think we can actually keep it how it is now but use core::error::Error

@mattsse
Copy link
Collaborator

mattsse commented Sep 13, 2024

closing and will also close the issue per comment above

@mattsse mattsse closed this Sep 13, 2024
@nkysg nkysg deleted the fix-reth-consensus-derive branch September 13, 2024 16:32
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