Exemple #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);
        }
        /// <param name="settings">Application config</param>
        /// <param name="storage">Permanent storage object</param>
        /// <param name="useLegacyOrderbook"></param>
        public ExecutionContext(BaseSettings settings, IStorage storage, StellarDataProviderBase stellarDataProvider, bool useLegacyOrderbook = false)
        {
            PermanentStorage    = storage ?? throw new ArgumentNullException(nameof(storage));
            Settings            = settings ?? throw new ArgumentNullException(nameof(settings));
            StellarDataProvider = stellarDataProvider ?? throw new ArgumentNullException(nameof(settings));

            ExtensionsManager = new ExtensionsManager(settings.ExtensionsConfigFilePath);

            PersistenceManager = new PersistenceManager(PermanentStorage);
            QuantumProcessor   = new QuantumProcessorsStorage();

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

            QuantumStorage = new QuantumStorage();

            this.useLegacyOrderbook = useLegacyOrderbook;
        }