Skip to content

Commit

Permalink
Invert iteration in GetFilterLinesExcluding algo (WalletWasabi#13371)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolay authored Sep 9, 2024
1 parent 88ab412 commit db0948b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions WalletWasabi/Blockchain/BlockFilters/IndexBuilderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,33 +339,33 @@ private void BlockNotifier_OnBlock(object? sender, Block e)
currentIndex = Index;
}

foreach (var filter in currentIndex)
if (currentIndex.Count == 0)
{
if (found)
{
filters.Add(filter);
if (filters.Count >= count)
{
break;
}
}
else
{
if (filter.Header.BlockHash == bestKnownBlockHash)
{
found = true;
}
}
return (Height.Unknown, []);
}

if (currentIndex.Count == 0)
// Search for bestKnownBlockHash from last to first
var i = currentIndex.Count - 1;
for (; i >= 0; i--)
{
return (Height.Unknown, Enumerable.Empty<FilterModel>());
if (!currentIndex[i].Header.BlockHash.Equals(bestKnownBlockHash))
{
continue;
}

found = true;
break;
}
else

if (found && i < currentIndex.Count - 1)
{
return ((int)currentIndex[^1].Header.Height, filters);
// Populate filters starting from the found index + 1
var startIndex = i + 1;
var filtersCount = Math.Min(count, currentIndex.Count - startIndex);
filters.AddRange(currentIndex.GetRange(startIndex, filtersCount));
}

return (new Height(currentIndex[^1].Header.Height), filters);
}

public FilterModel GetLastFilter()
Expand Down

0 comments on commit db0948b

Please sign in to comment.