-
Notifications
You must be signed in to change notification settings - Fork 458
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
Implement admin_addTrustedPeer rpc endpoint #7891
base: master
Are you sure you want to change the base?
Implement admin_addTrustedPeer rpc endpoint #7891
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good starting point. I do have some questions and comments before we finalize it.
|
||
namespace Nethermind.Network | ||
{ | ||
public interface ITrustedNodesManager : INodeSource |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: file should be named as type, so start from upper case I
.
string enodeString = arg.Node.ToString(Node.Format.ENode); | ||
|
||
if (_trustedNodesManager.IsTrusted(enodeString)) | ||
{ | ||
arg.Node.IsTrusted = true; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this logic should be in PeerManager
or NodesLoader
rather than in PeerPool
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can avoid it completely as the nodes from source are already marked as trusted?
if (_trustedNodesManager.IsTrusted(enodeString)) | ||
{ | ||
arg.Node.IsTrusted = true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simplified:
if (_trustedNodesManager.IsTrusted(enodeString)) | |
{ | |
arg.Node.IsTrusted = true; | |
} | |
arg.Node.IsTrusted = _trustedNodesManager.IsTrusted(enodeString); |
string data = await File.ReadAllTextAsync(_trustedNodesPath); | ||
string[] nodes = GetNodes(data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't expect this file to be big, but it is always better to deserialize as a stream or line by line.
foreach (string? n in nodes) | ||
{ | ||
try | ||
{ | ||
NetworkNode networkNode = new(n); | ||
networkNodes.Add(networkNode); | ||
} | ||
catch (Exception exception) when (exception is ArgumentException or SocketException) | ||
{ | ||
if (_logger.IsError) _logger.Error("Unable to process node. ", exception); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered reusing NetworkNode.ParseNodes
? Check NodesLoader
.
Channel<Node> ch = Channel.CreateBounded<Node>(128); | ||
|
||
foreach (Node node in _nodes.Values.Select(n => new Node(n) { IsTrusted = true })) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
yield return node; | ||
} | ||
|
||
void handler(object? _, NodeEventArgs args) | ||
{ | ||
ch.Writer.TryWrite(args.Node); | ||
} | ||
|
||
try | ||
{ | ||
NodeAdded += handler; | ||
|
||
await foreach (Node node in ch.Reader.ReadAllAsync(cancellationToken)) | ||
{ | ||
yield return node; | ||
} | ||
} | ||
finally | ||
{ | ||
NodeAdded -= handler; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a channel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If nodes are not expected to change often, we can return them directly without a channel. Does that sound good to you?
Task<bool> AddAsync(string enode, bool updateFile = true); | ||
Task<bool> RemoveAsync(string enode, bool updateFile = true); | ||
bool IsTrusted(string enode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we should have the interface based on strings? Maybe we should base it on PublicKeys
or Enode
?
Fixes #7811
Changes
Added ITrustedNodesManager Interface:
Created a new ITrustedNodesManager interface to handle trusted nodes. This interface is similar to IStaticNodesManager but for trusted nodes. It provides methods InitAsync(), AddAsync(), RemoveAsync(), IsTrusted(), and exposes Nodes.
Implemented TrustedNodesManager Class:
Implemented TrustedNodesManager, which reads/writes a trusted nodes file and manages a list of trusted nodes. It also implements INodeSource (or inherits from ITrustedNodesManager : INodeSource) so it can be used by CompositeNodeSource.
Added IsTrusted Property to Node:
Modified the Node class used by the networking code to include a public bool IsTrusted { get; set; } property. This allows peers to reflect if they originate from trusted nodes.
Integrated TrustedNodesManager into PeerPool:
Passed an ITrustedNodesManager instance into the PeerPool constructor and updated the peer creation logic (CreateNew methods) to mark nodes as trusted if they come from the trusted nodes manager.
Updated InitializeNetwork to Include TrustedNodesManager:
Created and initialized TrustedNodesManager inside InitializeNetwork.cs, added it to CompositeNodeSource so that trusted nodes are considered in the node discovery pipeline, and passed it to PeerPool.
Updated IApiWithNetwork and IInitConfig:
Extended IApiWithNetwork interface to have a ITrustedNodesManager? TrustedNodesManager { get; set; } property (if needed).
Added a TrustedNodesPath property to IInitConfig and InitConfig to define the file path for trusted nodes.
Modified AdminRpcModule Constructor:
Updated the AdminRpcModule constructor to require an ITrustedNodesManager and implemented the admin_addTrustedPeer method using the trusted nodes manager and peer pool.
Adjusted existing tests to provide an ITrustedNodesManager mock where needed. Added a new test (Test_admin_addTrustedPeer) to ensure admin_addTrustedPeer works correctly.
Types of changes
What types of changes does your code introduce?
Testing
Requires testing
If yes, did you write tests?
Documentation
Requires documentation update
If yes, link the PR to the docs update or the issue with the details labeled
docs
. Remove if not applicable.Requires explanation in Release Notes
If yes, fill in the details here. Remove if not applicable.
Remarks
Optional. Remove if not applicable.