Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryRemoveLastFilterIfNewerThanAsync should not throw #12778

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions WalletWasabi.Tests/UnitTests/Stores/IndexStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.Backend.Models;
using WalletWasabi.Blockchain.Blocks;
using WalletWasabi.Helpers;
using WalletWasabi.Stores;
Expand All @@ -19,14 +20,22 @@ public class IndexStoreTests
[Fact]
public async Task IndexStoreTestsAsync()
{
using CancellationTokenSource testDeadlineCts = new(TimeSpan.FromMinutes(1));
using CancellationTokenSource testCts = new(TimeSpan.FromMinutes(1));

string directory = GetWorkDirectory();
await IoHelpers.TryDeleteDirectoryAsync(directory);
IoHelpers.EnsureContainingDirectoryExists(directory);

await using var indexStore = new IndexStore(directory, Network.Main, new SmartHeaderChain());
await indexStore.InitializeAsync(testDeadlineCts.Token);
await indexStore.InitializeAsync(testCts.Token);

// Remove starting filter.
FilterModel? filterModel = await indexStore.TryRemoveLastFilterAsync();
Assert.NotNull(filterModel);

// No filter to remove.
filterModel = await indexStore.TryRemoveLastFilterAsync();
Assert.Null(filterModel);
}

private string GetWorkDirectory([CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "")
Expand Down
4 changes: 2 additions & 2 deletions WalletWasabi/Stores/IndexStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ public async Task<FilterModel[]> FetchBatchAsync(uint fromHeight, int batchSize,
{
if (!IndexStorage.TryRemoveLast(out filter))
{
throw new InvalidOperationException("No last filter.");
return null;
Copy link
Collaborator

@kiminuo kiminuo Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is important when there are no blocks in the database, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was some history about why we throw the exception but I can't recall why we did it and given the method's name and what it does I would expect the method NOT to throw, so I think it's a good change.

}
}
else
{
if (!IndexStorage.TryRemoveLastIfNewerThan(height.Value, out filter))
{
throw new InvalidOperationException("No last filter.");
return null;
}
}

Expand Down