Skip to content

Commit

Permalink
Calculate receipt blooms in parallel (#7473)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Sep 22, 2024
1 parent d6f21fc commit 33f00ca
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
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
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

0 comments on commit 33f00ca

Please sign in to comment.