Skip to content

Commit

Permalink
revert blobGasCalculator changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke26 committed Dec 9, 2024
1 parent bb63085 commit 4a83375
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/Nethermind/Ethereum.Test.Base/GeneralTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected EthereumTestResult RunTest(GeneralStateTest test, ITxTracer txTracer)
BlobGasUsed = (ulong)test.ParentBlobGasUsed,
ExcessBlobGas = (ulong)test.ParentExcessBlobGas,
};
header.ExcessBlobGas = BlobGasCalculator.CalculateExcessBlobGas(parent, spec, header);
header.ExcessBlobGas = BlobGasCalculator.CalculateExcessBlobGas(parent, spec);
}

ValidationResult txIsValid = _txValidator.IsWellFormed(test.Transaction, spec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ private void OnHeadChanged(object? sender, BlockReplacementEventArgs e)
BlockGasLimit = e.Block!.GasLimit;
CurrentBaseFee = e.Block.Header.BaseFeePerGas;
CurrentFeePerBlobGas =
BlobGasCalculator.TryCalculateFeePerBlobGas(e.Block.Header, out UInt256 currentFeePerBlobGas,
SpecProvider.GetSpec(e.Block.Header))
BlobGasCalculator.TryCalculateFeePerBlobGas(e.Block.Header, out UInt256 currentFeePerBlobGas)
? currentFeePerBlobGas
: UInt256.Zero;
HeadChanged?.Invoke(sender, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static bool HasEnoughFounds(Transaction transaction, in UInt256 senderBa
}

if (transaction.SupportsBlobs && (
!BlobGasCalculator.TryCalculateBlobBaseFee(block.Header, transaction, out UInt256 blobBaseFee, releaseSpec) ||
!BlobGasCalculator.TryCalculateBlobBaseFee(block.Header, transaction, out UInt256 blobBaseFee) ||
senderBalance < (maxFee += blobBaseFee)))
{
e.Set(TxAction.Skip, $"{maxFee} is higher than sender balance ({senderBalance}), MaxFeePerGas: ({transaction.MaxFeePerGas}), GasLimit {transaction.GasLimit}, BlobBaseFee: {blobBaseFee}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ private bool TryGetFullBlobTx(Transaction blobTx, [NotNullWhen(true)] out Transa

private bool TryUpdateFeePerBlobGas(Transaction lightBlobTx, BlockHeader parent, IReleaseSpec spec, out UInt256 feePerBlobGas)
{
ulong? excessDataGas = BlobGasCalculator.CalculateExcessBlobGas(parent, spec, parent);
ulong? excessDataGas = BlobGasCalculator.CalculateExcessBlobGas(parent, spec);
if (excessDataGas is null)
{
if (_logger.IsTrace) _logger.Trace($"Declining {lightBlobTx.ToShortString()}, the specification is not configured to handle shard blob transactions.");
feePerBlobGas = UInt256.Zero;
return false;
}

if (!BlobGasCalculator.TryCalculateFeePerBlobGas(excessDataGas.Value, out feePerBlobGas, parent.TargetBlobCount, spec))
if (!BlobGasCalculator.TryCalculateFeePerBlobGas(excessDataGas.Value, out feePerBlobGas))
{
if (_logger.IsTrace) _logger.Trace($"Declining {lightBlobTx.ToShortString()}, failed to calculate data gas price.");
feePerBlobGas = UInt256.Zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected virtual bool ValidateEip4844Fields(Block block, IReleaseSpec spec, out

if (feePerBlobGas.IsZero)
{
if (!BlobGasCalculator.TryCalculateFeePerBlobGas(block.Header, out feePerBlobGas, spec))
if (!BlobGasCalculator.TryCalculateFeePerBlobGas(block.Header, out feePerBlobGas))
{
error = BlockErrorMessages.BlobGasPriceOverflow;
if (_logger.IsDebug) _logger.Debug($"{Invalid(block)} {error}.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ protected virtual bool ValidateBlobGasFields(BlockHeader header, BlockHeader par
return false;
}

ulong? expectedExcessBlobGas = BlobGasCalculator.CalculateExcessBlobGas(parentHeader, spec, header);
ulong? expectedExcessBlobGas = BlobGasCalculator.CalculateExcessBlobGas(parentHeader, spec);
if (header.ExcessBlobGas != expectedExcessBlobGas)
{
if (_logger.IsWarn) _logger.Warn($"ExcessBlobGas field is incorrect: {header.ExcessBlobGas}, should be {expectedExcessBlobGas}.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private void TestExcessBlobGas(IReleaseSpec spec, bool areBlobsEnabled, (ulong p
.WithBlobGasUsed(BlobGasCalculator.CalculateBlobGas(testCase.parentBlobsCount))
.WithExcessBlobGas(testCase.parentExcessBlobGas).TestObject;

Assert.That(BlobGasCalculator.CalculateExcessBlobGas(parentHeader, spec, header), Is.EqualTo(areBlobsEnabled ? testCase.expectedExcessBlobGas : null));
Assert.That(BlobGasCalculator.CalculateExcessBlobGas(parentHeader, spec), Is.EqualTo(areBlobsEnabled ? testCase.expectedExcessBlobGas : null));
}

[TestCaseSource(nameof(ExcessBlobGasTestCaseSource))]
Expand Down
32 changes: 11 additions & 21 deletions src/Nethermind/Nethermind.Evm/BlobGasCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.IO;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Int256;
Expand Down Expand Up @@ -30,22 +29,24 @@ public static ulong CalculateBlobGas(Transaction[] transactions)
return CalculateBlobGas(blobCount);
}

public static bool TryCalculateBlobBaseFee(BlockHeader header, Transaction transaction, out UInt256 blobBaseFee, IReleaseSpec? spec = null)
public static bool TryCalculateBlobBaseFee(BlockHeader header, Transaction transaction, out UInt256 blobBaseFee)
{
if (!TryCalculateFeePerBlobGas(header.ExcessBlobGas, out UInt256 feePerBlobGas, header.TargetBlobCount, spec))
if (!TryCalculateFeePerBlobGas(header.ExcessBlobGas.Value, out UInt256 feePerBlobGas))
{
blobBaseFee = UInt256.MaxValue;
return false;
}
return !UInt256.MultiplyOverflow(CalculateBlobGas(transaction), feePerBlobGas, out blobBaseFee);
}

public static bool TryCalculateFeePerBlobGas(BlockHeader header, out UInt256 feePerBlobGas, IReleaseSpec? spec = null)
public static bool TryCalculateFeePerBlobGas(BlockHeader header, out UInt256 feePerBlobGas)
{
return TryCalculateFeePerBlobGas(header.ExcessBlobGas, out feePerBlobGas, header.TargetBlobCount, spec);
feePerBlobGas = UInt256.MaxValue;
return header.ExcessBlobGas is not null
&& TryCalculateFeePerBlobGas(header.ExcessBlobGas.Value, out feePerBlobGas);
}

public static bool TryCalculateFeePerBlobGas(ulong? excessBlobGas, out UInt256 feePerBlobGas, UInt256? targetBlobCount = null, IReleaseSpec? spec = null)
public static bool TryCalculateFeePerBlobGas(ulong excessBlobGas, out UInt256 feePerBlobGas)
{
static bool FakeExponentialOverflow(UInt256 factor, UInt256 num, UInt256 denominator, out UInt256 feePerBlobGas)
{
Expand Down Expand Up @@ -84,16 +85,10 @@ static bool FakeExponentialOverflow(UInt256 factor, UInt256 num, UInt256 denomin
return false;
}

var denominator = spec?.IsEip7742Enabled ?? false
? Eip7742Constants.BlobGasPriceUpdateFraction * targetBlobCount
?? throw new InvalidDataException("header is missing target blob count")
: Eip4844Constants.BlobGasPriceUpdateFraction;

feePerBlobGas = UInt256.MaxValue;
return excessBlobGas is not null && !FakeExponentialOverflow(Eip4844Constants.MinBlobGasPrice, excessBlobGas.Value, denominator, out feePerBlobGas);
return !FakeExponentialOverflow(Eip4844Constants.MinBlobGasPrice, excessBlobGas, Eip4844Constants.BlobGasPriceUpdateFraction, out feePerBlobGas);
}

public static ulong? CalculateExcessBlobGas(BlockHeader? parentBlockHeader, IReleaseSpec releaseSpec, BlockHeader header)
public static ulong? CalculateExcessBlobGas(BlockHeader? parentBlockHeader, IReleaseSpec releaseSpec)
{
if (!releaseSpec.IsEip4844Enabled)
{
Expand All @@ -107,13 +102,8 @@ static bool FakeExponentialOverflow(UInt256 factor, UInt256 num, UInt256 denomin

ulong excessBlobGas = parentBlockHeader.ExcessBlobGas ?? 0;
excessBlobGas += parentBlockHeader.BlobGasUsed ?? 0;
var targetBlobCount = releaseSpec.IsEip7742Enabled
? header.TargetBlobCount * Eip4844Constants.GasPerBlob
?? throw new InvalidDataException("header is missing target blob count")
: Eip4844Constants.TargetBlobGasPerBlock;

return excessBlobGas < targetBlobCount
return excessBlobGas < Eip4844Constants.TargetBlobGasPerBlock
? 0
: excessBlobGas - targetBlobCount;
: (excessBlobGas - Eip4844Constants.TargetBlobGasPerBlock);
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/TransactionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static TxGasInfo GetGasInfo(this Transaction tx, bool is1559Enabled, Bloc
throw new ArgumentException($"Block that contains Shard Blob Transactions should have {nameof(header.ExcessBlobGas)} set.", nameof(header.ExcessBlobGas));
}

if (!BlobGasCalculator.TryCalculateFeePerBlobGas(header, out UInt256 feePerBlobGas, spec))
if (!BlobGasCalculator.TryCalculateFeePerBlobGas(header, out UInt256 feePerBlobGas))
{
throw new OverflowException("Blob gas price calculation led to overflow.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ protected virtual TransactionResult BuyGas(Transaction tx, BlockHeader header, I
overflows = UInt256.MultiplyOverflow((UInt256)tx.GasLimit, effectiveGasPrice, out senderReservedGasPayment);
if (!overflows && tx.SupportsBlobs)
{
overflows = !BlobGasCalculator.TryCalculateBlobBaseFee(header, tx, out blobBaseFee, spec);
overflows = !BlobGasCalculator.TryCalculateBlobBaseFee(header, tx, out blobBaseFee);
if (!overflows)
{
overflows = UInt256.AddOverflow(senderReservedGasPayment, blobBaseFee, out senderReservedGasPayment);
Expand Down
16 changes: 2 additions & 14 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,23 +1515,11 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
}
case Instruction.BLOBBASEFEE:
{
if (!spec.BlobBaseFeeEnabled) goto InvalidInstruction;

if (blkCtx.BlobBaseFee.HasValue)
{
result = blkCtx.BlobBaseFee.Value;
}
else if (BlobGasCalculator.TryCalculateFeePerBlobGas(blkCtx.Header, out UInt256 baseFee))
{
result = baseFee;
}
else
{
goto InvalidInstruction;
}
if (!spec.BlobBaseFeeEnabled || !blkCtx.BlobBaseFee.HasValue) goto InvalidInstruction;

gasAvailable -= GasCostOf.Base;

result = blkCtx.BlobBaseFee.Value;
stack.PushUInt256(in result);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Facade/BlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private TransactionResult CallAndRestore(
{
callHeader.BlobGasUsed = BlobGasCalculator.CalculateBlobGas(transaction);
callHeader.ExcessBlobGas = treatBlockHeaderAsParentBlock
? BlobGasCalculator.CalculateExcessBlobGas(blockHeader, releaseSpec, blockHeader)
? BlobGasCalculator.CalculateExcessBlobGas(blockHeader, releaseSpec)
: blockHeader.ExcessBlobGas;
}
callHeader.MixHash = blockHeader.MixHash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private BlockHeader GetCallHeader(BlockStateCall<TransactionWithSourceDetails> b
? 0
: BaseFeeCalculator.Calculate(parent, spec);

result.ExcessBlobGas = spec.IsEip4844Enabled ? BlobGasCalculator.CalculateExcessBlobGas(parent, spec, result) : (ulong?)0;
result.ExcessBlobGas = spec.IsEip4844Enabled ? BlobGasCalculator.CalculateExcessBlobGas(parent, spec) : (ulong?)0;

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ public ResultWrapper<Address> eth_coinbase()
{
return ResultWrapper<UInt256?>.Success(UInt256.Zero);
}
IReleaseSpec? spec = _blockFinder.Head?.Header is null ? null : specProvider.GetSpec(_blockFinder.Head.Header);
if (!BlobGasCalculator.TryCalculateFeePerBlobGas(_blockFinder.Head?.Header?.ExcessBlobGas ?? 0,
out UInt256 feePerBlobGas, _blockFinder.Head?.Header.TargetBlobCount, spec))
out UInt256 feePerBlobGas))
{
return ResultWrapper<UInt256?>.Fail("Unable to calculate the current blob base fee");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private readonly record struct BlockFeeHistorySearchInfo(
{
BlockFeeHistorySearchInfo BlockFeeHistorySearchInfoFromBlock(Block b)
{
BlobGasCalculator.TryCalculateFeePerBlobGas(b.Header, out UInt256 feePerBlobGas, _specProvider.GetSpec(block.Header));
BlobGasCalculator.TryCalculateFeePerBlobGas(b.Header, out UInt256 feePerBlobGas);

var maxBlobGasPerBlock = (double)(block.TargetBlobCount * 2 ?? Eip4844Constants.GetMaxBlobsPerBlock()) * Eip4844Constants.GasPerBlob;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected virtual void AmendHeader(BlockHeader blockHeader, BlockHeader parent,
if (spec.IsEip4844Enabled)
{
blockHeader.BlobGasUsed = 0;
blockHeader.ExcessBlobGas = BlobGasCalculator.CalculateExcessBlobGas(parent, spec, blockHeader);
blockHeader.ExcessBlobGas = BlobGasCalculator.CalculateExcessBlobGas(parent, spec);
}
}
}
Expand Down

0 comments on commit 4a83375

Please sign in to comment.