Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

richardgreg
Copy link
Contributor

Fixes #7811

Changes

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  1. Updated Tests:
    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?

  • Bugfix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (a change that causes existing functionality not to work as expected)
  • Optimization
  • Refactoring
  • Documentation update
  • Build-related changes
  • Other: Description

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

Documentation

Requires documentation update

  • Yes
  • No

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

  • Yes
  • No

If yes, fill in the details here. Remove if not applicable.

Remarks

Optional. Remove if not applicable.

@richardgreg richardgreg requested a review from rubo as a code owner December 11, 2024 08:15
Copy link
Member

@LukaszRozmej LukaszRozmej left a 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
Copy link
Member

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.

Comment on lines +90 to +96
string enodeString = arg.Node.ToString(Node.Format.ENode);

if (_trustedNodesManager.IsTrusted(enodeString))
{
arg.Node.IsTrusted = true;
}

Copy link
Member

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?

Copy link
Member

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?

Comment on lines +92 to +95
if (_trustedNodesManager.IsTrusted(enodeString))
{
arg.Node.IsTrusted = true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified:

Suggested change
if (_trustedNodesManager.IsTrusted(enodeString))
{
arg.Node.IsTrusted = true;
}
arg.Node.IsTrusted = _trustedNodesManager.IsTrusted(enodeString);

Comment on lines +51 to +52
string data = await File.ReadAllTextAsync(_trustedNodesPath);
string[] nodes = GetNodes(data);
Copy link
Member

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.

Comment on lines +61 to +72
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);
}
}
Copy link
Member

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.

Comment on lines +144 to +169
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;
}
Copy link
Member

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?

Comment on lines +14 to +16
Task<bool> AddAsync(string enode, bool updateFile = true);
Task<bool> RemoveAsync(string enode, bool updateFile = true);
bool IsTrusted(string enode);
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement admin_addTrustedPeer rpc endpoint
2 participants