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

refactor: Adds type downcasting helpers for InvalidPoolTransactionError #14046

Merged

Conversation

PanGan21
Copy link
Contributor

@PanGan21 PanGan21 commented Jan 28, 2025

Closes: #14037

Adds type downcasting helpers for InvalidPoolTransactionError.

Copy link
Member

@emhane emhane left a comment

Choose a reason for hiding this comment

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

yes, we want to generalise the impl body too

impl<T: PoolTransaction, E> TransactionValidationOutcome<T, E> { .. }

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.

hmm,

this error already supports arbitrary errors via

/// Any other error that occurred while inserting/validating that is transaction specific
#[error(transparent)]
Other(Box<dyn PoolTransactionError>),

skeptical that it's worth integrating an error type

which I think would need to be added to

pub struct PoolInner<V, T, S>

@emhane
Copy link
Member

emhane commented Jan 30, 2025

rollups want to match on their error variants @mattsse . eventually all components that are customisable at sdk level should have an error associated type for good devx.

@mattsse
Copy link
Collaborator

mattsse commented Jan 30, 2025

rollups want to match on their error variants

what's a use case for this?

@emhane
Copy link
Member

emhane commented Jan 30, 2025

rollups want to match on their error variants

what's a use case for this?

do smthg based on the error variant

@mattsse
Copy link
Collaborator

mattsse commented Jan 30, 2025

can we add a helper that typechecking the dyn PollError instead, don't think a dedicated type for this is super useful

@emhane
Copy link
Member

emhane commented Jan 30, 2025

can we add a helper that typechecking the dyn PollError instead, don't think a dedicated type for this is super useful

sure rollups can work with down casting. though semantically it's nicer for the rollups to wrap l1 errors in a variant, so they can extend it. another place I'm having the same problem rn is PayloadError from alloy. in upcoming op fork there is a need for adding other error variants for failing payload validation. instead of moving this problem to alloy to add an Other variant, it would be nicer if the trait PayloadValidator had an associated type for error.

@PanGan21
Copy link
Contributor Author

yes, we want to generalise the impl body too

impl<T: PoolTransaction, E> TransactionValidationOutcome<T, E> { .. }

I added this for now.
In case the approach needs to change and add the type checking helper function please let me know :)

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.

@PanGan21

can we instead make this

pub trait PoolTransactionError: core::error::Error + Send + Sync {

`: Any``

and add

    #[inline]
    fn as_any(&self) -> &dyn std::any::Any;

to the trait?

Copy link
Member

@emhane emhane left a comment

Choose a reason for hiding this comment

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

lgtm

@mattsse
Copy link
Collaborator

mattsse commented Feb 3, 2025

actually @PanGan21 we probably don't need the additional Any fn if we require that this trait is 'static

and add these helper functions

#14180

to

pub enum InvalidPoolTransactionError {

@PanGan21
Copy link
Contributor Author

PanGan21 commented Feb 4, 2025

actually @PanGan21 we probably don't need the additional Any fn if we require that this trait is 'static

and add these helper functions

#14180

to

pub enum InvalidPoolTransactionError {

It seems that adding this:

/// Attempts to downcast the [`InvalidPoolTransactionError::Other`] variant to a concrete type
    pub fn downcast<T: core::error::Error + 'static>(self) -> Result<Box<T>, Self> {
        match self {
            Self::Other(err) => err.downcast().map_err(Self::Other),
            err => Err(err),
        }
    }

would give and error about downcast method not found in Box<dyn PoolTransactionError>. Any idea what is the trick to fix it @mattsse ?

@mattsse
Copy link
Collaborator

mattsse commented Feb 4, 2025

ah yeah this is an issue with dyn trait and casting because the trait is actually PoolTxError not dyn Error.

there's as_dyn_error, but I think the : Any approach I mentioned above would be better here, because in this case the trait must provide the cast to &Any,

let's start by adding fn is and downcast_ref, see #14180
the actual downcast we can solve later

Copy link

codspeed-hq bot commented Feb 4, 2025

CodSpeed Performance Report

Merging #14046 will not alter performance

Comparing PanGan21:14037-generic-transaction-validation-outcome (95bb0bf) with main (29e6e5c)

Summary

✅ 77 untouched benchmarks

@emhane emhane added A-op-reth Related to Optimism and op-reth A-sdk Related to reth's use as a library labels Feb 4, 2025
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.

smol naming nit, otherwise lgtm


/// Returns a reference to the [`InvalidPoolTransactionError::Other`] value if this type is a
/// [`InvalidPoolTransactionError::Other`] of that type. Returns None otherwise.
pub fn downcast_other<T: core::error::Error + 'static>(&self) -> Option<&T> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
pub fn downcast_other<T: core::error::Error + 'static>(&self) -> Option<&T> {
pub fn downcast_other_ref<T: core::error::Error + 'static>(&self) -> Option<&T> {

Copy link
Collaborator

Choose a reason for hiding this comment

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

messed up naming on the other pr

Copy link
Contributor Author

@PanGan21 PanGan21 Feb 4, 2025

Choose a reason for hiding this comment

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

Done, trying to fix one last issue in is_other, probably related to casting which i discovered from the unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @mattsse . It seems that downcasting doesn't work as expected due to rust casting rules. Essentially as you said dyn PoolTransactionError is recognized as core::error::Error + Send + Sync + 'static from err.is::<T>.
Any suggestion that this can be improved?

Copy link
Collaborator

Choose a reason for hiding this comment

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

what we can do is add an fn as_err(&self) &dyn Error to the trait function itself and impl is just self, this way the type is responsible for the cast
can also be Any instead of Error

like

fn as_err(&self) -> &dyn Error {
        self
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems Any did the trick, thanks!

@PanGan21 PanGan21 changed the title refactor: Make TransactionValidationOutcome generic over error type refactor: Adds type downcasting helpers for InvalidPoolTransactionError Feb 5, 2025
crates/transaction-pool/src/error.rs Outdated Show resolved Hide resolved
Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
@mattsse mattsse added this pull request to the merge queue Feb 5, 2025
Merged via the queue into paradigmxyz:main with commit 7789d93 Feb 5, 2025
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-op-reth Related to Optimism and op-reth A-sdk Related to reth's use as a library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make TransactionValidationOutcome generic over error type
4 participants