示例#1
0
    public async Task InitializeAsync(CancellationToken cancel = default)
    {
        using (BenchmarkLogger.Measure())
        {
            using (await IndexLock.LockAsync(cancel).ConfigureAwait(false))
                using (await MatureIndexAsyncLock.LockAsync(cancel).ConfigureAwait(false))
                    using (await ImmatureIndexAsyncLock.LockAsync(cancel).ConfigureAwait(false))
                    {
                        await EnsureBackwardsCompatibilityAsync().ConfigureAwait(false);

                        if (Network == Network.RegTest)
                        {
                            MatureIndexFileManager.DeleteMe();             // RegTest is not a global ledger, better to delete it.
                            ImmatureIndexFileManager.DeleteMe();
                        }
                        cancel.ThrowIfCancellationRequested();

                        if (!MatureIndexFileManager.Exists())
                        {
                            await MatureIndexFileManager.WriteAllLinesAsync(new[] { StartingFilter.ToLine() }, CancellationToken.None).ConfigureAwait(false);
                        }
                        cancel.ThrowIfCancellationRequested();

                        await InitializeFiltersAsync(cancel).ConfigureAwait(false);
                    }
        }
    }
        private async Task InitializeFiltersAsync()
        {
            try
            {
                if (MatureIndexFileManager.Exists())
                {
                    using var sr = MatureIndexFileManager.OpenText();
                    if (!sr.EndOfStream)
                    {
                        var    lineTask = sr.ReadLineAsync();
                        string line     = null;
                        while (lineTask != null)
                        {
                            if (line is null)
                            {
                                line = await lineTask.ConfigureAwait(false);
                            }

                            lineTask = sr.EndOfStream ? null : sr.ReadLineAsync();

                            ProcessLine(line, enqueue: false);

                            line = null;
                        }
                    }
                }
            }
            catch
            {
                // We found a corrupted entry. Stop here.
                // Delete the currupted file.
                // Do not try to autocorrect, because the internal data structures are throwing events that may confuse the consumers of those events.
                Logger.LogError("Mature index got corrupted. Deleting both mature and immature index...");
                MatureIndexFileManager.DeleteMe();
                ImmatureIndexFileManager.DeleteMe();
                throw;
            }

            try
            {
                if (ImmatureIndexFileManager.Exists())
                {
                    foreach (var line in await ImmatureIndexFileManager.ReadAllLinesAsync().ConfigureAwait(false))                     // We can load ImmatureIndexFileManager to the memory, no problem.
                    {
                        ProcessLine(line, enqueue: true);
                    }
                }
            }
            catch
            {
                // We found a corrupted entry. Stop here.
                // Delete the currupted file.
                // Do not try to autocorrect, because the internal data structures are throwing events that may confuse the consumers of those events.
                Logger.LogError("Immature index got corrupted. Deleting it...");
                ImmatureIndexFileManager.DeleteMe();
                throw;
            }
        }
示例#3
0
        private async Task InitializeFiltersAsync()
        {
            try
            {
                Height height = StartingHeight;

                if (MatureIndexFileManager.Exists())
                {
                    using (var sr = MatureIndexFileManager.OpenText())
                    {
                        if (!sr.EndOfStream)
                        {
                            var    lineTask = sr.ReadLineAsync();
                            string line     = null;
                            while (lineTask != null)
                            {
                                if (line is null)
                                {
                                    line = await lineTask;
                                }

                                lineTask = sr.EndOfStream ? null : sr.ReadLineAsync();

                                ProcessLine(height, line, enqueue: false);
                                height++;

                                line = null;
                            }
                        }
                    }
                }

                if (ImmatureIndexFileManager.Exists())
                {
                    foreach (var line in await ImmatureIndexFileManager.ReadAllLinesAsync())                     // We can load ImmatureIndexFileManager to the memory, no problem.
                    {
                        ProcessLine(height, line, enqueue: true);
                        height++;
                    }
                }
            }
            catch
            {
                // We found a corrupted entry. Stop here.
                // Delete the currupted file.
                // Don't try to autocorrect, because the internal data structures are throwing events those may confuse the consumers of those events.
                Logger.LogError <IndexStore>("An index file got corrupted. Deleting index files...");
                MatureIndexFileManager.DeleteMe();
                ImmatureIndexFileManager.DeleteMe();
                throw;
            }
        }
示例#4
0
    private async Task DeleteIfDeprecatedAsync(DigestableSafeIoManager ioManager)
    {
        string?firstLine;

        using (var content = ioManager.OpenText())
        {
            firstLine = await content.ReadLineAsync().ConfigureAwait(false);
        }

        try
        {
            FilterModel.FromLine(firstLine);
        }
        catch
        {
            Logger.LogWarning("Old Index file detected. Deleting it.");
            MatureIndexFileManager.DeleteMe();
            ImmatureIndexFileManager.DeleteMe();
            Logger.LogWarning("Successfully deleted old Index file.");
        }
    }
示例#5
0
    public async Task <IEnumerable <FilterModel> > RemoveAllImmmatureFiltersAsync(CancellationToken cancel, bool deleteAndCrashIfMature = false)
    {
        var removed = new List <FilterModel>();

        using (await IndexLock.LockAsync(cancel).ConfigureAwait(false))
        {
            if (ImmatureFilters.Any())
            {
                Logger.LogWarning($"Filters got corrupted. Reorging {ImmatureFilters.Count} immature filters in an attempt to fix them.");
            }
            else
            {
                Logger.LogCritical($"Filters got corrupted and have no more immature filters.");

                if (deleteAndCrashIfMature)
                {
                    Logger.LogCritical($"Deleting all filters and crashing the software...");

                    using (await MatureIndexAsyncLock.LockAsync(cancel).ConfigureAwait(false))
                        using (await ImmatureIndexAsyncLock.LockAsync(cancel).ConfigureAwait(false))
                        {
                            ImmatureIndexFileManager.DeleteMe();
                            MatureIndexFileManager.DeleteMe();
                        }

                    Environment.Exit(2);
                }
            }
        }

        while (ImmatureFilters.Any())
        {
            removed.Add(await RemoveLastFilterAsync(cancel).ConfigureAwait(false));
        }

        return(removed);
    }
示例#6
0
    private async Task InitializeFiltersAsync(CancellationToken cancel)
    {
        try
        {
            if (MatureIndexFileManager.Exists())
            {
                using (BenchmarkLogger.Measure(LogLevel.Debug, "MatureIndexFileManager loading"))
                {
                    int i = 0;
                    using StreamReader sr = MatureIndexFileManager.OpenText();
                    if (!sr.EndOfStream)
                    {
                        while (true)
                        {
                            i++;
                            cancel.ThrowIfCancellationRequested();
                            string?line = await sr.ReadLineAsync().ConfigureAwait(false);

                            if (line is null)
                            {
                                break;
                            }

                            ProcessLine(line, enqueue: false);
                        }
                    }

                    Logger.LogDebug($"Loaded {i} lines from the mature index file.");
                }
            }
        }
        catch (Exception ex) when(ex is not OperationCanceledException)
        {
            // We found a corrupted entry. Stop here.
            // Delete the corrupted file.
            // Do not try to autocorrect, because the internal data structures are throwing events that may confuse the consumers of those events.
            Logger.LogError("Mature index got corrupted. Deleting both mature and immature index...");
            MatureIndexFileManager.DeleteMe();
            ImmatureIndexFileManager.DeleteMe();
            throw;
        }
        cancel.ThrowIfCancellationRequested();

        try
        {
            if (ImmatureIndexFileManager.Exists())
            {
                foreach (var line in await ImmatureIndexFileManager.ReadAllLinesAsync(cancel).ConfigureAwait(false))                 // We can load ImmatureIndexFileManager to the memory, no problem.
                {
                    ProcessLine(line, enqueue: true);
                    cancel.ThrowIfCancellationRequested();
                }
            }
        }
        catch (Exception ex) when(ex is not OperationCanceledException)
        {
            // We found a corrupted entry. Stop here.
            // Delete the corrupted file.
            // Do not try to autocorrect, because the internal data structures are throwing events that may confuse the consumers of those events.
            Logger.LogError("Immature index got corrupted. Deleting it...");
            ImmatureIndexFileManager.DeleteMe();
            throw;
        }
    }