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

feat: impl TryFrom<Transaction> for TxEnvelope #343

Merged
merged 9 commits into from
Mar 22, 2024

Conversation

klkvr
Copy link
Member

@klkvr klkvr commented Mar 19, 2024

Motivation

  1. Adds conversion from alloy_rpc_types::Signature to alloy_primitives::Signature
  2. Adds conversion from alloy_rpc_types::AccessList to alloy_eips::AccessList
  3. Adds conversion from alloy_rpc_types::Transaction to TxEnvelope

Blocked by #338

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

/// Missing `chainId` field for EIP-155 transaction.
MissingChainId,
/// Signature error from [`alloy_primitives`].
SignatureError(SignatureError),
Copy link
Member

Choose a reason for hiding this comment

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

none of these are 2718 errors. they should not be in this enum

Copy link
Member Author

Choose a reason for hiding this comment

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

What'd be the best place for them?

Copy link
Member Author

Choose a reason for hiding this comment

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

created separate ConversionError for now

@klkvr klkvr requested a review from prestwich March 20, 2024 16:55
@prestwich
Copy link
Member

@mattsse the other way to approach this would be to enforce completeness requirements during serde deser. i.e. make it impossible to construct a Transaction that cannot be converted to a TxEnvelope, and then we could implement From/Into instead of TryFrom/TryInto

@prestwich
Copy link
Member

this could be done by embedding a TxEnvelope in the Transaction

@klkvr
Copy link
Member Author

klkvr commented Mar 20, 2024

wouldn't such approach cause issues with deposit transaction? we have optional signature for this case I believe

@prestwich
Copy link
Member

prestwich commented Mar 20, 2024

Optimism is on the hook for making their own rpc Transaction struct and consensus TxEnvelope struct, which could have different semantics

@mattsse
Copy link
Member

mattsse commented Mar 20, 2024

the other way to approach this would be to enforce completeness requirements during serde deser.

since all fields are public, I don't think we can guarantee this easily,
I think we want the validation checks when we convert from rpc -> consensus

it's also possible that impls are faulty/incomplete for this endpoint, like missing field, I've seen this on a few newer networks, but this should not affect the rpc response imo and only be a problem when converting to consensus tx

@prestwich
Copy link
Member

prestwich commented Mar 20, 2024

it's also possible that impls are faulty/incomplete for this endpoint, like missing field, I've seen this on a few newer networks, but this should not affect the rpc response imo and only be a problem when converting to consensus tx

we already have validity and correctness checks for the RPC response member data types, e.g. we reject an object in the gas_limit field. So why not check for type-level correctness? Is it just because we want to support faulty and/or off-spec nodes? That would imply we should significantly relax RPC deserialization overall, and seems like a non-goal anyway (or at least a goal for AnyNetwork instead of Ethereum)

@mattsse
Copy link
Member

mattsse commented Mar 20, 2024

Is it just because we want to support faulty and/or off-spec nodes

this and I don't think we should do any additional checks on deserialization

Also, in order to actually enforce we'd need to make the rpc response type's field private, which I think would be inconvenient

@prestwich
Copy link
Member

well wed just need a _:() to prevent incorrect instantiation?

@mattsse
Copy link
Member

mattsse commented Mar 20, 2024

this only solves instantiation, but not mut access, for example:

let mut tx = p.tx_byhash(<>);
tx.max_fee_per_gas.take();

we could solve this by changing the rpc::Tx to an enum instead, but I'd really prefer a simple object here and keep conversion fallible

@prestwich
Copy link
Member

ya, id like to revisit this in the future tho

@@ -115,6 +120,123 @@ impl Transaction {
}
}

impl TryFrom<Transaction> for Signed<TxLegacy> {
Copy link
Member

Choose a reason for hiding this comment

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

much improved 👌

@prestwich prestwich merged commit 5515915 into alloy-rs:main Mar 22, 2024
16 checks passed
prestwich added a commit that referenced this pull request Mar 22, 2024
@prestwich prestwich mentioned this pull request Mar 22, 2024
3 tasks
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.

4 participants