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

Fix DependencyGraph vsize calculation #12618

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
using NBitcoin;
using System.Linq;
using WalletWasabi.Helpers;
using WalletWasabi.Tests.Helpers;
using WalletWasabi.WabiSabi.Backend;
using WalletWasabi.WabiSabi.Client;
using WalletWasabi.WabiSabi.Client.Batching;
using WalletWasabi.WabiSabi.Client.CredentialDependencies;
using Xunit;

namespace WalletWasabi.Tests.UnitTests.WabiSabi.Client;

public class PaymentAwareOutputProviderTests
{
[Fact]
public void CreateOutputsForImpossiblePaymentTest()
{
var rpc = new MockRpcClient();
var wallet = new TestWallet("random-wallet", rpc);
var paymentBatch = new PaymentBatch();
var outputProvider = new PaymentAwareOutputProvider(wallet, paymentBatch);

var roundParameters = WabiSabiFactory.CreateRoundParameters(new WabiSabiConfig());
using Key key = new();
paymentBatch.AddPayment(
key.PubKey.GetAddress(ScriptPubKeyType.Segwit, rpc.Network),
Money.Coins(101.0001m)); // Too big, non-standard payment which cannot be done.

var registeredCoinsEffectiveValues = new[]
{ Money.Coins(100m) };
var theirCoinEffectiveValues = new[]
{ Money.Coins(0.2m), Money.Coins(0.1m), Money.Coins(0.05m), Money.Coins(0.0025m), Money.Coins(0.0001m) };
var availableVsize = roundParameters.MaxVsizeAllocationPerAlice - Constants.P2wpkhInputVirtualSize;

var outputs = outputProvider.GetOutputs(
roundId: uint256.Zero,
roundParameters,
registeredCoinsEffectiveValues,
theirCoinEffectiveValues,
availableVsize).ToArray();

var nonAwaredOutputProvider = new OutputProvider(wallet);
var decomposedOutputs = nonAwaredOutputProvider.GetOutputs(
uint256.Zero,
roundParameters,
registeredCoinsEffectiveValues,
theirCoinEffectiveValues,
availableVsize).ToArray();

decimal ToDecimal(TxOut o) => o.Value.ToDecimal(MoneyUnit.BTC);
Assert.Equal(outputs.Sum(ToDecimal), decomposedOutputs.Sum(ToDecimal));

// Make sure this doesn't throw
DependencyGraph.ResolveCredentialDependencies(registeredCoinsEffectiveValues, outputs, roundParameters.MiningFeeRate, availableVsize);
}

[Fact]
public void CreateOutputsForPaymentsTest()
{
Expand All @@ -23,18 +68,24 @@ public void CreateOutputsForPaymentsTest()
key.PubKey.GetAddress(ScriptPubKeyType.Segwit, rpc.Network),
Money.Coins(0.00005432m));

var registeredCoinsEffectiveValues = new[]
{ Money.Coins(0.00484323m), Money.Coins(0.003m), Money.Coins(0.00004323m) };
var totalRegisteredEffectiveValue = registeredCoinsEffectiveValues.Sum().ToDecimal(MoneyUnit.BTC);

var outputs = outputProvider.GetOutputs(
roundId: uint256.Zero,
roundParameters,
new[] { Money.Coins(0.00484323m), Money.Coins(0.003m), Money.Coins(0.00004323m) },
registeredCoinsEffectiveValues,
new[] { Money.Coins(0.2m), Money.Coins(0.1m), Money.Coins(0.05m), Money.Coins(0.0025m), Money.Coins(0.0001m) },
int.MaxValue).ToArray();

Assert.Equal(outputs[0].ScriptPubKey, key.PubKey.GetScriptPubKey(ScriptPubKeyType.Segwit));
Assert.Equal(outputs[0].Value, Money.Coins(0.00005432m));

Assert.True(outputs.Length > 2, $"There were {outputs.Length} outputs."); // The rest was decomposed
Assert.InRange(outputs.Sum(x => x.Value.ToDecimal(MoneyUnit.BTC)), 0.007500m, 0.007800m); // no money was lost
Assert.InRange(outputs.Sum(x => x.Value.ToDecimal(MoneyUnit.BTC)),
totalRegisteredEffectiveValue - 0.0002m,
totalRegisteredEffectiveValue); // no money was lost
}

[Theory]
Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi/WabiSabi/Client/CoinJoinClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ private async Task<IEnumerable<TxOut>> ProceedWithOutputRegistrationPhaseAsync(u

var outputTxOuts = OutputProvider.GetOutputs(roundId, roundParameters, registeredCoinEffectiveValues, theirCoinEffectiveValues, (int)availableVsize).ToArray();

DependencyGraph dependencyGraph = DependencyGraph.ResolveCredentialDependencies(inputEffectiveValuesAndSizes, outputTxOuts, roundParameters.MiningFeeRate, roundParameters.MaxVsizeAllocationPerAlice);
DependencyGraph dependencyGraph = DependencyGraph.ResolveCredentialDependencies(registeredCoinEffectiveValues, outputTxOuts, roundParameters.MiningFeeRate, availableVsize);
DependencyGraphTaskScheduler scheduler = new(dependencyGraph);

var combinedToken = linkedCts.Token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ public record DependencyGraph
/// and may contain additional nodes if reissuance requests are
/// required.</remarks>
///
public static DependencyGraph ResolveCredentialDependencies(IEnumerable<(Money EffectiveValue, int InputSize)> effectiveValuesAndSizes, IEnumerable<TxOut> outputs, FeeRate feeRate, long vsizeAllocationPerInput)
public static DependencyGraph ResolveCredentialDependencies(IEnumerable<Money> effectiveValues, IEnumerable<TxOut> outputs, FeeRate feeRate, long availableVSize)
{
var effectiveValues = effectiveValuesAndSizes.Select(x => x.EffectiveValue);
var inputSizes = effectiveValuesAndSizes.Select(x => x.InputSize);

if (effectiveValues.Any(x => x <= Money.Zero))
{
throw new InvalidOperationException($"Not enough funds to pay for the fees.");
Expand All @@ -65,7 +62,7 @@ public static DependencyGraph ResolveCredentialDependencies(IEnumerable<(Money E
var effectiveCosts = Enumerable.Zip(outputs, outputSizes, (txout, size) => txout.EffectiveCost(feeRate));

return ResolveCredentialDependencies(
Enumerable.Zip(effectiveValues.Select(a => a.Satoshi), inputSizes.Select(i => (vsizeAllocationPerInput - i)), ImmutableArray.Create).Cast<IEnumerable<long>>(),
Enumerable.Zip(effectiveValues.Select(a => a.Satoshi), [availableVSize], ImmutableArray.Create).Cast<IEnumerable<long>>(),
Enumerable.Zip(effectiveCosts.Select(a => a.Satoshi), outputSizes.Select(i => (long)i), ImmutableArray.Create).Cast<IEnumerable<long>>()
);
}
Expand Down