Refactor GetFilterAsync
GetFilterAsync now returns IEnumerable<FilterModel>
If NotFound is returned, an exception is thrown
Updated IndexDownloader to handle a not found exception
MODIFY
WalletWasabi.Tests/RegTests.cs
WalletWasabi.Tests/RegTests.cs
@@ -52,7 +52,7 @@ namespace WalletWasabi.Tests
{
using (var client = new WasabiClient(new Uri(RegTestFixture.BackendEndPoint)))
{
- var filters = await client.GetFiltersAsync(firstHash, 1000);
+ var filters = await client.GetFiltersAsync(firstHash, 0, 1000);
Assert.True(filters.NotNullAndNotEmpty());
var filterCount = filters.ToList().Count;

MODIFY
WalletWasabi/Services/IndexDownloader.cs
WalletWasabi/Services/IndexDownloader.cs
@@ -114,6 +114,8 @@ namespace WalletWasabi.Services
Task.Run(async () =>
{
+ FilterModel bestKnownFilter = null;
+
try
{
while (IsRunning)
@@ -123,29 +125,26 @@ namespace WalletWasabi.Services
// If stop was requested return.
if (IsRunning == false) return;
- FilterModel bestKnownFilter;
using (await IndexLock.LockAsync())
{
bestKnownFilter = Index.Last();
}
- var filters = await WasabiClient.GetFiltersAsync(bestKnownFilter.BlockHash, 1000);
+ var filters = await WasabiClient.GetFiltersAsync(bestKnownFilter.BlockHash, bestKnownFilter.BlockHeight, 1000);
- if (filters != null && !filters.Any()) // empty list
+ if (!filters.Any())
{
continue;
}
if (filters.NotNullAndNotEmpty())
{
- var filtersList = filters.ToList(); // performance
using (await IndexLock.LockAsync())
{
- for (int i = 0; i < filtersList.Count; i++)
+ var filtersList = filters.ToList(); // performance
+ foreach (var filter in filtersList)
{
- var filterModel = FilterModel.FromLine(filtersList[i], bestKnownFilter.BlockHeight + i + 1);
-
- Index.Add(filterModel);
- NewFilter?.Invoke(this, filterModel);
+ Index.Add(filter);
+ NewFilter?.Invoke(this, filter);
}
if (filtersList.Count == 1) // minor optimization
@@ -162,7 +161,10 @@ namespace WalletWasabi.Services
continue;
}
- else if (filters == null)
+ }
+ catch (Exception ex)
+ {
+ if (bestKnownFilter != null && ex.Message.Contains($"Provided bestKnownBlockHash is not found: {bestKnownFilter?.BlockHash}."))
{
// Reorg happened
var reorgedHash = bestKnownFilter.BlockHash;
@@ -182,11 +184,11 @@ namespace WalletWasabi.Services
// 3. Skip the last valid block.
continue;
}
- }
- catch (Exception ex)
+ else
{
Logger.LogError<IndexDownloader>(ex);
}
+ }
finally
{
await Task.Delay(requestInterval); // Ask for new index in every requestInterval.

MODIFY
WalletWasabi/WebClients/ChaumianCoinJoin/WasabiClient.cs
WalletWasabi/WebClients/ChaumianCoinJoin/WasabiClient.cs
@@ -24,7 +24,7 @@ namespace WalletWasabi.WebClients.ChaumianCoinJoin
#region blockchain
- public async Task<IEnumerable<string>> GetFiltersAsync(uint256 bestKnownBlockHash, int count)
+ public async Task<IEnumerable<FilterModel>> GetFiltersAsync(uint256 bestKnownBlockHash, Height blockHeight, int count)
{
using(var response = await TorClient.SendAndRetryAsync(HttpMethod.Get,
HttpStatusCode.OK,
@@ -32,11 +32,7 @@ namespace WalletWasabi.WebClients.ChaumianCoinJoin
{
if (response.StatusCode == HttpStatusCode.NoContent)
{
- return Enumerable.Empty<string>();
- }
- if (response.StatusCode == HttpStatusCode.NotFound)
- {
- return null;
+ return Enumerable.Empty<FilterModel>();
}
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -47,7 +43,9 @@ namespace WalletWasabi.WebClients.ChaumianCoinJoin
using(HttpContent content = response.Content)
{
- var ret = await content.ReadAsJsonAsync<IEnumerable<string>>();
+ var filters = (await content.ReadAsJsonAsync<IEnumerable<string>>()).ToList();
+
+ var ret = filters.Select(s => FilterModel.FromLine(s, blockHeight + filters.IndexOf(s) + 1));
return ret;
}
}

Refactor GetFilterAsync

GetFilterAsync now returns IEnumerable<FilterModel>
If NotFound is returned, an exception is thrown
Updated IndexDownloader to handle a not found exception