-
Notifications
You must be signed in to change notification settings - Fork 8
perf(payload): Avoid redundant encoding of bundle transactions #41
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use { | |
network::eip2718::Eip2718Error, | ||
primitives::{B256, Keccak256, TxHash}, | ||
}, | ||
alloy_origin::primitives::Bytes, | ||
core::{ | ||
convert::Infallible, | ||
fmt::Debug, | ||
|
@@ -21,6 +22,7 @@ use { | |
revm::db::BundleState, | ||
rpc::types::mev::EthSendBundle, | ||
}, | ||
reth_ethereum::primitives::WithEncoded, | ||
serde::{ | ||
Deserialize, | ||
Deserializer, | ||
|
@@ -42,6 +44,9 @@ pub trait Bundle<P: Platform>: | |
{ | ||
type PostExecutionError: core::error::Error + Send + Sync + 'static; | ||
|
||
type TransactionsEncoded<'a>: Iterator<Item = WithEncoded<&'a Recovered<types::Transaction<P>>>> | ||
+ 'a; | ||
|
||
/// Access to the transactions that are part of this bundle. | ||
fn transactions(&self) -> &[Recovered<types::Transaction<P>>]; | ||
|
||
|
@@ -104,6 +109,29 @@ pub trait Bundle<P: Platform>: | |
/// metadata attached to it, so two bundles with the same transactions but | ||
/// different metadata should have different hashes. | ||
fn hash(&self) -> B256; | ||
|
||
/// Returns an iterator that yields the bundle's transactions in a format | ||
/// ready for execution. | ||
/// | ||
/// By default, this wraps the plain `Recovered` transactions. Implementors | ||
/// that store pre-encoded bytes can override this to provide the more | ||
/// efficient `WithEncoded` wrapper. | ||
fn executables<'a>( | ||
&'a self, | ||
) -> Box< | ||
dyn Iterator<Item = WithEncoded<&'a Recovered<types::Transaction<P>>>> + 'a, | ||
> { | ||
Comment on lines
+119
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you can even use impl Iterator here but unsure if this trait should be object safe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think impl Iterator is good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I tried the impl Iterator and BundleExt suggestions, but hit a compiler error due to different iterator types between the optimised and fallback paths. I switched to using a GAT on the Bundle trait instead. Let me know what you think of this approach. |
||
Box::new( | ||
self | ||
.transactions() | ||
.iter() | ||
.map(|tx| WithEncoded::new(tx.encoded_2718().into(), tx)), | ||
) | ||
} | ||
|
||
/// Returns an iterator over the bundle's transactions in a format ready for | ||
/// execution. | ||
fn transactions_encoded(&self) -> Self::TransactionsEncoded<'_>; | ||
} | ||
|
||
/// The eligibility of a bundle for inclusion in a block. | ||
|
@@ -261,6 +289,15 @@ impl<P: Platform> FlashbotsBundle<P> { | |
|
||
impl<P: Platform> Bundle<P> for FlashbotsBundle<P> { | ||
type PostExecutionError = Infallible; | ||
type TransactionsEncoded<'a> = std::iter::Map< | ||
std::iter::Zip< | ||
std::slice::Iter<'a, Recovered<types::Transaction<P>>>, | ||
std::slice::Iter<'a, Bytes>, | ||
>, | ||
fn( | ||
(&'a Recovered<types::Transaction<P>>, &'a Bytes), | ||
) -> WithEncoded<&'a Recovered<types::Transaction<P>>>, | ||
>; | ||
|
||
fn transactions(&self) -> &[Recovered<types::Transaction<P>>] { | ||
&self.recovered | ||
|
@@ -341,6 +378,28 @@ impl<P: Platform> Bundle<P> for FlashbotsBundle<P> { | |
// extra fields not taken into account in the hash | ||
hasher.finalize() | ||
} | ||
|
||
fn executables<'a>( | ||
&'a self, | ||
) -> Box< | ||
dyn Iterator<Item = WithEncoded<&'a Recovered<types::Transaction<P>>>> + 'a, | ||
> { | ||
Box::new( | ||
self | ||
.recovered | ||
.iter() | ||
.zip(self.inner.txs.iter()) | ||
.map(|(tx, bytes)| WithEncoded::new(bytes.clone(), tx)), | ||
) | ||
} | ||
|
||
fn transactions_encoded(&self) -> Self::TransactionsEncoded<'_> { | ||
self | ||
.recovered | ||
.iter() | ||
.zip(self.inner.txs.iter()) | ||
.map(|(tx, bytes)| WithEncoded::new(bytes.clone(), tx)) | ||
} | ||
} | ||
|
||
impl<P: Platform> FlashbotsBundle<P> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this could be slightly confusing with the concept of
Executable
in the payload API, we could rename this function to something likefn transactions_encoded
instead