Skip to content

Commit

Permalink
Add has state
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap committed Dec 31, 2024
1 parent 73f913d commit 2fd5031
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public virtual async Task Execute(CancellationToken cancellationToken)
_api.PeerPool,
_api.StaticNodesManager,
_api.BlockingVerifyTrie!,
_api.WorldStateManager.GlobalStateReader,
_api.Enode,
initConfig.BaseDbPath,
pruningTrigger,
Expand Down
16 changes: 14 additions & 2 deletions src/Nethermind/Nethermind.JsonRpc.Test/Modules/AdminModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Nethermind.Network.Config;
using Nethermind.Serialization.Json;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.State;
using Nethermind.Stats.Model;
using Nethermind.Synchronization.FastSync;
using NSubstitute;
Expand All @@ -34,6 +35,7 @@ public class AdminModuleTests
private NetworkConfig _networkConfig = null!;
private IBlockTree _blockTree = null!;
private IBlockingVerifyTrie _blockingVerifyTrie = null!;
private IStateReader _stateReader = null!;
private const string _enodeString = "enode://e1b7e0dc09aae610c9dec8a0bee62bab9946cc27ebdd2f9e3571ed6d444628f99e91e43f4a14d42d498217608bb3e1d1bc8ec2aa27d7f7e423413b851bae02bc@127.0.0.1:30303";
private const string _exampleDataDir = "/example/dbdir";

Expand All @@ -42,6 +44,7 @@ public void Setup()
{
_blockTree = Build.A.BlockTree().OfChainLength(5).TestObject;
_blockingVerifyTrie = Substitute.For<IBlockingVerifyTrie>();
_stateReader = Substitute.For<IStateReader>();
_networkConfig = new NetworkConfig();
IPeerPool peerPool = Substitute.For<IPeerPool>();
ConcurrentDictionary<PublicKeyAsKey, Peer> dict = new();
Expand All @@ -61,6 +64,7 @@ public void Setup()
peerPool,
staticNodesManager,
_blockingVerifyTrie,
_stateReader,
enode,
_exampleDataDir,
new ManualPruningTrigger(),
Expand Down Expand Up @@ -117,9 +121,17 @@ public async Task Test_admin_dataDir()
[Test]
public async Task Test_admin_verifyTrie()
{
(await RpcTest.TestSerializedRequest(_adminRpcModule, "admin_verifyTrie")).Should().Contain("Unable to start verify trie");
(await RpcTest.TestSerializedRequest(_adminRpcModule, "admin_verifyTrie", "latest")).Should().Contain("Unable to start verify trie");
_blockingVerifyTrie.TryStartVerifyTrie(Arg.Any<BlockHeader>()).Returns(true);
(await RpcTest.TestSerializedRequest(_adminRpcModule, "admin_verifyTrie")).Should().Contain("Starting");
(await RpcTest.TestSerializedRequest(_adminRpcModule, "admin_verifyTrie", "latest")).Should().Contain("Starting");
}

[Test]
public async Task Test_hasStateForBlock()
{
(await RpcTest.TestSerializedRequest(_adminRpcModule, "admin_isStateRootAvailable", "latest")).Should().Contain("false");
_stateReader.HasStateForRoot(Arg.Any<Hash256>()).Returns(true);
(await RpcTest.TestSerializedRequest(_adminRpcModule, "admin_isStateRootAvailable", "latest")).Should().Contain("true");
}

[Test]
Expand Down
25 changes: 21 additions & 4 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Linq;
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.FullPruning;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Network;
using Nethermind.Network.Config;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.State;
using Nethermind.Stats.Model;
using Nethermind.Synchronization.FastSync;

Expand All @@ -28,6 +30,7 @@ public class AdminRpcModule : IAdminRpcModule
private readonly string _dataDir;
private readonly ManualPruningTrigger _pruningTrigger;
private readonly IBlockingVerifyTrie _blockingVerifyTrie;
private readonly IStateReader _stateReader;
private NodeInfo _nodeInfo = null!;

public AdminRpcModule(
Expand All @@ -36,6 +39,7 @@ public AdminRpcModule(
IPeerPool peerPool,
IStaticNodesManager staticNodesManager,
IBlockingVerifyTrie blockingVerifyTrie,
IStateReader stateReader,
IEnode enode,
string dataDir,
ManualPruningTrigger pruningTrigger,
Expand All @@ -48,6 +52,7 @@ public AdminRpcModule(
_networkConfig = networkConfig ?? throw new ArgumentNullException(nameof(networkConfig));
_staticNodesManager = staticNodesManager ?? throw new ArgumentNullException(nameof(staticNodesManager));
_blockingVerifyTrie = blockingVerifyTrie ?? throw new ArgumentNullException(nameof(blockingVerifyTrie));
_stateReader = stateReader ?? throw new ArgumentNullException(nameof(stateReader));
_pruningTrigger = pruningTrigger;
_parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));

Expand Down Expand Up @@ -139,19 +144,31 @@ public ResultWrapper<bool> admin_setSolc()
return ResultWrapper<bool>.Success(true);
}

public ResultWrapper<bool> admin_isStateRootAvailable(BlockParameter block)
{
SearchResult<BlockHeader> headerSearchResult = _blockTree.SearchForHeader(block);
if (headerSearchResult.Object is null)
{
return ResultWrapper<bool>.Fail("Unable to find block. Unable to know state root to verify.");
}

return ResultWrapper<bool>.Success(_stateReader.HasStateForBlock(headerSearchResult.Object));
}

public ResultWrapper<PruningStatus> admin_prune()
{
return ResultWrapper<PruningStatus>.Success(_pruningTrigger.Trigger());
}

public ResultWrapper<string> admin_verifyTrie()
public ResultWrapper<string> admin_verifyTrie(BlockParameter block)
{
if (_blockTree.Head is null)
SearchResult<BlockHeader> headerSearchResult = _blockTree.SearchForHeader(block);
if (headerSearchResult.Object is null)
{
return ResultWrapper<string>.Fail("Head is null. Unable to know state root to verify.");
return ResultWrapper<string>.Fail("Unable to find block. Unable to know state root to verify.");
}

if (!_blockingVerifyTrie.TryStartVerifyTrie(_blockTree.Head!.Header))
if (!_blockingVerifyTrie.TryStartVerifyTrie(headerSearchResult.Object))
{
return ResultWrapper<string>.Fail("Unable to start verify trie. Verify trie already running.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading.Tasks;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.FullPruning;

namespace Nethermind.JsonRpc.Modules.Admin;
Expand Down Expand Up @@ -57,20 +58,19 @@ ResultWrapper<PeerInfo[]> admin_peers(
IsImplemented = true)]
ResultWrapper<string> admin_dataDir();


[JsonRpcMethod(Description = "[DEPRECATED]",
IsImplemented = false)]
ResultWrapper<bool> admin_setSolc();

[JsonRpcMethod(Description = "Runs full pruning if enabled.",
[JsonRpcMethod(Description = "True if state root for the block is available",
EdgeCaseHint = "",
ExampleResponse = "\"Starting\"",
IsImplemented = true)]
ResultWrapper<PruningStatus> admin_prune();
ResultWrapper<bool> admin_isStateRootAvailable(BlockParameter block);

[JsonRpcMethod(Description = "Runs VerifyTrie.",
EdgeCaseHint = "",
ExampleResponse = "\"Starting\"",
IsImplemented = true)]
ResultWrapper<string> admin_verifyTrie();
ResultWrapper<string> admin_verifyTrie(BlockParameter block);
}

0 comments on commit 2fd5031

Please sign in to comment.