-
Notifications
You must be signed in to change notification settings - Fork 723
Expand file tree
/
Copy pathBlockhashStore.cs
More file actions
60 lines (50 loc) · 2.56 KB
/
Copy pathBlockhashStore.cs
File metadata and controls
60 lines (50 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-FileCopyrightText:2023 Demerzel Solutions Limited
// SPDX-License-Identifier:LGPL-3.0-only
using System;
using System.Runtime.CompilerServices;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Eip2930;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Evm.State;
using Nethermind.Int256;
[assembly: InternalsVisibleTo("Nethermind.Blockchain.Test")]
[assembly: InternalsVisibleTo("Nethermind.Merge.Plugin.Test")]
namespace Nethermind.Blockchain.Blocks;
public class BlockhashStore(IWorldState worldState) : IBlockhashStore, IHasAccessList
{
private static readonly byte[] EmptyBytes = [0];
public void ApplyBlockhashStateChanges(BlockHeader blockHeader, IReleaseSpec spec)
{
if (!TryGetParentHashCell(blockHeader, spec, out StorageCell blockHashStoreCell)) return;
worldState.Set(blockHashStoreCell, blockHeader.ParentHash!.Bytes.WithoutLeadingZeros().ToArray());
worldState.RecordBytecodeAccess(blockHashStoreCell.Address);
}
public AccessList? GetAccessList(Block block, IReleaseSpec spec) =>
TryGetParentHashCell(block.Header, spec, out StorageCell blockHashStoreCell)
? AccessList.ForSingleStorageCell(in blockHashStoreCell)
: null;
private bool TryGetParentHashCell(BlockHeader header, IReleaseSpec spec, out StorageCell blockHashStoreCell)
{
blockHashStoreCell = default;
if (!spec.IsEip2935Enabled || header.IsGenesis || header.ParentHash is null) return false;
Address eip2935Account = spec.Eip2935ContractAddress ?? Eip2935Constants.BlockHashHistoryAddress;
if (!worldState.IsContract(eip2935Account)) return false;
blockHashStoreCell = new StorageCell(eip2935Account, new UInt256((ulong)(header.Number - 1) % spec.Eip2935RingBufferSize));
return true;
}
public Hash256? GetBlockHashFromState(BlockHeader currentHeader, ulong requiredBlockNumber, IReleaseSpec spec)
{
if (requiredBlockNumber >= currentHeader.Number ||
requiredBlockNumber + spec.Eip2935RingBufferSize < currentHeader.Number)
{
return null;
}
UInt256 blockIndex = new(requiredBlockNumber % spec.Eip2935RingBufferSize);
Address? eip2935Account = spec.Eip2935ContractAddress ?? Eip2935Constants.BlockHashHistoryAddress;
StorageCell blockHashStoreCell = new(eip2935Account, blockIndex);
ReadOnlySpan<byte> data = worldState.Get(blockHashStoreCell);
return data.SequenceEqual(EmptyBytes) ? null : Hash256.FromBytesWithPadding(data);
}
}