-
Notifications
You must be signed in to change notification settings - Fork 492
/
WalletLoadWorkflow.cs
188 lines (154 loc) · 5.46 KB
/
WalletLoadWorkflow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using ReactiveUI;
using System.Diagnostics;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using WalletWasabi.Blockchain.BlockFilters;
using WalletWasabi.Blockchain.Blocks;
using WalletWasabi.Fluent.Extensions;
using WalletWasabi.Logging;
using WalletWasabi.Models;
using WalletWasabi.Wallets;
namespace WalletWasabi.Fluent.Models.Wallets;
public partial class WalletLoadWorkflow : IWalletLoadWorkflow
{
private readonly CompositeDisposable _disposables = new();
private readonly Wallet _wallet;
private Stopwatch? _stopwatch;
private uint _filtersToDownloadCount;
private uint _filtersToProcessCount;
private uint _filterProcessStartingHeight;
private Subject<(double PercentComplete, TimeSpan TimeRemaining)> _progress;
[AutoNotify] private bool _isLoading;
public WalletLoadWorkflow(Wallet wallet)
{
_wallet = wallet;
_progress = new();
_progress.OnNext((0, TimeSpan.Zero));
LoadCompleted =
Observable.FromEventPattern<WalletState>(_wallet, nameof(Wallet.StateChanged))
.ObserveOn(RxApp.MainThreadScheduler)
.Select(x => x.EventArgs)
.Where(x => x == WalletState.Started || (x == WalletState.Starting && wallet.KeyManager.SkipSynchronization))
.ToSignal();
}
public IObservable<(double PercentComplete, TimeSpan TimeRemaining)> Progress => _progress;
public IObservable<Unit> LoadCompleted { get; }
private uint TotalCount => _filtersToProcessCount + _filtersToDownloadCount;
private uint RemainingFiltersToDownload => (uint)Services.BitcoinStore.SmartHeaderChain.HashesLeft;
public void Start()
{
_stopwatch = Stopwatch.StartNew();
_disposables.Add(Disposable.Create(_stopwatch.Stop));
Services.Synchronizer.WhenAnyValue(x => x.BackendStatus)
.Where(status => status == BackendStatus.Connected)
.SubscribeAsync(async _ => await LoadWalletAsync(isBackendAvailable: true).ConfigureAwait(false))
.DisposeWith(_disposables);
Observable.FromEventPattern<bool>(Services.Synchronizer, nameof(Services.Synchronizer.ResponseArrivedIsGenSocksServFail))
.SubscribeAsync(async _ =>
{
if (Services.Synchronizer.BackendStatus == BackendStatus.Connected)
{
return;
}
await LoadWalletAsync(isBackendAvailable: false).ConfigureAwait(false);
})
.DisposeWith(_disposables);
Observable.Interval(TimeSpan.FromSeconds(1))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ =>
{
var processedCount = GetCurrentProcessedCount();
UpdateProgress(processedCount);
})
.DisposeWith(_disposables);
}
public void Stop()
{
_disposables.Dispose();
}
private async Task LoadWalletAsync(bool isBackendAvailable)
{
if (IsLoading)
{
return;
}
IsLoading = true;
await SetInitValuesAsync(isBackendAvailable).ConfigureAwait(false);
while (isBackendAvailable && RemainingFiltersToDownload > 0 && !_wallet.KeyManager.SkipSynchronization)
{
await Task.Delay(1000).ConfigureAwait(false);
}
if (_wallet.State != WalletState.Uninitialized)
{
throw new Exception("Wallet is already being logged in.");
}
try
{
await Task.Run(async () => await Services.WalletManager.StartWalletAsync(_wallet));
}
catch (OperationCanceledException ex)
{
Logger.LogTrace(ex);
}
catch (Exception ex)
{
Logger.LogError(ex);
}
}
private async Task SetInitValuesAsync(bool isBackendAvailable)
{
while (isBackendAvailable && Services.Synchronizer.LastResponse is null)
{
await Task.Delay(500).ConfigureAwait(false);
}
_filtersToDownloadCount = (uint)Services.BitcoinStore.SmartHeaderChain.HashesLeft;
if (Services.BitcoinStore.SmartHeaderChain.ServerTipHeight is { } serverTipHeight &&
Services.BitcoinStore.SmartHeaderChain.TipHeight is { } clientTipHeight)
{
var tipHeight = Math.Max(serverTipHeight, clientTipHeight);
var startingHeight = SmartHeader.GetStartingHeader(_wallet.Network, IndexType.SegwitTaproot).Height;
var bestHeight = (uint)_wallet.KeyManager.GetBestHeight().Value;
_filterProcessStartingHeight = bestHeight < startingHeight ? startingHeight : bestHeight;
_filtersToProcessCount = tipHeight - _filterProcessStartingHeight;
}
}
private uint GetCurrentProcessedCount()
{
uint downloadedFilters = 0;
if (_filtersToDownloadCount > 0)
{
downloadedFilters = _filtersToDownloadCount - RemainingFiltersToDownload;
}
uint processedFilters = 0;
if (_wallet.LastProcessedFilter?.Header?.Height is { } lastProcessedFilterHeight)
{
processedFilters = lastProcessedFilterHeight - _filterProcessStartingHeight - 1;
}
var processedCount = downloadedFilters + processedFilters;
return processedCount;
}
private void UpdateProgress(uint processedCount)
{
if (TotalCount == 0 || processedCount == 0 || processedCount > TotalCount || _stopwatch is null)
{
return;
}
var percent = (decimal)processedCount / TotalCount * 100;
var remainingCount = TotalCount - processedCount;
var tempPercent = (uint)Math.Round(percent);
if (tempPercent == 0)
{
return;
}
var remainingMilliseconds = (double)_stopwatch.ElapsedMilliseconds / processedCount * remainingCount;
var remainingTimeSpan = TimeSpan.FromMilliseconds(remainingMilliseconds);
if (remainingTimeSpan > TimeSpan.FromHours(1))
{
remainingTimeSpan = new TimeSpan(remainingTimeSpan.Days, remainingTimeSpan.Hours, remainingTimeSpan.Minutes, seconds: 0);
}
_progress.OnNext((tempPercent, remainingTimeSpan));
}
}