Add comments, make some variable names more expressive.
MODIFY
WalletWasabi/Transactions/TransactionBuilding/SmartCoinSelector.cs
WalletWasabi/Transactions/TransactionBuilding/SmartCoinSelector.cs
@@ -22,28 +22,31 @@ namespace WalletWasabi.Transactions.TransactionBuilding
public IEnumerable<ICoin> Select(IEnumerable<ICoin> coins, IMoney target)
{
- var targetMoney = target as Money;
+ Money targetMoney = target as Money;
- var available = UnspentCoins.Sum(x => x.Amount);
+ long available = UnspentCoins.Sum(x => x.Amount);
if (available < targetMoney)
{
throw new InsufficientBalanceException(targetMoney, available);
}
- var uniqueClusters = UnspentCoins
+ // Get unique clusters.
+ IEnumerable<Cluster> uniqueClusters = UnspentCoins
.Select(coin => coin.Clusters)
.Distinct();
- var clusters = (uniqueClusters.Count() < 10)
+ // Build all the possible coin clusters, except when it's computationally too expensive.
+ List<IEnumerable<SmartCoin>> coinClusters = (uniqueClusters.Count() < 10)
? uniqueClusters
.CombinationsWithoutRepetition(ofLength: 1, upToLength: 6)
.Select(clusterCombination => UnspentCoins.Where(coin => clusterCombination.Contains(coin.Clusters)))
.ToList()
: new List<IEnumerable<SmartCoin>>();
- clusters.Add(UnspentCoins);
+ coinClusters.Add(UnspentCoins);
- var coinsByCluster = clusters
+ // This operation is doing super advanced grouping on the coin clusters and adding properties to each of them.
+ var sayajinCoinClusters = coinClusters
.Select(coins => (Coins: coins, Privacy: 1.0m / coins.Sum(x => x.Clusters.Labels.Count())))
.Select(group => new
{
@@ -54,7 +57,8 @@ namespace WalletWasabi.Transactions.TransactionBuilding
Amount = group.Coins.Sum(x => x.Amount)
});
- var coinsInBestCluster = coinsByCluster
+ // Find the best coin cluster that we are going to use.
+ IEnumerable<SmartCoin> bestCoinCluster = sayajinCoinClusters
.Where(group => group.Amount >= targetMoney)
.OrderBy(group => group.Unconfirmed)
.ThenByDescending(group => group.AnonymitySet) // Always try to spend/merge the largest anonset coins first.
@@ -63,14 +67,14 @@ namespace WalletWasabi.Transactions.TransactionBuilding
.First()
.Coins;
- var coinsInBestClusterByScript = coinsInBestCluster
+ var coinsInBestClusterByScript = bestCoinCluster
.GroupBy(c => c.ScriptPubKey)
.Select(group => new
{
Coins = group
});
- var coinsToSpend = new List<SmartCoin>();
+ List<SmartCoin> coinsToSpend = new List<SmartCoin>();
foreach (IGrouping<Script, SmartCoin> coinsGroup in coinsInBestClusterByScript
.Select(group => group.Coins))

Add comments, make some variable names more expressive.