-
Notifications
You must be signed in to change notification settings - Fork 462
/
AdminRpcModule.cs
131 lines (115 loc) · 4.44 KB
/
AdminRpcModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Linq;
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Blockchain.FullPruning;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Network;
using Nethermind.Network.Config;
using Nethermind.Stats.Model;
namespace Nethermind.JsonRpc.Modules.Admin;
public class AdminRpcModule : IAdminRpcModule
{
private readonly IBlockTree _blockTree;
private readonly INetworkConfig _networkConfig;
private readonly IPeerPool _peerPool;
private readonly IStaticNodesManager _staticNodesManager;
private readonly IEnode _enode;
private readonly string _dataDir;
private readonly ManualPruningTrigger _pruningTrigger;
private NodeInfo _nodeInfo = null!;
public AdminRpcModule(
IBlockTree blockTree,
INetworkConfig networkConfig,
IPeerPool peerPool,
IStaticNodesManager staticNodesManager,
IEnode enode,
string dataDir,
ManualPruningTrigger pruningTrigger)
{
_enode = enode ?? throw new ArgumentNullException(nameof(enode));
_dataDir = dataDir ?? throw new ArgumentNullException(nameof(dataDir));
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
_peerPool = peerPool ?? throw new ArgumentNullException(nameof(peerPool));
_networkConfig = networkConfig ?? throw new ArgumentNullException(nameof(networkConfig));
_staticNodesManager = staticNodesManager ?? throw new ArgumentNullException(nameof(staticNodesManager));
_pruningTrigger = pruningTrigger;
BuildNodeInfo();
}
private void BuildNodeInfo()
{
_nodeInfo = new NodeInfo();
_nodeInfo.Name = ProductInfo.ClientId;
_nodeInfo.Enode = _enode.Info;
byte[] publicKeyBytes = _enode.PublicKey?.Bytes;
_nodeInfo.Id = (publicKeyBytes is null ? Keccak.Zero : Keccak.Compute(publicKeyBytes)).ToString(false);
_nodeInfo.Ip = _enode.HostIp?.ToString();
_nodeInfo.ListenAddress = $"{_enode.HostIp}:{_enode.Port}";
_nodeInfo.Ports.Discovery = _networkConfig.DiscoveryPort;
_nodeInfo.Ports.Listener = _networkConfig.P2PPort;
UpdateEthProtocolInfo();
}
private void UpdateEthProtocolInfo()
{
_nodeInfo.Protocols["eth"].Difficulty = _blockTree.Head?.TotalDifficulty ?? 0;
_nodeInfo.Protocols["eth"].NewtorkId = _blockTree.ChainId;
_nodeInfo.Protocols["eth"].HeadHash = _blockTree.HeadHash;
_nodeInfo.Protocols["eth"].GenesisHash = _blockTree.GenesisHash;
}
public async Task<ResultWrapper<string>> admin_addPeer(string enode, bool addToStaticNodes = false)
{
bool added;
if (addToStaticNodes)
{
added = await _staticNodesManager.AddAsync(enode);
}
else
{
NetworkNode networkNode = new(enode);
_peerPool.GetOrAdd(new Node(networkNode));
added = true;
}
return added
? ResultWrapper<string>.Success(enode)
: ResultWrapper<string>.Fail("Failed to add peer.");
}
public async Task<ResultWrapper<string>> admin_removePeer(string enode, bool removeFromStaticNodes = false)
{
bool removed;
if (removeFromStaticNodes)
{
removed = await _staticNodesManager.RemoveAsync(enode);
}
else
{
removed = _peerPool.TryRemove(new NetworkNode(enode).NodeId, out Peer _);
}
return removed
? ResultWrapper<string>.Success(enode)
: ResultWrapper<string>.Fail("Failed to remove peer.");
}
public ResultWrapper<PeerInfo[]> admin_peers(bool includeDetails = false)
=> ResultWrapper<PeerInfo[]>.Success(
_peerPool.ActivePeers.Select(p => new PeerInfo(p.Value, includeDetails)).ToArray());
public ResultWrapper<NodeInfo> admin_nodeInfo()
{
UpdateEthProtocolInfo();
return ResultWrapper<NodeInfo>.Success(_nodeInfo);
}
public ResultWrapper<string> admin_dataDir()
{
return ResultWrapper<string>.Success(_dataDir);
}
public ResultWrapper<bool> admin_setSolc()
{
return ResultWrapper<bool>.Success(true);
}
public ResultWrapper<PruningStatus> admin_prune()
{
return ResultWrapper<PruningStatus>.Success(_pruningTrigger.Trigger());
}
}