Skip to content

Commit

Permalink
7702 refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 committed Sep 3, 2024
1 parent 3dbb0ef commit 419d727
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -602,20 +603,33 @@ protected virtual long Refund(Transaction tx, BlockHeader header, IReleaseSpec s
in TransactionSubstate substate, in long unspentGas, in UInt256 gasPrice, int codeInsertRefunds)
{
long spentGas = tx.GasLimit;
var codeInsertRefund = (GasCostOf.NewAccount - GasCostOf.PerAuthBaseCost) * codeInsertRefunds;

if (!substate.IsError)
{
spentGas -= unspentGas;
long refund = substate.ShouldRevert
? 0
: RefundHelper.CalculateClaimableRefund(spentGas,
substate.Refund + substate.DestroyList.Count * RefundOf.Destroy(spec.IsEip3529Enabled)
+ (GasCostOf.NewAccount - GasCostOf.PerAuthBaseCost) * codeInsertRefunds, spec);

long totalToRefund = codeInsertRefund;
if (!substate.ShouldRevert)
totalToRefund += substate.Refund + substate.DestroyList.Count * RefundOf.Destroy(spec.IsEip3529Enabled);
long actualRefund = RefundHelper.CalculateClaimableRefund(spentGas, totalToRefund, spec);

if (Logger.IsTrace)
Logger.Trace("Refunding unused gas of " + unspentGas + " and refund of " + actualRefund);
// If noValidation we didn't charge for gas, so do not refund
if (!opts.HasFlag(ExecutionOptions.NoValidation))
WorldState.AddToBalance(tx.SenderAddress, (ulong)(unspentGas + actualRefund) * gasPrice, spec);
spentGas -= actualRefund;
}
else if(codeInsertRefund > 0)
{
long refund = RefundHelper.CalculateClaimableRefund(spentGas, codeInsertRefund, spec);

if (Logger.IsTrace)
Logger.Trace("Refunding unused gas of " + unspentGas + " and refund of " + refund);
Logger.Trace("Refunding delegations only: " + refund);
// If noValidation we didn't charge for gas, so do not refund
if (!opts.HasFlag(ExecutionOptions.NoValidation))
WorldState.AddToBalance(tx.SenderAddress, (ulong)(unspentGas + refund) * gasPrice, spec);
WorldState.AddToBalance(tx.SenderAddress, (ulong)refund * gasPrice, spec);
spentGas -= refund;
}

Expand Down

0 comments on commit 419d727

Please sign in to comment.