-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/Blocktree suggest pacer (#7648)
- Loading branch information
Showing
5 changed files
with
143 additions
and
61 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/Nethermind/Nethermind.Blockchain.Test/BlockTreeSuggestPacerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Test.Builders; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Blockchain.Test; | ||
|
||
public class BlockTreeSuggestPacerTests | ||
{ | ||
[Test] | ||
public void WillNotBlockIfInBatchLimit() | ||
{ | ||
IBlockTree blockTree = Substitute.For<IBlockTree>(); | ||
blockTree.Head.Returns(Build.A.Block.WithNumber(0).TestObject); | ||
using BlockTreeSuggestPacer pacer = new BlockTreeSuggestPacer(blockTree, 10, 5); | ||
|
||
pacer.WaitForQueue(1, default).IsCompleted.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void WillBlockIfBatchTooLarge() | ||
{ | ||
IBlockTree blockTree = Substitute.For<IBlockTree>(); | ||
blockTree.Head.Returns(Build.A.Block.WithNumber(0).TestObject); | ||
using BlockTreeSuggestPacer pacer = new BlockTreeSuggestPacer(blockTree, 10, 5); | ||
|
||
pacer.WaitForQueue(11, default).IsCompleted.Should().BeFalse(); | ||
} | ||
|
||
[Test] | ||
public void WillOnlyUnblockOnceHeadReachHighEnough() | ||
{ | ||
IBlockTree blockTree = Substitute.For<IBlockTree>(); | ||
blockTree.Head.Returns(Build.A.Block.WithNumber(0).TestObject); | ||
using BlockTreeSuggestPacer pacer = new BlockTreeSuggestPacer(blockTree, 10, 5); | ||
|
||
Task waitTask = pacer.WaitForQueue(11, default); | ||
waitTask.IsCompleted.Should().BeFalse(); | ||
|
||
blockTree.NewHeadBlock += Raise.EventWith(new BlockEventArgs(Build.A.Block.WithNumber(1).TestObject)); | ||
waitTask.IsCompleted.Should().BeFalse(); | ||
|
||
blockTree.NewHeadBlock += Raise.EventWith(new BlockEventArgs(Build.A.Block.WithNumber(5).TestObject)); | ||
waitTask.IsCompleted.Should().BeFalse(); | ||
|
||
blockTree.NewHeadBlock += Raise.EventWith(new BlockEventArgs(Build.A.Block.WithNumber(6).TestObject)); | ||
waitTask.IsCompleted.Should().BeTrue(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Nethermind/Nethermind.Blockchain/BlockTreeSuggestPacer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Nethermind.Core; | ||
|
||
namespace Nethermind.Blockchain; | ||
|
||
/// <summary> | ||
/// Utility class during bulk loading to prevent processing queue from becoming too large | ||
/// </summary> | ||
public class BlockTreeSuggestPacer : IDisposable | ||
{ | ||
private TaskCompletionSource? _dbBatchProcessed; | ||
private long _blockNumberReachedToUnlock = 0; | ||
private readonly long _stopBatchSize; | ||
private readonly long _resumeBatchSize; | ||
private readonly IBlockTree _blockTree; | ||
|
||
public BlockTreeSuggestPacer(IBlockTree blockTree, long stopBatchSize, long resumeBatchSize) | ||
{ | ||
blockTree.NewHeadBlock += BlockTreeOnNewHeadBlock; | ||
_blockTree = blockTree; | ||
_stopBatchSize = stopBatchSize; | ||
_resumeBatchSize = resumeBatchSize; | ||
} | ||
|
||
private void BlockTreeOnNewHeadBlock(object sender, BlockEventArgs e) | ||
{ | ||
TaskCompletionSource? completionSource = _dbBatchProcessed; | ||
if (completionSource is null) return; | ||
if (e.Block.Number < _blockNumberReachedToUnlock) return; | ||
|
||
_dbBatchProcessed = null; | ||
completionSource.SetResult(); | ||
} | ||
|
||
public async Task WaitForQueue(long currentBlockNumber, CancellationToken token) | ||
{ | ||
long currentHeadNumber = _blockTree.Head?.Number ?? 0; | ||
if (currentBlockNumber - currentHeadNumber > _stopBatchSize && _dbBatchProcessed is null) | ||
{ | ||
_blockNumberReachedToUnlock = currentBlockNumber - _stopBatchSize + _resumeBatchSize; | ||
TaskCompletionSource completionSource = new TaskCompletionSource(); | ||
_dbBatchProcessed = completionSource; | ||
} | ||
|
||
if (_dbBatchProcessed is not null) | ||
{ | ||
await using (token.Register(() => _dbBatchProcessed.TrySetCanceled())) | ||
{ | ||
await _dbBatchProcessed.Task; | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_blockTree.NewHeadBlock -= BlockTreeOnNewHeadBlock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters