Skip to content

Commit

Permalink
Expose Enode property and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgreg committed Feb 5, 2025
1 parent d60ff6a commit 88af561
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
6 changes: 5 additions & 1 deletion src/Nethermind/Nethermind.Config/NetworkNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public NetworkNode(PublicKey publicKey, string ip, int port, long reputation = 0
public string Host => _enode.HostIp.ToString();
public int Port => _enode.Port;
public long Reputation { get; set; }
public NetworkNode(Enode enode)
{
_enode = enode;
}

public NetworkNode(Enode enode) : this(enode.ToString()){}
public Enode Enode => _enode;
}
}
24 changes: 7 additions & 17 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,15 @@ public async Task<ResultWrapper<bool>> admin_addTrustedPeer(string enode)
{
Enode enodeObj = new(enode);

bool added = await _trustedNodesManager.AddAsync(enodeObj, updateFile: true);


if (!added && _trustedNodesManager.IsTrusted(enodeObj))
if (_trustedNodesManager.IsTrusted(enodeObj) || await _trustedNodesManager.AddAsync(enodeObj, updateFile: true))
{
// The node is already trusted—this is acceptable.
added = true;
}

if (added)
{
// Add it to the peer pool so that the node is connectable.
_peerPool.GetOrAdd(new NetworkNode(enodeObj.ToString()));
return ResultWrapper<bool>.Success(true);
}
_peerPool.GetOrAdd(new NetworkNode(enodeObj));
return ResultWrapper<bool>.Success(true);
}
else
{
return ResultWrapper<bool>.Fail("Failed to add trusted peer.");
}
{
return ResultWrapper<bool>.Fail("Failed to add trusted peer.");
}
}


Expand Down
12 changes: 1 addition & 11 deletions src/Nethermind/Nethermind.Network/PeerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,7 @@ private Peer CreateNew(PublicKeyAsKey key, (Node Node, ConcurrentDictionary<Publ

private Peer CreateNew(PublicKeyAsKey key, (NetworkNode Node, ConcurrentDictionary<PublicKeyAsKey, Peer> Statics) arg)
{
Node node = new(arg.Node);

string enodeString = node.ToString(Node.Format.ENode);

Enode enode = new Enode(enodeString);

// Check if this node is trusted
if (_trustedNodesManager.IsTrusted(enode))
{
node.IsTrusted = true;
}
Node node = new(arg.Node) { IsTrusted = _trustedNodesManager.IsTrusted(arg.Node.Enode) };

Peer peer = new(node, _stats.GetOrAdd(node));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public async Task InitAsync()
{
if (!File.Exists(_trustedNodesPath))
{
if (_logger.IsDebug)
{
_logger.Debug($"Trusted nodes file not found at: {_trustedNodesPath}");
}
return;
if (_logger.IsDebug) _logger.Debug($"Trusted nodes file not found at: {_trustedNodesPath}");
}

var nodes = new ConcurrentDictionary<PublicKey, NetworkNode>();
Expand Down Expand Up @@ -69,7 +65,7 @@ public async Task InitAsync()
{
_logger.Info($"Loaded {nodes.Count} trusted nodes from: {Path.GetFullPath(_trustedNodesPath)}");
}
if (nodes.Any() && _logger.IsDebug)
if (_logger.IsDebug && nodes.Any())
{
_logger.Debug("Trusted nodes:\n" + string.Join(Environment.NewLine, nodes.Values.Select(n => n.ToString())));
}
Expand Down

0 comments on commit 88af561

Please sign in to comment.