Skip to content

Commit

Permalink
Fix old blocks downloaded beyond old barrier (#6059)
Browse files Browse the repository at this point in the history
* Fix old blocks downloaded beyond old barrier

* Fix comment
  • Loading branch information
asdacap authored Aug 31, 2023
1 parent f4acb7a commit 3c42dd2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Test.Builders;
using Nethermind.Synchronization.FastBlocks;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Synchronization.Test.FastBlocks
Expand Down Expand Up @@ -32,14 +38,26 @@ public void Can_read_back_all_set_values()
{
const int length = 4096;


FastBlockStatusList list = CreateFastBlockStatusList(length, false);
for (int i = 0; i < length; i++)
{
Assert.IsTrue((FastBlockStatus)(i % 3) == list[i]);
}
}

[Test]
public void Will_not_go_below_ancient_barrier()
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
blockTree.FindCanonicalBlockInfo(Arg.Any<long>()).Returns(new BlockInfo(TestItem.KeccakA, 0));
SyncStatusList syncStatusList = new SyncStatusList(blockTree, 1000, null, 900);

BlockInfo?[] infos = new BlockInfo?[500];
syncStatusList.GetInfosForBatch(infos);

infos.Count((it) => it != null).Should().Be(101);
}

[Test]
public void Can_read_back_all_parallel_set_values()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ private void ResetSyncStatusList()
_syncStatusList = new SyncStatusList(
_blockTree,
_pivotNumber,
_blockTree.LowestInsertedBodyNumber);
_blockTree.LowestInsertedBodyNumber,
_syncConfig.AncientBodiesBarrier);
}

protected override SyncMode ActivationSyncModes { get; } = SyncMode.FastBodies & ~SyncMode.FastBlocks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ private void ResetSyncStatusList()
_syncStatusList = new SyncStatusList(
_blockTree,
_pivotNumber,
_receiptStorage.LowestInsertedReceiptBlockNumber);
_receiptStorage.LowestInsertedReceiptBlockNumber,
_syncConfig.AncientReceiptsBarrier);
}

protected override SyncMode ActivationSyncModes { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class SyncStatusList
private readonly FastBlockStatusList _statuses;
private readonly LruCache<long, BlockInfo> _cache = new(maxCapacity: 64, startCapacity: 64, "blockInfo Cache");
private long _lowestInsertWithoutGaps;
private readonly long _lowerBound;

public long LowestInsertWithoutGaps
{
Expand All @@ -25,19 +26,20 @@ public long LowestInsertWithoutGaps

public long QueueSize => _queueSize;

public SyncStatusList(IBlockTree blockTree, long pivotNumber, long? lowestInserted)
public SyncStatusList(IBlockTree blockTree, long pivotNumber, long? lowestInserted, long lowerBound)
{
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
_statuses = new FastBlockStatusList(pivotNumber + 1);

LowestInsertWithoutGaps = lowestInserted ?? pivotNumber;
_lowerBound = lowerBound;
}

public void GetInfosForBatch(BlockInfo?[] blockInfos)
{
int collected = 0;
long currentNumber = Volatile.Read(ref _lowestInsertWithoutGaps);
while (collected < blockInfos.Length && currentNumber != 0)
while (collected < blockInfos.Length && currentNumber != 0 && currentNumber >= _lowerBound)
{
if (blockInfos[collected] is not null)
{
Expand Down

0 comments on commit 3c42dd2

Please sign in to comment.