Skip to content

Commit

Permalink
Merge pull request WalletWasabi#12258 from turbolay/improveMostUsedLa…
Browse files Browse the repository at this point in the history
…bels

Improve most used labels
  • Loading branch information
molnard authored Jan 23, 2024
2 parents 55f2607 + 8f2f1be commit 7137843
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
23 changes: 23 additions & 0 deletions WalletWasabi.Fluent/Helpers/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,27 @@ public static int LastIndexOf<T>(this IEnumerable<T> source, T itemToFind, IEqua
}

public static bool IsEmpty<T>(this IEnumerable<T> source) => !source.Any();

/// <summary>
/// Splits the collection into two collections, containing the elements for which the given predicate returns True and False respectively. Element order is preserved in both of the created lists.
/// </summary>
public static (IEnumerable<T>, IEnumerable<T>) Partition<T>(this IEnumerable<T> me, Predicate<T> predicate)
{
var trueList = new List<T>();
var falseList = new List<T>();

foreach (var item in me)
{
if (predicate(item))
{
trueList.Add(item);
}
else
{
falseList.Add(item);
}
}

return (trueList, falseList);
}
}
13 changes: 5 additions & 8 deletions WalletWasabi.Fluent/Helpers/KeyManagerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using NBitcoin;
using WalletWasabi.Blockchain.Analysis.Clustering;
using WalletWasabi.Blockchain.Keys;
using WalletWasabi.Extensions;

namespace WalletWasabi.Fluent.Helpers;

public static class KeyManagerExtensions
{
public static IEnumerable<LabelsArray> GetChangeLabels(this KeyManager km) =>
km.GetKeys(isInternal: true).Select(x => x.Labels);

public static IEnumerable<LabelsArray> GetReceiveLabels(this KeyManager km) =>
km.GetKeys(isInternal: false).Select(x => x.Labels);
public static (List<string>, List<string>) GetLabels(this KeyManager km)
{
var (changeKeys, receiveKeys) = km.GetKeys().Partition(x => x.IsInternal);
return (changeKeys.SelectMany(x => x.Labels).ToList(), receiveKeys.SelectMany(x => x.Labels).ToList());
}
}
12 changes: 8 additions & 4 deletions WalletWasabi.Fluent/Helpers/LabelHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ public static class LabelHelpers
{
var labelPool = new Dictionary<string, int>(); // int: score.

// Make recent and receive labels count more for the current wallet
var labelsByWalletId = WalletHelpers.GetLabelsByWallets();

// Make recent and receive labels count more for the current wallet.
var multiplier = 100;
foreach (var label in wallet.KeyManager.GetReceiveLabels().Reverse().SelectMany(x => x))
var currentWalletReceiveLabels = labelsByWalletId.First(x => x.WalletId == wallet.WalletId).ReceiveLabels;
for (var i = currentWalletReceiveLabels.Count - 1; i >= 0; i--) // Iterate in reverse order.
{
var label = currentWalletReceiveLabels[i];
var score = (intent == Intent.Receive ? 100 : 1) * multiplier;
if (!labelPool.TryAdd(label, score))
{
Expand All @@ -28,7 +32,7 @@ public static class LabelHelpers
}

// Receive addresses should be more dominant.
foreach (var label in WalletHelpers.GetReceiveAddressLabels().SelectMany(x => x))
foreach (var label in labelsByWalletId.SelectMany(x => x.ReceiveLabels))
{
var score = intent == Intent.Receive ? 100 : 1;
if (!labelPool.TryAdd(label, score))
Expand All @@ -38,7 +42,7 @@ public static class LabelHelpers
}

// Change addresses shouldn't be much dominant, but should be present.
foreach (var label in WalletHelpers.GetChangeAddressLabels().SelectMany(x => x))
foreach (var label in labelsByWalletId.SelectMany(x => x.ChangeLabels))
{
var score = 1;
if (!labelPool.TryAdd(label, score))
Expand Down
25 changes: 14 additions & 11 deletions WalletWasabi.Fluent/Helpers/WalletHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ public static WalletType GetType(KeyManager keyManager)
/// <returns>Labels ordered by blockchain.</returns>
public static IEnumerable<LabelsArray> GetTransactionLabels() => Services.BitcoinStore.TransactionStore.GetLabels();

public static IEnumerable<LabelsArray> GetReceiveAddressLabels() =>
Services.WalletManager
.GetWallets(refreshWalletList: false) // Don't refresh wallet list as it may be slow.
.Select(x => x.KeyManager)
.SelectMany(x => x.GetReceiveLabels());

public static IEnumerable<LabelsArray> GetChangeAddressLabels() =>
Services.WalletManager
.GetWallets(refreshWalletList: false) // Don't refresh wallet list as it may be slow.
.Select(x => x.KeyManager)
.SelectMany(x => x.GetChangeLabels());
public static List<LabelsByWallet> GetLabelsByWallets()
{
var result = new List<LabelsByWallet>();

foreach (var wallet in Services.WalletManager.GetWallets(refreshWalletList: false))
{
var (changeLabels, receiveLabels) = wallet.KeyManager.GetLabels();
result.Add(new LabelsByWallet(wallet.WalletId, changeLabels, receiveLabels));
}

return result;
}

public record LabelsByWallet(WalletId WalletId, List<string> ChangeLabels, List<string> ReceiveLabels);
}

0 comments on commit 7137843

Please sign in to comment.