Skip to content

Commit

Permalink
Feature/exit on blocknumber (#6428)
Browse files Browse the repository at this point in the history
* Exit on block number

* Some minor log
  • Loading branch information
asdacap committed Dec 28, 2023
1 parent 2ad33cc commit be00812
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
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)]
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

0 comments on commit be00812

Please sign in to comment.