Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hawthorne-abendsen committed May 6, 2020
1 parent b76148e commit 848c34f
Show file tree
Hide file tree
Showing 26 changed files with 436 additions and 83 deletions.
3 changes: 3 additions & 0 deletions Centaurus.Common/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public abstract class BaseSettings
[Option("connection_string", Required = true, HelpText = "Database connection string.")]
public string ConnectionString { get; set; }

[Option("extensions_config_file_path", Required = false, HelpText = "Path to extensions config file.")]
public string ExtensionsConfigFilePath { get; set; }

public virtual void Build()
{
KeyPair = KeyPair.FromSecretSeed(Secret);
Expand Down
14 changes: 10 additions & 4 deletions Centaurus.DAL/Mongo/MongoStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,23 @@ public override async Task Update(DiffObject update)
if (update.StellarInfoData != null)
updateTasks.Add(constellationStateCollection.BulkWriteAsync(GetStellarDataUpdate(update.StellarInfoData)));

updateTasks.Add(accountsCollection.BulkWriteAsync(GetAccountUpdates(update.Accounts)));
if (update.Accounts != null && update.Accounts.Count > 0)
updateTasks.Add(accountsCollection.BulkWriteAsync(GetAccountUpdates(update.Accounts)));

updateTasks.Add(balancesCollection.BulkWriteAsync(GetBalanceUpdates(update.Balances)));
if (update.Balances != null && update.Balances.Count > 0)
updateTasks.Add(balancesCollection.BulkWriteAsync(GetBalanceUpdates(update.Balances)));

updateTasks.Add(ordersCollection.BulkWriteAsync(GetOrderUpdates(update.Orders)));
if (update.Orders != null && update.Orders.Count > 0)
updateTasks.Add(ordersCollection.BulkWriteAsync(GetOrderUpdates(update.Orders)));

updateTasks.Add(withdrawalsCollection.BulkWriteAsync(GetWithdrawalsUpdates(update.Widthrawals)));
if (update.Widthrawals != null && update.Widthrawals.Count > 0)
updateTasks.Add(withdrawalsCollection.BulkWriteAsync(GetWithdrawalsUpdates(update.Widthrawals)));

updateTasks.Add(quantaCollection.InsertManyAsync(update.Quanta));
updateTasks.Add(effectsCollection.InsertManyAsync(update.Effects));

await Task.WhenAll(updateTasks);

await session.CommitTransactionAsync();
}
catch
Expand Down
2 changes: 1 addition & 1 deletion Centaurus.Domain/Catchups/AlphaCatchup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static async Task ApplyAuditorsData()

alphaStateManager.AlphaRised();

Notifier.NotifyAuditors(alphaStateManager.GetCurrentAlphaState());
Notifier.NotifyAuditors(alphaStateManager.GetCurrentAlphaState().CreateEnvelope());
}

private static async Task ApplyQuanta(List<MessageEnvelope> quanta)
Expand Down
8 changes: 8 additions & 0 deletions Centaurus.Domain/Effects/EffectProcessorsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,13 @@ public void AddNonceUpdate(Account account, ulong newNonce, ulong currentNonce)
account
));
}

public void AddLedgerCommit(LedgerManager ledgerManager, long newLedger, long prevLedger)
{
Add(new LedgerUpdateEffectProcessor(
new LedgerUpdateEffect { Apex = Apex, Ledger = newLedger, PrevLedger = prevLedger },
ledgerManager
));
}
}
}
13 changes: 0 additions & 13 deletions Centaurus.Domain/Effects/LedgerUpdateEffectProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,5 @@ public override void RevertEffect()
MarkAsProcessed();
ledgerManager.SetLedger(Effect.PrevLedger);
}

public static LedgerUpdateEffectProcessor GetProcessor(long apex, long ledger, LedgerManager ledgerManager)
{
return GetProcessor(
new LedgerUpdateEffect { Apex = apex, Ledger = ledger, PrevLedger = ledgerManager.Ledger },
ledgerManager
);
}

public static LedgerUpdateEffectProcessor GetProcessor(LedgerUpdateEffect effect, LedgerManager ledgerManager)
{
return new LedgerUpdateEffectProcessor(effect, ledgerManager);
}
}
}
11 changes: 11 additions & 0 deletions Centaurus.Domain/Extensions/ExtensionConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Centaurus.Domain
{
public class ExtensionConfig
{
public List<ExtensionConfigItem> Extensions { get; set; }
}
}
15 changes: 15 additions & 0 deletions Centaurus.Domain/Extensions/ExtensionConfigItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Centaurus.Domain
{
public class ExtensionConfigItem
{
public bool IsDisabled { get; set; }

public string Name { get; set; }

public Dictionary<string, string> ExtensionConfig { get; set; }
}
}
43 changes: 43 additions & 0 deletions Centaurus.Domain/Extensions/ExtensionItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Centaurus.Domain
{
public class ExtensionItem
{
private ExtensionItem(string name, Version version, Dictionary<string, string> config, IExtension extensionInstance)
{
Name = name;
Version = version;
ExtensionInstance = extensionInstance;
Config = config;
}

public string Name { get; }

public Version Version { get; }

public IExtension ExtensionInstance { get; }

public Dictionary<string, string> Config { get; }

public static ExtensionItem Load(ExtensionConfigItem extensionConfigItem)
{
var extensionPath = Path.Combine(Path.GetDirectoryName(Global.Settings.ExtensionsConfigFilePath), extensionConfigItem.Name + ".dll");
if (!File.Exists(extensionPath))
throw new Exception($"Extension {extensionConfigItem.Name} is not found.");
var extensionAssenmbly = Assembly.LoadFile(extensionPath);
var extensionTypes = extensionAssenmbly.GetTypes().Where(t => typeof(IExtension).IsAssignableFrom(t));
if (extensionTypes.Count() < 1)
throw new Exception($"Extension {extensionConfigItem.Name} doesn't contain types that implement IExtension interface.");
else if (extensionTypes.Count() > 1)
throw new Exception($"Extension {extensionConfigItem.Name} contains multiple types that implement IExtension interface.");

return new ExtensionItem(extensionConfigItem.Name, extensionAssenmbly.GetName().Version, extensionConfigItem.ExtensionConfig, (IExtension)Activator.CreateInstance(extensionTypes.First()));
}
}
}
149 changes: 149 additions & 0 deletions Centaurus.Domain/Extensions/ExtensionsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using Centaurus.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Centaurus.Domain
{
public class EnvelopeEventArgs
{
public BaseWebSocketConnection Connection { get; set; }

public MessageEnvelope Message { get; set; }

}

public class EnvelopeErrorEventArgs : EnvelopeEventArgs
{
public Exception Exception { get; set; }
}

public class NotifyEventArgs
{
public RawPubKey Account { get; set; }
public MessageEnvelope Envelope { get; set; }
}

public class ExtensionsManager
{
public async Task RegisterAllExtensions()
{
if (string.IsNullOrWhiteSpace(Global.Settings.ExtensionsConfigFilePath))
return;

var configFilePath = Path.GetFullPath(Global.Settings.ExtensionsConfigFilePath);
if (!File.Exists(configFilePath))
throw new Exception("Extensions config file is not found.");

var extensionConfig = JsonConvert.DeserializeObject<ExtensionConfig>(File.ReadAllText(configFilePath));

foreach (var configItem in extensionConfig.Extensions)
{
var extension = ExtensionItem.Load(configItem);
await extension.ExtensionInstance.Init(configItem.ExtensionConfig);

extensions.Add(extension);
}
}

private List<ExtensionItem> extensions = new List<ExtensionItem>();

public IEnumerable<ExtensionItem> Extensions => extensions;

public event EventHandler<WebSocket> OnBeforeNewConnection;
public event EventHandler<BaseWebSocketConnection> OnConnectionValidated;
public void ConnectionValidated(BaseWebSocketConnection args)
{
OnConnectionValidated?.Invoke(this, args);
}
public void BeforeNewConnection(WebSocket args)
{
OnBeforeNewConnection?.Invoke(this, args);
}

public event EventHandler<EnvelopeErrorEventArgs> OnHandleMessageFailed;
public void HandleMessageFailed(EnvelopeErrorEventArgs args)
{
OnHandleMessageFailed?.Invoke(this, args);
}

public event EventHandler<EnvelopeEventArgs> OnBeforeSendMessage;
public event EventHandler<EnvelopeEventArgs> OnAfterSendMessage;
public event EventHandler<EnvelopeErrorEventArgs> OnSendMessageFailed;
public void BeforeSendMessage(EnvelopeEventArgs args)
{
OnBeforeSendMessage?.Invoke(this, args);
}
public void AfterSendMessage(EnvelopeEventArgs args)
{
OnAfterSendMessage?.Invoke(this, args);
}
public void SendMessageFailed(EnvelopeErrorEventArgs args)
{
OnSendMessageFailed?.Invoke(this, args);
}

public event EventHandler<BaseWebSocketConnection> OnBeforeConnectionClose;

public void BeforeConnectionClose(BaseWebSocketConnection args)
{
OnBeforeConnectionClose?.Invoke(this, args);
}

public event EventHandler<NotifyEventArgs> OnBeforeNotify;
public event EventHandler<MessageEnvelope> OnBeforeNotifyAuditors;


public void BeforeNotify(NotifyEventArgs args)
{
OnBeforeNotify?.Invoke(this, args);
}

public void BeforeNotifyAuditors(MessageEnvelope args)
{
OnBeforeNotifyAuditors?.Invoke(this, args);
}

public event EventHandler<EnvelopeEventArgs> OnBeforeValidateMessage;
public event EventHandler<EnvelopeEventArgs> OnAfterValidateMessage;
public event EventHandler<EnvelopeEventArgs> OnBeforeHandleMessage;
public event EventHandler<EnvelopeEventArgs> OnAfterHandleMessage;

public void BeforeValidateMessage(EnvelopeEventArgs args)
{
OnBeforeValidateMessage?.Invoke(this, args);
}

public void AfterValidateMessage(EnvelopeEventArgs args)
{
OnAfterValidateMessage?.Invoke(this, args);
}

public void BeforeHandleMessage(EnvelopeEventArgs args)
{
OnBeforeHandleMessage?.Invoke(this, args);
}

public void AfterHandleMessage(EnvelopeEventArgs args)
{
OnAfterHandleMessage?.Invoke(this, args);
}

public event EventHandler<MessageEnvelope> OnBeforeQuantumHandle;
public event EventHandler<ResultMessage> OnAfterQuantumHandle;
public void BeforeQuantumHandle(MessageEnvelope args)
{
OnBeforeQuantumHandle?.Invoke(this, args);
}

public void AfterQuantumHandle(ResultMessage args)
{
OnAfterQuantumHandle?.Invoke(this, args);
}
}
}
12 changes: 12 additions & 0 deletions Centaurus.Domain/Extensions/IExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Centaurus.Domain
{
public interface IExtension
{
Task Init(Dictionary<string, string> settings);
}
}
7 changes: 7 additions & 0 deletions Centaurus.Domain/Global.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Timers;
Expand All @@ -21,6 +22,8 @@ public static class Global
/// <param name="storage">Permanent storage object</param>
public static void Init(BaseSettings settings, BaseStorage storage)
{
ExtensionsManager = new ExtensionsManager();

Settings = settings ?? throw new ArgumentNullException(nameof(settings));

DynamicSerializersInitializer.Init();
Expand Down Expand Up @@ -79,6 +82,9 @@ public static void Setup(Snapshot snapshot)
WithdrawalStorage = new WithdrawalStorage(snapshot.Withdrawals);

LedgerManager = new LedgerManager(snapshot.Ledger);

ExtensionsManager = new ExtensionsManager();
ExtensionsManager.RegisterAllExtensions().Wait();
}

public static Exchange Exchange { get; private set; }
Expand Down Expand Up @@ -107,6 +113,7 @@ private set
public static AuditLedgerManager AuditLedgerManager { get; private set; }
public static AuditResultManager AuditResultManager { get; private set; }
public static LedgerManager LedgerManager { get; private set; }
public static ExtensionsManager ExtensionsManager { get; private set; }
public static StateManager AppState { get; private set; }
public static QuantumProcessorsStorage QuantumProcessor { get; private set; }

Expand Down
5 changes: 5 additions & 0 deletions Centaurus.Domain/MessageHandlers/Common/MessageHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ public static async Task<bool> HandleMessage(T connetction, MessageEnvelope enve
return false;

var handler = handlers[envelope.Message.MessageType];
var envelopeArgs = new EnvelopeEventArgs { Connection = connetction, Message = envelope };
Global.ExtensionsManager.BeforeValidateMessage(envelopeArgs);
await handler.Validate(connetction, envelope);
Global.ExtensionsManager.AfterValidateMessage(envelopeArgs);
Global.ExtensionsManager.BeforeHandleMessage(envelopeArgs);
await handler.HandleMessage(connetction, envelope);
Global.ExtensionsManager.AfterHandleMessage(envelopeArgs);
return true;
}
}
Expand Down
Loading

0 comments on commit 848c34f

Please sign in to comment.