fix mixing level off by one error
MODIFY
WalletWasabi/Models/ChaumianCoinJoin/CcjRound.cs
WalletWasabi/Models/ChaumianCoinJoin/CcjRound.cs
@@ -261,7 +261,36 @@ namespace WalletWasabi.Models.ChaumianCoinJoin
// 2.1 If the newDenomination equals to the Denomination, then we knew the denomination at
// registration time so we can tinker with additional mixing levels.
- bool tinkerWithAdditionalMixingLevels = newDenomination == MixingLevels.GetBaseDenomination();
+
+ bool tinkerWithAdditionalMixingLevels = true;
+ foreach (Alice alice in Alices)
+ {
+ // Check if inputs have enough coins.
+
+ Money networkFeeToPay = (alice.Inputs.Count() * FeePerInputs) + (2 * FeePerOutputs);
+ Money changeAmount = alice.InputSum - (newDenomination + networkFeeToPay);
+ var acceptedBlindedOutputScriptsCount = 1;
+
+ // Make sure we sign the proper number of additional blinded outputs.
+ var moneySoFar = Money.Zero;
+ for (int i = 1; i < alice.BlindedOutputScripts.Length; i++)
+ {
+ MixingLevel level = MixingLevels.GetLevel(i);
+ IEnumerable<Bob> bobsOnThisLevel = Bobs.Where(x => x.Level == level);
+ if (bobsOnThisLevel.Count() <= 1) break;
+
+ changeAmount -= (level.Denomination + FeePerOutputs + (level.Denomination.Percentange(CoordinatorFeePercent) * bobsOnThisLevel.Count()));
+
+ if (changeAmount < Money.Zero) break;
+ acceptedBlindedOutputScriptsCount++;
+ }
+
+ if (acceptedBlindedOutputScriptsCount < alice.BlindedOutputScripts.Count())
+ {
+ tinkerWithAdditionalMixingLevels = false;
+ }
+ }
+
if (tinkerWithAdditionalMixingLevels)
{
foreach (MixingLevel level in MixingLevels.GetLevelsExceptBase())

MODIFY
WalletWasabi/Services/CcjClient.cs
WalletWasabi/Services/CcjClient.cs
@@ -456,7 +456,7 @@ namespace WalletWasabi.Services
inputSum += coin.Amount;
}
- int maximumMixingLevel = 0;
+ int maximumMixingLevelCount = 1;
var denominations = new List<Money>
{
@@ -469,7 +469,7 @@ namespace WalletWasabi.Services
denominations.Add(denom);
if (inputSum > denom)
{
- maximumMixingLevel = i;
+ maximumMixingLevelCount = i + 1;
}
}
@@ -478,18 +478,18 @@ namespace WalletWasabi.Services
// Find the first one that we did not try to register in the current session.
foreach (var ac in allActiveKeys.Where(x => !AccessCache.ContainsKey(x)))
{
- if (activeKeys.Count >= maximumMixingLevel)
+ if (activeKeys.Count >= maximumMixingLevelCount)
{
break;
}
activeKeys.Add(ac);
}
// If there is no such a key, then use the oldest, but make sure it's not the same as the change.
- if (activeKeys.Count < maximumMixingLevel)
+ if (activeKeys.Count < maximumMixingLevelCount)
{
foreach (var ac in AccessCache.Where(x => allActiveKeys.Contains(x.Key) && changeAddress != x.Key.GetP2wpkhAddress(Network)).OrderBy(x => x.Value).Select(x => x.Key))
{
- if (activeKeys.Count >= maximumMixingLevel)
+ if (activeKeys.Count >= maximumMixingLevelCount)
{
break;
}
@@ -501,7 +501,7 @@ namespace WalletWasabi.Services
{
foreach (var ac in internalNotCachedLockedKeys.Where(x => changeAddress != x.GetP2wpkhAddress(Network)))
{
- if (activeKeys.Count >= maximumMixingLevel)
+ if (activeKeys.Count >= maximumMixingLevelCount)
{
break;
}

fix mixing level off by one error