-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathJsonRpcRunner.cs
129 lines (120 loc) · 5.01 KB
/
JsonRpcRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nethermind.Api;
using Nethermind.Api.Extensions;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Authentication;
using Nethermind.JsonRpc;
using Nethermind.Logging;
using Nethermind.Network;
using Nethermind.Runner.JsonRpc;
using Nethermind.Runner.Logging;
using Nethermind.Sockets;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace Nethermind.Runner.Ethereum
{
public class JsonRpcRunner
{
private readonly Nethermind.Logging.ILogger _logger;
private readonly IConfigProvider _configurationProvider;
private readonly IRpcAuthentication _rpcAuthentication;
private readonly ILogManager _logManager;
private readonly IJsonRpcProcessor _jsonRpcProcessor;
private readonly IJsonRpcUrlCollection _jsonRpcUrlCollection;
private readonly IWebSocketsManager _webSocketsManager;
private readonly IJsonRpcConfig _jsonRpcConfig;
private IWebHost? _webHost;
private readonly IInitConfig _initConfig;
private readonly INethermindApi _api;
public JsonRpcRunner(
IJsonRpcProcessor jsonRpcProcessor,
IJsonRpcUrlCollection jsonRpcUrlCollection,
IWebSocketsManager webSocketsManager,
IConfigProvider configurationProvider,
IRpcAuthentication rpcAuthentication,
ILogManager logManager,
INethermindApi api)
{
_jsonRpcConfig = configurationProvider.GetConfig<IJsonRpcConfig>();
_initConfig = configurationProvider.GetConfig<IInitConfig>();
_configurationProvider = configurationProvider;
_rpcAuthentication = rpcAuthentication;
_jsonRpcUrlCollection = jsonRpcUrlCollection;
_logManager = logManager;
_jsonRpcProcessor = jsonRpcProcessor;
_webSocketsManager = webSocketsManager;
_logger = logManager.GetClassLogger();
_api = api;
}
public async Task Start(CancellationToken cancellationToken)
{
if (_logger.IsDebug) _logger.Debug("Initializing JSON RPC");
string[] urls = _jsonRpcUrlCollection.Urls;
IWebHost webHost = new WebHostBuilder()
// Explicitly build from UseKestrelCore rather than UseKestrel to
// not add additional transports that we don't use e.g. msquic as that
// adds a lot of additional idle threads to the process.
.UseKestrelCore()
.UseKestrelHttpsConfiguration()
.ConfigureServices(s =>
{
s.AddRouting();
s.AddSingleton(_configurationProvider);
s.AddSingleton(_jsonRpcProcessor);
s.AddSingleton(_jsonRpcUrlCollection);
s.AddSingleton(_webSocketsManager);
s.AddSingleton(_rpcAuthentication);
s.AddSingleton(_api.TxPool);
s.AddSingleton(_api.SpecProvider);
s.AddSingleton(_api.ReceiptFinder);
s.AddSingleton(_api.BlockTree);
s.AddSingleton(_api.SyncPeerPool);
s.AddSingleton(_api.MainProcessingContext);
foreach (var plugin in _api.Plugins.OfType<INethermindServicesPlugin>())
{
plugin.AddServices(s);
}
})
.UseStartup<Startup>()
.UseUrls(urls)
.ConfigureLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Information);
logging.ClearProviders();
logging.AddProvider(new CustomMicrosoftLoggerProvider(_logManager));
})
.Build();
string urlsString = string.Join(" ; ", urls);
// TODO: replace http with ws where relevant
ThisNodeInfo.AddInfo("JSON RPC :", $"{urlsString}");
_webHost = webHost;
if (!cancellationToken.IsCancellationRequested)
{
await NetworkHelper.HandlePortTakenError(
() => _webHost.StartAsync(cancellationToken), urls
);
if (_logger.IsDebug) _logger.Debug($"JSON RPC : {urlsString}");
}
}
public async Task StopAsync()
{
try
{
await (_webHost?.StopAsync() ?? Task.CompletedTask);
if (_logger.IsInfo) _logger.Info("JSON RPC service stopped");
}
catch (Exception e)
{
if (_logger.IsInfo) _logger.Info($"Error when stopping JSON RPC service: {e}");
}
}
}
}