Skip to content

Commit 7917a2f

Browse files
sondrebMithrilMandangershony
authored
Migrate SignalR feature into WebHost (#121)
* Migrate SignalR feature into WebHost - Migrate the SignalR feature into WebHost. - Further refactor WebHost from "Api" to "WebHost. - Remove the versioning support from REST API. - Enable flags to enable/disable UI, API and WS. - Generate and include XML documentation in the REST API for all features. - Add a basic example on Web Socket listener. * Improve the Web Socket event handling - Return the EventBase directly to Web Socket consumers. - Remove unused code. - Remove references in WebHost project. * created a class to handle swagger api documentation xml file inclusion. * Fix issue with exceptions during type loading - Move the Assembly extension from Swagger scaffolding to Utilities. - Minor bug discovered in dispose when object is null (happened during an integration test). * Rename WebHost to NodeHost - Next commit will rename the physical folder. * Rename WebHost folder to NodeHost - Updates all references, etc. from WebHost to NodeHost. * Removing the dependency on a static service provider * Remove the Hash and Height - We're returning the whole object in the evernt, no longer needed to read out hash and height. * Revert "Remove the Hash and Height" This reverts commit 4f8af1b. * Move the resolving of HubContext so it only runs when WS is enabled - Minor cleanup of events, removing older properties. * Remove hardcoded URL to JS and WS endpoints - Update to use relative path when hosted on the NodeHost. - Add JsonIgnore on objects that can't serialize. This needs to be added to more. * Handle serialization of IPAddress, add ignore on structures that crash node - Certain types will crash the node when sent to the Web Socket consumer. - Beware of this issue in the future when working on the event types. - Remove the EventType and instead only have EventName. * Fix dependency issue for broadcasters when no IEventsSubscriptionService is registered - When using the node without .UseWebHost, no implementation of IEventsSubscriptionService is registered. This fixes the broadcasters by handling them as empty if one is not provided. * Update InitialBlockDownloadState.cs - Reverted the changes to check for nulls on IBD check. Co-authored-by: Mithril Man <red.angel@libero.it> Co-authored-by: dangershony <dan.gershony@gmail.com>
1 parent 9db4d40 commit 7917a2f

138 files changed

Lines changed: 6130 additions & 1402 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Blockcore.sln

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Tests", "Tests\Bl
1111
EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "External", "External", "{1B9A916F-DDAC-4675-B424-EDEDC1A58231}"
1313
EndProject
14-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.WebHost.Tests", "Tests\Blockcore.Features.WebHost.Tests\Blockcore.Features.WebHost.Tests.csproj", "{E538B76A-513F-46D5-9F14-E35E79B484CA}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.NodeHost.Tests", "Tests\Blockcore.Features.NodeHost.Tests\Blockcore.Features.NodeHost.Tests.csproj", "{E538B76A-513F-46D5-9F14-E35E79B484CA}"
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.RPC", "Features\Blockcore.Features.RPC\Blockcore.Features.RPC.csproj", "{5E0010F1-50B8-4FC5-A9C2-38D7DF73A0D2}"
1717
EndProject
@@ -39,7 +39,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{EAE139C2
3939
EndProject
4040
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Features", "Features", "{15D29FFD-6142-4DC5-AFFD-10BA0CA55C45}"
4141
EndProject
42-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.WebHost", "Features\Blockcore.Features.WebHost\Blockcore.Features.WebHost.csproj", "{FB78D861-F3DF-412D-8A3D-D1756ADA39D4}"
42+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.NodeHost", "Features\Blockcore.Features.NodeHost\Blockcore.Features.NodeHost.csproj", "{FB78D861-F3DF-412D-8A3D-D1756ADA39D4}"
4343
EndProject
4444
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBitcoin", "External\NBitcoin\NBitcoin.csproj", "{2051FCAC-D4C7-498D-B935-2E9E6DEA0C29}"
4545
EndProject
@@ -63,8 +63,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.ColdStak
6363
EndProject
6464
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.ColdStaking.Tests", "Tests\Blockcore.Features.ColdStaking.Tests\Blockcore.Features.ColdStaking.Tests.csproj", "{0FE19EFE-98D7-4243-A8CC-30BCFFFC4499}"
6565
EndProject
66-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.SignalR", "Features\Blockcore.Features.SignalR\Blockcore.Features.SignalR.csproj", "{6BBFFE59-0597-4ADA-A592-CED25C05C01A}"
67-
EndProject
6866
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blockcore.Features.Diagnostic", "Features\Blockcore.Features.Diagnostic\Blockcore.Features.Diagnostic.csproj", "{53209209-7239-4FEB-BE8D-B2DF6145BBDA}"
6967
EndProject
7068
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StratisD", "Networks\Stratis\Stratisd\StratisD.csproj", "{B69D6038-8731-4DF3-9D93-3045C77CBDE6}"
@@ -235,10 +233,6 @@ Global
235233
{0FE19EFE-98D7-4243-A8CC-30BCFFFC4499}.Debug|Any CPU.Build.0 = Debug|Any CPU
236234
{0FE19EFE-98D7-4243-A8CC-30BCFFFC4499}.Release|Any CPU.ActiveCfg = Release|Any CPU
237235
{0FE19EFE-98D7-4243-A8CC-30BCFFFC4499}.Release|Any CPU.Build.0 = Release|Any CPU
238-
{6BBFFE59-0597-4ADA-A592-CED25C05C01A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
239-
{6BBFFE59-0597-4ADA-A592-CED25C05C01A}.Debug|Any CPU.Build.0 = Debug|Any CPU
240-
{6BBFFE59-0597-4ADA-A592-CED25C05C01A}.Release|Any CPU.ActiveCfg = Release|Any CPU
241-
{6BBFFE59-0597-4ADA-A592-CED25C05C01A}.Release|Any CPU.Build.0 = Release|Any CPU
242236
{53209209-7239-4FEB-BE8D-B2DF6145BBDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
243237
{53209209-7239-4FEB-BE8D-B2DF6145BBDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
244238
{53209209-7239-4FEB-BE8D-B2DF6145BBDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -334,7 +328,6 @@ Global
334328
{A8EDFB26-17B5-48C0-BF38-E521EF5476E4} = {EAE139C2-B19C-4905-9117-8A4068ABD3D2}
335329
{072C5F7C-4B75-46C9-A54D-774DA77ED1D1} = {15D29FFD-6142-4DC5-AFFD-10BA0CA55C45}
336330
{0FE19EFE-98D7-4243-A8CC-30BCFFFC4499} = {EAE139C2-B19C-4905-9117-8A4068ABD3D2}
337-
{6BBFFE59-0597-4ADA-A592-CED25C05C01A} = {15D29FFD-6142-4DC5-AFFD-10BA0CA55C45}
338331
{53209209-7239-4FEB-BE8D-B2DF6145BBDA} = {15D29FFD-6142-4DC5-AFFD-10BA0CA55C45}
339332
{B69D6038-8731-4DF3-9D93-3045C77CBDE6} = {0F6F0DC5-EC62-465B-8FC7-F24449CD7359}
340333
{221FF9A3-4BE5-44CB-91C8-885EFE06CB2E} = {78DF115B-2471-41C3-8D9C-360720855CF5}

src/Blockcore/Base/BaseFeature.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,11 @@ private void StartAddressManager(NetworkPeerConnectionParameters connectionParam
311311
/// <inheritdoc />
312312
public override void Dispose()
313313
{
314-
this.logger.LogInformation("Flushing peers.");
315-
this.flushAddressManagerLoop.Dispose();
314+
if (this.flushAddressManagerLoop != null)
315+
{
316+
this.logger.LogInformation("Flushing peers.");
317+
this.flushAddressManagerLoop.Dispose();
318+
}
316319

317320
this.logger.LogInformation("Disposing peer address manager.");
318321
this.peerAddressManager.Dispose();

src/Features/Blockcore.Features.SignalR/Broadcasters/BroadcasterBase.cs renamed to src/Blockcore/Broadcasters/BroadcasterBase.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
11
using System;
22
using System.Collections.Generic;
33
using Blockcore.AsyncWork;
4+
using Blockcore.EventBus;
45
using Blockcore.Utilities;
6+
using Microsoft.AspNetCore.SignalR;
57
using Microsoft.Extensions.Logging;
68

7-
namespace Blockcore.Features.SignalR.Broadcasters
9+
namespace Blockcore.Broadcasters
810
{
911
/// <summary>
10-
/// Base class for all SignalR Broadcasters
12+
/// Base class for all Web Socket Broadcasters
1113
/// </summary>
1214
public abstract class ClientBroadcasterBase : IClientEventBroadcaster
1315
{
14-
private readonly EventsHub eventsHub;
16+
private readonly IEventsSubscriptionService eventsHub;
1517
private readonly INodeLifetime nodeLifetime;
1618
private readonly IAsyncProvider asyncProvider;
17-
protected readonly ILogger logger;
1819
private IAsyncLoop asyncLoop;
20+
protected readonly ILogger log;
1921

2022
protected ClientBroadcasterBase(
21-
EventsHub eventsHub,
2223
ILoggerFactory loggerFactory,
2324
INodeLifetime nodeLifetime,
24-
IAsyncProvider asyncProvider)
25+
IAsyncProvider asyncProvider,
26+
IEventsSubscriptionService subscriptionService = null
27+
)
2528
{
26-
this.eventsHub = eventsHub;
29+
this.eventsHub = subscriptionService;
2730
this.nodeLifetime = nodeLifetime;
2831
this.asyncProvider = asyncProvider;
29-
this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
32+
this.log = loggerFactory.CreateLogger(this.GetType().FullName);
3033
}
3134

3235
public void Init(ClientEventBroadcasterSettings broadcasterSettings)
3336
{
34-
this.logger.LogDebug($"Initialising SignalR Broadcaster {this.GetType().Name}");
37+
this.log.LogDebug($"Initialising Web Socket Broadcaster {this.GetType().Name}");
38+
3539
this.asyncLoop = this.asyncProvider.CreateAndRunAsyncLoop(
3640
$"Broadcast {this.GetType().Name}",
3741
async token =>
3842
{
39-
foreach (IClientEvent clientEvent in this.GetMessages())
43+
foreach (EventBase clientEvent in this.GetMessages())
4044
{
41-
await this.eventsHub.SendToClientsAsync(clientEvent).ConfigureAwait(false);
45+
this.eventsHub.OnEvent(clientEvent);
4246
}
4347
},
4448
this.nodeLifetime.ApplicationStopping,
45-
repeatEvery: TimeSpan.FromSeconds(Math.Max(broadcasterSettings.BroadcastFrequencySeconds, 5)));
49+
repeatEvery: TimeSpan.FromSeconds(Math.Max(broadcasterSettings.BroadcastFrequencySeconds, 5)),
50+
startAfter: TimeSpans.FiveSeconds);
4651
}
4752

48-
protected abstract IEnumerable<IClientEvent> GetMessages();
53+
protected abstract IEnumerable<EventBase> GetMessages();
4954
}
5055
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Blockcore.Broadcasters
2+
{
3+
public class ClientEventBroadcasterSettings
4+
{
5+
public int BroadcastFrequencySeconds { get; set; }
6+
}
7+
}

src/Features/Blockcore.Features.SignalR/IClientEventBroadcaster.cs renamed to src/Blockcore/Broadcasters/IClientEventBroadcaster.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Blockcore.Features.SignalR
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace Blockcore.Broadcasters
24
{
35
public interface IClientEventBroadcaster
46
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Threading.Tasks;
2+
using Blockcore.Broadcasters;
3+
using Blockcore.EventBus;
4+
using Microsoft.AspNetCore.SignalR;
5+
6+
namespace Blockcore.Broadcasters
7+
{
8+
public interface IEventsSubscriptionService
9+
{
10+
void Init();
11+
12+
void Subscribe(string id, string name);
13+
14+
void Unsubscribe(string id, string name);
15+
16+
void UnsubscribeAll(string id);
17+
18+
void OnEvent(EventBase @event);
19+
20+
void SetHub<T>(IHubContext<T> hubContext) where T : Hub;
21+
}
22+
}

src/Blockcore/Configuration/NodeSettings.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ public class NodeSettings : IDisposable
109109
/// </summary>
110110
public FeeRate MinRelayTxFeeRate { get; private set; }
111111

112-
/// <summary>
113-
/// If true then the node will add and start the SignalR feature.
114-
/// </summary>
115-
public bool EnableSignalR { get; private set; }
116-
117112
/// <summary>
118113
/// Initializes a new instance of the object.
119114
/// </summary>
@@ -233,8 +228,6 @@ public NodeSettings(Network network = null, ProtocolVersion protocolVersion = Su
233228
this.ReadConfigurationFile();
234229
}
235230

236-
this.EnableSignalR = this.ConfigReader.GetOrDefault<bool>("enableSignalR", false, this.Logger);
237-
238231
// Create the custom logger factory.
239232
this.LoggerFactory.AddFilters(this.Log, this.DataFolder);
240233

src/Blockcore/EventBus/CoreEvents/BlockConnected.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Blockcore.Primitives;
2+
using NBitcoin;
3+
using Newtonsoft.Json;
24

35
namespace Blockcore.EventBus.CoreEvents
46
{
@@ -8,11 +10,20 @@ namespace Blockcore.EventBus.CoreEvents
810
/// <seealso cref="EventBase" />
911
public class BlockConnected : EventBase
1012
{
13+
[JsonIgnore]
1114
public ChainedHeaderBlock ConnectedBlock { get; }
1215

16+
public uint256 Hash { get; set; }
17+
18+
public int Height { get; set; }
19+
1320
public BlockConnected(ChainedHeaderBlock connectedBlock)
1421
{
1522
this.ConnectedBlock = connectedBlock;
23+
24+
this.Hash = this.ConnectedBlock.ChainedHeader.HashBlock;
25+
26+
this.Height = this.ConnectedBlock.ChainedHeader.Height;
1627
}
1728
}
1829
}

src/Blockcore/EventBus/CoreEvents/BlockDisconnected.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Blockcore.Primitives;
2+
using NBitcoin;
3+
using Newtonsoft.Json;
24

35
namespace Blockcore.EventBus.CoreEvents
46
{
@@ -8,11 +10,20 @@ namespace Blockcore.EventBus.CoreEvents
810
/// <seealso cref="EventBase" />
911
public class BlockDisconnected : EventBase
1012
{
13+
[JsonIgnore]
1114
public ChainedHeaderBlock DisconnectedBlock { get; }
1215

16+
public uint256 Hash { get; set; }
17+
18+
public int Height { get; set; }
19+
1320
public BlockDisconnected(ChainedHeaderBlock disconnectedBlock)
1421
{
1522
this.DisconnectedBlock = disconnectedBlock;
23+
24+
this.Hash = this.DisconnectedBlock.ChainedHeader.HashBlock;
25+
26+
this.Height = this.DisconnectedBlock.ChainedHeader.Height;
1627
}
1728
}
1829
}

src/Blockcore/EventBus/CoreEvents/Peer/PeerEventBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net;
2+
using Newtonsoft.Json;
23

34
namespace Blockcore.EventBus.CoreEvents.Peer
45
{

0 commit comments

Comments
 (0)