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

Calculate receipt blooms in parallel #7473

Merged
merged 2 commits into from
Sep 22, 2024
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
29 changes: 20 additions & 9 deletions src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
Expand All @@ -12,7 +11,6 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
Expand Down Expand Up @@ -282,23 +280,25 @@ protected virtual TxReceipt[] ProcessBlock(
IBlockTracer blockTracer,
ProcessingOptions options)
{
IReleaseSpec spec = _specProvider.GetSpec(block.Header);
BlockHeader header = block.Header;
IReleaseSpec spec = _specProvider.GetSpec(header);

ReceiptsTracer.SetOtherTracer(blockTracer);
ReceiptsTracer.StartNewBlockTrace(block);

StoreBeaconRoot(block, spec);
_blockhashStore.ApplyBlockhashStateChanges(block.Header);
_blockhashStore.ApplyBlockhashStateChanges(header);
_stateProvider.Commit(spec, commitStorageRoots: false);

TxReceipt[] receipts = _blockTransactionsExecutor.ProcessTransactions(block, options, ReceiptsTracer, spec);
CalculateBlooms(receipts);

if (spec.IsEip4844Enabled)
{
block.Header.BlobGasUsed = BlobGasCalculator.CalculateBlobGas(block.Transactions);
header.BlobGasUsed = BlobGasCalculator.CalculateBlobGas(block.Transactions);
}

block.Header.ReceiptsRoot = _receiptsRootCalculator.GetReceiptsRoot(receipts, spec, block.ReceiptsRoot);
header.ReceiptsRoot = _receiptsRootCalculator.GetReceiptsRoot(receipts, spec, block.ReceiptsRoot);
ApplyMinerRewards(block, blockTracer, spec);
_withdrawalProcessor.ProcessWithdrawals(block, spec);
ReceiptsTracer.EndBlockTrace();
Expand All @@ -311,17 +311,28 @@ protected virtual TxReceipt[] ProcessBlock(
block.AccountChanges = _stateProvider.GetAccountChanges();
}

if (ShouldComputeStateRoot(block.Header))
if (ShouldComputeStateRoot(header))
{
_stateProvider.RecalculateStateRoot();
block.Header.StateRoot = _stateProvider.StateRoot;
header.StateRoot = _stateProvider.StateRoot;
}

block.Header.Hash = block.Header.CalculateHash();
header.Hash = header.CalculateHash();

return receipts;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void CalculateBlooms(TxReceipt[] receipts)
{
int index = 0;
Parallel.For(0, receipts.Length, _ =>
{
int i = Interlocked.Increment(ref index) - 1;
receipts[i].CalculateBloom();
});
}

private void StoreBeaconRoot(Block block, IReleaseSpec spec)
{
try
Expand Down
7 changes: 6 additions & 1 deletion src/Nethermind/Nethermind.Core/TransactionReceipt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Nethermind.Core
{
public class TxReceipt
{
private Bloom? _boom;

public TxReceipt()
{
}
Expand Down Expand Up @@ -60,7 +62,7 @@ public TxReceipt(TxReceipt other)
/// Removed in EIP-658
/// </summary>
public Hash256? PostTransactionState { get; set; }
public Bloom? Bloom { get; set; }
public Bloom? Bloom { get => _boom ?? CalculateBloom(); set => _boom = value; }
public LogEntry[]? Logs { get; set; }
public string? Error { get; set; }

Expand All @@ -69,6 +71,9 @@ public TxReceipt(TxReceipt other)
/// Output is either StateRoot or StatusCode depending on eip configuration.
/// </summary>
public bool SkipStateAndStatusInRlp { get; set; }

public Bloom CalculateBloom()
=> _boom = Logs?.Length == 0 ? Bloom.Empty : new Bloom(Logs);
}

public ref struct TxReceiptStructRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected virtual TxReceipt BuildReceipt(Address recipient, long spentGas, byte
{
Logs = logEntries,
TxType = transaction.Type,
Bloom = logEntries.Length == 0 ? Bloom.Empty : new Bloom(logEntries),
// Bloom calculated in parallel with other receipts
Copy link
Member

@LukaszRozmej LukaszRozmej Sep 22, 2024

Choose a reason for hiding this comment

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

Maybe better would be to start a task here, and then await it in BlockProcessor or TransactionsExecutor?

Copy link
Member Author

Choose a reason for hiding this comment

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

🤔

GasUsedTotal = Block.GasUsed,
StatusCode = statusCode,
Recipient = transaction.IsContractCreation ? null : recipient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override TxReceipt BuildReceipt(Address recipient, long spentGas, byte
{
Logs = logEntries,
TxType = transaction.Type,
Bloom = logEntries.Length == 0 ? Bloom.Empty : new Bloom(logEntries),
// Bloom calculated in parallel with other receipts
GasUsedTotal = Block.GasUsed,
StatusCode = statusCode,
Recipient = transaction.IsContractCreation ? null : recipient,
Expand Down