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

Create ActiveOutput class (instead of the touple we used previously.) #1523

Merged
merged 1 commit into from Jun 1, 2019
Merged
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
22 changes: 11 additions & 11 deletions WalletWasabi.Tests/RegTests.cs
Expand Up @@ -2267,16 +2267,16 @@ public async Task CcjTestsAsync()
var roundState = await satoshiClient.GetRoundStateAsync(aliceClient1.RoundId);
Assert.Equal(CcjRoundPhase.OutputRegistration, roundState.Phase);

if (!round.MixingLevels.GetBaseLevel().Signer.VerifyUnblindedSignature(connConfResp2.activeOutputs.First().signature, outputAddress2.ScriptPubKey.ToBytes()))
if (!round.MixingLevels.GetBaseLevel().Signer.VerifyUnblindedSignature(connConfResp2.activeOutputs.First().Signature, outputAddress2.ScriptPubKey.ToBytes()))
{
throw new NotSupportedException("Coordinator did not sign the blinded output properly.");
}

using (var bobClient1 = new BobClient(baseUri, null))
using (var bobClient2 = new BobClient(baseUri, null))
{
await bobClient1.PostOutputAsync(aliceClient1.RoundId, outputAddress1, connConfResp.activeOutputs.First().signature, 0);
await bobClient2.PostOutputAsync(aliceClient2.RoundId, outputAddress2, connConfResp2.activeOutputs.First().signature, 0);
await bobClient1.PostOutputAsync(aliceClient1.RoundId, new ActiveOutput(outputAddress1, connConfResp.activeOutputs.First().Signature, 0));
await bobClient2.PostOutputAsync(aliceClient2.RoundId, new ActiveOutput(outputAddress2, connConfResp2.activeOutputs.First().Signature, 0));
}

roundState = await satoshiClient.GetRoundStateAsync(aliceClient1.RoundId);
Expand Down Expand Up @@ -2436,7 +2436,7 @@ public async Task CcjEqualInputTestsAsync()
}

// CONNECTION CONFIRMATION PHASE --
var activeOutputs = new List<IEnumerable<(BitcoinAddress output, UnblindedSignature signature, int level)>>();
var activeOutputs = new List<IEnumerable<ActiveOutput>>();
var j = 0;
foreach (var (aliceClient, _, _) in participants)
{
Expand All @@ -2457,7 +2457,7 @@ public async Task CcjEqualInputTestsAsync()
var i = 0;
foreach (var output in outputs.Take(activeOutputs[l].Count()))
{
await bobClient.PostOutputAsync(aliceClient.RoundId, output.outputAddress, activeOutputs[l].ElementAt(i).signature, i);
await bobClient.PostOutputAsync(aliceClient.RoundId, new ActiveOutput(output.outputAddress, activeOutputs[l].ElementAt(i).Signature, i));
i++;
}
}
Expand Down Expand Up @@ -2685,7 +2685,7 @@ public async Task BanningTestsAsync()

Assert.Equal(users.Count, roundConfig.AnonymitySet);

var confirmationRequests = new List<Task<(CcjRoundPhase currentPhase, IEnumerable<(BitcoinAddress output, UnblindedSignature signature, int level)>)>>();
var confirmationRequests = new List<Task<(CcjRoundPhase currentPhase, IEnumerable<ActiveOutput>)>>();

foreach (var user in users)
{
Expand All @@ -2707,7 +2707,7 @@ public async Task BanningTestsAsync()
}

var user = users.ElementAt(k);
user.unblindedSignature = resp.Item2.First().signature;
user.unblindedSignature = resp.Item2.First().Signature;
}

using (var satoshiClient = new SatoshiClient(baseUri, null))
Expand Down Expand Up @@ -2756,7 +2756,7 @@ public async Task BanningTestsAsync()

Assert.Equal(users.Count, roundConfig.AnonymitySet);

confirmationRequests = new List<Task<(CcjRoundPhase currentPhase, IEnumerable<(BitcoinAddress output, UnblindedSignature signature, int level)>)>>();
confirmationRequests = new List<Task<(CcjRoundPhase currentPhase, IEnumerable<ActiveOutput>)>>();

foreach (var user in users)
{
Expand Down Expand Up @@ -2889,7 +2889,7 @@ public async Task Ccj100ParticipantsTestsAsync()
Logger.TurnOn();

Assert.Equal(users.Count, roundConfig.AnonymitySet);
var confirmationRequests = new List<Task<(CcjRoundPhase currentPhase, IEnumerable<(BitcoinAddress output, UnblindedSignature signature, int level)>)>>();
var confirmationRequests = new List<Task<(CcjRoundPhase currentPhase, IEnumerable<ActiveOutput>)>>();

foreach (var user in users)
{
Expand All @@ -2913,14 +2913,14 @@ public async Task Ccj100ParticipantsTestsAsync()
// Because it's valuetuple.
var user = users.ElementAt(k);
users.RemoveAt(k);
users.Add((user.requester, user.blinded, user.activeOutputAddress, user.changeOutputAddress, user.inputProofModels, user.userInputData, user.aliceClient, resp.Item2.First().signature));
users.Add((user.requester, user.blinded, user.activeOutputAddress, user.changeOutputAddress, user.inputProofModels, user.userInputData, user.aliceClient, resp.Item2.First().Signature));
}

var outputRequests = new List<(BobClient, Task)>();
foreach (var user in users)
{
var bobClient = new BobClient(baseUri, null);
outputRequests.Add((bobClient, bobClient.PostOutputAsync(roundId, user.activeOutputAddress, user.unblindedSignature, 0)));
outputRequests.Add((bobClient, bobClient.PostOutputAsync(roundId, new ActiveOutput(user.activeOutputAddress, user.unblindedSignature, 0))));
}

foreach (var request in outputRequests)
Expand Down
23 changes: 23 additions & 0 deletions WalletWasabi/Models/ChaumianCoinJoin/ActiveOutput.cs
@@ -0,0 +1,23 @@
using NBitcoin;
using NBitcoin.Crypto;
using System;
using System.Collections.Generic;
using System.Text;
using WalletWasabi.Helpers;

namespace WalletWasabi.Models.ChaumianCoinJoin
{
public class ActiveOutput
{
public BitcoinAddress Address { get; }
public UnblindedSignature Signature { get; }
public int MixingLevel { get; }

public ActiveOutput(BitcoinAddress address, UnblindedSignature signature, int mixingLevel)
{
Address = Guard.NotNull(nameof(address), address);
Signature = Guard.NotNull(nameof(signature), signature);
MixingLevel = Guard.MinimumAndNotNull(nameof(mixingLevel), mixingLevel, 0);
}
}
}
Expand Up @@ -19,7 +19,7 @@ public class ClientRoundRegistration : IDisposable

public BitcoinAddress ChangeAddress { get; }

public IEnumerable<(BitcoinAddress address, UnblindedSignature signature, int mixingLevel)> ActiveOutputs { get; set; }
public IEnumerable<ActiveOutput> ActiveOutputs { get; set; }

public IEnumerable<SmartCoin> CoinsRegistered { get; }

Expand All @@ -31,7 +31,7 @@ public ClientRoundRegistration(AliceClient aliceClient, IEnumerable<SmartCoin> c
CoinsRegistered = Guard.NotNullOrEmpty(nameof(coinsRegistereds), coinsRegistereds);
ChangeAddress = Guard.NotNull(nameof(changeAddress), changeAddress);

ActiveOutputs = Enumerable.Empty<(BitcoinAddress address, UnblindedSignature signature, int mixingLevel)>();
ActiveOutputs = Enumerable.Empty<ActiveOutput>();
}

public bool IsPhaseActionsComleted(CcjRoundPhase phase) => CompletedPhase >= phase;
Expand Down
10 changes: 5 additions & 5 deletions WalletWasabi/Services/CcjClient.cs
Expand Up @@ -340,7 +340,7 @@ private async Task TryProcessRoundStateAsync(long ongoingRoundId)
{
TxOut[] myOutputs = unsignedCoinJoin.Outputs
.Where(x => x.ScriptPubKey == ongoingRound.Registration.ChangeAddress.ScriptPubKey
|| ongoingRound.Registration.ActiveOutputs.Select(y => y.address.ScriptPubKey).Contains(x.ScriptPubKey))
|| ongoingRound.Registration.ActiveOutputs.Select(y => y.Address.ScriptPubKey).Contains(x.ScriptPubKey))
.ToArray();
Money amountBack = myOutputs.Sum(y => y.Value);

Expand Down Expand Up @@ -415,11 +415,11 @@ private async Task RegisterOutputAsync(CcjClientRound ongoingRound)

var shuffledOutputs = ongoingRound.Registration.ActiveOutputs.ToList();
shuffledOutputs.Shuffle();
foreach ((BitcoinAddress address, UnblindedSignature signature, int mixingLevel) activeOutput in shuffledOutputs)
foreach (var activeOutput in shuffledOutputs)
{
using (var bobClient = new BobClient(CcjHostUriAction, TorSocks5EndPoint))
{
if (!await bobClient.PostOutputAsync(ongoingRound.RoundId, activeOutput.address, activeOutput.signature, activeOutput.mixingLevel))
if (!await bobClient.PostOutputAsync(ongoingRound.RoundId, activeOutput))
{
Logger.LogWarning<AliceClient>($"Round ({ongoingRound.State.RoundId}) Bobs did not have enough time to post outputs before timeout. If you see this message, contact nopara73, so he can optimize the phase timeout periods to the worst Internet/Tor connections, which may be yours.)");
break;
Expand All @@ -430,15 +430,15 @@ private async Task RegisterOutputAsync(CcjClientRound ongoingRound)
{
if (ExposedLinks.ContainsKey(input)) // Should never not contain, but oh well, let's not disrupt the round for this.
{
var found = ExposedLinks[input].FirstOrDefault(x => x.Key.GetP2wpkhAddress(Network) == activeOutput.address);
var found = ExposedLinks[input].FirstOrDefault(x => x.Key.GetP2wpkhAddress(Network) == activeOutput.Address);
if (found != default)
{
found.IsBlinded = false;
}
else
{
// Should never happen, but oh well we can autocorrect it so why not.
ExposedLinks[input] = ExposedLinks[input].Append(new HdPubKeyBlindedPair(KeyManager.GetKeyForScriptPubKey(activeOutput.address.ScriptPubKey), false));
ExposedLinks[input] = ExposedLinks[input].Append(new HdPubKeyBlindedPair(KeyManager.GetKeyForScriptPubKey(activeOutput.Address.ScriptPubKey), false));
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions WalletWasabi/WebClients/Wasabi/ChaumianCoinJoin/AliceClient.cs
@@ -1,4 +1,4 @@
using NBitcoin;
using NBitcoin;
using NBitcoin.BouncyCastle.Math;
using NBitcoin.Crypto;
using Newtonsoft.Json;
Expand Down Expand Up @@ -139,8 +139,7 @@ public class AliceClient : TorDisposableBase
Func<Uri> baseUriAction,
IPEndPoint torSocks5EndPoint)
{
var request = new InputsRequest
{
var request = new InputsRequest {
RoundId = roundId,
BlindedOutputScripts = blindedOutputScriptHashes,
ChangeOutputAddress = changeOutput,
Expand All @@ -149,7 +148,7 @@ public class AliceClient : TorDisposableBase
return await CreateNewAsync(roundId, registeredAddresses, schnorrPubKeys, requesters, network, request, baseUriAction, torSocks5EndPoint);
}

public async Task<(CcjRoundPhase currentPhase, IEnumerable<(BitcoinAddress output, UnblindedSignature signature, int level)> activeOutputs)> PostConfirmationAsync()
public async Task<(CcjRoundPhase currentPhase, IEnumerable<ActiveOutput> activeOutputs)> PostConfirmationAsync()
{
using (HttpResponseMessage response = await TorClient.SendAsync(HttpMethod.Post, $"/api/v{Helpers.Constants.BackendMajorVersion}/btc/chaumiancoinjoin/confirmation?uniqueId={UniqueId}&roundId={RoundId}"))
{
Expand All @@ -161,7 +160,7 @@ public async Task<(CcjRoundPhase currentPhase, IEnumerable<(BitcoinAddress outpu
ConnConfResp resp = await response.Content.ReadAsJsonAsync<ConnConfResp>();
Logger.LogInfo<AliceClient>($"Round ({RoundId}), Alice ({UniqueId}): Confirmed connection. Phase: {resp.CurrentPhase}.");

var activeOutputs = new List<(BitcoinAddress output, UnblindedSignature signature, int level)>();
var activeOutputs = new List<ActiveOutput>();
if (resp.BlindedOutputSignatures != null && resp.BlindedOutputSignatures.Any())
{
var unblindedSignatures = new List<UnblindedSignature>();
Expand Down Expand Up @@ -189,7 +188,9 @@ public async Task<(CcjRoundPhase currentPhase, IEnumerable<(BitcoinAddress outpu
var sig = unblindedSignatures[i];
var addr = RegisteredAddresses[i];
var lvl = i;
activeOutputs.Add((addr, sig, lvl));

var actOut = new ActiveOutput(addr, sig, lvl);
activeOutputs.Add(actOut);
}
}

Expand Down
11 changes: 5 additions & 6 deletions WalletWasabi/WebClients/Wasabi/ChaumianCoinJoin/BobClient.cs
@@ -1,4 +1,4 @@
using NBitcoin;
using NBitcoin;
using NBitcoin.Crypto;
using System;
using System.Net;
Expand All @@ -7,6 +7,7 @@
using WalletWasabi.Backend.Models.Requests;
using WalletWasabi.Bases;
using WalletWasabi.Helpers;
using WalletWasabi.Models.ChaumianCoinJoin;

namespace WalletWasabi.WebClients.Wasabi.ChaumianCoinJoin
{
Expand All @@ -23,14 +24,12 @@ public BobClient(Uri baseUri, IPEndPoint torSocks5EndPoint) : base(baseUri, torS
}

/// <returns>If the phase is still in OutputRegistration.</returns>
public async Task<bool> PostOutputAsync(long roundId, BitcoinAddress activeOutputAddress, UnblindedSignature unblindedSignature, int level)
public async Task<bool> PostOutputAsync(long roundId, ActiveOutput activeOutput)
{
Guard.MinimumAndNotNull(nameof(roundId), roundId, 0);
Guard.NotNull(nameof(activeOutputAddress), activeOutputAddress);
Guard.NotNull(nameof(unblindedSignature), unblindedSignature);
Guard.MinimumAndNotNull(nameof(level), level, 0);
Guard.NotNull(nameof(activeOutput), activeOutput);

var request = new OutputRequest { OutputAddress = activeOutputAddress, UnblindedSignature = unblindedSignature, Level = level };
var request = new OutputRequest { OutputAddress = activeOutput.Address, UnblindedSignature = activeOutput.Signature, Level = activeOutput.MixingLevel };
using (var response = await TorClient.SendAsync(HttpMethod.Post, $"/api/v{Constants.BackendMajorVersion}/btc/chaumiancoinjoin/output?roundId={roundId}", request.ToHttpStringContent()))
{
if (response.StatusCode == HttpStatusCode.Conflict)
Expand Down