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

update trace_filter, trace block only to highest matching index #4974

Merged
merged 22 commits into from
Oct 12, 2023

Conversation

smatthewenglish
Copy link
Contributor

This PR aims to address the following TODO:

// TODO: this could be optimized to only trace the block until the highest matching index in
// that block

This is my first contribution to reth, I've read through the CONTRIBUTING.md doc and executed all the specified checks. If there's anything I've overlooked or missed please let me know and I'll be happy to address it.

@mattsse mattsse added C-perf A change motivated by improving speed, memory usage or disk footprint A-rpc Related to the RPC implementation labels Oct 11, 2023
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.

that's a great start.

I have some nits,
the biggest improvement would still be to only trace the block until the highest index, for that we'd need a new function that is similar to:
https://github.com/paradigmxyz/reth/blob/main/crates/rpc/rpc/src/trace.rs#L363-L369
pub only traces the block until a given transaction,
we can unify some code by moving this loop to a standalone function so it can be reused:

https://github.com/paradigmxyz/reth/blob/main/crates/rpc/rpc/src/trace.rs#L408-L408

Comment on lines 286 to 288
if idx > highest_matching_index {
highest_matching_index = idx;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this check is redundant because all tx_idx are only increasing

Comment on lines 304 to 306
if idx > highest_idx {
return Ok(None);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is a smol improvement, however this still traces the entire block,
https://github.com/paradigmxyz/reth/blob/main/crates/rpc/rpc/src/trace.rs#L353-L362

// trace all relevant blocks
let mut block_traces = Vec::with_capacity(target_blocks.len());
for (num, indices) in target_blocks {
for (num, indices, highest_idx) in target_blocks {
let traces = self.trace_block_with(
Copy link
Collaborator

Choose a reason for hiding this comment

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

there's something else we need to do here:
skipping blocks with empty indices entirely

@smatthewenglish
Copy link
Contributor Author

i wanted to localize this code in one place, instead of defining it twice, once for each function/

F: for<'a> Fn(
        TransactionInfo,
        TracingInspector,
        ExecutionResult,
        &'a revm_primitives::State,
        &'a CacheDB<StateProviderDatabase<StateProviderBox<'a>>>,
    ) -> EthResult<R>
    + Send
    + 'static,

so i defined it like this at the top of the file

trait TransactionCallback<R> 
where
    Self: for<'a> Fn(
        TransactionInfo,
        TracingInspector,
        ExecutionResult,
        &'a State,
        &'a CacheDB<StateProviderDatabase<StateProviderBox<'a>>>,
    ) -> EthResult<R>
    + Send
    + 'static,
{
}

impl<F, R> TransactionCallback<R> for F
where
    F: for<'a> Fn(
        TransactionInfo,
        TracingInspector,
        ExecutionResult,
        &'a State,
        &'a CacheDB<StateProviderDatabase<StateProviderBox<'a>>>,
    ) -> EthResult<R>
    + Send
    + 'static,
{
}

but i think that's still pretty redundant. I'm not that familiar with rust, it's why I wanna become a contributor to this project, to learn rust. is there a more idiomatic way to do that^?

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.

nice, almost there

Comment on lines 442 to 447
// Check if current index exceeds the highest_index and break if it does
if let Some(highest) = highest_index {
if idx as u64 > highest {
break;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to move this to transactions.into_iter().take() instead

@@ -310,6 +338,7 @@ where
.into_localized_transaction_traces(tx_info);
Ok(Some(traces))
},
Some(highest_idx),
Copy link
Collaborator

Choose a reason for hiding this comment

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

this argument should be moved and should be the second argument, so that the closure is always the last argument

@codecov
Copy link

codecov bot commented Oct 12, 2023

Codecov Report

Merging #4974 (d50470b) into main (67773ad) will increase coverage by 0.16%.
Report is 22 commits behind head on main.
The diff coverage is 67.64%.

Impacted file tree graph

Files Coverage Δ
crates/rpc/rpc/src/trace.rs 43.67% <67.64%> (+1.77%) ⬆️

... and 65 files with indirect coverage changes

Flag Coverage Δ
integration-tests 15.87% <67.64%> (+0.60%) ⬆️
unit-tests 62.43% <0.00%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
reth binary 30.61% <ø> (-0.15%) ⬇️
blockchain tree 80.81% <ø> (+0.18%) ⬆️
pipeline 88.37% <ø> (-0.09%) ⬇️
storage (db) 74.40% <ø> (-0.01%) ⬇️
trie 94.44% <ø> (-0.01%) ⬇️
txpool 48.66% <ø> (+0.13%) ⬆️
networking 76.44% <ø> (+0.35%) ⬆️
rpc 57.99% <67.64%> (-0.06%) ⬇️
consensus 63.01% <ø> (ø)
revm 27.89% <ø> (+0.10%) ⬆️
payload builder 7.96% <ø> (ø)
primitives 86.31% <ø> (+0.88%) ⬆️

@@ -436,15 +436,10 @@ where
let mut results = Vec::with_capacity(transactions.len());
let mut db = CacheDB::new(StateProviderDatabase::new(state));

let mut transactions = transactions.into_iter().enumerate().peekable();
let max_transactions = highest_index.map_or(transactions.len(), |highest| highest as usize + 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

this looks wrong, we should not need +1

Comment on lines 465 to 471
// need to apply the state changes of this transaction before executing the
// next transaction
if transactions.peek().is_some() {
// need to apply the state changes of this transaction before executing
// the next transaction
db.commit(state)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

we still need this

Comment on lines 42 to 67
trait TransactionCallback<R>
where
Self: for<'a> Fn(
TransactionInfo,
TracingInspector,
ExecutionResult,
&'a State,
&'a CacheDB<StateProviderDatabase<StateProviderBox<'a>>>,
) -> EthResult<R>
+ Send
+ 'static,
{
}

impl<F, R> TransactionCallback<R> for F where
F: for<'a> Fn(
TransactionInfo,
TracingInspector,
ExecutionResult,
&'a State,
&'a CacheDB<StateProviderDatabase<StateProviderBox<'a>>>,
) -> EthResult<R>
+ Send
+ 'static
{
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't really like this because this is just a hack to make signatures nicer and not really needed

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.

nice, lgtm

@mattsse mattsse added this pull request to the merge queue Oct 12, 2023
Merged via the queue into paradigmxyz:main with commit d3794f2 Oct 12, 2023
23 checks passed
mattsse added a commit that referenced this pull request Nov 8, 2023
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rpc Related to the RPC implementation C-perf A change motivated by improving speed, memory usage or disk footprint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants