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

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.

@richardgreg richardgreg force-pushed the feature-7811-admin_addTrustedPeer-rpc branch from ae99897 to 6423863 Compare January 10, 2025 10:18
@richardgreg
Copy link
Contributor Author

Merge conflicts fixed 🙂

@richardgreg
Copy link
Contributor Author

A gentle reminder @LukaszRozmej. If there are no more notes, I can resolve the conflict, and it should be ready to be merged

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.

Sorry, missed it, still few things to polish

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.

Few more details, but really close!

@richardgreg richardgreg force-pushed the feature-7811-admin_addTrustedPeer-rpc branch 2 times, most recently from 7aaf7b1 to fe2ed13 Compare February 6, 2025 08:14
Comment on lines 61 to 60
public NetworkNode(Enode enode)
{
_enode = enode;
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you put constructor next to the other?

@flcl42
Copy link
Contributor

flcl42 commented Feb 6, 2025

Should we have admin_removeTrustedPeer? Not necessary in this pr.

@@ -23,6 +24,7 @@ public class TrustedNodesManager : ITrustedNodesManager
private ConcurrentDictionary<PublicKey, NetworkNode> _nodes = new();
private readonly string _trustedNodesPath;
private readonly ILogger _logger;
private readonly Channel<Node> _nodeChannel = Channel.CreateUnbounded<Node>();
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 we need bounded channel, otherwise potential spam could take down the node? 2^16 limit looks ok to me.

@richardgreg richardgreg force-pushed the feature-7811-admin_addTrustedPeer-rpc branch from 6755983 to b1515f9 Compare February 7, 2025 08:06
@richardgreg richardgreg force-pushed the feature-7811-admin_addTrustedPeer-rpc branch from b1515f9 to 7b7650c Compare February 7, 2025 08:19
@LukaszRozmej
Copy link
Member

some tests are failing

}

// continuously yield new nodes as they are added via the channel.
while (await _nodeChannel.Reader.WaitToReadAsync(cancellationToken))
Copy link
Contributor

@flcl42 flcl42 Feb 7, 2025

Choose a reason for hiding this comment

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

Instead of doing two whiles, could you just _nodeChannel.Reader.ReadAllAsync in one await foreach

@richardgreg
Copy link
Contributor Author

Hi, @LukaszRozmej, could you please give me pointers on tackling the failing tests? 😅

@LukaszRozmej
Copy link
Member

LukaszRozmej commented Feb 12, 2025

Hi, @LukaszRozmej, could you please give me pointers on tackling the failing tests? 😅

  Failed Proof_module_is_registered_if_configured [945 ms]
  Error Message:
   Nethermind.Init.Steps.StepDependencyException : _api.TrustedNodesManager
  Stack Trace:
     at Nethermind.Init.Steps.StepDependencyException.ThrowIfNull(Object argument, String paramName) in /_/src/Nethermind/Nethermind.Init/Steps/StepInitializationException.cs:line 23
   at Nethermind.Init.Steps.RegisterRpcModules.Execute(CancellationToken cancellationToken) in /_/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs:line 123
   at Nethermind.Runner.Test.Ethereum.Steps.RegisterRpcModulesTests.Proof_module_is_registered_if_configured() in /_/src/Nethermind/Nethermind.Runner.Test/Ethereum/Steps/RegisterRpcModulesTests.cs:line 31
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.GetResult()
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await[TResult](TestExecutionContext context, Func`1 invoke)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(TestExecutionContext context, Func`1 invoke)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
   at NUnit.Framework.Internal.Execution.SimpleWorkItem.<>c__DisplayClass3_0.<PerformWork>b__0()
   at NUnit.Framework.Internal.ContextUtils.<>c__DisplayClass1_0`1.<DoIsolated>b__0(Object _)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at NUnit.Framework.Internal.ContextUtils.DoIsolated(ContextCallback callback, Object state)
   at NUnit.Framework.Internal.ContextUtils.DoIsolated[T](Func`1 func)
   at NUnit.Framework.Internal.Execution.SimpleWorkItem.PerformWork()

That one is definitely about incorrect setup in test.

  Failed TestDiscovery [770 ms]
  Error Message:
   Autofac.Core.DependencyResolutionException : An exception was thrown while activating Nethermind.Network.PeerPool.
Data:
  ActivatorChain: Nethermind.Network.PeerPool
  ----> Autofac.Core.DependencyResolutionException : None of the constructors found on type 'Nethermind.Network.PeerPool' can be invoked with the available services and parameters:
Cannot resolve parameter 'Nethermind.Network.ITrustedNodesManager trustedNodesManager' of constructor 'Void .ctor(Nethermind.Network.INodeSource, Nethermind.Stats.INodeStatsManager, Nethermind.Network.INetworkStorage, Nethermind.Network.Config.INetworkConfig, Nethermind.Logging.ILogManager, Nethermind.Network.ITrustedNodesManager)'.

See https://autofac.rtfd.io/help/no-constructors-bindable for more info.
  Stack Trace:
     at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext context)
   at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext context)
   at Autofac.Core.Resolving.Middleware.RegistrationPipelineInvokeMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext context)
   at Autofac.Core.Resolving.Middleware.SharingMiddleware.<>c__DisplayClass5_0.<Execute>b__0()
   at Autofac.Core.Lifetime.LifetimeScope.CreateSharedInstance(Guid id, Func`1 creator)
   at Autofac.Core.Lifetime.LifetimeScope.CreateSharedInstance(Guid primaryId, Nullable`1 qualifyingId, Func`1 creator)
   at Autofac.Core.Resolving.Middleware.SharingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext context)
   at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext context)
   at Autofac.Core.Resolving.Middleware.CircularDependencyDetectorMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext context)
   at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext context)
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest& request)
   at Autofac.Core.Resolving.ResolveOperation.ExecuteOperation(ResolveRequest& request)
   at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest& request)
   at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest& request)
   at Autofac.Core.Lifetime.LifetimeScope.Autofac.IComponentContext.ResolveComponent(ResolveRequest& request)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)
   at Nethermind.Core.Test.Modules.PseudoNethermindRunner.StartDiscovery(CancellationToken cancellationToken) in /_/src/Nethermind/Nethermind.Core.Test/Modules/PseudoNethermindRunner.cs:line 100
   at Nethermind.Network.Discovery.Test.E2EDiscoveryTests.TestDiscovery() in /_/src/Nethermind/Nethermind.Network.Discovery.Test/E2EDiscoveryTests.cs:line 83
   at Nethermind.Network.Discovery.Test.E2EDiscoveryTests.TestDiscovery() in /_/src/Nethermind/Nethermind.Network.Discovery.Test/E2EDiscoveryTests.cs:line 86
   at Nethermind.Network.Discovery.Test.E2EDiscoveryTests.TestDiscovery() in /_/src/Nethermind/Nethermind.Network.Discovery.Test/E2EDiscoveryTests.cs:line 86
   at Nethermind.Network.Discovery.Test.E2EDiscoveryTests.TestDiscovery() in /_/src/Nethermind/Nethermind.Network.Discovery.Test/E2EDiscoveryTests.cs:line 86
   at Nethermind.Network.Discovery.Test.E2EDiscoveryTests.TestDiscovery() in /_/src/Nethermind/Nethermind.Network.Discovery.Test/E2EDiscoveryTests.cs:line 86
   at Nethermind.Network.Discovery.Test.E2EDiscoveryTests.TestDiscovery() in /_/src/Nethermind/Nethermind.Network.Discovery.Test/E2EDiscoveryTests.cs:line 86
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()
   at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await[TResult](TestExecutionContext context, Func`1 invoke)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(TestExecutionContext context, Func`1 invoke)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
   at NUnit.Framework.Internal.Execution.SimpleWorkItem.<>c__DisplayClass3_0.<PerformWork>b__0()
   at NUnit.Framework.Internal.ContextUtils.<>c__DisplayClass1_0`1.<DoIsolated>b__0(Object _)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at NUnit.Framework.Internal.ContextUtils.DoIsolated(ContextCallback callback, Object state)
   at NUnit.Framework.Internal.ContextUtils.DoIsolated[T](Func`1 func)
   at NUnit.Framework.Internal.Execution.SimpleWorkItem.PerformWork()
--DependencyResolutionException
   at Autofac.Core.Activators.Reflection.ReflectionActivator.<>c__DisplayClass14_0.<UseSingleConstructorActivation>b__0(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext context)
   at Autofac.Core.Resolving.Middleware.DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
   at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext context)
   at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)

This is similar, probably need some changes on the autofac module (dependency injection).

@LukaszRozmej
Copy link
Member

Also please update to newest master and resolve conflicts

@richardgreg richardgreg force-pushed the feature-7811-admin_addTrustedPeer-rpc branch from 6fdca67 to 251c554 Compare February 13, 2025 21:37
@LukaszRozmej LukaszRozmej merged commit d2089f3 into NethermindEth:master Feb 14, 2025
79 checks passed
@LukaszRozmej
Copy link
Member

Caused regression, that was fixed in #8211

@richardgreg
Copy link
Contributor Author

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 🙏

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