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/alpha switch #86

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions Centaurus.Common/Common/SettingsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Centaurus
{
public static class SettingsExtensions
{
public static bool IsPrimeNode(this Settings settings)
{
if (settings == null)
throw new ArgumentNullException(nameof(settings));

return settings.ParticipationLevel == ParticipationLevel.Prime;
}
}
}
2 changes: 1 addition & 1 deletion Centaurus.Common/Helpers/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static bool TryCreateUriBuilder(string address, bool useSecureConnection,
public static bool TryCreateWsConnection(string address, bool useSecureConnection, out Uri uri)
{
uri = null;
if (!TryCreateUriBuilder(address, useSecureConnection, out var uriBuilder))
if (!(address != null && TryCreateUriBuilder(address, useSecureConnection, out var uriBuilder)))
return false;
uriBuilder.Scheme = useSecureConnection ? "wss" : "ws";
uri = uriBuilder.Uri;
Expand Down
12 changes: 12 additions & 0 deletions Centaurus.Common/Settings/ParticipationLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Centaurus
{
public enum ParticipationLevel
{
Prime = 1,
Auditor = 2
}
}
7 changes: 5 additions & 2 deletions Centaurus.Common/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public class Settings
[Option("sync_batch_size", Default = 500, HelpText = "Max quanta sync batch size.")]
public int SyncBatchSize { get; set; }

[Option("participation_level", Required = true, HelpText = "Centaurus node participation level. '1' = Prime and '2' = Auditor")]
public int ParticipationLevel { get; set; }
[Option("catchup_timeout", Default = 15, HelpText = "Catchup timeout in seconds.")]
public int CatchupTimeout { get; set; }

[Option("participation_level", Required = true, HelpText = "Centaurus node participation level. '1' or 'Prime' for the prime nodes, and '2' or 'Auditor' for the auditor nodes.")]
public ParticipationLevel ParticipationLevel { get; set; }

[Option("payment_config", Required = true, HelpText = "Payment providers config path.")]
public string PaymentConfigPath { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using Centaurus.Models;

namespace Centaurus
namespace Centaurus.Domain.Models
{
public class ConstellationInfo
{
public string Alpha { get; set; }

public ulong Apex { get; set; }

public string PubKey { get; set; }

public State State { get; set; }

public ProviderSettings[] Providers { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Centaurus.Domain.Models/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Snapshot

public byte[] LastHash { get; set; }

public ConstellationSettings Settings { get; set; }
public ConstellationSettings ConstellationSettings { get; set; }

public List<Account> Accounts { get; set; }

Expand Down
267 changes: 137 additions & 130 deletions Centaurus.Domain/Catchups/Catchup.cs

Large diffs are not rendered by default.

102 changes: 63 additions & 39 deletions Centaurus.Domain/Centaurus.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,48 +1,72 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>0.1.21</AssemblyVersion>
<FileVersion>0.1.21</FileVersion>
<Version>0.1.21</Version>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>0.1.21</AssemblyVersion>
<FileVersion>0.1.21</FileVersion>
<Version>0.1.21</Version>
</PropertyGroup>

<ItemGroup>
<Compile Remove="WebSockets\Info\Subscriptions\**" />
<EmbeddedResource Remove="WebSockets\Info\Subscriptions\**" />
<None Remove="WebSockets\Info\Subscriptions\**" />
</ItemGroup>
<ItemGroup>
<Compile Remove="WebSockets\Info\Subscriptions\**" />
<EmbeddedResource Remove="WebSockets\Info\Subscriptions\**" />
<None Remove="WebSockets\Info\Subscriptions\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Catchups\AuditorCatchup.cs" />
<Compile Remove="WebSockets\Info\Messages\MarketUpdate.cs" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Catchups\AuditorCatchup.cs" />
<Compile Remove="WebSockets\Info\Messages\MarketUpdate.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="WebSockets\Info\Subscriptions\AllMarketTickersSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\BaseSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\DepthsSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\MarketTickerSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\PerformanceStatisticsSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\PriceHistorySubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\SubscriptionsManager.cs" />
<Compile Include="WebSockets\Info\Subscriptions\TradesFeedSubscription.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="WebSockets\Info\Subscriptions\AllMarketTickersSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\BaseSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\DepthsSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\MarketTickerSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\PerformanceStatisticsSubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\PriceHistorySubscription.cs" />
<Compile Include="WebSockets\Info\Subscriptions\SubscriptionsManager.cs" />
<Compile Include="WebSockets\Info\Subscriptions\TradesFeedSubscription.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="4.7.8" />
<PackageReference Include="System.Data.HashFunction.FarmHash" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.8" />
<PackageReference Include="System.Data.HashFunction.FarmHash" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Centaurus.Common\Centaurus.Common.csproj" />
<ProjectReference Include="..\Centaurus.Domain.Models\Centaurus.Domain.Models.csproj" />
<ProjectReference Include="..\Centaurus.DynamicSerializers\Centaurus.DynamicSerializers.csproj" />
<ProjectReference Include="..\Centaurus.Exchange.Analytics\Centaurus.Exchange.Analytics.csproj" />
<ProjectReference Include="..\Centaurus.Models.Extensions\Centaurus.Models.Extensions.csproj" />
<ProjectReference Include="..\Centaurus.Models\Centaurus.Models.csproj" />
<ProjectReference Include="..\Centaurus.PaymentProvider\Centaurus.PaymentProvider.csproj" />
<ProjectReference Include="..\Centaurus.PersistentStorage.Abstraction\Centaurus.PersistentStorage.Abstraction.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Centaurus.Common\Centaurus.Common.csproj" />
<ProjectReference Include="..\Centaurus.Domain.Models\Centaurus.Domain.Models.csproj" />
<ProjectReference Include="..\Centaurus.DynamicSerializers\Centaurus.DynamicSerializers.csproj" />
<ProjectReference Include="..\Centaurus.Exchange.Analytics\Centaurus.Exchange.Analytics.csproj" />
<ProjectReference Include="..\Centaurus.Models.Extensions\Centaurus.Models.Extensions.csproj" />
<ProjectReference Include="..\Centaurus.Models\Centaurus.Models.csproj" />
<ProjectReference Include="..\Centaurus.PaymentProvider\Centaurus.PaymentProvider.csproj" />
<ProjectReference Include="..\Centaurus.PersistentStorage.Abstraction\Centaurus.PersistentStorage.Abstraction.csproj" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Centaurus.Test.Domain</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Centaurus.Test.Exchange.Analytics</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Centaurus.Test.Integration</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Centaurus.Test.Utils</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
193 changes: 193 additions & 0 deletions Centaurus.Domain/Contexts/ConstellationSettingsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using Centaurus.Models;
using Microsoft.Extensions.Caching.Memory;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Centaurus.Domain
{
/// <summary>
/// This class holds recent constellation settings, to be able to obtain relevant node ids
/// </summary>
public class ConstellationSettingsManager : ContextualBase, IDisposable
{
private ConstellationSettingsCollection settingsCache;

public ConstellationSettingsManager(ExecutionContext context, ConstellationSettings currentSettings)
: base(context)
{
settingsCache = new ConstellationSettingsCollection(Context.DataProvider.GetConstellationSettings);
if (currentSettings != null)
Update(currentSettings);
}

public ConstellationSettings Current { get; private set; }

public void Update(ConstellationSettings newSettings)
{
if (newSettings == null)
throw new ArgumentNullException(nameof(newSettings));

if (Current != null && Current.Apex >= newSettings.Apex)
throw new ArgumentException("New constellation settings apex is lower than the current one.", nameof(newSettings));

settingsCache.Add(newSettings);
Current = newSettings;
}

public bool TryGetForApex(ulong apex, out ConstellationSettings apexSettings)
{
var current = Current;
if (apex > current.Apex)
apexSettings = current;
else
settingsCache.TryGetSettings(apex, out apexSettings);
return apexSettings != null;
}

public void Dispose()
{
settingsCache.Dispose();
}
}

class ConstellationSettingsCollection : IDisposable
{
public ConstellationSettingsCollection(Func<ulong, ConstellationSettings> constellationSettingsDataProvider)
{
getConstellationSettings = constellationSettingsDataProvider ?? throw new ArgumentNullException(nameof(constellationSettingsDataProvider));
InitCleanupTimer();
}

public void Add(ConstellationSettings settings)
{
lock (syncRoot)
{
var settingsWrapper = new ConstellationSettingsWrapper(settings);
settingsCache.Add(settingsWrapper);
}
}

public bool TryGetSettings(ulong apex, out ConstellationSettings settings)
{
settings = null;
lock (syncRoot)
{
//try to find the settings in cache
for (int i = settingsCache.Count; i-- > 0;)
{
var currentSettings = settingsCache[i];
if (apex >= currentSettings.Apex)
{
currentSettings.AccessDate = DateTime.UtcNow;
settings = currentSettings.Value;
return true;
}
}

//try to load from db
var lastLoadedSettingsApex = settingsCache.First().Apex;
while (true)
{
var prevSettingsLastApex = lastLoadedSettingsApex - 1;
var loadedItem = getConstellationSettings(apex);
//db doesn't contains settings for the apex
if (loadedItem == null)
break;
var item = InsertFirst(loadedItem, prevSettingsLastApex);
if (apex >= item.Apex)
{
settings = item.Value;
return true;
}
//set last loaded apex
lastLoadedSettingsApex = item.Apex;
}
//no settings found
return false;
}
}

public void Dispose()
{
cleanupTimer.Dispose();
}

private ConstellationSettingsWrapper InsertFirst(ConstellationSettings settings, ulong validToApex)
{
lock (syncRoot)
{
var settingsWrapper = new ConstellationSettingsWrapper(settings);
settingsCache.Insert(0, settingsWrapper);
return settingsWrapper;
}
}

private static Logger logger = LogManager.GetCurrentClassLogger();

private List<ConstellationSettingsWrapper> settingsCache = new List<ConstellationSettingsWrapper>();

private void InitCleanupTimer()
{
cleanupTimer = new Timer();
cleanupTimer.AutoReset = false;
cleanupTimer.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds;
cleanupTimer.Elapsed += CleanupTimer_Elapsed;
cleanupTimer.Start();
}

private void CleanupTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
lock (syncRoot)
{
//cache 1000 items
if (settingsCache.Count < 1000)
return;
var currentDate = DateTime.UtcNow;
foreach (var settings in settingsCache)
{
if (currentDate - settings.AccessDate > TimeSpan.FromSeconds(15))
{
settingsCache.RemoveAt(0);
continue;
}
//break cleanup to keep settings chain
break;
}
}
}
catch (Exception exc)
{
logger.Error(exc, "Error during settings cache cleanup.");
}
finally
{
cleanupTimer.Start();
}
}

private object syncRoot = new { };
private Timer cleanupTimer;
private Func<ulong, ConstellationSettings> getConstellationSettings;

class ConstellationSettingsWrapper
{
public ConstellationSettingsWrapper(ConstellationSettings settings)
{
Value = settings ?? throw new ArgumentNullException(nameof(settings));
AccessDate = DateTime.UtcNow;
}

public ulong Apex => Value.Apex;

public ConstellationSettings Value { get; }

public DateTime AccessDate { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion Centaurus.Domain/Contexts/ExecutionContext.Analytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void DisposeAnalyticsManager()

private void AnalyticsManager_OnError(Exception exc)
{
StateManager.Failed(new Exception("Analytics manager error.", exc));
NodesManager.CurrentNode.Failed(new Exception("Analytics manager error.", exc));
}

private void Exchange_OnUpdates(ExchangeUpdate updates)
Expand Down
Loading