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: add ovm BlockFileCodec #12247

Merged
merged 31 commits into from
Nov 12, 2024
Merged

Conversation

lean-apple
Copy link
Contributor

@lean-apple lean-apple commented Nov 1, 2024

Closes #12184.

@emhane
Copy link
Member

emhane commented Nov 4, 2024

First of all I moved the file into ret-optimism-cli, alongside the ovm receipt file codec.

I left you a hint about how to approach this. You can import the Header type from alloy, as is done for the original block. However, you must copy the BlockBody type, just as I have done with Block type. Also the TransactionSigned type must be copied, AND for the TransactionSigned type, we must also copy its impl body

impl Decodable for TransactionSigned {
/// This `Decodable` implementation only supports decoding rlp encoded transactions as it's used
/// by p2p.
///
/// The p2p encoding format always includes an RLP header, although the type RLP header depends
/// on whether or not the transaction is a legacy transaction.
///
/// If the transaction is a legacy transaction, it is just encoded as a RLP list:
/// `rlp(tx-data)`.
///
/// If the transaction is a typed transaction, it is encoded as a RLP string:
/// `rlp(tx-type || rlp(tx-data))`
///
/// This can be used for decoding all signed transactions in p2p `BlockBodies` responses.
///
/// This cannot be used for decoding EIP-4844 transactions in p2p `PooledTransactions`, since
/// the EIP-4844 variant of [`TransactionSigned`] does not include the blob sidecar.
///
/// For a method suitable for decoding pooled transactions, see [`PooledTransactionsElement`].
///
/// CAUTION: Due to a quirk in [`Header::decode`], this method will succeed even if a typed
/// transaction is encoded in this format, and does not start with a RLP header:
/// `tx-type || rlp(tx-data)`.
///
/// This is because [`Header::decode`] does not advance the buffer, and returns a length-1
/// string header if the first byte is less than `0xf7`.
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::network_decode(buf).map_err(Into::into)
}
}

and all other of its methods that are called via that.

Let me know if it doesn't make sense, then I can finish it off.

@lean-apple
Copy link
Contributor Author

First of all I moved the file into ret-optimism-cli, alongside the ovm receipt file codec.

I left you a hint about how to approach this. You can import the Header type from alloy, as is done for the original block. However, you must copy the BlockBody type, just as I have done with Block type. Also the TransactionSigned type must be copied, AND for the TransactionSigned type, we must also copy its impl body

impl Decodable for TransactionSigned {
/// This `Decodable` implementation only supports decoding rlp encoded transactions as it's used
/// by p2p.
///
/// The p2p encoding format always includes an RLP header, although the type RLP header depends
/// on whether or not the transaction is a legacy transaction.
///
/// If the transaction is a legacy transaction, it is just encoded as a RLP list:
/// `rlp(tx-data)`.
///
/// If the transaction is a typed transaction, it is encoded as a RLP string:
/// `rlp(tx-type || rlp(tx-data))`
///
/// This can be used for decoding all signed transactions in p2p `BlockBodies` responses.
///
/// This cannot be used for decoding EIP-4844 transactions in p2p `PooledTransactions`, since
/// the EIP-4844 variant of [`TransactionSigned`] does not include the blob sidecar.
///
/// For a method suitable for decoding pooled transactions, see [`PooledTransactionsElement`].
///
/// CAUTION: Due to a quirk in [`Header::decode`], this method will succeed even if a typed
/// transaction is encoded in this format, and does not start with a RLP header:
/// `tx-type || rlp(tx-data)`.
///
/// This is because [`Header::decode`] does not advance the buffer, and returns a length-1
/// string header if the first byte is less than `0xf7`.
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::network_decode(buf).map_err(Into::into)
}
}

and all other of its methods that are called via that.

Let me know if it doesn't make sense, then I can finish it off.

Thanks for the hints, it makes more sense, continuing on it now.

@emhane emhane added D-good-first-issue Nice and easy! A great choice to get started A-op-reth Related to Optimism and op-reth labels Nov 4, 2024
@joshieDo joshieDo self-requested a review November 5, 2024 04:32
@lean-apple
Copy link
Contributor Author

lean-apple commented Nov 5, 2024

First of all I moved the file into ret-optimism-cli, alongside the ovm receipt file codec.

I left you a hint about how to approach this. You can import the Header type from alloy, as is done for the original block. However, you must copy the BlockBody type, just as I have done with Block type. Also the TransactionSigned type must be copied, AND for the TransactionSigned type, we must also copy its impl body

impl Decodable for TransactionSigned {
/// This `Decodable` implementation only supports decoding rlp encoded transactions as it's used
/// by p2p.
///
/// The p2p encoding format always includes an RLP header, although the type RLP header depends
/// on whether or not the transaction is a legacy transaction.
///
/// If the transaction is a legacy transaction, it is just encoded as a RLP list:
/// `rlp(tx-data)`.
///
/// If the transaction is a typed transaction, it is encoded as a RLP string:
/// `rlp(tx-type || rlp(tx-data))`
///
/// This can be used for decoding all signed transactions in p2p `BlockBodies` responses.
///
/// This cannot be used for decoding EIP-4844 transactions in p2p `PooledTransactions`, since
/// the EIP-4844 variant of [`TransactionSigned`] does not include the blob sidecar.
///
/// For a method suitable for decoding pooled transactions, see [`PooledTransactionsElement`].
///
/// CAUTION: Due to a quirk in [`Header::decode`], this method will succeed even if a typed
/// transaction is encoded in this format, and does not start with a RLP header:
/// `tx-type || rlp(tx-data)`.
///
/// This is because [`Header::decode`] does not advance the buffer, and returns a length-1
/// string header if the first byte is less than `0xf7`.
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::network_decode(buf).map_err(Into::into)
}
}

and all other of its methods that are called via that.

Let me know if it doesn't make sense, then I can finish it off.

Thanks for letting me the time to go through this despite it's a blocker for other tasks @emhane @mattsse @JosepBove

I have still a couple of questions and observations :

  • if I let the ovm codec file into the optimism-cli and want to use then in the reth-downloaders, I got some cycle dependency so atm I let it in the reth-downloaders

  • I move the logic for the feature specification that we want to remove there directly into the TransactionSigned::decode_rlp_legacy_transaction_tuple, are there more changes expected to adapt this TransactionSigned to OVM ? Do we plan to have a special Signature type for optimism ?

  • Also I notice to make compatible with the optimism feature for the file client module, we have to use this kind of ugly imports for each case :

#[cfg(not(feature = "optimism"))]
use super::file_codec::BlockFileCodec;
#[cfg(not(feature = "optimism"))]
use reth_primitives::BlockBody;
#[cfg(feature = "optimism")]
use super::ovm_file_codec::OvmBlockFileCodec as BlockFileCodec;
#[cfg(feature = "optimism")]
use super::ovm_file_codec::BlockBody;

... and while fixing the test test_chunk_download_headers_from_file in the module file_client, I noticed we have many conditional imports for different BlockBody types in this whole crate. Instead of managing multiple BlockBody imports through feature flags, we could have a single BlockBody type that uses Vec<Box<dyn TransactionSignedTrait>>, where the trait handles the signature differences between standard and optimism transactions through separate implementations, what do you think @emhane ?

@emhane
Copy link
Member

emhane commented Nov 6, 2024

  • if I let the ovm codec file into the optimism-cli and want to use then in the reth-downloaders, I got some cycle dependency so atm I let it in the reth-downloaders

this issue is part of a bigger whole, eventually we will have a separate import block command for ovm, and make the block FileClient generic over codec. check out how it was done for receipts. however, this will be addressed in succeeding issues, this issue is simply for implementing the codec, which already is a pretty big scope by itself as you noticed.

  • I move the logic for the feature specification that we want to remove there directly into the TransactionSigned::decode_rlp_legacy_transaction_tuple, are there more changes expected to adapt this TransactionSigned to OVM ? Do we plan to have a special Signature type for optimism ?

this pr should add unit tests to prove the decoding works as expected. any bugs that may have come into existence from the refactor you describe, should be caught by these tests. it's ok for this specific issue to copy paste the needed code into reth-optimism-cli and here it's ok if it's a lot. as for signature - there will not be a separate op signature type. see review of #11567 for context.

@lean-apple
Copy link
Contributor Author

  • if I let the ovm codec file into the optimism-cli and want to use then in the reth-downloaders, I got some cycle dependency so atm I let it in the reth-downloaders

this issue is part of a bigger whole, eventually we will have a separate import block command for ovm, and make the block FileClient generic over codec. check out how it was done for receipts. however, this will be addressed in succeeding issues, this issue is simply for implementing the codec, which already is a pretty big scope by itself as you noticed.

  • I move the logic for the feature specification that we want to remove there directly into the TransactionSigned::decode_rlp_legacy_transaction_tuple, are there more changes expected to adapt this TransactionSigned to OVM ? Do we plan to have a special Signature type for optimism ?

this pr should add unit tests to prove the decoding works as expected. any bugs that may have come into existence from the refactor you describe, should be caught by these tests. it's ok for this specific issue to copy paste the needed code into reth-optimism-cli and here it's ok if it's a lot. as for signature - there will not be a separate op signature type. see review of #11567 for context.

Ok got it, I will remove the integration of the codec - for now, and add units tests for the decoding.

@lean-apple lean-apple marked this pull request as ready for review November 7, 2024 14:42
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.

on the right track

crates/optimism/cli/src/ovm_file_codec.rs Outdated Show resolved Hide resolved
Comment on lines +469 to +498
impl Decodable for TransactionSigned {
/// This `Decodable` implementation only supports decoding rlp encoded transactions as it's used
/// by p2p.
///
/// The p2p encoding format always includes an RLP header, although the type RLP header depends
/// on whether or not the transaction is a legacy transaction.
///
/// If the transaction is a legacy transaction, it is just encoded as a RLP list:
/// `rlp(tx-data)`.
///
/// If the transaction is a typed transaction, it is encoded as a RLP string:
/// `rlp(tx-type || rlp(tx-data))`
///
/// This can be used for decoding all signed transactions in p2p `BlockBodies` responses.
///
/// This cannot be used for decoding EIP-4844 transactions in p2p `PooledTransactions`, since
/// the EIP-4844 variant of [`TransactionSigned`] does not include the blob sidecar.
///
/// For a method suitable for decoding pooled transactions, see \[`PooledTransactionsElement`\].
///
/// CAUTION: Due to a quirk in [`Header::decode`], this method will succeed even if a typed
/// transaction is encoded in this format, and does not start with a RLP header:
/// `tx-type || rlp(tx-data)`.
///
/// This is because [`Header::decode`] does not advance the buffer, and returns a length-1
/// string header if the first byte is less than `0xf7`.
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::network_decode(buf).map_err(Into::into)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

only methods called indirectly by this impl, need to be included, i.e. the methods that are called in the function body of Self::network_decode, need to stay. the other methods can be removed.

Comment on lines 446 to 467
impl Encodable for TransactionSigned {
/// This encodes the transaction _with_ the signature, and an rlp header.
///
/// For legacy transactions, it encodes the transaction data:
/// `rlp(tx-data)`
///
/// For EIP-2718 typed transactions, it encodes the transaction type followed by the rlp of the
/// transaction:
/// `rlp(tx-type || rlp(tx-data))`
fn encode(&self, out: &mut dyn BufMut) {
self.network_encode(out);
}

fn length(&self) -> usize {
let mut payload_length = self.encode_2718_len();
if !self.is_legacy() {
payload_length += alloy_rlp::Header { list: false, payload_length }.length();
}

payload_length
}
}
Copy link
Member

Choose a reason for hiding this comment

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

also certainly not needed since the codec is for decoding blocks from a file of rlp encoded blocks, and will not be used for encoding (in foreseeable future)

@emhane
Copy link
Member

emhane commented Nov 12, 2024

need any help to wrap this up @lean-apple ?

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.

great job!

crates/optimism/cli/src/ovm_file_codec.rs Show resolved Hide resolved
@emhane emhane added this pull request to the merge queue Nov 12, 2024
@lean-apple
Copy link
Contributor Author

lean-apple commented Nov 12, 2024

great job!

Thanks, sorry for the late commits, I was a bit absorbed by the Devcon, I've added a last test for eip 1559 type.

Merged via the queue into paradigmxyz:main with commit 0cd34f9 Nov 12, 2024
38 checks passed
@lean-apple lean-apple deleted the ovm-block-file-codec branch November 14, 2024 07:18
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 D-good-first-issue Nice and easy! A great choice to get started
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OVM block file codec
2 participants