Fix UI blocks filter downloading bug
MODIFY
WalletWasabi.Gui/ViewModels/StatusBarViewModel.cs
WalletWasabi.Gui/ViewModels/StatusBarViewModel.cs
@@ -78,7 +78,7 @@ namespace WalletWasabi.Gui.ViewModels
IndexDownloader = indexDownloader;
IndexDownloader.NewFilter += IndexDownloader_NewFilter;
- Filters = IndexDownloader.GetBestHeight().Value;
+ Filters = IndexDownloader.BestFilter.BlockHeight.Value;
this.WhenAnyValue(x => x.BlocksLeft).Subscribe(blocks =>
{
@@ -95,7 +95,7 @@ namespace WalletWasabi.Gui.ViewModels
private void IndexDownloader_NewFilter(object sender, Backend.Models.FilterModel e)
{
- Filters = IndexDownloader.GetBestHeight().Value;
+ Filters = IndexDownloader.BestFilter.BlockHeight.Value;
}
private void MemPoolService_TransactionReceived(object sender, Models.SmartTransaction e)

MODIFY
WalletWasabi.Tests/RegTests.cs
WalletWasabi.Tests/RegTests.cs
@@ -467,7 +467,7 @@ namespace WalletWasabi.Tests
Assert.Single(wallet.Coins);
var firstCoin = wallet.Coins.Single();
Assert.Equal(Money.Coins(0.1m), firstCoin.Amount);
- Assert.Equal(indexDownloader.GetBestFilter().BlockHeight, firstCoin.Height);
+ Assert.Equal(indexDownloader.BestFilter.BlockHeight, firstCoin.Height);
Assert.InRange(firstCoin.Index, 0U, 1U);
Assert.False(firstCoin.SpentOrCoinJoinInProcess);
Assert.Equal("foo label", firstCoin.Label);
@@ -496,9 +496,9 @@ namespace WalletWasabi.Tests
var thirdCoin = wallet.Coins.OrderBy(x => x.Height).Last();
Assert.Equal(Money.Coins(0.01m), secondCoin.Amount);
Assert.Equal(Money.Coins(0.02m), thirdCoin.Amount);
- Assert.Equal(indexDownloader.GetBestFilter().BlockHeight.Value - 2, firstCoin.Height.Value);
- Assert.Equal(indexDownloader.GetBestFilter().BlockHeight.Value - 1, secondCoin.Height.Value);
- Assert.Equal(indexDownloader.GetBestFilter().BlockHeight, thirdCoin.Height);
+ Assert.Equal(indexDownloader.BestFilter.BlockHeight.Value - 2, firstCoin.Height.Value);
+ Assert.Equal(indexDownloader.BestFilter.BlockHeight.Value - 1, secondCoin.Height.Value);
+ Assert.Equal(indexDownloader.BestFilter.BlockHeight, thirdCoin.Height);
Assert.False(thirdCoin.SpentOrCoinJoinInProcess);
Assert.Equal("foo label", firstCoin.Label);
Assert.Equal("bar label", secondCoin.Label);
@@ -554,7 +554,7 @@ namespace WalletWasabi.Tests
var rbfCoin = wallet.Coins.Where(x => x.TransactionId == tx4bumpRes.TransactionId).Single();
Assert.Equal(Money.Coins(0.03m), rbfCoin.Amount);
- Assert.Equal(indexDownloader.GetBestFilter().BlockHeight.Value - 2, rbfCoin.Height.Value);
+ Assert.Equal(indexDownloader.BestFilter.BlockHeight.Value - 2, rbfCoin.Height.Value);
Assert.False(rbfCoin.SpentOrCoinJoinInProcess);
Assert.Equal("bar label", rbfCoin.Label);
Assert.Equal(key2.GetP2wpkhScript(), rbfCoin.ScriptPubKey);
@@ -588,7 +588,7 @@ namespace WalletWasabi.Tests
await rpc.GenerateAsync(1);
await WaitForFiltersToBeProcessedAsync(TimeSpan.FromSeconds(120), 1);
var res = await rpc.GetTxOutAsync(mempoolCoin.TransactionId, (int)mempoolCoin.Index, true);
- Assert.Equal(indexDownloader.GetBestFilter().BlockHeight, mempoolCoin.Height);
+ Assert.Equal(indexDownloader.BestFilter.BlockHeight, mempoolCoin.Height);
}
finally
{
@@ -999,7 +999,7 @@ namespace WalletWasabi.Tests
await rpc.GenerateAsync(1);
await WaitForFiltersToBeProcessedAsync(TimeSpan.FromSeconds(120), 1);
- var bestHeight = wallet.IndexDownloader.GetBestFilter().BlockHeight;
+ var bestHeight = wallet.IndexDownloader.BestFilter.BlockHeight;
Assert.Contains("change of (outgoing, outgoing2)", wallet.Coins.Where(x => x.Height == bestHeight).Select(x => x.Label));
Assert.Contains("change of (outgoing, outgoing2)", keyManager.GetKeys().Select(x => x.Label));

MODIFY
WalletWasabi/Services/IndexDownloader.cs
WalletWasabi/Services/IndexDownloader.cs
@@ -21,6 +21,8 @@ namespace WalletWasabi.Services
{
public Network Network { get; }
+ public FilterModel BestFilter { get; private set; }
+
public WasabiClient WasabiClient { get; }
public string IndexFilePath { get; }
@@ -102,6 +104,8 @@ namespace WalletWasabi.Services
Index.Add(StartingFilter);
File.WriteAllLines(IndexFilePath, Index.Select(x => x.ToLine()));
}
+
+ BestFilter = Index.Last();
}
public void Synchronize(TimeSpan requestInterval)
@@ -122,10 +126,7 @@ namespace WalletWasabi.Services
// If stop was requested return.
if (!IsRunning) return;
- using (await IndexLock.LockAsync())
- {
- bestKnownFilter = Index.Last();
- }
+ bestKnownFilter = BestFilter;
var filters = await WasabiClient.GetFiltersAsync(bestKnownFilter.BlockHash, 1000);
@@ -141,19 +142,20 @@ namespace WalletWasabi.Services
var filterModel = FilterModel.FromLine(filtersList[i], bestKnownFilter.BlockHeight + i + 1);
Index.Add(filterModel);
+ BestFilter = filterModel;
NewFilter?.Invoke(this, filterModel);
}
if (filtersList.Count == 1) // minor optimization
{
- await File.AppendAllLinesAsync(IndexFilePath, new[] { Index.Last().ToLine() });
+ await File.AppendAllLinesAsync(IndexFilePath, new[] { BestFilter.ToLine() });
}
else
{
await File.WriteAllLinesAsync(IndexFilePath, Index.Select(x => x.ToLine()));
}
- Logger.LogInfo<IndexDownloader>($"Downloaded filters for blocks from {bestKnownFilter.BlockHeight.Value + 1} to {Index.Last().BlockHeight}.");
+ Logger.LogInfo<IndexDownloader>($"Downloaded filters for blocks from {bestKnownFilter.BlockHeight.Value + 1} to {BestFilter.BlockHeight}.");
}
continue;
@@ -167,6 +169,7 @@ namespace WalletWasabi.Services
using (await IndexLock.LockAsync())
{
Index.RemoveAt(Index.Count - 1);
+ BestFilter = Index.Last();
}
Reorged?.Invoke(this, reorgedHash);
@@ -224,22 +227,6 @@ namespace WalletWasabi.Services
WasabiClient?.Dispose();
}
- public FilterModel GetBestFilter()
- {
- using (IndexLock.Lock())
- {
- return Index.Last();
- }
- }
-
- public Height GetBestHeight()
- {
- using (IndexLock.Lock())
- {
- return Index.Last().BlockHeight;
- }
- }
-
public IEnumerable<FilterModel> GetFiltersIncluding(uint256 blockHash)
{
using (IndexLock.Lock())

Fix UI blocks filter downloading bug