Beispiel #1
0
        /// <summary>
        /// Setups Global object
        /// </summary>
        /// <param name="settings">Application config</param>
        /// <param name="storage">Permanent storage object</param>
        public static async Task Setup(BaseSettings settings, IStorage storage)
        {
            ExtensionsManager = new ExtensionsManager();

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

            DynamicSerializersInitializer.Init();

            IsAlpha = Settings is AlphaSettings;

            PermanentStorage = storage;
            await PermanentStorage.OpenConnection(settings.ConnectionString);

            PersistenceManager = new PersistenceManager(PermanentStorage);

            StellarNetwork   = new StellarNetwork(Settings.NetworkPassphrase, Settings.HorizonUrl);
            QuantumProcessor = new QuantumProcessorsStorage();

            PendingUpdatesManager = new PendingUpdatesManager();
            PendingUpdatesManager.OnBatchSaved += PendingUpdatesManager_OnBatchSaved;

            AppState = IsAlpha ? new AlphaStateManager() : (StateManager) new AuditorStateManager();
            AppState.StateChanged += AppState_StateChanged;

            //try to load last settings, we need it to know current auditors
            var lastHash = new byte[] { };
            var lastApex = await PersistenceManager.GetLastApex();

            if (lastApex >= 0)
            {
                var lastQuantum = await PersistenceManager.GetQuantum(lastApex);

                lastHash = lastQuantum.Message.ComputeHash();
                logger.Trace($"Last hash is {Convert.ToBase64String(lastHash)}");
                var snapshot = await PersistenceManager.GetSnapshot(lastApex);
                await Setup(snapshot);

                if (IsAlpha)
                {
                    AppState.State = ApplicationState.Rising;//Alpha should ensure that it has all quanta from auditors
                }
                else
                {
                    AppState.State = ApplicationState.Running;
                }
            }
            else
            {
                //if no snapshot, the app is in init state
                AppState.State = ApplicationState.WaitingForInit;
            }

            var lastQuantumApex = lastApex < 0 ? 0 : lastApex;

            QuantumStorage = IsAlpha
                ? (QuantumStorageBase) new AlphaQuantumStorage(lastQuantumApex, lastHash)
                : new AuditorQuantumStorage(lastQuantumApex, lastHash);

            QuantumHandler = new QuantumHandler(QuantumStorage.CurrentApex);
        }
Beispiel #2
0
 public override void Dispose()
 {
     base.Dispose();
     QuantumHandler.Dispose();
     AuditLedgerManager?.Dispose();
     AuditResultManager?.Dispose();
     DisposeAnalyticsManager().Wait();
     DisposePerformanceStatisticsManager();
 }
Beispiel #3
0
        private async Task Cleanup(Dictionary <byte[], WithdrawalWrapper> withdrawals)
        {
            byte[][] expiredTransactions = null;
            var      currentTimeSeconds  = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

            expiredTransactions = withdrawals.Where(w => w.Value.IsExpired(currentTimeSeconds)).Select(w => w.Key).ToArray();

            if (expiredTransactions.Length < 1)
            {
                return;
            }

            //we must ignore all transactions that was submitted. TxListener will handle submitted transactions.
            var unhandledTxs = await GetUnhandledTx();

            foreach (var expiredTransaction in expiredTransactions.Where(tx => !unhandledTxs.Contains(tx, ByteArrayComparer.Default)))
            {
                _ = QuantumHandler.HandleAsync(new WithrawalsCleanupQuantum {
                    ExpiredWithdrawal = expiredTransaction
                }.CreateEnvelope());
            }
        }
        public virtual async Task Init()
        {
            DynamicSerializersInitializer.Init();

            await PermanentStorage.OpenConnection(Settings.ConnectionString);

            //try to load last settings, we need it to know current auditors
            var lastHash = new byte[] { };
            var lastApex = await PersistenceManager.GetLastApex();

            if (lastApex >= 0)
            {
                var lastQuantum = await PersistenceManager.GetQuantum(lastApex);

                lastHash = lastQuantum.Message.ComputeHash();
                var snapshot = await PersistenceManager.GetSnapshot(lastApex);
                await Setup(snapshot);

                if (IsAlpha)
                {
                    AppState.State = ApplicationState.Rising;//Alpha should ensure that it has all quanta from auditors
                }
                else
                {
                    AppState.State = ApplicationState.Running;
                }
            }
            else
            {
                //if no snapshot, the application is in initialization state
                AppState.State = ApplicationState.WaitingForInit;
            }

            var lastQuantumApex = lastApex < 0 ? 0 : lastApex;

            QuantumStorage.Init(lastQuantumApex, lastHash);
            QuantumHandler.Start();
        }
 public override void Dispose()
 {
     base.Dispose();
     QuantumHandler?.Dispose();
     DisposePerformanceStatisticsManager();
 }