Skip to content

Commit

Permalink
Merge branch 'master' into 7746_update_jwt_path
Browse files Browse the repository at this point in the history
  • Loading branch information
ssonthal authored Dec 2, 2024
2 parents 29d40fc + 1d42b4a commit 0cf237c
Show file tree
Hide file tree
Showing 31 changed files with 195 additions and 96 deletions.
32 changes: 32 additions & 0 deletions src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,38 @@ public void Loads_lowest_inserted_header_correctly(long beginIndex, long inserte
Assert.That(loadedTree.LowestInsertedHeader?.Number, Is.EqualTo(expectedResult), "loaded tree");
}

[TestCase(5, 10)]
[TestCase(10, 10)]
[TestCase(12, 0)]
public void Does_not_load_bestKnownNumber_before_syncPivot(long syncPivot, long expectedBestKnownNumber)
{
SyncConfig syncConfig = new()
{
FastSync = true,
PivotNumber = $"{syncPivot}"
};

MemDb blockInfosDb = new MemDb();
MemDb headersDb = new MemDb();
MemDb blockDb = new MemDb();

_ = Build.A.BlockTree()
.WithHeadersDb(headersDb)
.WithBlockInfoDb(blockInfosDb)
.WithBlocksDb(blockDb)
.OfChainLength(11)
.TestObject;

BlockTree tree = Build.A.BlockTree()
.WithSyncConfig(syncConfig)
.WithHeadersDb(headersDb)
.WithBlockInfoDb(blockInfosDb)
.WithBlocksDb(blockDb)
.TestObject;

Assert.That(tree.BestKnownNumber, Is.EqualTo(expectedBestKnownNumber));
}

private static readonly object[] SourceOfBSearchTestCases =
{
new object[] {1L, 0L},
Expand Down
29 changes: 7 additions & 22 deletions src/Nethermind/Nethermind.Blockchain/BlockTree.Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,15 @@ private void LoadBestKnown()

long right = Math.Max(0, left) + BestKnownSearchLimit;

long bestKnownNumberFound =
BinarySearchBlockNumber(1, left, LevelExists) ?? 0;
long bestKnownNumberAlternative =
BinarySearchBlockNumber(left, right, LevelExists) ?? 0;

long bestSuggestedHeaderNumber =
BinarySearchBlockNumber(1, left, HeaderExists) ?? 0;
long bestSuggestedHeaderNumberAlternative
= BinarySearchBlockNumber(left, right, HeaderExists) ?? 0;

long bestSuggestedBodyNumber
= BinarySearchBlockNumber(1, left, BodyExists) ?? 0;
long bestSuggestedBodyNumberAlternative
= BinarySearchBlockNumber(left, right, BodyExists) ?? 0;
long bestKnownNumberFound = BinarySearchBlockNumber(left, right, LevelExists) ?? 0;
long bestSuggestedHeaderNumber = BinarySearchBlockNumber(left, right, HeaderExists) ?? 0;
long bestSuggestedBodyNumber = BinarySearchBlockNumber(left, right, BodyExists) ?? 0;

if (_logger.IsInfo)
_logger.Info("Numbers resolved, " +
$"level = Max({bestKnownNumberFound}, {bestKnownNumberAlternative}), " +
$"header = Max({bestSuggestedHeaderNumber}, {bestSuggestedHeaderNumberAlternative}), " +
$"body = Max({bestSuggestedBodyNumber}, {bestSuggestedBodyNumberAlternative})");

bestKnownNumberFound = Math.Max(bestKnownNumberFound, bestKnownNumberAlternative);
bestSuggestedHeaderNumber = Math.Max(bestSuggestedHeaderNumber, bestSuggestedHeaderNumberAlternative);
bestSuggestedBodyNumber = Math.Max(bestSuggestedBodyNumber, bestSuggestedBodyNumberAlternative);
$"level = {bestKnownNumberFound}, " +
$"header = {bestSuggestedHeaderNumber}, " +
$"body = {bestSuggestedBodyNumber}");

if (bestKnownNumberFound < 0 ||
bestSuggestedHeaderNumber < 0 ||
Expand All @@ -187,7 +172,7 @@ long bestSuggestedBodyNumberAlternative
}
}

BestKnownNumber = Math.Max(bestKnownNumberFound, bestKnownNumberAlternative);
BestKnownNumber = bestKnownNumberFound;
BestSuggestedHeader = FindHeader(bestSuggestedHeaderNumber, BlockTreeLookupOptions.None);
BlockHeader? bestSuggestedBodyHeader = FindHeader(bestSuggestedBodyNumber, BlockTreeLookupOptions.None);
BestSuggestedBody = bestSuggestedBodyHeader is null
Expand Down
9 changes: 9 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/BlockTreeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ public static class BlockTreeExtensions
public static ReadOnlyBlockTree AsReadOnly(this IBlockTree blockTree) => new(blockTree);

public static BlockHeader? GetProducedBlockParent(this IBlockTree blockTree, BlockHeader? parentHeader) => parentHeader ?? blockTree.Head?.Header;

public static (bool isSyncing, long headNumber, long bestSuggested) IsSyncing(this IBlockTree blockTree, int maxDistanceForSynced = 0)
{
long bestSuggestedNumber = blockTree.FindBestSuggestedHeader()?.Number ?? 0;
long headNumberOrZero = blockTree.Head?.Number ?? 0;
bool isSyncing = bestSuggestedNumber == 0 || bestSuggestedNumber > headNumberOrZero + maxDistanceForSynced;

return (isSyncing, headNumberOrZero, bestSuggestedNumber);
}
}
}
19 changes: 19 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/ChainHeadInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ namespace Nethermind.Blockchain
{
public class ChainHeadInfoProvider : IChainHeadInfoProvider
{
private readonly IBlockTree _blockTree;
// For testing
public bool HasSynced { private get; init; }

public ChainHeadInfoProvider(ISpecProvider specProvider, IBlockTree blockTree, IStateReader stateReader, ICodeInfoRepository codeInfoRepository)
: this(new ChainHeadSpecProvider(specProvider, blockTree), blockTree, new ChainHeadReadOnlyStateProvider(blockTree, stateReader), codeInfoRepository)
{
Expand All @@ -35,6 +39,7 @@ public ChainHeadInfoProvider(IChainHeadSpecProvider specProvider, IBlockTree blo
CodeInfoRepository = codeInfoRepository;

blockTree.BlockAddedToMain += OnHeadChanged;
_blockTree = blockTree;
}

public IChainHeadSpecProvider SpecProvider { get; }
Expand All @@ -51,6 +56,20 @@ public ChainHeadInfoProvider(IChainHeadSpecProvider specProvider, IBlockTree blo

public UInt256 CurrentFeePerBlobGas { get; internal set; }

public bool IsSyncing
{
get
{
if (HasSynced)
{
return false;
}

(bool isSyncing, _, _) = _blockTree.IsSyncing(maxDistanceForSynced: 2);
return isSyncing;
}
}

public event EventHandler<BlockReplacementEventArgs>? HeadChanged;

private void OnHeadChanged(object? sender, BlockReplacementEventArgs e)
Expand Down
18 changes: 10 additions & 8 deletions src/Nethermind/Nethermind.Consensus.Clique/CliqueBlockProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Timers;
using Nethermind.Blockchain;
Expand Down Expand Up @@ -67,8 +68,7 @@ public CliqueBlockProducerRunner(
_timer.Start();
}

private readonly BlockingCollection<Block> _signalsQueue =
new(new ConcurrentQueue<Block>());
private readonly Channel<Block> _signalsQueue = Channel.CreateUnbounded<Block>();

private Block? _scheduledBlock;

Expand All @@ -91,7 +91,7 @@ public void UncastVote(Address signer)

public void ProduceOnTopOf(Hash256 hash)
{
_signalsQueue.Add(_blockTree.FindBlock(hash, BlockTreeLookupOptions.None));
_signalsQueue.Writer.TryWrite(_blockTree.FindBlock(hash, BlockTreeLookupOptions.None));
}

public IReadOnlyDictionary<Address, bool> GetProposals() => _blockProducer.Proposals.ToDictionary();
Expand All @@ -111,7 +111,7 @@ private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
if (_blockTree.Head.Timestamp + _config.BlockPeriod < _timestamper.UnixTime.Seconds)
{
_signalsQueue.Add(_blockTree.FindBlock(_blockTree.Head.Hash, BlockTreeLookupOptions.None));
_signalsQueue.Writer.TryWrite(_blockTree.FindBlock(_blockTree.Head.Hash, BlockTreeLookupOptions.None));
}

_timer.Enabled = true;
Expand Down Expand Up @@ -216,17 +216,17 @@ private Task RunConsumeSignal()

private void BlockTreeOnNewHeadBlock(object? sender, BlockEventArgs e)
{
_signalsQueue.Add(e.Block);
_signalsQueue.Writer.TryWrite(e.Block);
}

private async Task ConsumeSignal()
{
_lastProducedBlock = DateTime.UtcNow;
foreach (Block signal in _signalsQueue.GetConsumingEnumerable(_cancellationTokenSource.Token))
await foreach (Block signal in _signalsQueue.Reader.ReadAllAsync(_cancellationTokenSource.Token))
{
// TODO: Maybe use IBlockProducer specific to clique?
Block parentBlock = signal;
while (_signalsQueue.TryTake(out Block? nextSignal))
while (_signalsQueue.Reader.TryRead(out Block? nextSignal))
{
if (parentBlock.Number <= nextSignal.Number)
{
Expand Down Expand Up @@ -259,6 +259,7 @@ public async Task StopAsync()
_blockTree.NewHeadBlock -= BlockTreeOnNewHeadBlock;
_cancellationTokenSource?.Cancel();
await (_producerTask ?? Task.CompletedTask);
_signalsQueue.Writer.TryComplete();
}

bool IBlockProducerRunner.IsProducingBlocks(ulong? maxProducingInterval)
Expand All @@ -273,11 +274,12 @@ bool IBlockProducerRunner.IsProducingBlocks(ulong? maxProducingInterval)

public event EventHandler<BlockEventArgs>? BlockProduced;


public void Dispose()
{
_cancellationTokenSource?.Dispose();
_timer?.Dispose();
_signalsQueue.Writer.TryComplete();
BlockProduced = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,8 @@ public CensorshipDetector(

private bool IsSyncing()
{
long bestSuggestedNumber = _blockTree.FindBestSuggestedHeader()?.Number ?? 0;
if (bestSuggestedNumber == 0)
{
return true;
}
long headNumberOrZero = _blockTree.Head?.Number ?? 0;
return bestSuggestedNumber > headNumberOrZero;
(bool isSyncing, _, _) = _blockTree.IsSyncing();
return isSyncing;
}

private void OnBlockProcessing(object? sender, BlockEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected virtual TxPool.TxPool CreateTxPool(CodeInfoRepository codeInfoReposito
new(
EthereumEcdsa,
new BlobTxStorage(),
new ChainHeadInfoProvider(new FixedForkActivationChainHeadSpecProvider(SpecProvider), BlockTree, ReadOnlyState, codeInfoRepository),
new ChainHeadInfoProvider(new FixedForkActivationChainHeadSpecProvider(SpecProvider), BlockTree, ReadOnlyState, codeInfoRepository) { HasSynced = true },
new TxPoolConfig { BlobsSupport = BlobsSupportMode.InMemory },
new TxValidator(SpecProvider.ChainId),
LogManager,
Expand Down
23 changes: 12 additions & 11 deletions src/Nethermind/Nethermind.Core/Caching/ClockCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using System.Runtime.InteropServices;
using Nethermind.Core.Collections;
using Nethermind.Core.Threading;

Expand All @@ -24,7 +24,7 @@ public TValue Get(TKey key)
{
if (MaxCapacity == 0) return default!;

if (_cacheMap.TryGetValue(key, out LruCacheItem? ov))
if (_cacheMap.TryGetValue(key, out LruCacheItem ov))
{
MarkAccessed(ov.Offset);
return ov.Value;
Expand All @@ -37,7 +37,7 @@ public bool TryGet(TKey key, out TValue value)
value = default!;
if (MaxCapacity == 0) return false;

if (_cacheMap.TryGetValue(key, out LruCacheItem? ov))
if (_cacheMap.TryGetValue(key, out LruCacheItem ov))
{
MarkAccessed(ov.Offset);
value = ov.Value;
Expand All @@ -56,9 +56,9 @@ public bool Set(TKey key, TValue val)
return Delete(key);
}

if (_cacheMap.TryGetValue(key, out LruCacheItem? ov))
if (_cacheMap.TryGetValue(key, out LruCacheItem ov))
{
ov.Value = val;
_cacheMap[key] = new(val, ov.Offset);
MarkAccessed(ov.Offset);
return false;
}
Expand All @@ -71,9 +71,9 @@ private bool SetSlow(TKey key, TValue val)
using var lockRelease = _lock.Acquire();

// Recheck under lock
if (_cacheMap.TryGetValue(key, out LruCacheItem? ov))
if (_cacheMap.TryGetValue(key, out LruCacheItem ov))
{
ov.Value = val;
_cacheMap[key] = new(val, ov.Offset);
MarkAccessed(ov.Offset);
return false;
}
Expand All @@ -89,7 +89,7 @@ private bool SetSlow(TKey key, TValue val)
offset = Replace(key);
}

_cacheMap[key] = new LruCacheItem(offset, val);
_cacheMap[key] = new(val, offset);
KeyToOffset[offset] = key;
_count++;
Debug.Assert(_cacheMap.Count == _count);
Expand Down Expand Up @@ -138,7 +138,7 @@ public bool Delete(TKey key)

using var lockRelease = _lock.Acquire();

if (_cacheMap.Remove(key, out LruCacheItem? ov))
if (_cacheMap.Remove(key, out LruCacheItem ov))
{
_count--;
KeyToOffset[ov.Offset] = default;
Expand Down Expand Up @@ -166,9 +166,10 @@ public bool Contains(TKey key)
return _cacheMap.ContainsKey(key);
}

private class LruCacheItem(int offset, TValue v)
[StructLayout(LayoutKind.Auto)]
private readonly struct LruCacheItem(TValue v, int offset)
{
public readonly TValue Value = v;
public readonly int Offset = offset;
public TValue Value = v;
}
}
Loading

0 comments on commit 0cf237c

Please sign in to comment.