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 gas fees accounting in TransactionProcessor.Trace #5743

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using FluentAssertions;
using Nethermind.Core;
using Nethermind.Core.Test.Builders;
using Nethermind.Evm.Tracing.ParityStyle;
using Nethermind.Int256;
using Nethermind.Specs;
using NUnit.Framework;

namespace Nethermind.Evm.Test;

public class TransactionProcessorTraceTest : VirtualMachineTestsBase
{
protected override long BlockNumber => MainnetSpecProvider.GrayGlacierBlockNumber;
protected override ulong Timestamp => MainnetSpecProvider.ShanghaiBlockTimestamp;

[TestCase(21000)]
[TestCase(50000)]
public void Traces_gas_fess_properly(long gasLimit)
{
(Block block, Transaction transaction) = PrepareTx(BlockNumber, gasLimit);
ParityLikeTxTracer tracer = new(block, transaction, ParityTraceTypes.All);
_processor.Trace(transaction, block.Header, tracer);
var senderBalance = tracer.BuildResult().StateChanges[TestItem.AddressA].Balance;
(senderBalance.Before - senderBalance.After).Should().Be((UInt256)21000 + transaction.Value);
}
}
8 changes: 6 additions & 2 deletions src/Nethermind/Nethermind.Evm.Test/VirtualMachineTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected TestAllTracerWithOutput Execute(long blockNumber, long gasLimit, byte[
protected (Block block, Transaction transaction) PrepareTx(
long blockNumber,
long gasLimit,
byte[] code,
byte[] code = null,
Copy link
Member

Choose a reason for hiding this comment

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

mark it nullable

SenderRecipientAndMiner senderRecipientAndMiner = null,
int value = 1,
long blockGasLimit = DefaultBlockGasLimit,
Expand All @@ -140,7 +140,11 @@ protected TestAllTracerWithOutput Execute(long blockNumber, long gasLimit, byte[
TestState.CreateAccount(senderRecipientAndMiner.Recipient, 100.Ether());
else
TestState.AddToBalance(senderRecipientAndMiner.Recipient, 100.Ether(), SpecProvider.GenesisSpec);
TestState.InsertCode(senderRecipientAndMiner.Recipient, code, SpecProvider.GenesisSpec);

if (code is not null)
{
TestState.InsertCode(senderRecipientAndMiner.Recipient, code, SpecProvider.GenesisSpec);
}

GetLogManager().GetClassLogger().Debug("Committing initial state");
TestState.Commit(SpecProvider.GenesisSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private void Execute(Transaction transaction, BlockHeader block, ITxTracer txTra
}
}

UInt256 senderReservedGasPayment = noValidation ? UInt256.Zero : (ulong)gasLimit * effectiveGasPrice;
UInt256 senderReservedGasPayment = (ulong)gasLimit * effectiveGasPrice;
deffrian marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Can you write an explanation for TransactionProcessor changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How it works right now:

  1. If noValidation subtract zero(senderReservedGasPayment) as gas fee
  2. Refund gas. So if noValidation we refund gas that we didn't take.

After the fix:

  1. If noValidation do not subtract gas fee at all
  2. Refund only if noValidation is false. This fixes mentioned above issue

Another issue:
Right now Trace function uses noValidation, so we don't charge gas from account during trace. And as we discussed this behavior shouldn't be changed.
When we execute trace_replayBlock or trace_replayTransaction not charging fees causes inconsistency between chain state and stat that we get in trace. So I changed these functions to use Execute instead of Trace.


if (notSystemTransaction)
{
Expand Down