-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathBlockhashStore.cs
48 lines (42 loc) · 2.1 KB
/
BlockhashStore.cs
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
// 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.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Int256;
using Nethermind.State;
[assembly: InternalsVisibleTo("Nethermind.Blockchain.Test")]
[assembly: InternalsVisibleTo("Nethermind.Merge.Plugin.Test")]
namespace Nethermind.Blockchain.Blocks;
public class BlockhashStore(ISpecProvider specProvider, IWorldState worldState)
: IBlockhashStore
{
public void ApplyBlockhashStateChanges(BlockHeader blockHeader)
{
IReleaseSpec spec = specProvider.GetSpec(blockHeader);
if (!spec.IsEip2935Enabled || blockHeader.IsGenesis || blockHeader.ParentHash is null) return;
Address? eip2935Account = spec.Eip2935ContractAddress ?? Eip2935Constants.BlockHashHistoryAddress;
if (!worldState.IsContract(eip2935Account)) return;
Hash256 parentBlockHash = blockHeader.ParentHash;
var parentBlockIndex = new UInt256((ulong)((blockHeader.Number - 1) % Eip2935Constants.RingBufferSize));
StorageCell blockHashStoreCell = new(eip2935Account, parentBlockIndex);
worldState.Set(blockHashStoreCell, new StorageValue(parentBlockHash!.Bytes));
}
public Hash256? GetBlockHashFromState(BlockHeader currentHeader, long requiredBlockNumber)
{
IReleaseSpec? spec = specProvider.GetSpec(currentHeader);
if (requiredBlockNumber >= currentHeader.Number ||
requiredBlockNumber + Eip2935Constants.RingBufferSize < currentHeader.Number)
{
return null;
}
var blockIndex = new UInt256((ulong)(requiredBlockNumber % Eip2935Constants.RingBufferSize));
Address? eip2935Account = spec.Eip2935ContractAddress ?? Eip2935Constants.BlockHashHistoryAddress;
StorageCell blockHashStoreCell = new(eip2935Account, blockIndex);
ref readonly StorageValue data = ref worldState.Get(blockHashStoreCell);
return data.IsZero ? null : Hash256.FromBytesWithPadding(data.Bytes);
}
}