Implement admin_addTrustedPeer rpc endpoint#7891
Implement admin_addTrustedPeer rpc endpoint#7891LukaszRozmej merged 15 commits intoNethermindEth:masterfrom
Conversation
LukaszRozmej
left a comment
There was a problem hiding this comment.
Very good starting point. I do have some questions and comments before we finalize it.
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
ae99897 to
6423863
Compare
|
Merge conflicts fixed 🙂 |
|
A gentle reminder @LukaszRozmej. If there are no more notes, I can resolve the conflict, and it should be ready to be merged |
LukaszRozmej
left a comment
There was a problem hiding this comment.
Sorry, missed it, still few things to polish
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
LukaszRozmej
left a comment
There was a problem hiding this comment.
Few more details, but really close!
src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Network/TrustedNodes/TrustedNodesManager.cs
Outdated
Show resolved
Hide resolved
7aaf7b1 to
fe2ed13
Compare
| public NetworkNode(Enode enode) | ||
| { | ||
| _enode = enode; | ||
| } |
There was a problem hiding this comment.
Can you put constructor next to the other?
|
Should we have admin_removeTrustedPeer? Not necessary in this pr. |
| private ConcurrentDictionary<PublicKey, NetworkNode> _nodes = new(); | ||
| private readonly string _trustedNodesPath; | ||
| private readonly ILogger _logger; | ||
| private readonly Channel<Node> _nodeChannel = Channel.CreateUnbounded<Node>(); |
There was a problem hiding this comment.
I think we need bounded channel, otherwise potential spam could take down the node? 2^16 limit looks ok to me.
6755983 to
b1515f9
Compare
b1515f9 to
7b7650c
Compare
|
some tests are failing |
| } | ||
|
|
||
| // continuously yield new nodes as they are added via the channel. | ||
| while (await _nodeChannel.Reader.WaitToReadAsync(cancellationToken)) |
There was a problem hiding this comment.
Instead of doing two whiles, could you just _nodeChannel.Reader.ReadAllAsync in one await foreach
|
Hi, @LukaszRozmej, could you please give me pointers on tackling the failing tests? 😅 |
That one is definitely about incorrect setup in test. This is similar, probably need some changes on the autofac module (dependency injection). |
|
Also please update to newest master and resolve conflicts |
6fdca67 to
251c554
Compare
|
Caused regression, that was fixed in #8211 |
|
I'm a little sad that my implementation wasn't impeccable. I am glad the error was quickly caught and fixed. I'll learn from it 🙏 |
|
|
||
| var nodes = new ConcurrentDictionary<PublicKey, NetworkNode>(); | ||
|
|
||
| await foreach (string line in File.ReadLinesAsync(_trustedNodesPath)) |
There was a problem hiding this comment.
Here, it reads the file line by line like a new-line-delimited file, but later it writes a JSON array below in SaveFileAsync. How is it supposed to work? It fails to read the data written by its own. Am I missing something?
| NetworkNode node = new NetworkNode(line); | ||
| nodes.TryAdd(node.NodeId, node); | ||
| } | ||
| catch (Exception ex) when (ex is ArgumentException or SocketException) |
There was a problem hiding this comment.
SocketException looks irrelevant here
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.