Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions src/Nethermind/Nethermind.Core/BlockExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Int256;

Expand Down Expand Up @@ -46,4 +47,18 @@ bool ParentBeforeTTD()
/// <seealso cref="https://github.com/ethereum/EIPs/blob/d896145678bd65d3eafd8749690c1b5228875c39/EIPS/eip-3675.md#specification"/>
/// </remarks>
public static bool IsTerminalBlock(this Block block, ISpecProvider specProvider) => block.Header.IsTerminalBlock(specProvider);

public static int GetTransactionIndex(this Block block, in ValueHash256 txHash)
{
Transaction[] blockTransactions = block.Transactions;
for (int index = 0; index < blockTransactions.Length; index++)
{
if (blockTransactions[index].Hash == txHash)
{
return index;
}
}

return -1;
}
}
35 changes: 22 additions & 13 deletions src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,28 @@ public void get_transaction_returns_null_when_block_not_found()
public void get_transaction_returns_receipt_and_transaction_when_found()
{
int index = 5;
var receipt = Build.A.Receipt
.WithBlockHash(TestItem.KeccakB)
.WithTransactionHash(TestItem.KeccakA)
.WithIndex(index)
.TestObject;
IEnumerable<Transaction> transactions = Enumerable.Range(0, 10)
.Select(static i => Build.A.Transaction.WithNonce((UInt256)i).TestObject);
var block = Build.A.Block
Transaction[] transactions = Enumerable.Range(0, 10)
.Select(static i => Build.A.Transaction.WithNonce((UInt256)i).WithHash(TestItem.Keccaks[i]).TestObject)
.ToArray();
Block block = Build.A.Block
.WithTransactions(transactions.ToArray())
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

[nitpick] The transactions variable is already an array, so the ToArray() call is redundant. Consider passing transactions directly.

Suggested change
.WithTransactions(transactions.ToArray())
.WithTransactions(transactions)

Copilot uses AI. Check for mistakes.
.TestObject;
TxReceipt[] receipts = block.Transactions.Select((t, i) => Build.A.Receipt
.WithBlockHash(TestItem.KeccakB)
.WithIndex(i)
.WithTransactionHash(t.Hash)
.TestObject
).ToArray();
;
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

[nitpick] Stray semicolon on its own line; it can be removed to improve clarity.

Suggested change
;

Copilot uses AI. Check for mistakes.
_blockTree.FindBlock(TestItem.KeccakB, Arg.Any<BlockTreeLookupOptions>()).Returns(block);
_receiptStorage.FindBlockHash(TestItem.KeccakA).Returns(TestItem.KeccakB);
_receiptStorage.Get(block).Returns(new[] { receipt });
_blockchainBridge.GetTransaction(TestItem.KeccakA).Should()
.BeEquivalentTo((receipt, Build.A.Transaction.WithNonce((UInt256)index).TestObject, UInt256.Zero));
foreach (TxReceipt receipt in receipts)
{
_receiptStorage.FindBlockHash(receipt.TxHash!).Returns(TestItem.KeccakB);
}
_receiptStorage.Get(block).Returns(receipts);
var expectation = (receipts[index], Build.A.Transaction.WithNonce((UInt256)index).WithHash(TestItem.Keccaks[index]).TestObject, UInt256.Zero);
var result = _blockchainBridge.GetTransaction(transactions[index].Hash!);
result.Should().BeEquivalentTo(expectation);
}

[Test]
Expand Down Expand Up @@ -193,10 +200,12 @@ public void GetReceiptAndGasInfo_returns_correct_results(bool isCanonical, bool
.WithType(TxType.Blob)
.WithMaxFeePerBlobGas(2)
.WithBlobVersionedHashes(2)
.WithHash(txHash)
.TestObject
: Build.A.Transaction
.WithGasPrice(effectiveGasPrice)
.WithMaxFeePerGas(effectiveGasPrice)
.WithHash(txHash)
.TestObject;
Block block = postEip4844
? Build.A.Block
Expand All @@ -218,7 +227,7 @@ public void GetReceiptAndGasInfo_returns_correct_results(bool isCanonical, bool
_blockTree.FindBlock(blockHash, Arg.Is(BlockTreeLookupOptions.RequireCanonical)).Returns(isCanonical ? block : null);
_blockTree.FindBlock(blockHash, Arg.Is(BlockTreeLookupOptions.TotalDifficultyNotNeeded)).Returns(block);
_receiptStorage.FindBlockHash(txHash).Returns(blockHash);
_receiptStorage.Get(block).Returns(new[] { receipt });
_receiptStorage.Get(block).Returns([receipt]);

(TxReceipt? Receipt, TxGasInfo? GasInfo, int LogIndexStart) result = postEip4844
? (receipt, new(effectiveGasPrice, 1, 262144), 0)
Expand Down
10 changes: 7 additions & 3 deletions src/Nethermind/Nethermind.Facade/BlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ private bool TryGetCanonicalTransaction(
if (block is not null)
{
receipts = receiptStorage.Get(block);
receipt = receipts.ForTransaction(txHash);
transaction = block.Transactions[receipt.Index];
return true;
int txIndex = block.GetTransactionIndex(txHash.ValueHash256);
if (txIndex != -1)
{
transaction = block.Transactions[txIndex];
receipt = receipts.Length > txIndex && receipts[txIndex].TxHash == txHash ? receipts[txIndex] : null;
return true;
}
}
}

Expand Down