Skip to content

Commit a54897f

Browse files
committed
Cache
1 parent d8fc589 commit a54897f

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: LGPL-3.0-only
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
67
using System.Linq;
78
using System.Security;
@@ -67,6 +68,8 @@ public partial class EthRpcModule(
6768
IForkInfo forkInfo,
6869
ulong? secondsPerSlot) : IEthRpcModule
6970
{
71+
private static readonly ConcurrentDictionary<ForkId, ForkConfig> forkConfigCache = [];
72+
7073
protected readonly Encoding _messageEncoding = Encoding.UTF8;
7174
protected readonly IJsonRpcConfig _rpcConfig = rpcConfig ?? throw new ArgumentNullException(nameof(rpcConfig));
7275
protected readonly IBlockchainBridge _blockchainBridge = blockchainBridge ?? throw new ArgumentNullException(nameof(blockchainBridge));
@@ -805,19 +808,17 @@ private ResultWrapper<TResult> GetStateFailureResult<TResult>(BlockHeader header
805808
public ResultWrapper<JsonNode> eth_config(bool showAllForks = false)
806809
{
807810
ForkActivationsSummary forks = forkInfo.GetForkActivationsSummary(_blockFinder.Head?.Header);
808-
IReadOnlyList<ForkConfig>? allForkConfigs = null;
811+
List<ForkConfig>? allForkConfigs = null;
809812

810813
if (showAllForks)
811814
{
812-
Fork[] forkSchedule = forkInfo.GetAllForks();
813-
List<ForkConfig> forkConfigs = new(forkSchedule.Length);
815+
ReadOnlySpan<Fork> forkSchedule = forkInfo.GetAllForks();
816+
allForkConfigs = new(forkSchedule.Length);
814817

815818
foreach (Fork scheduledFork in forkSchedule)
816819
{
817-
forkConfigs.Add(BuildForkConfig(scheduledFork, _specProvider));
820+
allForkConfigs.Add(BuildForkConfig(scheduledFork, _specProvider));
818821
}
819-
820-
allForkConfigs = forkConfigs;
821822
}
822823

823824
return ResultWrapper<JsonNode>.Success(JsonNode.Parse(JsonSerializer.Serialize(new ForkConfigSummary
@@ -833,9 +834,14 @@ public ResultWrapper<JsonNode> eth_config(bool showAllForks = false)
833834

834835
static ForkConfig BuildForkConfig(Fork fork, ISpecProvider specProvider)
835836
{
837+
if (forkConfigCache.TryGetValue(fork.Id, out ForkConfig config))
838+
{
839+
return config;
840+
}
841+
836842
IReleaseSpec spec = specProvider.GetSpec(fork.Activation.BlockNumber, fork.Activation.Timestamp);
837843

838-
return new ForkConfig
844+
return forkConfigCache[fork.Id] = new ForkConfig
839845
{
840846
ActivationTime = fork.Activation.Timestamp is not null ? (int)fork.Activation.Timestamp : null,
841847
ActivationBlock = fork.Activation.Timestamp is null ? (int)fork.Activation.BlockNumber : null,

src/Nethermind/Nethermind.Network/ForkInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public ForkActivationsSummary GetForkActivationsSummary(BlockHeader? head)
177177
};
178178
}
179179

180-
public Fork[] GetAllForks()
180+
public ReadOnlySpan<Fork> GetAllForks()
181181
{
182182
EnsureInitialized();
183183
return Forks;

src/Nethermind/Nethermind.Network/IForkInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Nethermind.Core;
55
using Nethermind.Core.Specs;
6+
using System;
67

78
namespace Nethermind.Network;
89

@@ -20,7 +21,7 @@ public interface IForkInfo
2021

2122
ForkActivationsSummary GetForkActivationsSummary(BlockHeader? head);
2223

23-
Fork[] GetAllForks();
24+
ReadOnlySpan<Fork> GetAllForks();
2425
}
2526

2627
public readonly record struct Fork(ForkActivation Activation, ForkId Id);

0 commit comments

Comments
 (0)