From 86cee9e96d86b502334e589042c0729e6ab56e20 Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Thu, 9 Jun 2022 17:38:51 +0200 Subject: [PATCH] Do not return non-canonical receipts. Move code for logIndexStart from eth_getTransactionReceipt to BlockchainBridge.GetReceiptAndEffectiveGasPrice --- .../BlockchainBridgeTests.cs | 2 +- .../Nethermind.Facade/BlockchainBridge.cs | 25 ++++++++----------- .../Nethermind.Facade/IBlockchainBridge.cs | 2 +- .../Modules/Eth/EthRpcModuleTests.cs | 6 ++--- .../Modules/Eth/EthRpcModule.cs | 13 +++------- 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index c22263b1ee2..2c085941e02 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -222,7 +222,7 @@ public void GetReceiptAndEffectiveGasPrice_returns_correct_results(bool isCanoni _receiptStorage.FindBlockHash(txHash).Returns(blockHash); _receiptStorage.Get(block).Returns(new[] {receipt}); - (TxReceipt Receipt, UInt256? EffectiveGasPrice) result = isCanonical || isRemoved ? (receipt, effectiveGasPrice) : (null, null); + (TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) result = isCanonical ? (receipt, effectiveGasPrice, 0) : (null, null, 0); _blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash).Should().BeEquivalentTo(result); } } diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index aeee7c44ddd..49baf366bde 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -90,30 +90,25 @@ public Block? HeadBlock public bool IsMining { get; } - public (TxReceipt Receipt, UInt256? EffectiveGasPrice) GetReceiptAndEffectiveGasPrice(Keccak txHash) + public (TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) GetReceiptAndEffectiveGasPrice(Keccak txHash) { Keccak blockHash = _receiptFinder.FindBlockHash(txHash); if (blockHash != null) { Block? block = _processingEnv.BlockTree.FindBlock(blockHash, BlockTreeLookupOptions.RequireCanonical); - bool isNotCanonical = block is null; - if (isNotCanonical) + if (block is not null) { - block = _processingEnv.BlockTree.FindBlock(blockHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded); + TxReceipt[] txReceipts = _receiptFinder.Get(block); + TxReceipt txReceipt = txReceipts.ForTransaction(txHash); + int logIndexStart = txReceipts.GetBlockLogFirstIndex(txReceipt.Index); + Transaction tx = block.Transactions[txReceipt.Index]; + bool is1559Enabled = _specProvider.GetSpec(block.Number).IsEip1559Enabled; + UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); + return (txReceipt, effectiveGasPrice, logIndexStart); } - TxReceipt txReceipt = _receiptFinder.Get(block).ForTransaction(txHash); - if (isNotCanonical && !txReceipt.Removed) - { - return (null, null); - } - Transaction tx = block?.Transactions[txReceipt.Index]; - bool is1559Enabled = _specProvider.GetSpec(block.Number).IsEip1559Enabled; - UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); - - return (txReceipt, effectiveGasPrice); } - return (null, null); + return (null, null, 0); } public (TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash) diff --git a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs index 31147c67786..da8fbb618fd 100644 --- a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs @@ -33,7 +33,7 @@ public interface IBlockchainBridge : ILogFinder void RecoverTxSenders(Block block); void RecoverTxSender(Transaction tx); TxReceipt GetReceipt(Keccak txHash); - (TxReceipt Receipt, UInt256? EffectiveGasPrice) GetReceiptAndEffectiveGasPrice(Keccak txHash); + (TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) GetReceiptAndEffectiveGasPrice(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); diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs index 133970364dc..43395c81362 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs @@ -593,7 +593,7 @@ public async Task Eth_get_transaction_receipt() .WithLogs(entries).TestObject; TxReceipt[] receiptsTab = {receipt}; - blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt, UInt256.One)); + blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt, UInt256.One, 0)); blockFinder.FindBlock(Arg.Any()).Returns(block); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); receiptFinder.Get(Arg.Any()).Returns(receiptsTab); @@ -656,7 +656,7 @@ public async Task Eth_get_transaction_receipt_when_block_has_few_receipts() Logs = logEntries }; - blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt2, UInt256.One)); + blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt2, UInt256.One, 2)); TxReceipt[] receipts = {receipt1, receipt2}; @@ -712,7 +712,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)); + blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any()).Returns((receipt, 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/Modules/Eth/EthRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs index 6642da1f9b3..f8fb5f0bebe 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs @@ -498,19 +498,14 @@ public ResultWrapper eth_getTransactionByBlockNumberAndIndex( public Task> eth_getTransactionReceipt(Keccak txHash) { - (TxReceipt Receipt, UInt256? EffectiveGasPrice) result = _blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash); - if (result.Receipt == null) + (TxReceipt receipt, UInt256? effectiveGasPrice, int logIndexStart) = _blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash); + if (receipt == null) { return Task.FromResult(ResultWrapper.Success(null)); } - - Keccak blockHash = result.Receipt.BlockHash; - TxReceipt[] receipts = _receiptFinder.Get(blockHash!); - int logIndexStart = receipts.GetBlockLogFirstIndex(result.Receipt.Index); - ReceiptForRpc receiptModel = new(txHash, result.Receipt, result.EffectiveGasPrice, logIndexStart); - + if (_logger.IsTrace) _logger.Trace($"eth_getTransactionReceipt request {txHash}, result: {txHash}"); - return Task.FromResult(ResultWrapper.Success(receiptModel)); + return Task.FromResult(ResultWrapper.Success(new(txHash, receipt, effectiveGasPrice, logIndexStart))); } public ResultWrapper eth_getUncleByBlockHashAndIndex(Keccak blockHash, UInt256 positionIndex)