Skip to content

Commit

Permalink
add BlockhashStore
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke26 committed Feb 25, 2025
1 parent c5f07cb commit 7cdace9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Evm.Tracing;
using Nethermind.Int256;
using Nethermind.State;
using Nethermind.State.Tracing;

[assembly: InternalsVisibleTo("Nethermind.Blockchain.Test")]
[assembly: InternalsVisibleTo("Nethermind.Merge.Plugin.Test")]
Expand All @@ -19,7 +21,7 @@ public class BlockhashStore(ISpecProvider specProvider, IWorldState worldState)
{
private static readonly byte[] EmptyBytes = [0];

public void ApplyBlockhashStateChanges(BlockHeader blockHeader)
public void ApplyBlockhashStateChanges(BlockHeader blockHeader, ITxTracer tracer)
{
IReleaseSpec spec = specProvider.GetSpec(blockHeader);
if (!spec.IsEip2935Enabled || blockHeader.IsGenesis || blockHeader.ParentHash is null) return;
Expand All @@ -31,6 +33,10 @@ public void ApplyBlockhashStateChanges(BlockHeader blockHeader)
var parentBlockIndex = new UInt256((ulong)((blockHeader.Number - 1) % Eip2935Constants.RingBufferSize));
StorageCell blockHashStoreCell = new(eip2935Account, parentBlockIndex);
worldState.Set(blockHashStoreCell, parentBlockHash!.Bytes.WithoutLeadingZeros().ToArray());
if (tracer.IsTracingOpLevelStorage)
{
tracer.SetOperationStorage(blockHashStoreCell.Address, blockHashStoreCell.Index, parentBlockHash!.Bytes.WithoutLeadingZeros().ToArray(), []);
}
}

public Hash256? GetBlockHashFromState(BlockHeader currentHeader, long requiredBlockNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Evm.Tracing;

namespace Nethermind.Blockchain.Blocks;

public interface IBlockhashStore
{
public void ApplyBlockhashStateChanges(BlockHeader blockHeader);
public void ApplyBlockhashStateChanges(BlockHeader blockHeader, ITxTracer tracer);
public Hash256? GetBlockHashFromState(BlockHeader currentBlockHeader, long requiredBlockNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ protected virtual TxReceipt[] ProcessBlock(
ReceiptsTracer.StartNewBlockTrace(block);

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

TxReceipt[] receipts = _blockTransactionsExecutor.ProcessTransactions(block, options, ReceiptsTracer, spec);
Expand Down
5 changes: 3 additions & 2 deletions tools/Evm/Evm/T8n/JsonTypes/T8nExecutionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static T8nExecutionResult ConstructT8nExecutionResult(WorldState statePro
private static Dictionary<Address, AccountState> CollectAccounts(T8nTest test, WorldState stateProvider,
StorageTxTracer storageTracer, Block block, List<ProofTxTracer> proofTxTracerList)
{
var addresses = CollectAccountAddresses(test, block, proofTxTracerList);
var addresses = CollectAccountAddresses(test, block, proofTxTracerList, storageTracer);
Dictionary<Address, AccountState> accounts = new();
foreach (var address in addresses)
{
Expand All @@ -93,7 +93,7 @@ private static Dictionary<Address, AccountState> CollectAccounts(T8nTest test, W
return accounts;
}

private static HashSet<Address> CollectAccountAddresses(T8nTest test, Block block, List<ProofTxTracer> proofTxTracerList)
private static HashSet<Address> CollectAccountAddresses(T8nTest test, Block block, List<ProofTxTracer> proofTxTracerList, StorageTxTracer storageTracer)
{
HashSet<Address> addresses = [];
addresses.AddRange(test.Alloc.Keys);
Expand All @@ -106,6 +106,7 @@ private static HashSet<Address> CollectAccountAddresses(T8nTest test, Block bloc
if (tx.To is not null) addresses.Add(tx.To);
}
addresses.AddRange(proofTxTracerList.SelectMany(tracer => tracer.Accounts));
addresses.AddRange(storageTracer.Storages.Keys);

return addresses;
}
Expand Down
10 changes: 5 additions & 5 deletions tools/Evm/Evm/T8n/StorageTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ namespace Evm.T8n;

public class StorageTxTracer : TxTracer, IBlockTracer
{
private readonly Dictionary<Address, List<UInt256>> _storages = new();
public readonly Dictionary<Address, List<UInt256>> Storages = new();
public bool IsTracingRewards => false;
public override bool IsTracingOpLevelStorage => true;

public override void SetOperationStorage(Address address, UInt256 storageIndex, ReadOnlySpan<byte> newValue,
ReadOnlySpan<byte> currentValue)
{
if (!_storages.TryGetValue(address, out _))
if (!Storages.TryGetValue(address, out _))
{
_storages[address] = [];
Storages[address] = [];
}

_storages[address].Add(storageIndex);
Storages[address].Add(storageIndex);
}

public List<UInt256> GetStorageKeys(Address address)
{
_storages.TryGetValue(address, out List<UInt256>? storage);
Storages.TryGetValue(address, out List<UInt256>? storage);
return storage ?? [];
}

Expand Down
2 changes: 2 additions & 0 deletions tools/Evm/Evm/T8n/T8nExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Evm.T8n.Errors;
using Evm.T8n.JsonTypes;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Consensus.ExecutionRequests;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
Expand Down Expand Up @@ -77,6 +78,7 @@ public static T8nExecutionResult Execute(T8nCommandArguments arguments)
blockReceiptsTracer.SetOtherTracer(compositeBlockTracer);
blockReceiptsTracer.StartNewBlockTrace(block);

new BlockhashStore(test.SpecProvider, stateProvider).ApplyBlockhashStateChanges(block.Header, storageTxTracer);
BeaconBlockRootHandler beaconBlockRootHandler = new(transactionProcessor, stateProvider);
if (block.ParentBeaconBlockRoot is not null)
{
Expand Down

0 comments on commit 7cdace9

Please sign in to comment.