Lock cluster and optimize coins contains.
MODIFY
WalletWasabi/Blockchain/Analysis/Clustering/Cluster.cs
WalletWasabi/Blockchain/Analysis/Clustering/Cluster.cs
@@ -9,7 +9,9 @@ namespace WalletWasabi.Blockchain.Analysis.Clustering
{
public class Cluster : NotifyPropertyChangedBase, IEquatable<Cluster>
{
+ private object Lock { get; }
private List<SmartCoin> Coins { get; set; }
+ private HashSet<SmartCoin> CoinsSet { get; set; }
private SmartLabel _labels;
@@ -28,18 +30,22 @@ namespace WalletWasabi.Blockchain.Analysis.Clustering
public Cluster(IEnumerable<SmartCoin> coins)
{
+ Lock = new object();
Coins = coins.ToList();
+ CoinsSet = Coins.ToHashSet();
Labels = SmartLabel.Merge(Coins.Select(x => x.Label));
}
public void Merge(Cluster clusters) => Merge(clusters.Coins);
public void Merge(IEnumerable<SmartCoin> coins)
+ {
+ lock (Lock)
{
var insertPosition = 0;
foreach (var coin in coins.ToList())
{
- if (!Coins.Contains(coin))
+ if (CoinsSet.Add(coin))
{
Coins.Insert(insertPosition++, coin);
}
@@ -50,6 +56,15 @@ namespace WalletWasabi.Blockchain.Analysis.Clustering
Labels = SmartLabel.Merge(Coins.Select(x => x.Label));
}
}
+ }
+
+ public IEnumerable<SmartCoin> GetCoins()
+ {
+ lock (Lock)
+ {
+ return Coins.ToList();
+ }
+ }
#region EqualityAndComparison
@@ -58,9 +73,11 @@ namespace WalletWasabi.Blockchain.Analysis.Clustering
public bool Equals(Cluster other) => this == other;
public override int GetHashCode()
+ {
+ lock (Lock)
{
int hash = 0;
- if (Coins != null)
+ if (Coins is { })
{
foreach (var coin in Coins)
{
@@ -69,6 +86,7 @@ namespace WalletWasabi.Blockchain.Analysis.Clustering
}
return hash;
}
+ }
public static bool operator ==(Cluster x, Cluster y)
{
@@ -90,11 +108,14 @@ namespace WalletWasabi.Blockchain.Analysis.Clustering
return false;
}
else
+ {
+ lock (x.Lock)
{
return x.Coins.SequenceEqual(y.Coins);
}
}
}
+ }
public static bool operator !=(Cluster x, Cluster y) => !(x == y);

Lock cluster and optimize coins contains.