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

[DRAFT] Fix refund calculation #268

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

paronikyanarmen
Copy link

Fix refund calculation

Fixes

Fixes issue: #267

Description

Should allow refund value in a call to be a negative value, since EVM supports negative values

@paronikyanarmen paronikyanarmen changed the title Fix refund calculation [DRAFT] Fix refund calculation Mar 13, 2025
Copy link
Contributor

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

great, all of these look good so far, the more challenging part is tracking the total gas refunds per call which I think we need to do when we create a new call object

but maybe there are some things we also need to consider on call_end @rakita ?

gas_used,
decoded: None,
immediate_bytes,

// fields will be populated end of call
gas_cost: 0,
gas_refund_counter: 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

this makes sense because we always need to init this with 0

Comment on lines +100 to +101
/// The total gas refunded in the call.
pub gas_refunded: u64
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't necessarily need this here because we could derive this from the sum of all refunds in the steps?

@@ -518,6 +518,8 @@ impl TracingInspector {
// TODO: Figure out why this can overflow. https://github.com/paradigmxyz/revm-inspectors/pull/38
step.gas_cost = step.gas_remaining.saturating_sub(interp.control.gas().remaining());

step.gas_refund_counter = interp.control.gas().refunded();
Copy link
Contributor

Choose a reason for hiding this comment

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

makes sense

@@ -707,7 +709,7 @@ impl CallTraceStep {
gas_cost: self.gas_cost,
op: self.op.to_string(),
pc: self.pc as u64,
refund_counter: (self.gas_refund_counter > 0).then_some(self.gas_refund_counter),
refund_counter: (self.gas_refund_counter > 0).then_some(self.gas_refund_counter as u64),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can do it like this but only if we update the refund counters accordingly, because we're now only tracking the refund on a per call level, so what we need to di is one post processing step that updates the counter when a call ended.

I believe we could do this when we initialize the first calltrace using the sum of all gas refunds up until this point.

@rakita
Copy link
Contributor

rakita commented Mar 18, 2025

Yeah, there should be a global_refund_counter that gets updated added the refund of subcall in call_end, create_end, eofcreate_end.

And traced refund should be a self.global_refund_counter+interp.control.gas().refunded() it makes sense for this to be calculated on step_end

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.

3 participants