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

Pre-calculate Privacy Suggestions for changeless transaction #11512

Merged
merged 6 commits into from
Sep 20, 2023

Conversation

Szpoti
Copy link
Collaborator

@Szpoti Szpoti commented Sep 19, 2023

Alternate of #11496

This PR removes redundant PrivacySuggestionsResult class, and pre-calculates the PrivacyWarnings and PrivacySuggestions, so we don't iterate through the list twice unnecessarily, calling VerifyChangeAsync both times. Instead use OfType on the already calculated PrivacyItems.

molnard
molnard previously approved these changes Sep 19, 2023
Copy link
Collaborator

@molnard molnard left a comment

Choose a reason for hiding this comment

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

tACK, just a nit.

molnard
molnard previously approved these changes Sep 19, 2023
@molnard
Copy link
Collaborator

molnard commented Sep 19, 2023

@adamPetho pls check and merge if 👌

Copy link
Collaborator

@soosr soosr left a comment

Choose a reason for hiding this comment

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

This PR reintroduces #11031

@adamPetho
Copy link
Collaborator

adamPetho commented Sep 19, 2023

I have a rather ugly solution to not introduce back that issue. #11512 (review)

PrivacySuggestionsModel.cs

public async IAsyncEnumerable<PrivacyItem> BuildPrivacySuggestionsAsync(TransactionInfo info, BuildTransactionResult transactionResult, [EnumeratorCancellation] CancellationToken cancellationToken)
{
	using CancellationTokenSource singleRunCts = new();

	lock (_lock)
	{
		_singleRunCancellationTokenSource?.Cancel();
		_linkedCancellationTokenSource?.Cancel();
		_singleRunCancellationTokenSource = singleRunCts;
		CancellationTokenSource timeoutCts = new(TimeSpan.FromSeconds(15));
		CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, singleRunCts.Token, cancellationToken);
		_linkedCancellationTokenSource = linkedCts;
	}

	using (await _asyncLock.LockAsync(CancellationToken.None))
	{
		foreach (var item in VerifyLabels(info, transactionResult))
		{
			yield return item;
		}

		foreach (var item in VerifyPrivacyLevel(info, transactionResult))
		{
			yield return item;
		}

		foreach (var item in VerifyConsolidation(transactionResult))
		{
			yield return item;
		}

		foreach (var item in VerifyUnconfirmedInputs(transactionResult))
		{
			yield return item;
		}

		foreach (var item in VerifyCoinjoiningInputs(transactionResult))
		{
			yield return item;
		}

		var changeItems = new List<PrivacyItem>();

		try
		{
			await foreach (var item in VerifyChangeAsync(info, transactionResult, _linkedCancellationTokenSource).ConfigureAwait(false))
			{
				changeItems.Add(item); // Can't yield return in try-catch block
			}
		}
		catch (OperationCanceledException)
		{
			Logger.LogTrace("Operation was cancelled.");
		}
		finally
		{
			lock (_lock)
			{
				_singleRunCancellationTokenSource = null;
			}
		}

		foreach (var item in changeItems)
		{
			yield return item;
		}
	}
}

We can even Concat these foreaches together to have only one foreach.
The Task.Delays are obviously for testing purpose only!!!

Then we can have this inside PrivacySuggestionsFlyoutViewModel.cs

await foreach(var result in _privacySuggestionsModel.BuildPrivacySuggestionsAsync(info, transaction, cancellationToken))
{
	if (result is PrivacyWarning warning)
	{
		Warnings.Add(warning);
	}
	if (result is PrivacySuggestion suggestion)
	{
		Suggestions.Add(suggestion);
	}
}

WDYT @soosr ? I tested it and looks to work well. If it's acceptable, then I can push my changes.

@soosr
Copy link
Collaborator

soosr commented Sep 20, 2023

We can even Concat these foreaches together to have only one foreach.
The Task.Delays are obviously for testing purpose only!!!

Please make a final version that you show

@Szpoti
Copy link
Collaborator Author

Szpoti commented Sep 20, 2023

Check now pls and address

Comment on lines -75 to -79
catch (OperationCanceledException)
await foreach (var item in VerifyChangeAsync(info, transactionResult, _linkedCancellationTokenSource).ConfigureAwait(false))
{
Logger.LogTrace("Operation was cancelled.");
yield return item;
}
finally
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

molnard
molnard previously approved these changes Sep 20, 2023
Copy link
Collaborator

@molnard molnard left a comment

Choose a reason for hiding this comment

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

tACK

@molnard molnard requested a review from soosr September 20, 2023 09:37
adamPetho
adamPetho previously approved these changes Sep 20, 2023
Copy link
Collaborator

@adamPetho adamPetho left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +65 to +69
result.Add(VerifyLabels(info, transactionResult));
result.Add(VerifyPrivacyLevel(info, transactionResult));
result.Add(VerifyConsolidation(transactionResult));
result.Add(VerifyUnconfirmedInputs(transactionResult));
result.Add(VerifyCoinjoiningInputs(transactionResult));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note for the future:
if one of these is a heavy operation, it will block all the other results.
This is not the case at the moment.

@Szpoti Szpoti dismissed stale reviews from adamPetho and molnard via cdcf358 September 20, 2023 12:18
Copy link
Collaborator

@soosr soosr left a comment

Choose a reason for hiding this comment

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

ACK

@soosr soosr merged commit 6ab6747 into WalletWasabi:master Sep 20, 2023
3 of 6 checks passed
@Szpoti Szpoti deleted the calculatePrivacySuggestions branch September 20, 2023 13:45
adamPetho pushed a commit that referenced this pull request Nov 28, 2023
Pre-calculate Privacy Suggestions for changeless transaction

(cherry picked from commit 6ab6747)
adamPetho added a commit that referenced this pull request Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants