From 90f96c870ca48be78c3c802608ccd6c27454dcc0 Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Sun, 4 Jun 2023 23:43:35 +0900 Subject: [PATCH 1/8] Blob gas info in receipts --- .../Nethermind.Evm/IntrinsicGasCalculator.cs | 11 +++++---- .../BlockchainBridgeTests.cs | 8 ++++--- .../Nethermind.Facade/BlockchainBridge.cs | 16 +++++++++---- .../Nethermind.Facade/IBlockchainBridge.cs | 23 +++++++++++++++++-- .../Data/ReceiptsForRpcTests.cs | 2 +- .../Modules/Eth/EthRpcModuleTests.cs | 6 ++--- .../Converters/TxReceiptConverter.cs | 2 +- .../Nethermind.JsonRpc/Data/ReceiptForRpc.cs | 10 +++++--- .../Modules/Eth/EthRpcModule.cs | 4 ++-- .../Modules/Parity/ParityRpcModule.cs | 19 +++++++++++++-- .../Modules/Proof/ProofRpcModule.cs | 21 +++++++++++++---- 11 files changed, 92 insertions(+), 30 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs b/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs index 10f71605ec2..767a2128335 100644 --- a/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs +++ b/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs @@ -96,21 +96,22 @@ private static long AccessListCost(Transaction transaction, IReleaseSpec release public static ulong CalculateDataGas(int blobCount) => (ulong)blobCount * Eip4844Constants.DataGasPerBlob; + public static ulong CalculateDataGas(Transaction transaction) => + CalculateDataGas(transaction.BlobVersionedHashes?.Length ?? 0); + public static UInt256 CalculateDataGasPrice(BlockHeader header, Transaction transaction) => - CalculateDataGas(transaction.BlobVersionedHashes?.Length ?? 0) * CalculateDataGasPrice(header); + CalculateDataGas(transaction) * CalculateDataGasPricePerUnit(header); public static UInt256 CalculateDataGasPrice(BlockHeader header) => - header.DataGasUsed.Value * CalculateDataGasPrice(header); + CalculateDataGasPricePerUnit(header); - public static UInt256 CalculateDataGasPricePerUnit(BlockHeader header) + private static UInt256 CalculateDataGasPricePerUnit(BlockHeader header) { if (header.ExcessDataGas is null) { throw new ArgumentException(nameof(BlockHeader.ExcessDataGas)); } - ulong excessDataGas = header.ExcessDataGas.Value; - return CalculateDataGasPricePerUnit(header.ExcessDataGas.Value); } diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index 6ace9a200d0..e24cfe29f81 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -233,7 +233,7 @@ public void Bridge_head_is_correct(long headNumber) [TestCase(true)] [TestCase(false)] - public void GetReceiptAndEffectiveGasPrice_returns_correct_results(bool isCanonical) + public void GetReceiptAndGasInfo_returns_correct_results(bool isCanonical) { Keccak txHash = TestItem.KeccakA; Keccak blockHash = TestItem.KeccakB; @@ -255,8 +255,10 @@ public void GetReceiptAndEffectiveGasPrice_returns_correct_results(bool isCanoni _receiptStorage.FindBlockHash(txHash).Returns(blockHash); _receiptStorage.Get(block).Returns(new[] { receipt }); - (TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) result = isCanonical ? (receipt, effectiveGasPrice, 0) : (null, null, 0); - _blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash).Should().BeEquivalentTo(result); + (TxReceipt Receipt, TxGasInfo GasInfo, int LogIndexStart) result = isCanonical + ? (receipt, new(effectiveGasPrice), 0) + : (null, null, 0); + _blockchainBridge.GetReceiptAndGasInfo(txHash).Should().BeEquivalentTo(result); } } } diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index 0746848502a..2bfc6e4d6b7 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -83,7 +83,7 @@ public Block? HeadBlock public bool IsMining { get; } - public (TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) GetReceiptAndEffectiveGasPrice(Keccak txHash) + public (TxReceipt? Receipt, TxGasInfo? GasInfo, int LogIndexStart) GetReceiptAndGasInfo(Keccak txHash) { Keccak blockHash = _receiptFinder.FindBlockHash(txHash); if (blockHash is not null) @@ -97,14 +97,22 @@ public Block? HeadBlock Transaction tx = block.Transactions[txReceipt.Index]; bool is1559Enabled = _specProvider.GetSpecFor1559(block.Number).IsEip1559Enabled; UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); - return (txReceipt, effectiveGasPrice, logIndexStart); + ulong? dataGas = null; + UInt256? dataGasPrice = null; + if (_specProvider.GetSpec(block.Number, block.Timestamp).IsEip4844Enabled) + { + dataGas = IntrinsicGasCalculator.CalculateDataGas(tx); + dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); + } + + return (txReceipt, new(effectiveGasPrice, dataGasPrice, dataGas), logIndexStart); } } return (null, null, 0); } - public (TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash) + public (TxReceipt? Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash) { Keccak blockHash = _receiptFinder.FindBlockHash(txHash); if (blockHash is not null) @@ -271,7 +279,7 @@ private void CallAndRestore( if (releaseSpec.IsEip4844Enabled) { - callHeader.DataGasUsed = IntrinsicGasCalculator.CalculateDataGas(transaction.BlobVersionedHashes?.Length ?? 0); + callHeader.DataGasUsed = IntrinsicGasCalculator.CalculateDataGas(transaction); callHeader.ExcessDataGas = treatBlockHeaderAsParentBlock ? IntrinsicGasCalculator.CalculateExcessDataGas(blockHeader, releaseSpec) : blockHeader.ExcessDataGas; diff --git a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs index 0b39fac85b9..19a25fcb028 100644 --- a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs @@ -21,8 +21,8 @@ public interface IBlockchainBridge : ILogFinder void RecoverTxSenders(Block block); Address? RecoverTxSender(Transaction tx); TxReceipt GetReceipt(Keccak txHash); - (TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) GetReceiptAndEffectiveGasPrice(Keccak txHash); - (TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash); + (TxReceipt? Receipt, TxGasInfo? GasInfo, int LogIndexStart) GetReceiptAndGasInfo(Keccak txHash); + (TxReceipt? Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash); BlockchainBridge.CallOutput Call(BlockHeader header, Transaction tx, CancellationToken cancellationToken); BlockchainBridge.CallOutput EstimateGas(BlockHeader header, Transaction tx, CancellationToken cancellationToken); BlockchainBridge.CallOutput CreateAccessList(BlockHeader header, Transaction tx, CancellationToken cancellationToken, bool optimize); @@ -45,4 +45,23 @@ public interface IBlockchainBridge : ILogFinder void RunTreeVisitor(ITreeVisitor treeVisitor, Keccak stateRoot); } + + public class TxGasInfo + { + public TxGasInfo(UInt256? effectiveGasPrice, UInt256? dataGasPrice, ulong? dataGasUsed) + { + EffectiveGasPrice = effectiveGasPrice; + DataGasPrice = dataGasPrice; + DataGasUsed = dataGasUsed; + } + + public TxGasInfo(UInt256? effectiveGasPrice) + { + EffectiveGasPrice = effectiveGasPrice; + } + + public UInt256? EffectiveGasPrice; + public UInt256? DataGasPrice; + public ulong? DataGasUsed; + } } diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Data/ReceiptsForRpcTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Data/ReceiptsForRpcTests.cs index 06e71db2bf5..2ab81efce34 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Data/ReceiptsForRpcTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Data/ReceiptsForRpcTests.cs @@ -38,7 +38,7 @@ public void Are_log_indexes_unique() }; UInt256 effectiveGasPrice = new(5526); - ReceiptForRpc receiptForRpc = new(txHash, receipt1, effectiveGasPrice); + ReceiptForRpc receiptForRpc = new(txHash, receipt1, new(effectiveGasPrice)); long?[] indexes = receiptForRpc.Logs.Select(log => log.LogIndex).ToArray(); long?[] expected = { 0, 1, 2 }; diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs index 36a6105b6b8..71b59058e94 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs @@ -840,7 +840,7 @@ public async Task Eth_get_transaction_receipt() .WithLogs(entries).TestObject; TxReceipt[] receiptsTab = { receipt }; - blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt, UInt256.One, 0)); + blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt, new(UInt256.One), 0)); blockFinder.FindBlock(Arg.Any()).Returns(block); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); @@ -903,7 +903,7 @@ public async Task Eth_get_transaction_receipt_when_block_has_few_receipts() Logs = logEntries }; - blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt2, UInt256.One, 2)); + blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt2, new(UInt256.One), 2)); TxReceipt[] receipts = { receipt1, receipt2 }; @@ -959,7 +959,7 @@ public async Task Eth_getTransactionReceipt_return_info_about_mined_tx() blockFinder.FindBlock(Arg.Any()).Returns(block); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); - blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt, UInt256.One, 0)); + blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt, new(UInt256.One), 0)); ctx.Test = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).WithBlockFinder(blockFinder).WithReceiptFinder(receiptFinder).WithBlockchainBridge(blockchainBridge).Build(); string serialized = ctx.Test.TestEthRpc("eth_getTransactionReceipt", tx.Hash!.ToString()); diff --git a/src/Nethermind/Nethermind.JsonRpc/Converters/TxReceiptConverter.cs b/src/Nethermind/Nethermind.JsonRpc/Converters/TxReceiptConverter.cs index 682247df55b..34da8afada2 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Converters/TxReceiptConverter.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Converters/TxReceiptConverter.cs @@ -13,7 +13,7 @@ public class TxReceiptConverter : JsonConverter { public override void WriteJson(JsonWriter writer, TxReceipt value, JsonSerializer serializer) { - serializer.Serialize(writer, new ReceiptForRpc(value.TxHash!, value, UInt256.Zero)); + serializer.Serialize(writer, new ReceiptForRpc(value.TxHash!, value, new(UInt256.Zero))); } public override TxReceipt ReadJson(JsonReader reader, Type objectType, TxReceipt existingValue, bool hasExistingValue, JsonSerializer serializer) diff --git a/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs b/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs index 00de50c6f1b..e9f62d71d68 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs @@ -1,10 +1,10 @@ // SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only -using System; using System.Linq; using Nethermind.Core; using Nethermind.Core.Crypto; +using Nethermind.Facade; using Nethermind.Int256; using Newtonsoft.Json; @@ -16,7 +16,7 @@ public ReceiptForRpc() { } - public ReceiptForRpc(Keccak txHash, TxReceipt receipt, UInt256? effectiveGasPrice, int logIndexStart = 0) + public ReceiptForRpc(Keccak txHash, TxReceipt receipt, TxGasInfo gasInfo, int logIndexStart = 0) { TransactionHash = txHash; TransactionIndex = receipt.Index; @@ -24,7 +24,9 @@ public ReceiptForRpc(Keccak txHash, TxReceipt receipt, UInt256? effectiveGasPric BlockNumber = receipt.BlockNumber; CumulativeGasUsed = receipt.GasUsedTotal; GasUsed = receipt.GasUsed; - EffectiveGasPrice = effectiveGasPrice; + EffectiveGasPrice = gasInfo.EffectiveGasPrice; + DataGasUsed = gasInfo.DataGasUsed; + DataGasPrice = gasInfo.DataGasPrice; From = receipt.Sender; To = receipt.Recipient; ContractAddress = receipt.ContractAddress; @@ -42,6 +44,8 @@ public ReceiptForRpc(Keccak txHash, TxReceipt receipt, UInt256? effectiveGasPric public long BlockNumber { get; set; } public long CumulativeGasUsed { get; set; } public long GasUsed { get; set; } + public ulong? DataGasUsed { get; set; } + public UInt256? DataGasPrice { get; set; } public UInt256? EffectiveGasPrice { get; set; } public Address From { get; set; } diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs index 743f656c121..82052f9bcb3 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs @@ -494,14 +494,14 @@ public ResultWrapper eth_getTransactionByBlockNumberAndIndex( public Task> eth_getTransactionReceipt(Keccak txHash) { - (TxReceipt receipt, UInt256? effectiveGasPrice, int logIndexStart) = _blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash); + (TxReceipt? receipt, TxGasInfo? gasInfo, int logIndexStart) = _blockchainBridge.GetReceiptAndGasInfo(txHash); if (receipt is null) { return Task.FromResult(ResultWrapper.Success(null)); } if (_logger.IsTrace) _logger.Trace($"eth_getTransactionReceipt request {txHash}, result: {txHash}"); - return Task.FromResult(ResultWrapper.Success(new(txHash, receipt, effectiveGasPrice, logIndexStart))); + return Task.FromResult(ResultWrapper.Success(new(txHash, receipt, gasInfo, logIndexStart))); } public ResultWrapper eth_getUncleByBlockHashAndIndex(Keccak blockHash, UInt256 positionIndex) diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs index 07680e96e90..6a62ed0178e 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs @@ -11,6 +11,7 @@ using Nethermind.Core; using Nethermind.Core.Specs; using Nethermind.Crypto; +using Nethermind.Evm; using Nethermind.Int256; using Nethermind.JsonRpc.Data; using Nethermind.KeyStore; @@ -74,10 +75,24 @@ public ResultWrapper parity_getBlockReceipts(BlockParameter blo Block block = searchResult.Object; TxReceipt[] receipts = _receiptFinder.Get(block) ?? new TxReceipt[block.Transactions.Length]; - bool isEip1559Enabled = _specProvider.GetSpec(block.Header).IsEip1559Enabled; + IReleaseSpec spec = _specProvider.GetSpec(block.Header); + bool isEip1559Enabled = spec.IsEip1559Enabled; IEnumerable result = receipts .Zip(block.Transactions, (r, t) => - new ReceiptForRpc(t.Hash, r, t.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas), receipts.GetBlockLogFirstIndex(r.Index))); + { + UInt256 effectiveGasPrice = t.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas); + UInt256? dataGasPrice = null; + ulong? dataGasUsed = null; + if (spec.IsEip4844Enabled) + { + dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, t); + dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(t); + } + + return new ReceiptForRpc(t.Hash, r, + new(effectiveGasPrice, dataGasPrice, dataGasUsed), + receipts.GetBlockLogFirstIndex(r.Index)); + }); ReceiptForRpc[] resultAsArray = result.ToArray(); return ResultWrapper.Success(resultAsArray); } diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs index 05427d8a01b..c7b5865255a 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs @@ -10,12 +10,12 @@ using Nethermind.Consensus.Tracing; using Nethermind.Core; using Nethermind.Core.Crypto; -using Nethermind.Core.Extensions; using Nethermind.Core.Specs; using Nethermind.Crypto; +using Nethermind.Evm; using Nethermind.Evm.Tracing; using Nethermind.Evm.Tracing.Proofs; -using Nethermind.Facade; +using Nethermind.Int256; using Nethermind.JsonRpc.Data; using Nethermind.Logging; using Nethermind.Serialization.Rlp; @@ -155,11 +155,24 @@ public ResultWrapper proof_getTransactionReceipt(Keccak txHash TxReceipt[] receipts = receiptsTracer.TxReceipts.ToArray(); Transaction[] txs = block.Transactions; ReceiptWithProof receiptWithProof = new(); - bool isEip1559Enabled = _specProvider.GetSpec(block.Header).IsEip1559Enabled; + IReleaseSpec spec = _specProvider.GetSpec(block.Header); + bool isEip1559Enabled = spec.IsEip1559Enabled; Transaction? tx = txs.FirstOrDefault(x => x.Hash == txHash); int logIndexStart = _receiptFinder.Get(block).GetBlockLogFirstIndex(receipt.Index); - receiptWithProof.Receipt = new ReceiptForRpc(txHash, receipt, tx?.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas), logIndexStart); + UInt256? effectiveGasPrice = tx?.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas); + + UInt256? dataGasPrice = null; + ulong? dataGasUsed = null; + if (spec.IsEip4844Enabled && tx is not null) + { + dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); + dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(tx); + } + + receiptWithProof.Receipt = new ReceiptForRpc(txHash, receipt, + new(effectiveGasPrice, dataGasPrice, dataGasUsed), + logIndexStart); receiptWithProof.ReceiptProof = BuildReceiptProofs(block.Header, receipts, receipt.Index); receiptWithProof.TxProof = BuildTxProofs(txs, _specProvider.GetSpec(block.Header), receipt.Index); From c498646fb8d5337c5db2eb792b781f61ab74a1f4 Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Mon, 5 Jun 2023 00:06:08 +0900 Subject: [PATCH 2/8] Fix gas price --- src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs b/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs index 767a2128335..918adc46069 100644 --- a/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs +++ b/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs @@ -103,7 +103,7 @@ public static UInt256 CalculateDataGasPrice(BlockHeader header, Transaction tran CalculateDataGas(transaction) * CalculateDataGasPricePerUnit(header); public static UInt256 CalculateDataGasPrice(BlockHeader header) => - CalculateDataGasPricePerUnit(header); + header.DataGasUsed!.Value * CalculateDataGasPricePerUnit(header); private static UInt256 CalculateDataGasPricePerUnit(BlockHeader header) { From 7a12d4fdde823f3598c89f134be4c5cbd646166c Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Mon, 5 Jun 2023 00:08:35 +0900 Subject: [PATCH 3/8] Fix spaces --- .../Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs index c7b5865255a..b4294db591f 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs @@ -166,8 +166,8 @@ public ResultWrapper proof_getTransactionReceipt(Keccak txHash ulong? dataGasUsed = null; if (spec.IsEip4844Enabled && tx is not null) { - dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); - dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(tx); + dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); + dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(tx); } receiptWithProof.Receipt = new ReceiptForRpc(txHash, receipt, From 03b9bf83346a76a3cc357736810c6e448607a861 Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Mon, 5 Jun 2023 19:19:09 +0900 Subject: [PATCH 4/8] Add tests --- .../BlockchainBridgeTests.cs | 71 +++++++++++++++---- .../Nethermind.Facade.Test.csproj | 1 + .../Nethermind.Facade/BlockchainBridge.cs | 2 +- .../Nethermind.Facade/IBlockchainBridge.cs | 6 +- .../Modules/Eth/EthRpcModuleTests.cs | 15 ++-- 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index e24cfe29f81..52a717e941f 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -28,6 +28,8 @@ using NSubstitute; using NUnit.Framework; using Nethermind.Config; +using Nethermind.Specs.Forks; +using Nethermind.Specs.Test; namespace Nethermind.Facade.Test { @@ -231,20 +233,59 @@ public void Bridge_head_is_correct(long headNumber) _blockchainBridge.HeadBlock.Should().Be(head); } - [TestCase(true)] - [TestCase(false)] - public void GetReceiptAndGasInfo_returns_correct_results(bool isCanonical) + [TestCase(true, true)] + [TestCase(false, true)] + [TestCase(true, false)] + [TestCase(false, false)] + public void GetReceiptAndGasInfo_returns_correct_results(bool isCanonical, bool postEip4844) { Keccak txHash = TestItem.KeccakA; Keccak blockHash = TestItem.KeccakB; UInt256 effectiveGasPrice = 123; - Transaction tx = Build.A.Transaction - .WithGasPrice(effectiveGasPrice) - .TestObject; - Block block = Build.A.Block - .WithTransactions(tx) - .TestObject; + if (postEip4844) + { + _specProvider = new CustomSpecProvider(((ForkActivation)0, Cancun.Instance)); + + ReadOnlyTxProcessingEnv processingEnv = new( + new ReadOnlyDbProvider(_dbProvider, false), + new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), + new ReadOnlyBlockTree(_blockTree), + _specProvider, + LimboLogs.Instance); + + processingEnv.TransactionProcessor = _transactionProcessor; + + _blockchainBridge = new BlockchainBridge( + processingEnv, + _txPool, + _receiptStorage, + _filterStore, + _filterManager, + _ethereumEcdsa, + _timestamper, + Substitute.For(), + _specProvider, + new BlocksConfig(), + false); + } + + Transaction tx = postEip4844 + ? Build.A.Transaction + .WithGasPrice(effectiveGasPrice) + .WithBlobVersionedHashes(2) + .TestObject + : Build.A.Transaction + .WithGasPrice(effectiveGasPrice) + .TestObject; + Block block = postEip4844 + ? Build.A.Block + .WithTransactions(tx) + .WithExcessDataGas(2) + .TestObject + : Build.A.Block + .WithTransactions(tx) + .TestObject; TxReceipt receipt = Build.A.Receipt .WithBlockHash(blockHash) .WithTransactionHash(txHash) @@ -255,9 +296,15 @@ public void GetReceiptAndGasInfo_returns_correct_results(bool isCanonical) _receiptStorage.FindBlockHash(txHash).Returns(blockHash); _receiptStorage.Get(block).Returns(new[] { receipt }); - (TxReceipt Receipt, TxGasInfo GasInfo, int LogIndexStart) result = isCanonical - ? (receipt, new(effectiveGasPrice), 0) - : (null, null, 0); + (TxReceipt? Receipt, TxGasInfo? GasInfo, int LogIndexStart) result = postEip4844 + ? (receipt, new(effectiveGasPrice, 262144, 262144), 0) + : (receipt, new(effectiveGasPrice), 0); + + if (!isCanonical) + { + result = (null, null, 0); + } + _blockchainBridge.GetReceiptAndGasInfo(txHash).Should().BeEquivalentTo(result); } } diff --git a/src/Nethermind/Nethermind.Facade.Test/Nethermind.Facade.Test.csproj b/src/Nethermind/Nethermind.Facade.Test/Nethermind.Facade.Test.csproj index 15608eea2e8..5feafc0ae10 100644 --- a/src/Nethermind/Nethermind.Facade.Test/Nethermind.Facade.Test.csproj +++ b/src/Nethermind/Nethermind.Facade.Test/Nethermind.Facade.Test.csproj @@ -3,6 +3,7 @@ net7.0 true + annotations diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index 2bfc6e4d6b7..a39b46134d9 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -99,7 +99,7 @@ public Block? HeadBlock UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); ulong? dataGas = null; UInt256? dataGasPrice = null; - if (_specProvider.GetSpec(block.Number, block.Timestamp).IsEip4844Enabled) + if (_specProvider.GetSpec(block.Header).IsEip4844Enabled) { dataGas = IntrinsicGasCalculator.CalculateDataGas(tx); dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); diff --git a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs index 19a25fcb028..57fd3d38e57 100644 --- a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs @@ -60,8 +60,8 @@ public TxGasInfo(UInt256? effectiveGasPrice) EffectiveGasPrice = effectiveGasPrice; } - public UInt256? EffectiveGasPrice; - public UInt256? DataGasPrice; - public ulong? DataGasUsed; + public UInt256? EffectiveGasPrice { get; private set; } + public UInt256? DataGasPrice { get; private set; } + public ulong? DataGasUsed { get; private set; } } } diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs index 71b59058e94..13e12e19845 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs @@ -818,8 +818,9 @@ public async Task Eth_get_block_by_number_with_recovering_sender_from_receipts() Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":{\"author\":\"0x0000000000000000000000000000000000000000\",\"difficulty\":\"0xf4240\",\"extraData\":\"0x010203\",\"gasLimit\":\"0x3d0900\",\"gasUsed\":\"0x0\",\"hash\":\"0xe3026a6708b90d5cb25557ac38ddc3f5ef550af10f31e1cf771524da8553fa1c\",\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"miner\":\"0x0000000000000000000000000000000000000000\",\"mixHash\":\"0x2ba5557a4c62a513c7e56d1bf13373e0da6bec016755483e91589fe1c6d212e2\",\"nonce\":\"0x00000000000003e8\",\"number\":\"0x1\",\"parentHash\":\"0xff483e972a04a9a62bb4b7d04ae403c615604e4090521ecc5bb7af67f71be09c\",\"receiptsRoot\":\"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",\"sha3Uncles\":\"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347\",\"size\":\"0x221\",\"stateRoot\":\"0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f\",\"totalDifficulty\":\"0x0\",\"timestamp\":\"0xf4240\",\"transactions\":[{\"nonce\":\"0x0\",\"blockHash\":\"0xe3026a6708b90d5cb25557ac38ddc3f5ef550af10f31e1cf771524da8553fa1c\",\"blockNumber\":\"0x1\",\"transactionIndex\":\"0x0\",\"from\":\"0x2d36e6c27c34ea22620e7b7c45de774599406cf3\",\"to\":\"0x0000000000000000000000000000000000000000\",\"value\":\"0x1\",\"gasPrice\":\"0x1\",\"gas\":\"0x5208\",\"data\":\"0x\",\"input\":\"0x\",\"type\":\"0x0\"}],\"transactionsRoot\":\"0x29cc403075ed3d1d6af940d577125cc378ee5a26f7746cbaf87f1cf4a38258b5\",\"uncles\":[]},\"id\":67}")); } - [Test] - public async Task Eth_get_transaction_receipt() + [TestCase(false)] + [TestCase(true)] + public async Task Eth_get_transaction_receipt(bool postEip4844) { using Context ctx = await Context.Create(); IBlockchainBridge blockchainBridge = Substitute.For(); @@ -840,7 +841,10 @@ public async Task Eth_get_transaction_receipt() .WithLogs(entries).TestObject; TxReceipt[] receiptsTab = { receipt }; - blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt, new(UInt256.One), 0)); + if (postEip4844) + blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt, new(UInt256.One, 2, 3), 0)); + else + blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt, new(UInt256.One), 0)); blockFinder.FindBlock(Arg.Any()).Returns(block); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); @@ -848,7 +852,10 @@ public async Task Eth_get_transaction_receipt() ctx.Test = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).WithBlockFinder(blockFinder).WithReceiptFinder(receiptFinder).WithBlockchainBridge(blockchainBridge).Build(); string serialized = ctx.Test.TestEthRpc("eth_getTransactionReceipt", TestItem.KeccakA.ToString()); - Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":{\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"transactionIndex\":\"0x2\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"cumulativeGasUsed\":\"0x3e8\",\"gasUsed\":\"0x64\",\"effectiveGasPrice\":\"0x1\",\"from\":\"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099\",\"to\":\"0x942921b14f1b1c385cd7e0cc2ef7abe5598c8358\",\"contractAddress\":\"0x76e68a8696537e4141926f3e528733af9e237d69\",\"logs\":[{\"removed\":false,\"logIndex\":\"0x0\",\"transactionIndex\":\"0x2\",\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"address\":\"0x0000000000000000000000000000000000000000\",\"data\":\"0x\",\"topics\":[\"0x0000000000000000000000000000000000000000000000000000000000000000\"]},{\"removed\":false,\"logIndex\":\"0x1\",\"transactionIndex\":\"0x2\",\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"address\":\"0x0000000000000000000000000000000000000000\",\"data\":\"0x\",\"topics\":[\"0x0000000000000000000000000000000000000000000000000000000000000000\"]}],\"logsBloom\":\"0x00000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000\",\"root\":\"0x1f675bff07515f5df96737194ea945c36c41e7b4fcef307b7cd4d0e602a69111\",\"status\":\"0x1\",\"error\":\"error\",\"type\":\"0x0\"},\"id\":67}")); + if (postEip4844) + Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":{\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"transactionIndex\":\"0x2\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"cumulativeGasUsed\":\"0x3e8\",\"gasUsed\":\"0x64\",\"dataGasUsed\":\"0x3\",\"dataGasPrice\":\"0x2\",\"effectiveGasPrice\":\"0x1\",\"from\":\"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099\",\"to\":\"0x942921b14f1b1c385cd7e0cc2ef7abe5598c8358\",\"contractAddress\":\"0x76e68a8696537e4141926f3e528733af9e237d69\",\"logs\":[{\"removed\":false,\"logIndex\":\"0x0\",\"transactionIndex\":\"0x2\",\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"address\":\"0x0000000000000000000000000000000000000000\",\"data\":\"0x\",\"topics\":[\"0x0000000000000000000000000000000000000000000000000000000000000000\"]},{\"removed\":false,\"logIndex\":\"0x1\",\"transactionIndex\":\"0x2\",\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"address\":\"0x0000000000000000000000000000000000000000\",\"data\":\"0x\",\"topics\":[\"0x0000000000000000000000000000000000000000000000000000000000000000\"]}],\"logsBloom\":\"0x00000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000\",\"root\":\"0x1f675bff07515f5df96737194ea945c36c41e7b4fcef307b7cd4d0e602a69111\",\"status\":\"0x1\",\"error\":\"error\",\"type\":\"0x0\"},\"id\":67}")); + else + Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":{\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"transactionIndex\":\"0x2\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"cumulativeGasUsed\":\"0x3e8\",\"gasUsed\":\"0x64\",\"effectiveGasPrice\":\"0x1\",\"from\":\"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099\",\"to\":\"0x942921b14f1b1c385cd7e0cc2ef7abe5598c8358\",\"contractAddress\":\"0x76e68a8696537e4141926f3e528733af9e237d69\",\"logs\":[{\"removed\":false,\"logIndex\":\"0x0\",\"transactionIndex\":\"0x2\",\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"address\":\"0x0000000000000000000000000000000000000000\",\"data\":\"0x\",\"topics\":[\"0x0000000000000000000000000000000000000000000000000000000000000000\"]},{\"removed\":false,\"logIndex\":\"0x1\",\"transactionIndex\":\"0x2\",\"transactionHash\":\"0x03783fac2efed8fbc9ad443e592ee30e61d65f471140c10ca155e937b435b760\",\"blockHash\":\"0x017e667f4b8c174291d1543c466717566e206df1bfd6f30271055ddafdb18f72\",\"blockNumber\":\"0x2\",\"address\":\"0x0000000000000000000000000000000000000000\",\"data\":\"0x\",\"topics\":[\"0x0000000000000000000000000000000000000000000000000000000000000000\"]}],\"logsBloom\":\"0x00000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000\",\"root\":\"0x1f675bff07515f5df96737194ea945c36c41e7b4fcef307b7cd4d0e602a69111\",\"status\":\"0x1\",\"error\":\"error\",\"type\":\"0x0\"},\"id\":67}")); } From e92d6de5eef8f687fc579125b1456091f203266f Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Mon, 5 Jun 2023 19:45:41 +0900 Subject: [PATCH 5/8] Supports blobs --- .../BlockchainBridgeTests.cs | 29 ++----------------- .../Nethermind.Facade/BlockchainBridge.cs | 2 +- .../Modules/Parity/ParityRpcModule.cs | 5 ++-- .../Modules/Proof/ProofRpcModule.cs | 5 ++-- 4 files changed, 7 insertions(+), 34 deletions(-) diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index 52a717e941f..86ad49a492a 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -243,36 +243,11 @@ public void GetReceiptAndGasInfo_returns_correct_results(bool isCanonical, bool Keccak blockHash = TestItem.KeccakB; UInt256 effectiveGasPrice = 123; - if (postEip4844) - { - _specProvider = new CustomSpecProvider(((ForkActivation)0, Cancun.Instance)); - - ReadOnlyTxProcessingEnv processingEnv = new( - new ReadOnlyDbProvider(_dbProvider, false), - new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), - new ReadOnlyBlockTree(_blockTree), - _specProvider, - LimboLogs.Instance); - - processingEnv.TransactionProcessor = _transactionProcessor; - - _blockchainBridge = new BlockchainBridge( - processingEnv, - _txPool, - _receiptStorage, - _filterStore, - _filterManager, - _ethereumEcdsa, - _timestamper, - Substitute.For(), - _specProvider, - new BlocksConfig(), - false); - } - Transaction tx = postEip4844 ? Build.A.Transaction .WithGasPrice(effectiveGasPrice) + .WithType(TxType.Blob) + .WithMaxFeePerDataGas(2) .WithBlobVersionedHashes(2) .TestObject : Build.A.Transaction diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index a39b46134d9..18a7cb30835 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -99,7 +99,7 @@ public Block? HeadBlock UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); ulong? dataGas = null; UInt256? dataGasPrice = null; - if (_specProvider.GetSpec(block.Header).IsEip4844Enabled) + if (tx.SupportsBlobs) { dataGas = IntrinsicGasCalculator.CalculateDataGas(tx); dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs index 6a62ed0178e..876a859f124 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs @@ -75,15 +75,14 @@ public ResultWrapper parity_getBlockReceipts(BlockParameter blo Block block = searchResult.Object; TxReceipt[] receipts = _receiptFinder.Get(block) ?? new TxReceipt[block.Transactions.Length]; - IReleaseSpec spec = _specProvider.GetSpec(block.Header); - bool isEip1559Enabled = spec.IsEip1559Enabled; + bool isEip1559Enabled = _specProvider.GetSpec(block.Header).IsEip1559Enabled; IEnumerable result = receipts .Zip(block.Transactions, (r, t) => { UInt256 effectiveGasPrice = t.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas); UInt256? dataGasPrice = null; ulong? dataGasUsed = null; - if (spec.IsEip4844Enabled) + if (t.SupportsBlobs) { dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, t); dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(t); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs index b4294db591f..c8bd9b4de00 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs @@ -155,8 +155,7 @@ public ResultWrapper proof_getTransactionReceipt(Keccak txHash TxReceipt[] receipts = receiptsTracer.TxReceipts.ToArray(); Transaction[] txs = block.Transactions; ReceiptWithProof receiptWithProof = new(); - IReleaseSpec spec = _specProvider.GetSpec(block.Header); - bool isEip1559Enabled = spec.IsEip1559Enabled; + bool isEip1559Enabled = _specProvider.GetSpec(block.Header).IsEip1559Enabled; Transaction? tx = txs.FirstOrDefault(x => x.Hash == txHash); int logIndexStart = _receiptFinder.Get(block).GetBlockLogFirstIndex(receipt.Index); @@ -164,7 +163,7 @@ public ResultWrapper proof_getTransactionReceipt(Keccak txHash UInt256? dataGasPrice = null; ulong? dataGasUsed = null; - if (spec.IsEip4844Enabled && tx is not null) + if (tx is not null && tx.SupportsBlobs) { dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(tx); From fbc07ab501fad9e70dce02fa6ca6adcd11216029 Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Mon, 5 Jun 2023 19:54:28 +0900 Subject: [PATCH 6/8] Ignore on null --- src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs b/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs index e9f62d71d68..db296ea7ff7 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs @@ -44,7 +44,9 @@ public ReceiptForRpc(Keccak txHash, TxReceipt receipt, TxGasInfo gasInfo, int lo public long BlockNumber { get; set; } public long CumulativeGasUsed { get; set; } public long GasUsed { get; set; } + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public ulong? DataGasUsed { get; set; } + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public UInt256? DataGasPrice { get; set; } public UInt256? EffectiveGasPrice { get; set; } From 5f7c8e28ee2bf9ea07cf215408285f7e061234e1 Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Mon, 5 Jun 2023 22:25:34 +0900 Subject: [PATCH 7/8] Fix suggestions --- .../Nethermind.Evm/TransactionExtensions.cs | 35 +++++++++++++++++++ .../BlockchainBridgeTests.cs | 3 +- .../Nethermind.Facade/BlockchainBridge.cs | 11 +----- .../Nethermind.Facade/IBlockchainBridge.cs | 20 +---------- .../Modules/Eth/EthRpcModuleTests.cs | 7 ++-- .../Nethermind.JsonRpc/Data/ReceiptForRpc.cs | 2 +- .../Modules/Eth/EthRpcModule.cs | 5 +-- .../Modules/Parity/ParityRpcModule.cs | 14 +------- .../Modules/Proof/ProofRpcModule.cs | 15 +------- 9 files changed, 47 insertions(+), 65 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/TransactionExtensions.cs b/src/Nethermind/Nethermind.Evm/TransactionExtensions.cs index a5f85fe7075..e0bd18c1860 100644 --- a/src/Nethermind/Nethermind.Evm/TransactionExtensions.cs +++ b/src/Nethermind/Nethermind.Evm/TransactionExtensions.cs @@ -14,5 +14,40 @@ tx.To is not null : tx.IsSystem() ? tx.SenderAddress : ContractAddress.From(tx.SenderAddress, nonce > 0 ? nonce - 1 : nonce); + + public static TxGasInfo GetGasInfo(this Transaction tx, bool is1559Enabled, BlockHeader header) + { + UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, header.BaseFeePerGas); + ulong? dataGas = null; + UInt256? dataGasPrice = null; + if (tx.SupportsBlobs) + { + dataGas = IntrinsicGasCalculator.CalculateDataGas(tx); + dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(header, tx); + } + + return new(effectiveGasPrice, dataGasPrice, dataGas); + } + } + + public struct TxGasInfo + { + public TxGasInfo() { } + + public TxGasInfo(UInt256? effectiveGasPrice, UInt256? dataGasPrice, ulong? dataGasUsed) + { + EffectiveGasPrice = effectiveGasPrice; + DataGasPrice = dataGasPrice; + DataGasUsed = dataGasUsed; + } + + public TxGasInfo(UInt256? effectiveGasPrice) + { + EffectiveGasPrice = effectiveGasPrice; + } + + public UInt256? EffectiveGasPrice { get; private set; } + public UInt256? DataGasPrice { get; private set; } + public ulong? DataGasUsed { get; private set; } } } diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index 86ad49a492a..4dba1905c19 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -28,8 +28,7 @@ using NSubstitute; using NUnit.Framework; using Nethermind.Config; -using Nethermind.Specs.Forks; -using Nethermind.Specs.Test; +using Nethermind.Evm; namespace Nethermind.Facade.Test { diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index 18a7cb30835..dbc10993fb9 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -96,16 +96,7 @@ public Block? HeadBlock int logIndexStart = txReceipts.GetBlockLogFirstIndex(txReceipt.Index); Transaction tx = block.Transactions[txReceipt.Index]; bool is1559Enabled = _specProvider.GetSpecFor1559(block.Number).IsEip1559Enabled; - UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); - ulong? dataGas = null; - UInt256? dataGasPrice = null; - if (tx.SupportsBlobs) - { - dataGas = IntrinsicGasCalculator.CalculateDataGas(tx); - dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); - } - - return (txReceipt, new(effectiveGasPrice, dataGasPrice, dataGas), logIndexStart); + return (txReceipt, tx.GetGasInfo(is1559Enabled, block.Header), logIndexStart); } } diff --git a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs index 57fd3d38e57..cccff0b75ea 100644 --- a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs @@ -7,6 +7,7 @@ using Nethermind.Blockchain.Find; using Nethermind.Core; using Nethermind.Core.Crypto; +using Nethermind.Evm; using Nethermind.Facade.Filters; using Nethermind.Int256; using Nethermind.Trie; @@ -45,23 +46,4 @@ public interface IBlockchainBridge : ILogFinder void RunTreeVisitor(ITreeVisitor treeVisitor, Keccak stateRoot); } - - public class TxGasInfo - { - public TxGasInfo(UInt256? effectiveGasPrice, UInt256? dataGasPrice, ulong? dataGasUsed) - { - EffectiveGasPrice = effectiveGasPrice; - DataGasPrice = dataGasPrice; - DataGasUsed = dataGasUsed; - } - - public TxGasInfo(UInt256? effectiveGasPrice) - { - EffectiveGasPrice = effectiveGasPrice; - } - - public UInt256? EffectiveGasPrice { get; private set; } - public UInt256? DataGasPrice { get; private set; } - public ulong? DataGasUsed { get; private set; } - } } diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs index 13e12e19845..4f97badc663 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs @@ -841,10 +841,9 @@ public async Task Eth_get_transaction_receipt(bool postEip4844) .WithLogs(entries).TestObject; TxReceipt[] receiptsTab = { receipt }; - if (postEip4844) - blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt, new(UInt256.One, 2, 3), 0)); - else - blockchainBridge.GetReceiptAndGasInfo(Arg.Any()).Returns((receipt, new(UInt256.One), 0)); + + blockchainBridge.GetReceiptAndGasInfo(Arg.Any()) + .Returns((receipt, postEip4844 ? new(UInt256.One, 2, 3) : new(UInt256.One), 0)); blockFinder.FindBlock(Arg.Any()).Returns(block); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); diff --git a/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs b/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs index db296ea7ff7..7ce43ff3411 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Data/ReceiptForRpc.cs @@ -4,7 +4,7 @@ using System.Linq; using Nethermind.Core; using Nethermind.Core.Crypto; -using Nethermind.Facade; +using Nethermind.Evm; using Nethermind.Int256; using Newtonsoft.Json; diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs index 82052f9bcb3..2fb9ce4c21a 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs @@ -15,6 +15,7 @@ using Nethermind.Core.Crypto; using Nethermind.Core.Extensions; using Nethermind.Core.Specs; +using Nethermind.Evm; using Nethermind.Facade; using Nethermind.Facade.Eth; using Nethermind.Facade.Filters; @@ -495,13 +496,13 @@ public ResultWrapper eth_getTransactionByBlockNumberAndIndex( public Task> eth_getTransactionReceipt(Keccak txHash) { (TxReceipt? receipt, TxGasInfo? gasInfo, int logIndexStart) = _blockchainBridge.GetReceiptAndGasInfo(txHash); - if (receipt is null) + if (receipt is null || gasInfo is null) { return Task.FromResult(ResultWrapper.Success(null)); } if (_logger.IsTrace) _logger.Trace($"eth_getTransactionReceipt request {txHash}, result: {txHash}"); - return Task.FromResult(ResultWrapper.Success(new(txHash, receipt, gasInfo, logIndexStart))); + return Task.FromResult(ResultWrapper.Success(new(txHash, receipt, gasInfo.Value, logIndexStart))); } public ResultWrapper eth_getUncleByBlockHashAndIndex(Keccak blockHash, UInt256 positionIndex) diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs index 876a859f124..7e0b3a92a38 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs @@ -12,7 +12,6 @@ using Nethermind.Core.Specs; using Nethermind.Crypto; using Nethermind.Evm; -using Nethermind.Int256; using Nethermind.JsonRpc.Data; using Nethermind.KeyStore; using Nethermind.Serialization.Rlp; @@ -79,18 +78,7 @@ public ResultWrapper parity_getBlockReceipts(BlockParameter blo IEnumerable result = receipts .Zip(block.Transactions, (r, t) => { - UInt256 effectiveGasPrice = t.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas); - UInt256? dataGasPrice = null; - ulong? dataGasUsed = null; - if (t.SupportsBlobs) - { - dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, t); - dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(t); - } - - return new ReceiptForRpc(t.Hash, r, - new(effectiveGasPrice, dataGasPrice, dataGasUsed), - receipts.GetBlockLogFirstIndex(r.Index)); + return new ReceiptForRpc(t.Hash, r, t.GetGasInfo(isEip1559Enabled, block.Header), receipts.GetBlockLogFirstIndex(r.Index)); }); ReceiptForRpc[] resultAsArray = result.ToArray(); return ResultWrapper.Success(resultAsArray); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs index c8bd9b4de00..0ead70e46c1 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofRpcModule.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Nethermind.Blockchain; using Nethermind.Blockchain.Find; using Nethermind.Blockchain.Receipts; using Nethermind.Consensus.Tracing; @@ -15,7 +14,6 @@ using Nethermind.Evm; using Nethermind.Evm.Tracing; using Nethermind.Evm.Tracing.Proofs; -using Nethermind.Int256; using Nethermind.JsonRpc.Data; using Nethermind.Logging; using Nethermind.Serialization.Rlp; @@ -159,19 +157,8 @@ public ResultWrapper proof_getTransactionReceipt(Keccak txHash Transaction? tx = txs.FirstOrDefault(x => x.Hash == txHash); int logIndexStart = _receiptFinder.Get(block).GetBlockLogFirstIndex(receipt.Index); - UInt256? effectiveGasPrice = tx?.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas); - UInt256? dataGasPrice = null; - ulong? dataGasUsed = null; - if (tx is not null && tx.SupportsBlobs) - { - dataGasPrice = IntrinsicGasCalculator.CalculateDataGasPrice(block.Header, tx); - dataGasUsed = IntrinsicGasCalculator.CalculateDataGas(tx); - } - - receiptWithProof.Receipt = new ReceiptForRpc(txHash, receipt, - new(effectiveGasPrice, dataGasPrice, dataGasUsed), - logIndexStart); + receiptWithProof.Receipt = new ReceiptForRpc(txHash, receipt, tx?.GetGasInfo(isEip1559Enabled, block.Header) ?? new(), logIndexStart); receiptWithProof.ReceiptProof = BuildReceiptProofs(block.Header, receipts, receipt.Index); receiptWithProof.TxProof = BuildTxProofs(txs, _specProvider.GetSpec(block.Header), receipt.Index); From a020c7ffb75aa07c59532eb9b84d92b082404b99 Mon Sep 17 00:00:00 2001 From: Nikita Mescheryakov Date: Tue, 6 Jun 2023 17:32:50 +0900 Subject: [PATCH 8/8] Fix build --- src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs b/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs index 918adc46069..631d182503b 100644 --- a/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs +++ b/src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs @@ -105,7 +105,7 @@ public static UInt256 CalculateDataGasPrice(BlockHeader header, Transaction tran public static UInt256 CalculateDataGasPrice(BlockHeader header) => header.DataGasUsed!.Value * CalculateDataGasPricePerUnit(header); - private static UInt256 CalculateDataGasPricePerUnit(BlockHeader header) + public static UInt256 CalculateDataGasPricePerUnit(BlockHeader header) { if (header.ExcessDataGas is null) {