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

fix(anvil, evm): gas accounting in debug_traceTransaction #3230

Merged
merged 10 commits into from
Sep 18, 2022

Conversation

shekhirin
Copy link
Contributor

@shekhirin shekhirin commented Sep 15, 2022

Motivation

#3206

Solution

  1. GasInspector was introduced to revm, so we can use it to count gas easier and in a less error-prone way.
  2. Gas used by whole tx taken from receipt to be consistent with geth's output.

@shekhirin
Copy link
Contributor Author

shekhirin commented Sep 15, 2022

ok, only jump opcodes gas is due to be fixed
image

@shekhirin
Copy link
Contributor Author

shekhirin commented Sep 16, 2022

ok, gas seems to be fixed in bluealloy/revm#222 + I refactored the gas counting into inspector, so we (and other users) could re-use it in other places (e.g. https://github.com/foundry-rs/foundry/blob/master/evm/src/executor/inspector/debugger.rs) since gas blocks make it a bit complicated.

image

@shekhirin shekhirin changed the title fix(anvil, evm): gas accounting in debug_traceTransaction DO NOT MERGE: fix(anvil, evm): gas accounting in debug_traceTransaction Sep 16, 2022
@shekhirin
Copy link
Contributor Author

@mattsse this is ready for review but bluealloy/revm#222 isn't merged yet, so I marked this PR as DO NOT MERGE for now

@shekhirin shekhirin marked this pull request as ready for review September 16, 2022 16:55

/// The [`revm::Inspector`] used when transacting in the evm
#[derive(Debug, Clone, Default)]
pub struct Inspector {
pub gas: Option<Arc<RwLock<GasInspector>>>,
Copy link
Member

Choose a reason for hiding this comment

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

I see this was added in the revm PR

why do we need to guard this via RwLock here?

all Inspector functions are mut

ah is this because GasInspector is itself a Inspector, that is also used by the Tracer inspector?

since all of them are executed in sequence, there can be no race condition, so perhaps this could be a Rc<Refcell<>>?

or we move the GasInspector inside the Tracer, but then we don't have a standalone GasInspector

there's probably not much overhead in gas_inspector.write but could add up since this is called per step

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but then we don't have a standalone GasInspector

yep, I wanted to call it in a more explicitly way without behaviour hidden in Tracer + that way it can be shared between Inspector (I suspect we might need it in the Debugger later)

since all of them are executed in sequence, there can be no race condition, so perhaps this could be a Rc<Refcell<>>?

agree, fixed

Copy link
Member

@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.

lgtm, pending revm PR

@shekhirin shekhirin changed the title DO NOT MERGE: fix(anvil, evm): gas accounting in debug_traceTransaction fix(anvil, evm): gas accounting in debug_traceTransaction Sep 18, 2022
@mattsse mattsse merged commit 5372f59 into foundry-rs:master Sep 18, 2022
@gakonst
Copy link
Member

gakonst commented Sep 18, 2022

@shekhirin should we be using the new GasInspector in other areas of the foundry code?

@shekhirin
Copy link
Contributor Author

shekhirin commented Sep 18, 2022

@gakonst yeah, I think we can at least use it in the debugger like so: #3261. I still need to check that it behaves the same way though since I'm not very familiar with the debugger impl.

@gakonst
Copy link
Member

gakonst commented Sep 18, 2022

gotcha!

iFrostizz pushed a commit to iFrostizz/foundry that referenced this pull request Nov 9, 2022
…#3230)

* fix(anvil, evm): gas accounting in debug_traceTransaction

* checked sub for previous pc

* use revm gas inspector

* bump ethers

* bump revm

* Arc<RwLock<_>> -> Rc<RefCell<_>> for GasInspector

* bump revm
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.

None yet

3 participants