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

Feature/Single Pluggable DI Container #7630

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
18fef4d
Migrate more of network initialization
asdacap Sep 28, 2024
dae8d74
Fix build
asdacap Sep 30, 2024
8de5f2c
Unnecessary code
asdacap Sep 30, 2024
2bff61f
Fix startup
asdacap Oct 16, 2024
f38c7c9
Pluggable DI container
asdacap Oct 17, 2024
618e488
Rename PluginEnabled to Enabled and use Module instead
asdacap Oct 17, 2024
eb7a8a8
Remove InitializationPlugin
asdacap Oct 17, 2024
d789018
Add fallback
asdacap Oct 17, 2024
d52ac63
Merge remote-tracking branch 'origin/master' into feature/network-di
asdacap Oct 17, 2024
e72cf0d
Whitespace
asdacap Oct 17, 2024
5496d74
Dedup optimism logic
asdacap Oct 17, 2024
a663975
Merge remote-tracking branch 'origin/master' into feature/network-di
asdacap Oct 17, 2024
77c9bdb
Minor cleanup
asdacap Oct 17, 2024
213f7e6
Fix test
asdacap Oct 17, 2024
0bbd6a7
Fix optimism start
asdacap Oct 17, 2024
505e863
These checks are no longer necessary.
asdacap Oct 17, 2024
40a6047
Fix not using all hierarcy
asdacap Oct 17, 2024
e4907f2
Merge branch 'feature/network-di' into feature/single-pluggable-di-co…
asdacap Oct 17, 2024
a00d590
Remove some integration
asdacap Oct 17, 2024
7b8b4bb
Fix most tests
asdacap Oct 18, 2024
2cbf306
Small refactor
asdacap Oct 18, 2024
4cf5b8a
Addedcomponent keys
asdacap Oct 18, 2024
01f4f1d
Merge branch 'feature/network-di' into feature/single-pluggable-di-co…
asdacap Oct 18, 2024
9627760
Fix some tests
asdacap Oct 18, 2024
d3e89c2
Minor cleanup
asdacap Oct 18, 2024
4e64380
Fix ambiguous ProtectedPrivateKey
asdacap Oct 18, 2024
5e4b952
Merge branch 'feature/network-di' into feature/single-pluggable-di-co…
asdacap Oct 18, 2024
7986dc2
Add back aura plugin tests
asdacap Oct 18, 2024
11a687b
Whitespace
asdacap Oct 18, 2024
18ad3f5
Add unit test for resolve
asdacap Oct 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@

namespace Nethermind.Analytics
{
public class AnalyticsPlugin : INethermindPlugin
public class AnalyticsPlugin(IAnalyticsConfig analyticsConfig, IInitConfig initConfig) : INethermindPlugin
{
private IAnalyticsConfig _analyticsConfig;
private IList<IPublisher> _publishers;
private INethermindApi _api;

private bool _isOn;

public ValueTask DisposeAsync() { return ValueTask.CompletedTask; }

public string Name => "Analytics";
Expand All @@ -28,19 +25,20 @@ public class AnalyticsPlugin : INethermindPlugin

public string Author => "Nethermind";


public bool Enabled => initConfig.WebSocketsEnabled &&
(analyticsConfig.PluginsEnabled ||
analyticsConfig.StreamBlocks ||
analyticsConfig.StreamTransactions);

public Task Init(INethermindApi api)
{
_api = api;
var (getFromAPi, _) = _api.ForInit;
_analyticsConfig = getFromAPi.Config<IAnalyticsConfig>();

IInitConfig initConfig = getFromAPi.Config<IInitConfig>();
_isOn = initConfig.WebSocketsEnabled &&
(_analyticsConfig.PluginsEnabled ||
_analyticsConfig.StreamBlocks ||
_analyticsConfig.StreamTransactions);

if (!_isOn)
if (!Enabled)
{
if (!initConfig.WebSocketsEnabled)
{
Expand All @@ -57,7 +55,7 @@ public Task Init(INethermindApi api)

private void TxPoolOnNewDiscovered(object sender, TxEventArgs e)
{
if (_analyticsConfig.StreamTransactions)
if (analyticsConfig.StreamTransactions)
{
foreach (IPublisher publisher in _publishers)
{
Expand All @@ -70,12 +68,12 @@ private void TxPoolOnNewDiscovered(object sender, TxEventArgs e)
public Task InitNetworkProtocol()
{
var (getFromAPi, _) = _api.ForNetwork;
if (_isOn)
if (Enabled)
{
getFromAPi.TxPool!.NewDiscovered += TxPoolOnNewDiscovered;
}

if (_isOn)
if (Enabled)
{
AnalyticsWebSocketsModule webSocketsModule = new(getFromAPi.EthereumJsonSerializer, getFromAPi.LogManager);
getFromAPi.WebSocketsManager!.AddModule(webSocketsModule, true);
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Api.Test/TestPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ public Task InitRpcModules()
{
throw new System.NotImplementedException();
}

public bool Enabled => true;
}
}
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Api.Test/TestPlugin2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ public Task InitRpcModules()
{
throw new System.NotImplementedException();
}

public bool Enabled => true;
}
}
9 changes: 0 additions & 9 deletions src/Nethermind/Nethermind.Api/Extensions/IConsensusPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading.Tasks;
using Nethermind.Config;
using Nethermind.Consensus;
using Nethermind.Consensus.Producers;
using Nethermind.Logging;
using Nethermind.Serialization.Json;
using Nethermind.Specs.ChainSpecStyle;

namespace Nethermind.Api.Extensions
{
public interface IConsensusPlugin : INethermindPlugin, IBlockProducerFactory
{
string SealEngineType { get; }

INethermindApi CreateApi(IConfigProvider configProvider, IJsonSerializer jsonSerializer,
ILogManager logManager, ChainSpec chainSpec) => new NethermindApi(configProvider, jsonSerializer, logManager, chainSpec);

IBlockProducerRunner CreateBlockProducerRunner();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface IConsensusWrapperPlugin : INethermindPlugin
/// </summary>
int Priority => 0;

bool Enabled { get; }
bool ConsensusWrapperEnabled { get; }
}
}
19 changes: 0 additions & 19 deletions src/Nethermind/Nethermind.Api/Extensions/IInitializationPlugin.cs

This file was deleted.

5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Api/Extensions/INethermindPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Threading.Tasks;
using Autofac;
using Autofac.Core;

namespace Nethermind.Api.Extensions;

Expand All @@ -23,4 +25,7 @@ void InitTxTypesAndRlpDecoders(INethermindApi api) { }
Task InitRpcModules() => Task.CompletedTask;

bool MustInitialize => false;

bool Enabled { get; }
IModule? ContainerModule => null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading.Tasks;
using Autofac;

namespace Nethermind.Api.Extensions
{
Expand Down
14 changes: 4 additions & 10 deletions src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Nethermind.Consensus.Scheduler;
using Nethermind.Consensus.Validators;
using Nethermind.Core;
using Nethermind.Core.Container;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Facade;
using Nethermind.Facade.Eth;
Expand Down Expand Up @@ -52,7 +53,7 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory
/// <summary>
/// PoS switcher for The Merge
/// </summary>
IPoSSwitcher PoSSwitcher { get; set; }
IPoSSwitcher PoSSwitcher { get; }
ISealer? Sealer { get; set; }
ISealValidator? SealValidator { get; set; }
ISealEngine SealEngine { get; set; }
Expand Down Expand Up @@ -93,20 +94,13 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory

IGasPriceOracle? GasPriceOracle { get; set; }

IEthSyncingInfo? EthSyncingInfo { get; set; }
IEthSyncingInfo? EthSyncingInfo { get; }

CompositePruningTrigger PruningTrigger { get; }

IBlockProductionPolicy? BlockProductionPolicy { get; set; }
INodeStorageFactory NodeStorageFactory { get; set; }
BackgroundTaskScheduler BackgroundTaskScheduler { get; set; }
IBackgroundTaskScheduler BackgroundTaskScheduler { get; set; }
CensorshipDetector CensorshipDetector { get; set; }

public ContainerBuilder ConfigureContainerBuilderFromApiWithBlockchain(ContainerBuilder builder)
{
return ConfigureContainerBuilderFromApiWithStores(builder)
.AddPropertiesFrom<IApiWithBlockchain>(this)
.AddSingleton<INodeStorage>(NodeStorageFactory.WrapKeyValueStore(DbProvider!.StateDb));
}
}
}
34 changes: 11 additions & 23 deletions src/Nethermind/Nethermind.Api/IApiWithNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Autofac;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Container;
using Nethermind.Core.PubSub;
using Nethermind.Grpc;
using Nethermind.JsonRpc;
Expand All @@ -26,40 +27,27 @@ public interface IApiWithNetwork : IApiWithBlockchain
{
(IApiWithNetwork GetFromApi, IApiWithNetwork SetInApi) ForNetwork => (this, this);

IDisconnectsAnalyzer? DisconnectsAnalyzer { get; set; }
IDiscoveryApp? DiscoveryApp { get; set; }
IDiscoveryApp? DiscoveryApp { get; }
IGrpcServer? GrpcServer { get; set; }
IIPResolver? IpResolver { get; set; }
IMessageSerializationService MessageSerializationService { get; }
IGossipPolicy GossipPolicy { get; set; }
IMonitoringService MonitoringService { get; set; }
INodeStatsManager? NodeStatsManager { get; set; }
IPeerManager? PeerManager { get; set; }
IPeerPool? PeerPool { get; set; }
IProtocolsManager? ProtocolsManager { get; set; }
IProtocolValidator? ProtocolValidator { get; set; }
INodeStatsManager? NodeStatsManager { get; }
IPeerManager? PeerManager { get; }
IPeerPool? PeerPool { get; }
IList<IPublisher> Publishers { get; }
IRlpxHost? RlpxPeer { get; set; }
IRlpxHost? RlpxPeer { get; }
IRpcModuleProvider? RpcModuleProvider { get; set; }
IJsonRpcLocalStats? JsonRpcLocalStats { get; set; }
ISessionMonitor? SessionMonitor { get; set; }
IStaticNodesManager? StaticNodesManager { get; set; }
ISessionMonitor? SessionMonitor { get; }
IStaticNodesManager? StaticNodesManager { get; }
ISynchronizer? Synchronizer { get; }
ISyncModeSelector SyncModeSelector { get; }
ISyncProgressResolver? SyncProgressResolver { get; }
IPivot? Pivot { get; set; }
ISyncPeerPool? SyncPeerPool { get; set; }
IPeerDifficultyRefreshPool? PeerDifficultyRefreshPool { get; set; }
ISyncServer? SyncServer { get; set; }
ISyncPeerPool? SyncPeerPool { get; }
IPeerDifficultyRefreshPool? PeerDifficultyRefreshPool { get; }
ISyncServer? SyncServer { get; }
IWebSocketsManager WebSocketsManager { get; set; }
ISubscriptionFactory? SubscriptionFactory { get; set; }

IContainer? ApiWithNetworkServiceContainer { get; set; }

public ContainerBuilder ConfigureContainerBuilderFromApiWithNetwork(ContainerBuilder builder)
{
return ConfigureContainerBuilderFromApiWithBlockchain(builder)
.AddPropertiesFrom<IApiWithNetwork>(this);
}
}
}
9 changes: 3 additions & 6 deletions src/Nethermind/Nethermind.Api/IApiWithStores.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Container;
using Nethermind.Crypto;
using Nethermind.Db.Blooms;
using Nethermind.Facade.Find;
Expand All @@ -26,17 +27,13 @@ public interface IApiWithStores : IBasicApi
ILogFinder? LogFinder { get; set; }
ISigner? EngineSigner { get; set; }
ISignerStore? EngineSignerStore { get; set; }

[ComponentKey(ComponentKey.NodeKey)]
ProtectedPrivateKey? NodeKey { get; set; }
IReceiptStorage? ReceiptStorage { get; set; }
IReceiptFinder? ReceiptFinder { get; set; }
IReceiptMonitor? ReceiptMonitor { get; set; }
IWallet? Wallet { get; set; }
IBlockStore? BadBlocksStore { get; set; }

public ContainerBuilder ConfigureContainerBuilderFromApiWithStores(ContainerBuilder builder)
{
return ConfigureContainerBuilderFromBasicApi(builder)
.AddPropertiesFrom<IApiWithStores>(this);
}
}
}
33 changes: 13 additions & 20 deletions src/Nethermind/Nethermind.Api/IBasicApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
using Nethermind.Blockchain.Synchronization;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Container;
using Nethermind.Core.Specs;
using Nethermind.Core.Timers;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.KeyStore;
using Nethermind.Logging;
using Nethermind.Network.Config;
using Nethermind.Serialization.Json;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Synchronization;
Expand All @@ -28,46 +30,37 @@ public interface IBasicApi
DisposableStack DisposeStack { get; }

IAbiEncoder AbiEncoder { get; }
ChainSpec ChainSpec { get; set; }
IConfigProvider ConfigProvider { get; set; }
ChainSpec ChainSpec { get; }
IConfigProvider ConfigProvider { get; }
ICryptoRandom CryptoRandom { get; }
IDbProvider? DbProvider { get; set; }
IDbFactory? DbFactory { get; set; }
IEthereumEcdsa? EthereumEcdsa { get; set; }
IJsonSerializer EthereumJsonSerializer { get; set; }
IJsonSerializer EthereumJsonSerializer { get; }
IFileSystem FileSystem { get; set; }
IKeyStore? KeyStore { get; set; }
ILogManager LogManager { get; set; }
ILogManager LogManager { get; }
[SkipServiceCollection]
ProtectedPrivateKey? OriginalSignerKey { get; set; }
IReadOnlyList<INethermindPlugin> Plugins { get; }
[SkipServiceCollection]
string SealEngineType { get; set; }
ISpecProvider? SpecProvider { get; set; }
IBetterPeerStrategy? BetterPeerStrategy { get; set; }
string SealEngineType { get; }
ISpecProvider? SpecProvider { get; }
IBetterPeerStrategy? BetterPeerStrategy { get; }
ITimestamper Timestamper { get; }
ITimerFactory TimerFactory { get; }
IProcessExitSource? ProcessExit { get; set; }
IProcessExitSource? ProcessExit { get; }
ILifetimeScope BaseContainer { get; }

public IConsensusPlugin? GetConsensusPlugin() =>
Plugins
.OfType<IConsensusPlugin>()
.SingleOrDefault(cp => cp.SealEngineType == SealEngineType);

public IEnumerable<IConsensusWrapperPlugin> GetConsensusWrapperPlugins() =>
Plugins.OfType<IConsensusWrapperPlugin>().Where(p => p.Enabled);
Plugins.OfType<IConsensusWrapperPlugin>().Where(p => p.ConsensusWrapperEnabled);

public IEnumerable<ISynchronizationPlugin> GetSynchronizationPlugins() =>
Plugins.OfType<ISynchronizationPlugin>();

public ContainerBuilder ConfigureContainerBuilderFromBasicApi(ContainerBuilder builder)
{
builder
.AddPropertiesFrom<IBasicApi>(this)
.AddSingleton(ConfigProvider.GetConfig<ISyncConfig>());

DbProvider!.ConfigureServiceCollection(builder);

return builder;
}
}
}
Loading