Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/exit on blocknumber #6428

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Api/IInitConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public interface IInitConfig : IConfig

[ConfigItem(Description = "[TECHNICAL] Disable setting malloc options. Set to true if using different memory allocator or manually setting malloc opts.", DefaultValue = "false", HiddenFromDocs = true)]
bool DisableMallocOpts { get; set; }

[ConfigItem(Description = "[TECHNICAL] Exit when block number is reached. Useful for scripting and testing.", DefaultValue = "null", HiddenFromDocs = true)]
long? ExitOnBlockNumber { get; set; }
}

public enum DiagnosticMode
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Api/InitConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class InitConfig : IInitConfig
public long? MemoryHint { get; set; }
public bool DisableGcOnNewPayload { get; set; } = true;
public bool DisableMallocOpts { get; set; } = false;
public long? ExitOnBlockNumber { get; set; } = null;

[Obsolete("Use DiagnosticMode with MemDb instead")]
public bool UseMemDb
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Test.Builders;
using Nethermind.Logging;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Blockchain.Test;

public class ExitOnBlocknumberHandlerTests
{
[TestCase(10, false)]
[TestCase(99, false)]
[TestCase(100, true)]
[TestCase(101, true)]
public void Will_Exit_When_BlockReached(long blockNumber, bool exitReceived)
{
IBlockTree blockTree = Substitute.For<IBlockTree>();
IProcessExitSource processExitSource = Substitute.For<IProcessExitSource>();
new ExitOnBlockNumberHandler(blockTree, processExitSource, 100, LimboLogs.Instance);

blockTree.BlockAddedToMain += Raise.EventWith(
new BlockReplacementEventArgs(Build.A.Block.WithNumber(blockNumber).TestObject));

if (exitReceived)
{
processExitSource.Received().Exit(0);
}
else
{
processExitSource.DidNotReceive().Exit(0);
}
}
}
31 changes: 31 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/ExitOnBlockNumberHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Config;
using Nethermind.Logging;

namespace Nethermind.Blockchain;

/// <summary>
/// Just call exit if blocktree report added block is more than a specific number.
/// </summary>
public class ExitOnBlockNumberHandler
{
public ExitOnBlockNumberHandler(
IBlockTree blockTree,
IProcessExitSource processExitSource,
long initConfigExitOnBlockNumber,
ILogManager logManager)
{
ILogger logger = logManager.GetClassLogger();

blockTree.BlockAddedToMain += (sender, args) =>
{
if (args.Block.Number >= initConfigExitOnBlockNumber)
{
logger.Info($"Block {args.Block.Number} reached. Exiting.");
processExitSource.Exit(0);
}
};
}
}
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Init/Steps/InitializeBlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public Task Execute(CancellationToken cancellationToken)

_set.LogFinder = logFinder;

if (initConfig.ExitOnBlockNumber != null)
{
new ExitOnBlockNumberHandler(blockTree, _get.ProcessExit!, initConfig.ExitOnBlockNumber.Value, _get.LogManager);
}

return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Nethermind.Synchronization.Test.FastSync
[TestFixture(1, 100)]
[TestFixture(4, 0)]
[TestFixture(4, 100)]
[Parallelizable(ParallelScope.Children)]
[Parallelizable(ParallelScope.All)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make test run a tiny bit faster.

public class StateSyncFeedTests : StateSyncFeedTestsBase
{
// Useful for set and forget run. But this test is taking a long time to have it set to other than 1.
Expand Down